Skip to content

Commit

Permalink
Made working with $ projection operator possible in instances
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Jan 12, 2014
1 parent 790ba12 commit bcdb7c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
13 changes: 9 additions & 4 deletions README.md
Expand Up @@ -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
Expand Down
29 changes: 24 additions & 5 deletions lib/Instance.js
Expand Up @@ -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) {
/// <signature>
/// <summary>Saves changes made to the current instance to the database without waiting for a response</summary>
/// </signature>
Expand All @@ -69,10 +69,28 @@ Instance.prototype.save = function(changes, callback) {
/// <summary>Saves changes made to the current instance to the database</summary>
/// <param name="changes" type="Object">MongoDB changes query to be used instead of differential patching</param>
/// <param name="callback" type="Function">A function which is called when the save has been completed</param>
/// </signature>
/// <signature>
/// <summary>Saves changes made to the current instance to the database</summary>
/// <param name="conditions" type="Object">A set of conditions used to determine aspects of the document to update, merged with _id: ...</param>
/// <param name="changes" type="Object">MongoDB changes query to be used instead of differential patching</param>
/// </signature>
/// <signature>
/// <summary>Saves changes made to the current instance to the database</summary>
/// <param name="conditions" type="Object">A set of conditions used to determine aspects of the document to update, merged with _id: ...</param>
/// <param name="changes" type="Object">MongoDB changes query to be used instead of differential patching</param>
/// <param name="callback" type="Function">A function which is called when the save has been completed</param>
/// </signature>

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) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "iridium",
"version": "2.8.0",
"version": "2.9.0",
"author": "Benjamin Pannell <admin@sierrasoftworks.com>",
"description": "A custom lightweight ORM for MongoDB designed for power-users",
"homepage": "https://sierrasoftworks.com/iridium",
Expand Down

0 comments on commit bcdb7c9

Please sign in to comment.