Merge pull request #196 from anatoliychakkaev/master

findOrCreate, misc amends
This commit is contained in:
Anatoliy Chakkaev 2013-01-21 10:22:50 -08:00
commit a58ace2f6b
4 changed files with 69 additions and 30 deletions

View File

@ -258,6 +258,34 @@ AbstractClass.upsert = AbstractClass.updateOrCreate = function upsert(data, call
}
};
/**
* Find one record, same as `all`, limited by 1 and return object, not collection,
* if not found, create using data provided as second argument
*
* @param {Object} query - search conditions: {where: {test: 'me'}}.
* @param {Object} data - object to create.
* @param {Function} cb - callback called with (err, instance)
*/
AbstractClass.findOrCreate = function findOrCreate(query, data, callback) {
if (typeof query === 'undefined') {
query = {where: {}};
}
if (typeof data === 'function' || typeof data === 'undefined') {
callback = data;
data = query && query.where;
}
if (typeof callback === 'undefined') {
callback = function () {};
}
var t = this;
this.findOne(query, function (err, record) {
if (err) return callback(err);
if (record) return callback(null, record);
t.create(data, callback);
});
};
/**
* Check whether object exitst in database
*
@ -341,7 +369,7 @@ AbstractClass.all = function all(params, cb) {
/**
* Find one record, same as `all`, limited by 1 and return object, not collection
*
* @param {Object} params - search conditions
* @param {Object} params - search conditions: {where: {test: 'me'}}
* @param {Function} cb - callback called with (err, instance)
*/
AbstractClass.findOne = function findOne(params, cb) {

View File

@ -20,7 +20,9 @@ Memory.prototype.create = function create(model, data, callback) {
var id = data.id || this.ids[model]++;
data.id = id;
this.cache[model][id] = data;
callback(null, id);
process.nextTick(function () {
callback(null, id);
});
};
Memory.prototype.updateOrCreate = function (model, data, callback) {
@ -39,20 +41,26 @@ Memory.prototype.updateOrCreate = function (model, data, callback) {
Memory.prototype.save = function save(model, data, callback) {
this.cache[model][data.id] = data;
callback(null, data);
process.nextTick(function () {
callback(null, data);
});
};
Memory.prototype.exists = function exists(model, id, callback) {
callback(null, this.cache[model].hasOwnProperty(id));
process.nextTick(function () {
callback(null, this.cache[model].hasOwnProperty(id));
}.bind(this));
};
Memory.prototype.find = function find(model, id, callback) {
callback(null, this.cache[model][id]);
process.nextTick(function () {
callback(null, this.cache[model][id]);
}.bind(this));
};
Memory.prototype.destroy = function destroy(model, id, callback) {
delete this.cache[model][id];
callback();
process.nextTick(callback);
};
Memory.prototype.all = function all(model, filter, callback) {
@ -132,7 +140,7 @@ Memory.prototype.destroyAll = function destroyAll(model, callback) {
delete this.cache[model][id];
}.bind(this));
this.cache[model] = {};
callback();
process.nextTick(callback);
};
Memory.prototype.count = function count(model, callback, where) {
@ -149,7 +157,9 @@ Memory.prototype.count = function count(model, callback, where) {
return ok;
});
}
callback(null, data.length);
process.nextTick(function () {
callback(null, data.length);
});
};
Memory.prototype.updateAttributes = function updateAttributes(model, id, data, cb) {

10
test/adapters_test.js Normal file
View File

@ -0,0 +1,10 @@
var jdb = require('jugglingdb'),
Schema = jdb.Schema,
test = jdb.test;
var schema = new Schema('memory');
test(module.exports, schema);
test.skip('hasMany should be cached');

View File

@ -543,28 +543,6 @@ function testOrm(schema) {
//User.create(function (e, u) {
// u.posts.create({}, function (e, p) {
// find all posts for a user.
/* User.all(function (err,users) {
for (var i=0;i<users.length;i++) {
u = users[i];
Posts.find(user.id, function(err, posts) {
// now check to see that the user has these posts testing the all method of hasMany.
u.posts.all(null,function(err, uposts) {
test.equal(posts.length,uposts.length);
if (post.length == uposts.length) {
for (var j=0;j<uposts.length;j++) {
for (var k= 0,found=false;k<posts.length;k++) {
if (uposts[j] == uposts[k].id) { found = true; break; }
}
if (!found) test.equal(1,0); // not familliar with test framework here... test.fail()?
}
}
})
})
// find the posts with this user id.
// find the posts of the user.
}
})*/
// Finding one post with an existing author associated
Post.all(function (err, posts) {
// We try to get the first post with a userId != NULL
@ -1448,4 +1426,17 @@ function testOrm(schema) {
});
});
it('should find or create', function (test) {
var email = 'some email ' + Math.random();
User.findOrCreate({where: {email: email, age: 23}}, function (err, u) {
test.ok(u);
test.equals(u.age, 23);
User.findOrCreate({where: {email: email}}, {age: 21}, function (err, u2) {
test.equals(u.id.toString(), u2.id.toString(), 'Same user ids');
test.equals(u2.age, 23);
test.done();
});
});
});
}