Comparing version 1.0.6 to 1.1.0
@@ -490,13 +490,20 @@ var eventEmitter = require('events').EventEmitter; | ||
Document.prototype.save = function() { | ||
return this._save({}, false, {}); | ||
Document.prototype.save = function(callback) { | ||
return this._save({}, false, {}, callback); | ||
} | ||
Document.prototype.saveAll = function(modelToSave) { | ||
var saveAll = (modelToSave === undefined) ? true: false; | ||
modelToSave = modelToSave || {}; | ||
return this._save(modelToSave, saveAll, {}); | ||
Document.prototype.saveAll = function(modelToSave, callback) { | ||
var saveAll; | ||
if (typeof modelToSave === 'function') { | ||
callback = modelToSave; | ||
saveAll = true; | ||
modelToSave = {}; | ||
} | ||
else { | ||
saveAll = (modelToSave === undefined) ? true: false; | ||
modelToSave = modelToSave || {}; | ||
} | ||
return this._save(modelToSave, saveAll, {}, callback); | ||
} | ||
Document.prototype._save = function(modelToSave, saveAll, savedModel) { | ||
Document.prototype._save = function(modelToSave, saveAll, savedModel, callback) { | ||
//TOIMPROVE? How should we handle circular references outsides of joined fields? Now we throw with a maximum call stack size exceed | ||
@@ -760,3 +767,4 @@ var self = this; | ||
Document.prototype.__validate(result.new_val, model._schema, '', model._options, self) | ||
self.merge(result.new_val) | ||
self._merge(result.new_val) | ||
self._setOldValue(result.old_val); | ||
self.setSaved(); | ||
@@ -803,3 +811,4 @@ self.emit('saved', self); | ||
Document.prototype.__validate(result.new_val, model._schema, '', model._options, self) | ||
self.merge(result.new_val) | ||
self._merge(result.new_val) | ||
self._setOldValue(result.old_val); | ||
self.setSaved(); | ||
@@ -896,4 +905,17 @@ self.emit('saved', self); | ||
}); | ||
if (typeof callback === 'function') { | ||
p.then(function(result) { | ||
callback(null, result); | ||
}).error(function(error) { | ||
callback(error); | ||
}); | ||
} | ||
return p; | ||
} | ||
Document.prototype.getOldValue = function() { | ||
return this.__proto__.oldValue; | ||
} | ||
Document.prototype._setOldValue = function(value) { | ||
return this.__proto__.oldValue = value; | ||
} | ||
@@ -957,4 +979,4 @@ Document.prototype.isSaved = function() { | ||
// TL;DR: Delete self + links/foreign keys if possible | ||
Document.prototype.delete = function() { | ||
return this._delete({}, false, {}, true) | ||
Document.prototype.delete = function(callback) { | ||
return this._delete({}, false, {}, true, callback) | ||
} | ||
@@ -971,7 +993,15 @@ | ||
// TL;DR: Delete self + joined documents/links | ||
Document.prototype.deleteAll = function(modelToDelete) { | ||
var deleteAll = (modelToDelete === undefined) ? true: false; | ||
modelToDelete = modelToDelete || {}; | ||
Document.prototype.deleteAll = function(modelToDelete, callback) { | ||
var deleteAll; | ||
if (typeof modelToDelete === 'function') { | ||
callback = modelToDelete; | ||
deleteAll = true; | ||
modelToDelete = {}; | ||
} | ||
else { | ||
deleteAll = (modelToDelete === undefined) ? true: false; | ||
modelToDelete = modelToDelete || {}; | ||
} | ||
return this._delete(modelToDelete, deleteAll, {}, true) | ||
return this._delete(modelToDelete, deleteAll, {}, true, true, callback) | ||
} | ||
@@ -981,3 +1011,3 @@ | ||
// deleteSelf, default: true | ||
Document.prototype._delete = function(modelToDelete, deleteAll, deletedModel, deleteSelf, updateParents) { | ||
Document.prototype._delete = function(modelToDelete, deleteAll, deletedModel, deleteSelf, updateParents, callback) { | ||
var self = this; | ||
@@ -1198,3 +1228,3 @@ | ||
return new Promise(function(resolve, reject) { | ||
var p = new Promise(function(resolve, reject) { | ||
Promise.all(promises).then(function() { | ||
@@ -1206,2 +1236,10 @@ resolve(self); | ||
}) | ||
if (typeof callback === 'function') { | ||
p.then(function(result) { | ||
callback(null, result); | ||
}).error(function(error) { | ||
callback(error); | ||
}); | ||
} | ||
return p; | ||
} | ||
@@ -1212,3 +1250,3 @@ | ||
// Clean the database | ||
Document.prototype.purge = function() { | ||
Document.prototype.purge = function(callback) { | ||
var self = this; | ||
@@ -1313,3 +1351,3 @@ | ||
return new Promise(function(resolve, reject) { | ||
var p = new Promise(function(resolve, reject) { | ||
Promise.all(promises).then(function() { | ||
@@ -1319,5 +1357,13 @@ resolve(self); | ||
}) | ||
if (typeof callback === 'function') { | ||
p.then(function(result) { | ||
callback(null, result); | ||
}).error(function(error) { | ||
callback(error); | ||
}); | ||
} | ||
return p; | ||
} | ||
Document.prototype.merge = function(obj) { | ||
Document.prototype._merge = function(obj) { | ||
for(var key in this) { | ||
@@ -1336,2 +1382,16 @@ if (this.hasOwnProperty(key)) { | ||
Document.prototype.merge = function(obj) { | ||
for(var key in obj) { | ||
// Recursively merge only if both fields are objects, else we'll overwrite the field | ||
if (util.isPlainObject(obj[key]) && util.isPlainObject(this[key])) { | ||
Document.prototype.merge.call(this[key], obj[key]) | ||
} | ||
else { | ||
this[key] = obj[key]; | ||
} | ||
} | ||
return this; | ||
} | ||
module.exports = Document; |
@@ -19,10 +19,21 @@ var Promise = require('bluebird'); | ||
Query.prototype.run = function(options) { | ||
return this._execute(options, true); | ||
Query.prototype.run = function(options, callback) { | ||
if (typeof options === 'function') { | ||
return this._execute({}, true, options); | ||
} | ||
else { | ||
return this._execute(options, true, callback); | ||
} | ||
} | ||
Query.prototype.execute = function(options) { | ||
return this._execute(options, false); | ||
Query.prototype.execute = function(options, callback) { | ||
if (typeof options === 'function') { | ||
return this._execute({}, false, options); | ||
} | ||
else { | ||
return this._execute(options, false); | ||
} | ||
} | ||
Query.prototype._execute = function(options, parse) { | ||
Query.prototype._execute = function(options, parse, callback) { | ||
options = options || {}; | ||
@@ -79,2 +90,9 @@ var fullOptions = {groupFormat: 'raw'} | ||
} | ||
if (typeof callback === 'function') { | ||
p.then(function(result) { | ||
callback(null, result); | ||
}).error(function(error) { | ||
callback(error); | ||
}); | ||
} | ||
return p; | ||
@@ -81,0 +99,0 @@ } |
{ | ||
"name": "thinky", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "RethinkDB ORM for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/thinky.js", |
116191
2582