Skip to content

Commit

Permalink
Upsert with setters
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Apr 19, 2012
1 parent 619377b commit 7d748e9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
11 changes: 9 additions & 2 deletions lib/abstract-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,15 @@ AbstractClass.upsert = AbstractClass.updateOrCreate = function upsert(data, call
var Model = this;
if (!data.id) return this.create(data, callback);
if (this.schema.adapter.updateOrCreate) {
this.schema.adapter.updateOrCreate(Model.modelName, data, function (err, data) {
var obj = data ? new Model(data) : null;
var inst = new Model(data);
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) {
addToCache(Model, obj);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jugglingdb",
"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>",
"contributors": [
{ "name": "Anatoliy Chakkaev", "email": "rpm1602@gmail.com" },
Expand Down
16 changes: 13 additions & 3 deletions test/common_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,14 +731,18 @@ function testOrm(schema) {
test.ok(updatedPost);
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.content, updatedPost.toObject().content);

Post.find(updatedPost.id, function (err, post) {
if (err) throw err;
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.content, post.toObject().content);
Post.updateOrCreate({id: 100001, title: 'hey'}, function (err, post) {
Expand Down Expand Up @@ -769,7 +773,13 @@ function testOrm(schema) {
test.equal(users[0].passwd, 'qwertysalt');
User.create({passwd: 'asalat'}, function (err, usr) {
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();
});
});
});
});
});
Expand Down

0 comments on commit 7d748e9

Please sign in to comment.