Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upsert support to Model.save, Document.save, and Document.saveAll #142

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,26 +747,32 @@ Document.prototype.__validate = function(doc, schema, prefix, options, originalD
}
}

Document.prototype.save = function(callback) {
Document.prototype.save = function(options, callback) {
var self = this;

return self._save({}, false, {}, callback);
if (typeof options === 'function') {
callback = options;
options = { conflict: 'error' };
}

return self._save({}, false, {}, options, callback);
}
Document.prototype.saveAll = function(modelToSave, callback) {
Document.prototype.saveAll = function(modelToSave, options, callback) {
var self = this;

var saveAll;
if (typeof modelToSave === 'function') {
callback = modelToSave;
saveAll = true;
modelToSave = {};
}
else {
saveAll = (modelToSave === undefined) ? true: false;
modelToSave = modelToSave || {};
} else if (typeof options === 'function') {
callback = options;
options = { conflict: 'error' };
}

return self._save(modelToSave, saveAll, {}, callback);
saveAll = !modelToSave;
modelToSave || (modelToSave = {});

return self._save(modelToSave, saveAll, {}, options, callback);
}

Document.prototype._makeSavableCopy = function() {
Expand Down Expand Up @@ -860,11 +866,17 @@ Document.prototype.__makeSavableCopy = function(doc, schema, model, r) {
}


Document.prototype._save = function(modelToSave, saveAll, savedModel, callback) {
Document.prototype._save = function(modelToSave, saveAll, savedModel, options, callback) {
//TOIMPROVE? How should we handle circular references outsides of joined fields? Now we throw with a maximum call stack size exceed
var self = this;
self.emit('saving', self);

var conflict = 'error';

if (options && /^(error|update|replace)$/.test(options.conflict)) {
conflict = options.conflict;
}

return util.hook({
preHooks: self._getModel()._pre.save,
postHooks: self._getModel()._post.save,
Expand Down Expand Up @@ -1008,7 +1020,7 @@ Document.prototype._save = function(modelToSave, saveAll, savedModel, callback)
self[key][model._joins[key].rightKey] = self[model._joins[key].leftKey];
(function(key) {
promisesMany.push(new Promise(function(resolve, reject) {
self[key]._save(modelToSave[key], saveAll, savedModel).then(function() {
self[key]._save(modelToSave[key], saveAll, savedModel, options).then(function() {
self.__proto__._hasOne[key] = {
doc: self[key],
foreignKey: self._getModel()._joins[key].rightKey
Expand All @@ -1028,7 +1040,7 @@ Document.prototype._save = function(modelToSave, saveAll, savedModel, callback)
else if ((self[key] == null) && (self.__proto__._hasOne[key] != null)) {
var doc = self.__proto__._hasOne[key].doc;
delete doc[self.__proto__._hasOne[key].foreignKey];
promisesMany.push(doc._save(modelToSave[key], saveAll, savedModel))
promisesMany.push(doc._save(modelToSave[key], saveAll, savedModel, options))
self.__proto__._hasOne[key] = null;
}
}
Expand All @@ -1054,7 +1066,7 @@ Document.prototype._save = function(modelToSave, saveAll, savedModel, callback)
for(var i=0; i<self.__proto__._hasMany[key].length; i++) {
if (pkMap[self.__proto__._hasMany[key][i].doc[[model._joins[key].model._pk]]] == null) {
delete self.__proto__._hasMany[key][i].doc[self.__proto__._hasMany[key][i].foreignKey];
promisesMany.push(self.__proto__._hasMany[key][i].doc._save(modelToSave[key], saveAll, savedModel));
promisesMany.push(self.__proto__._hasMany[key][i].doc._save(modelToSave[key], saveAll, savedModel, options));
}
}
}
Expand All @@ -1064,7 +1076,7 @@ Document.prototype._save = function(modelToSave, saveAll, savedModel, callback)
self[key][i][model._joins[key].rightKey] = self[model._joins[key].leftKey];
(function(key, i) {
promisesMany.push(new Promise(function(resolve, reject) {
self[key][i]._save(modelToSave[key], saveAll, savedModel).then(function(doc) {
self[key][i]._save(modelToSave[key], saveAll, savedModel, options).then(function(doc) {
if (!Array.isArray(self.__proto__._hasMany[key])) {
self.__proto__._hasMany[key] = [];
}
Expand Down Expand Up @@ -1099,7 +1111,7 @@ Document.prototype._save = function(modelToSave, saveAll, savedModel, callback)
for(var i=0; i<self[key].length; i++) {
(function(key, i) {
promisesMany.push(new Promise(function(resolve, reject) {
self[key][i]._save(modelToSave[key], saveAll, savedModel).then(function() {
self[key][i]._save(modelToSave[key], saveAll, savedModel, options).then(function() {
// self.__proto__._links will be saved in saveLinks
if (self[key][i].__proto__._parents._belongsLinks[self._getModel()._name] == null) {
self[key][i].__proto__._parents._belongsLinks[self._getModel()._name] = [];
Expand Down Expand Up @@ -1152,7 +1164,7 @@ Document.prototype._save = function(modelToSave, saveAll, savedModel, callback)

var querySaveSelf;
if (self.__proto__._saved === false) {
querySaveSelf = r.table(constructor.getTableName()).insert(copy, {returnChanges: true})
querySaveSelf = r.table(constructor.getTableName()).insert(copy, {conflict: conflict, returnChanges: true})
}
else {
querySaveSelf = r.table(constructor.getTableName()).get(copy[model._pk]).replace(copy, {returnChanges: true})
Expand Down Expand Up @@ -1221,10 +1233,10 @@ Document.prototype._save = function(modelToSave, saveAll, savedModel, callback)
if (self[key] != null) {
savedModel[model._joins[key].model.getTableName()] = true;
if (saveAll === true) {
promises.push(self[key]._save({}, true, savedModel))
promises.push(self[key]._save({}, true, savedModel, options))
}
else {
promises.push(self[key]._save(modelToSave[model._joins[key].model.getTableName()], false, savedModel))
promises.push(self[key]._save(modelToSave[model._joins[key].model.getTableName()], false, savedModel, options))
}
}
}
Expand Down
49 changes: 28 additions & 21 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,14 +969,20 @@ Model.prototype.execute = function(options) {
var query = new Query(this);
return query.execute(options);
}
Model.prototype.save = function(docs) {
Model.prototype.save = function(docs, options) {
var self = this;
var r = self._getModel()._thinky.r;

if (Array.isArray(docs) === false) {
docs = [docs];
}

var conflict = 'error';

if (options && /^(error|update|replace)$/.test(options.conflict)) {
conflict = options.conflict;
}

var p = new Promise(function(resolve, reject) {
var insert = function() {
var promises = [];
Expand All @@ -991,27 +997,28 @@ Model.prototype.save = function(docs) {
}, reject);

var executeInsert = function () {
r.table(self.getTableName()).insert(docs).run().then(function(results) {
if ((results.errors === 0) && (results.inserted === docs.length)) {
util.tryCatch(function() {
var index = 0;
// Set the primary keys
for(var i=0; i<docs.length; i++) {
if (docs[i].hasOwnProperty(self._getModel()._pk) === false) {
docs[i][self._getModel()._pk] = results.generated_keys[index];
index++
}
}
// Create instances of the model
for(i=0; i<docs.length; i++) {
docs[i].setSaved()
}
resolve(docs);
}, reject)
}
else {
reject(new Error("An error occurred during the batch insert."))
r.table(self.getTableName()).insert(docs, { conflict: conflict }).run().then(function(results) {
if (results.errors) {
var e = new Error('An error occurred during the batch insert.');
e.firstError = results.first_error;
return reject(e);
}

util.tryCatch(function() {
var index = 0;
// Set the primary keys
for(var i=0; i<docs.length; i++) {
if (docs[i].hasOwnProperty(self._getModel()._pk) === false) {
docs[i][self._getModel()._pk] = results.generated_keys[index];
index++
}
}
// Create instances of the model
for(i=0; i<docs.length; i++) {
docs[i].setSaved()
}
resolve(docs);
}, reject)
}).error(reject);
}

Expand Down
34 changes: 34 additions & 0 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,45 @@ describe("Batch insert", function() {
done(new Error("Was expecting an error"));
}).error(function(e) {
assert.equal(e.message, "An error occurred during the batch insert.")
assert.ok(e.firstError, "Expected firstError to be set")
done();
});
});
});

it('Batch insert should not fail when options.conflict is set to `update`', function(done) {
var docs = [];
for(var i=0; i<10; i++) {
docs.push({num: i})
}
Model.save(docs, { conflict: 'update' }).then(function(result) {
assert.strictEqual(result, docs);
for(i=0; i<10; i++) {
assert.equal(typeof docs[i].id, 'string');
assert(docs[i].isSaved());
}
done();
}).error(function(e) {
done(e);
});
})

it('Batch insert should not fail when options.conflict is set to `replace`', function(done) {
var docs = [];
for(var i=0; i<10; i++) {
docs.push({num: i})
}
Model.save(docs, { conflict: 'replace' }).then(function(result) {
assert.strictEqual(result, docs);
for(i=0; i<10; i++) {
assert.equal(typeof docs[i].id, 'string');
assert(docs[i].isSaved());
}
done();
}).error(function(e) {
done(e);
});
})
});

describe("Joins", function() {
Expand Down