Upsert with setters
This commit is contained in:
parent
619377b9c7
commit
7d748e9c02
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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" },
|
||||||
|
|
|
@ -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!');
|
||||||
|
|
||||||
|
if (schema.name !== 'mongodb') {
|
||||||
test.equal(newData.id, updatedPost.toObject().id);
|
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!');
|
||||||
|
if (schema.name !== 'mongodb') {
|
||||||
test.equal(newData.id, post.toObject().id);
|
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,12 +773,18 @@ 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');
|
||||||
|
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();
|
test.done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('all tests done', function (test) {
|
it('all tests done', function (test) {
|
||||||
test.done();
|
test.done();
|
||||||
|
|
Loading…
Reference in New Issue