Permalink
Browse files

Upsert with setters

  • Loading branch information...
1 parent 619377b commit 7d748e9c0217be84a695fcf4aea8104893db9a79 @1602 committed Apr 19, 2012
Showing with 23 additions and 6 deletions.
  1. +9 −2 lib/abstract-class.js
  2. +1 −1 package.json
  3. +13 −3 test/common_test.js
View
@@ -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);
}
View
@@ -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" },
View
@@ -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();
+ });
+ });
});
});
});

0 comments on commit 7d748e9

Please sign in to comment.