diff --git a/lib/abstract-class.js b/lib/abstract-class.js index 72033d13..1a098888 100644 --- a/lib/abstract-class.js +++ b/lib/abstract-class.js @@ -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); } diff --git a/package.json b/package.json index ec5735a0..3630ea0c 100644 --- a/package.json +++ b/package.json @@ -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 ", "contributors": [ { "name": "Anatoliy Chakkaev", "email": "rpm1602@gmail.com" }, diff --git a/test/common_test.js b/test/common_test.js index 3e28f3d6..98520adc 100644 --- a/test/common_test.js +++ b/test/common_test.js @@ -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) { @@ -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(); + }); + }); }); }); });