Make memory adapter async

This commit is contained in:
Anatoliy Chakkaev 2013-01-22 01:21:43 +07:00
parent 9dbc8fa50b
commit 91fee1d565
2 changed files with 30 additions and 29 deletions

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) {

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();
});
});
});
}