From bcdb7c90b4c88032c4ad66b90c323c150a67c078 Mon Sep 17 00:00:00 2001 From: Benjamin Pannell Date: Sun, 12 Jan 2014 21:25:03 +0200 Subject: [PATCH] Made working with $ projection operator possible in instances --- README.md | 13 +++++++++---- lib/Instance.js | 29 ++++++++++++++++++++++++----- package.json | 2 +- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 105a190..2fd0743 100644 --- a/README.md +++ b/README.md @@ -258,19 +258,24 @@ An instance represents a database object retrieved by Iridium, and will inherit ```javascript // Saves any changes made to the instance (only affects properties in the schema, or already retrieved from the DB) Instance.save(); -Instance.save(function(err, instance)); +Instance.save(function(err, instance) {}); // Executes the requested MongoDB changes on the current instance ({ $push: { sessions: 'session_key' }} etc.) Instance.save(mongoChanges); -Instance.save(mongoChanges, function(err, instance)); +Instance.save(mongoChanges, function(err, instance) {}); + +// Used for manipulating specific array elements, you can use extraConditions to select the array element to manipulate +// For example Instance.save({ array: { $elemMatch: { id: 1 }}}, { $inc: { 'array.$.hits': 1 }}); +Instance.save(extraConditions, mongoChanges); +Instance.save(extraConditions, mongoChanges, function(err,instance) {}) // Updates the instance's data to match the latest available data from the database Instance.update(); -Instance.update(function(err, instance)); +Instance.update(function(err, instance) {}); // Removes the instance from the database Instance.remove(); -Instance.remove(function(err)); +Instance.remove(function(err) {}); ``` ## Caching Framework diff --git a/lib/Instance.js b/lib/Instance.js index 601f0ff..68b304d 100644 --- a/lib/Instance.js +++ b/lib/Instance.js @@ -53,7 +53,7 @@ function Instance(model, doc, isNew) { Instance.prototype.__proto__ = EventEmitter.prototype; -Instance.prototype.save = function(changes, callback) { +Instance.prototype.save = function(conditions, changes, callback) { /// /// Saves changes made to the current instance to the database without waiting for a response /// @@ -69,10 +69,28 @@ Instance.prototype.save = function(changes, callback) { /// Saves changes made to the current instance to the database /// MongoDB changes query to be used instead of differential patching /// A function which is called when the save has been completed + /// + /// + /// Saves changes made to the current instance to the database + /// A set of conditions used to determine aspects of the document to update, merged with _id: ... + /// MongoDB changes query to be used instead of differential patching + /// + /// + /// Saves changes made to the current instance to the database + /// A set of conditions used to determine aspects of the document to update, merged with _id: ... + /// MongoDB changes query to be used instead of differential patching + /// A function which is called when the save has been completed + /// + + var args = Array.prototype.splice.call(arguments, 0); + conditions = {}; + changes = null; + callback = null; - if(!callback && typeof changes == 'function') { - callback = changes; - changes = null; + for(var i = 0; i < args.length; i++) { + if('function' == typeof args[i]) callback = args[i]; + else if(!changes) changes = args[i]; + else conditions = args[i]; } var onError = (function (err) { @@ -121,7 +139,8 @@ Instance.prototype.save = function(changes, callback) { if(err) return onError(err); this.emit('saving', this, changes); - var conditions = this.__state.model.uniqueConditions(this.__state.modified); + this.__state.model.toSource(conditions); + _.merge(conditions, this.__state.model.uniqueConditions(this.__state.modified)); this.__state.model.collection.update(conditions, changes, { w : 1 }, (function(err, changed) { if(err) return onError(err); diff --git a/package.json b/package.json index b9fe98c..b78db4a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "iridium", - "version": "2.8.0", + "version": "2.9.0", "author": "Benjamin Pannell ", "description": "A custom lightweight ORM for MongoDB designed for power-users", "homepage": "https://sierrasoftworks.com/iridium",