model.find => model.findById, model.all => model.find

This commit is contained in:
Ritchie Martori 2013-06-24 12:42:58 -07:00
parent ff37cc001a
commit 008b406dd7
9 changed files with 45 additions and 44 deletions

View File

@ -196,7 +196,7 @@ DataAccessObject.upsert = DataAccessObject.updateOrCreate = function upsert(data
callback(err, obj);
});
} else {
this.find(data.id, function (err, inst) {
this.findById(data.id, function (err, inst) {
if (err) return callback(err);
if (inst) {
inst.updateAttributes(data, callback);
@ -262,7 +262,7 @@ DataAccessObject.exists.accepts = {arg: 'id', type: 'any'};
* @param {id} id - primary key value
* @param {Function} cb - callback called with (err, instance)
*/
DataAccessObject.find = function find(id, cb) {
DataAccessObject.findById = function find(id, cb) {
if (stillConnecting(this.schema, this, arguments)) return;
this.schema.adapter.find(this.modelName, id, function (err, data) {
@ -279,11 +279,11 @@ DataAccessObject.find = function find(id, cb) {
};
// find ~ remoting attributes
DataAccessObject.find.accepts = [
DataAccessObject.findById.accepts = [
{arg: 'id', type: 'any'}
];
DataAccessObject.find.shared = true;
DataAccessObject.find.http = [
DataAccessObject.findById.shared = true;
DataAccessObject.findById.http = [
{verb: 'get', path: '/:id'}
];
@ -304,8 +304,9 @@ DataAccessObject.find.http = [
* - err (null or Error)
* - Array of instances
*/
DataAccessObject.all = function all(params, cb) {
DataAccessObject.all =
DataAccessObject.find = function find(params, cb) {
if (stillConnecting(this.schema, this, arguments)) return;
if (arguments.length === 1) {
@ -335,9 +336,9 @@ DataAccessObject.all = function all(params, cb) {
};
// all ~ remoting attributes
DataAccessObject.all.accepts = {arg: 'filter', type: 'object'};
DataAccessObject.all.shared = true;
DataAccessObject.all.http = [
DataAccessObject.find.accepts = {arg: 'filter', type: 'object'};
DataAccessObject.find.shared = true;
DataAccessObject.find.http = [
{verb: 'get', path: '/'},
{verb: 'get', path: '/all'}
];
@ -356,7 +357,7 @@ DataAccessObject.findOne = function findOne(params, cb) {
params = {};
}
params.limit = 1;
this.all(params, function (err, collection) {
this.find(params, function (err, collection) {
if (err || !collection || !collection.length > 0) return cb(err, null);
cb(err, collection[0]);
});
@ -621,7 +622,7 @@ DataAccessObject.prototype.updateAttributes.http = [
DataAccessObject.prototype.reload = function reload(callback) {
if (stillConnecting(this.constructor.schema, this, arguments)) return;
this.constructor.find(this.id, callback);
this.constructor.findById(this.id, callback);
};
DataAccessObject.prototype.reload.shared = true;

View File

@ -56,7 +56,7 @@ Relation.hasMany = function hasMany(anotherClass, params) {
// pluralize(anotherClass.modelName)
// which is actually just anotherClass.all({where: {thisModelNameId: this.id}}, cb);
var scopeMethods = {
find: find,
findById: find,
destroy: destroy
};
if (params.through) {
@ -129,7 +129,7 @@ Relation.hasMany = function hasMany(anotherClass, params) {
}
function find(id, cb) {
anotherClass.find(id, function (err, inst) {
anotherClass.findById(id, function (err, inst) {
if (err) return cb(err);
if (!inst) return cb(new Error('Not found'));
if (inst[fk] && inst[fk].toString() == this.id.toString()) {
@ -141,7 +141,7 @@ Relation.hasMany = function hasMany(anotherClass, params) {
}
function destroy(id, cb) {
this.find(id, function (err, inst) {
this.findById(id, function (err, inst) {
if (err) return cb(err);
if (inst) {
inst.destroy(cb);
@ -210,7 +210,7 @@ Relation.belongsTo = function (anotherClass, params) {
cb(null, null);
return;
}
anotherClass.find(id, function (err,inst) {
anotherClass.findById(id, function (err,inst) {
if (err) return cb(err);
if (!inst) return cb(null, null);
if (inst.id === this[fk]) {

View File

@ -58,7 +58,7 @@ function defineScope(cls, targetClass, name, params, methods) {
if (!this.__cachedRelations || (typeof this.__cachedRelations[name] == 'undefined') || actualRefresh) {
var self = this;
var params = mergeParams(actualCond, caller._scope);
return targetClass.all(params, function(err, data) {
return targetClass.find(params, function(err, data) {
if (!err && saveOnCache) {
if (!self.__cachedRelations) {
self.__cachedRelations = {};

View File

@ -289,7 +289,7 @@ function validateCustom(attr, conf, err, done) {
function validateUniqueness(attr, conf, err, done) {
var cond = {where: {}};
cond.where[attr] = this[attr];
this.constructor.all(cond, function (error, found) {
this.constructor.find(cond, function (error, found) {
if (error) {
return err();
}

View File

@ -19,14 +19,14 @@ describe('basic-querying', function() {
});
describe('find', function() {
describe('findById', function() {
before(function(done) {
User.destroyAll(done);
});
it('should query by id: not found', function(done) {
User.find(1, function(err, u) {
User.findById(1, function(err, u) {
should.not.exist(u);
should.not.exist(err);
done();
@ -37,7 +37,7 @@ describe('basic-querying', function() {
User.create(function(err, u) {
should.not.exist(err);
should.exist(u.id);
User.find(u.id, function(err, u) {
User.findById(u.id, function(err, u) {
should.exist(u);
should.not.exist(err);
u.should.be.an.instanceOf(User);
@ -48,12 +48,12 @@ describe('basic-querying', function() {
});
describe('all', function() {
describe('find', function() {
before(seed);
it('should query collection', function(done) {
User.all(function(err, users) {
User.find(function(err, users) {
should.exists(users);
should.not.exists(err);
users.should.have.lengthOf(6);
@ -62,7 +62,7 @@ describe('basic-querying', function() {
});
it('should query limited collection', function(done) {
User.all({limit: 3}, function(err, users) {
User.find({limit: 3}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.should.have.lengthOf(3);
@ -71,7 +71,7 @@ describe('basic-querying', function() {
});
it('should query offset collection with limit', function(done) {
User.all({skip: 1, limit: 4}, function(err, users) {
User.find({skip: 1, limit: 4}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.should.have.lengthOf(4);
@ -80,7 +80,7 @@ describe('basic-querying', function() {
});
it('should query filtered collection', function(done) {
User.all({where: {role: 'lead'}}, function(err, users) {
User.find({where: {role: 'lead'}}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.should.have.lengthOf(2);
@ -89,7 +89,7 @@ describe('basic-querying', function() {
});
it('should query collection sorted by numeric field', function(done) {
User.all({order: 'order'}, function(err, users) {
User.find({order: 'order'}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.forEach(function(u, i) {
@ -100,7 +100,7 @@ describe('basic-querying', function() {
});
it('should query collection desc sorted by numeric field', function(done) {
User.all({order: 'order DESC'}, function(err, users) {
User.find({order: 'order DESC'}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.forEach(function(u, i) {
@ -111,7 +111,7 @@ describe('basic-querying', function() {
});
it('should query collection sorted by string field', function(done) {
User.all({order: 'name'}, function(err, users) {
User.find({order: 'name'}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.shift().name.should.equal('George Harrison');
@ -122,7 +122,7 @@ describe('basic-querying', function() {
});
it('should query collection desc sorted by string field', function(done) {
User.all({order: 'name DESC'}, function(err, users) {
User.find({order: 'name DESC'}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.pop().name.should.equal('George Harrison');

View File

@ -254,7 +254,7 @@ function testOrm(schema) {
test.ok(obj.id);
test.equals(obj.title, title);
test.equals(obj.date, date);
Post.find(obj.id, function () {
Post.findById(obj.id, function () {
test.equal(obj.title, title);
test.equal(obj.date.toString(), date.toString());
test.done();
@ -282,7 +282,7 @@ function testOrm(schema) {
Post.create({ title: title }, function (err, post) {
test.ok(post.id, 'Object should have id');
test.equals(post.title, title);
Post.find(post.id, function (err, foundPost) {
Post.findById(post.id, function (err, foundPost) {
if (err) throw err;
test.equal(post.title, title);
test.strictEqual(post, foundPost);
@ -309,7 +309,7 @@ function testOrm(schema) {
Post.exists(post.id, function (err, exists) {
if (err) console.log(err);
test.ok(!exists, 'Hey! ORM told me that object exists, but it looks like it doesn\'t. Something went wrong...');
Post.find(post.id, function (err, obj) {
Post.findById(post.id, function (err, obj) {
test.equal(obj, null, 'Param obj should be null');
test.done();
});
@ -483,7 +483,7 @@ function testOrm(schema) {
var post = posts[i];
if (post.userId) {
// We could get the user with belongs to relationship but it is better if there is no interactions.
User.find(post.userId, function(err, user) {
User.findById(post.userId, function(err, user) {
User.create(function(err, voidUser) {
Post.create({userId: user.id}, function() {
@ -1028,7 +1028,7 @@ function testOrm(schema) {
test.equal(newData.title, updatedPost.toObject().title);
test.equal(newData.content, updatedPost.toObject().content);
Post.find(updatedPost.id, function (err, post) {
Post.findById(updatedPost.id, function (err, post) {
if (err) throw err;
if (!post) throw Error('No post!');
if (schema.name !== 'mongodb') {
@ -1039,7 +1039,7 @@ function testOrm(schema) {
Post.updateOrCreate({id: 100001, title: 'hey'}, function (err, post) {
if (schema.name !== 'mongodb') test.equal(post.id, 100001);
test.equal(post.title, 'hey');
Post.find(post.id, function (err, post) {
Post.findById(post.id, function (err, post) {
if (!post) throw Error('No post!');
test.done();
});
@ -1056,7 +1056,7 @@ function testOrm(schema) {
var u = new User({passwd: 'qwerty'});
test.equal(u.passwd, 'qwertysalt');
u.save(function (err, user) {
User.find(user.id, function (err, user) {
User.findById(user.id, function (err, user) {
test.ok(user !== u);
test.equal(user.passwd, 'qwertysalt');
User.all({where: {passwd: 'qwertysalt'}}, function (err, users) {
@ -1066,7 +1066,7 @@ function testOrm(schema) {
test.equal(usr.passwd, 'asalatsalt');
User.upsert({passwd: 'heyman'}, function (err, us) {
test.equal(us.passwd, 'heymansalt');
User.find(us.id, function (err, user) {
User.findById(us.id, function (err, user) {
test.equal(user.passwd, 'heymansalt');
test.done();
});
@ -1088,7 +1088,7 @@ function testOrm(schema) {
p.likes.push({second: 2});
p.likes.push({third: 3});
p.save(function (err) {
Post.find(p.id, function (err, pp) {
Post.findById(p.id, function (err, pp) {
test.equal(pp.likes.length, 3);
test.ok(pp.likes[3].third);
test.ok(pp.likes[2].second);
@ -1101,7 +1101,7 @@ function testOrm(schema) {
test.ok(!pp.likes[1]);
test.ok(pp.likes[3]);
pp.save(function () {
Post.find(p.id, function (err, pp) {
Post.findById(p.id, function (err, pp) {
test.equal(pp.likes.length, 1);
test.ok(!pp.likes[1]);
test.ok(pp.likes[3]);

View File

@ -35,7 +35,7 @@ describe('datatypes', function() {
});
function testFind(next) {
Model.find(id, function(err, m) {
Model.findById(id, function(err, m) {
should.not.exist(err);
should.exist(m);
m.str.should.be.a('string');

View File

@ -32,7 +32,7 @@ describe('manipulation', function() {
p.name.should.equal('Anatoliy');
should.not.exist(err);
should.exist(p);
Person.find(p.id, function(err, person) {
Person.findById(p.id, function(err, person) {
person.id.should.equal(p.id);
person.name.should.equal('Anatoliy');
done();
@ -67,7 +67,7 @@ describe('manipulation', function() {
should.not.exist(err);
should.exist(p);
should.not.exists(p.name);
Person.find(p.id, function(err, person) {
Person.findById(p.id, function(err, person) {
person.id.should.equal(p.id);
should.not.exists(person.name);
done();

View File

@ -109,7 +109,7 @@ describe('relations', function() {
});
function fetch(book) {
book.chapters.find(id, function(err, ch) {
book.chapters.findById(id, function(err, ch) {
should.not.exist(err);
should.exist(ch);
ch.id.should.equal(id);