Skip to content

Commit

Permalink
Don't allow changes to _id in instances
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Jan 23, 2014
1 parent b52262c commit ad8c1fc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
40 changes: 20 additions & 20 deletions lib/Instance.js
@@ -1,5 +1,3 @@
/// <reference path="../nodelib/node.js"/>
/// <reference path="utils/transforms.js"/>
/// <reference path="utils/validation.js"/>

var ObjectID = require('mongodb').ObjectID,
Expand Down Expand Up @@ -37,7 +35,7 @@ function Instance(model, doc, isNew) {
isNew: isNew,
original: _.cloneDeep(doc),
modified: _.cloneDeep(doc)
}
};

this.__extendSchema();

Expand All @@ -52,7 +50,7 @@ function Instance(model, doc, isNew) {
this.emit('ready', this);
}

Instance.prototype.__proto__ = EventEmitter.prototype;
Instance.prototype.prototype = EventEmitter.prototype;

Object.defineProperty(Instance.prototype, 'document', {
get: function() { return this.__state.modified; },
Expand Down Expand Up @@ -141,6 +139,8 @@ Instance.prototype.save = function(conditions, changes, callback) {
this.__state.model.toSource(original);
this.__state.model.toSource(modified);

if(modified._id != original._id) return onError(new Error('Cannot change _id, if you want to create a new document please use Model.create'));

changes = Instance.diff(original, modified);
}

Expand All @@ -153,15 +153,15 @@ Instance.prototype.save = function(conditions, changes, callback) {
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);
if(!changed) return (callback || function() { })(null, this);
this.__state.model.collection.update(conditions, changes, { w: 1 }, (function (err, changed) {
if (err) return onError(err);
if (!changed) return (callback || function () { })(null, this);

var conditions = this.__state.model.uniqueConditions(this.__state.modified);
this.__state.model.collection.findOne(conditions, (function(err, latest) {
if(err) return onError(err);
this.__state.model.collection.findOne(conditions, (function (err, latest) {
if (err) return onError(err);

this.__state.model.onRetrieved(conditions, latest, callback || function() { }, (function(value) {
this.__state.model.onRetrieved(conditions, latest, callback || function () { }, (function (value) {
this.__state.model.fromSource(value);
this.__state.original = _.cloneDeep(value);
this.__state.modified = _.cloneDeep(value);
Expand All @@ -170,7 +170,7 @@ Instance.prototype.save = function(conditions, changes, callback) {
return this;
}).bind(this));
}).bind(this));
}).bind(this))
}).bind(this));
}).bind(this));
};

Expand Down Expand Up @@ -249,7 +249,7 @@ Instance.prototype.select = function(collection, filter) {
return results;
};

Instance.prototype.first = function(collection, filter) {
Instance.prototype.first = function (collection, filter) {
/// <signature>
/// <summary>Finds the first element in the object for which the filter function returns truey</summary>
/// <param name="collection" type="Object">The array to search through for matches</param>
Expand All @@ -265,25 +265,25 @@ Instance.prototype.first = function(collection, filter) {

var result;

_.each(collection, function(value, key) {
if(filter.call(this, value, key)) {
_.each(collection, function (value, key) {
if (filter.call(this, value, key)) {
result = value;
return false;
}
}, this);

return result;
}
};

Instance.prototype.__extendSchema = function() {
var $ = this;

var schema = {};
var schema = {}, property;

for(var property in this.__state.modified)
for(property in this.__state.modified)
schema[property] = false;

for(var property in this.__state.model.schema)
for(property in this.__state.model.schema)
if(schema[property]) delete schema[property];

for(var targetProperty in schema) {
Expand All @@ -307,7 +307,7 @@ Instance.prototype.__extendSchema = function() {
Instance.forModel = function(model) {
/// <summary>Creates an instance wrapper for the specified model</summary>
/// <param name="model" type="Model">The model which the instance wraps</param>
/// <return type="Function"/>
/// <returns type="Function"/>

function ModelInstance(doc, isNew) {
/// <signature>
Expand Down Expand Up @@ -351,7 +351,7 @@ Instance.forModel = function(model) {
});
});

proto.__proto__ = Instance.prototype;
proto.prototype = Instance.prototype;

_.each(model.options.methods, function(method, name) {
proto[name] = method;
Expand Down
6 changes: 3 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "iridium",
"version": "2.10.1",
"version": "2.10.2",
"author": "Benjamin Pannell <admin@sierrasoftworks.com>",
"description": "A custom lightweight ORM for MongoDB designed for power-users",
"homepage": "https://sierrasoftworks.com/iridium",
Expand All @@ -23,8 +23,8 @@
"mongodb": "*",
"lodash": "*",
"async": "*",
"concoction": "*",
"debug": "*"
"concoction": "~1.0.0",
"debug": "~0.7.4"
},
"devDependencies": {
"mocha": "*",
Expand Down

0 comments on commit ad8c1fc

Please sign in to comment.