Upsert with setters

This commit is contained in:
Anatoliy Chakkaev 2012-04-19 20:06:38 +04:00
parent 619377b9c7
commit 7d748e9c02
3 changed files with 23 additions and 6 deletions

View File

@ -212,8 +212,15 @@ AbstractClass.upsert = AbstractClass.updateOrCreate = function upsert(data, call
var Model = this; var Model = this;
if (!data.id) return this.create(data, callback); if (!data.id) return this.create(data, callback);
if (this.schema.adapter.updateOrCreate) { if (this.schema.adapter.updateOrCreate) {
this.schema.adapter.updateOrCreate(Model.modelName, data, function (err, data) { var inst = new Model(data);
var obj = data ? new Model(data) : null; this.schema.adapter.updateOrCreate(Model.modelName, inst.toObject(), function (err, data) {
var obj;
if (data) {
inst._initProperties(data);
obj = inst;
} else {
obj = null;
}
if (obj) { if (obj) {
addToCache(Model, obj); addToCache(Model, obj);
} }

View File

@ -1,7 +1,7 @@
{ {
"name": "jugglingdb", "name": "jugglingdb",
"description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite", "description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite",
"version": "0.1.7", "version": "0.1.8",
"author": "Anatoliy Chakkaev <rpm1602@gmail.com>", "author": "Anatoliy Chakkaev <rpm1602@gmail.com>",
"contributors": [ "contributors": [
{ "name": "Anatoliy Chakkaev", "email": "rpm1602@gmail.com" }, { "name": "Anatoliy Chakkaev", "email": "rpm1602@gmail.com" },

View File

@ -731,14 +731,18 @@ function testOrm(schema) {
test.ok(updatedPost); test.ok(updatedPost);
if (!updatedPost) throw Error('No post!'); if (!updatedPost) throw Error('No post!');
test.equal(newData.id, updatedPost.toObject().id); if (schema.name !== 'mongodb') {
test.equal(newData.id, updatedPost.toObject().id);
}
test.equal(newData.title, updatedPost.toObject().title); test.equal(newData.title, updatedPost.toObject().title);
test.equal(newData.content, updatedPost.toObject().content); test.equal(newData.content, updatedPost.toObject().content);
Post.find(updatedPost.id, function (err, post) { Post.find(updatedPost.id, function (err, post) {
if (err) throw err; if (err) throw err;
if (!post) throw Error('No post!'); if (!post) throw Error('No post!');
test.equal(newData.id, post.toObject().id); if (schema.name !== 'mongodb') {
test.equal(newData.id, post.toObject().id);
}
test.equal(newData.title, post.toObject().title); test.equal(newData.title, post.toObject().title);
test.equal(newData.content, post.toObject().content); test.equal(newData.content, post.toObject().content);
Post.updateOrCreate({id: 100001, title: 'hey'}, function (err, post) { Post.updateOrCreate({id: 100001, title: 'hey'}, function (err, post) {
@ -769,7 +773,13 @@ function testOrm(schema) {
test.equal(users[0].passwd, 'qwertysalt'); test.equal(users[0].passwd, 'qwertysalt');
User.create({passwd: 'asalat'}, function (err, usr) { User.create({passwd: 'asalat'}, function (err, usr) {
test.equal(usr.passwd, 'asalatsalt'); test.equal(usr.passwd, 'asalatsalt');
test.done(); User.upsert({passwd: 'heyman'}, function (err, us) {
test.equal(us.passwd, 'heymansalt');
User.find(us.id, function (err, user) {
test.equal(user.passwd, 'heymansalt');
test.done();
});
});
}); });
}); });
}); });