Skip to content

Commit

Permalink
More improvements to _id inferrence heurestics
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Dec 13, 2013
1 parent c7dff1f commit 1bafab7
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions lib/Model.js
Expand Up @@ -32,14 +32,14 @@ function Model(database, collection, schema, options) {
}),
new Concoction.Convert({
id: {
apply: function (value) { return new ObjectID(value.id).toHexString(); },
apply: function (value) { return (value && value.id) ? new ObjectID(value.id).toHexString() : value; },
reverse: function (value) { return value ? ObjectID.createFromHexString(value) : undefined; }
}
})
]
});

Object.defineProperty(this, 'preprocessors', {
Object.defineProperty(this, 'preprocessor', {
value: new Concoction(options.preprocessors)
});

Expand Down Expand Up @@ -90,14 +90,14 @@ Model.prototype.fromSource = function(document) {
/// <summary>Applies the model's preprocessors to convert the document from the source</summary>
/// <param name="document" type="Object">The object to apply the preprocessors to</param>

this.preprocessors.apply(document);
this.preprocessor.apply(document);
};

Model.prototype.toSource = function(document) {
/// <summary>Applies the model's preprocessors to convert the document from the source</summary>
/// <param name="document" type="Object">The object to apply the preprocessors to</param>

this.preprocessors.reverse(document);
this.preprocessor.reverse(document);
};

Model.prototype.uniqueConditions = function(document) {
Expand All @@ -114,6 +114,37 @@ Model.prototype.uniqueConditions = function(document) {
return conditions;
};

Model.prototype.downstreamID = function(id) {
/// <signature>
/// <summary>Gets the downstream _id field's identifier after preprocessing</summary>
/// <returns type="String"/>
/// </signature>
/// <signature>
/// <summary>Gets the set of conditions representing the downstream _id field for the given downstream identifier</summary>
/// <param name="id" type="Mixed">The identifier to created the conditions from</param>
/// <returns type="Object"/>
/// </signature>

var test_doc = {
_id: true
};

this.fromSource(test_doc);

var _id = null;
for(var k in test_doc)
if(test_doc[k] === true) {
_id = k;
break;
}

if(id) {
var conditions = {};
conditions[_id] = id;
return conditions;
} else return _id;
};

Model.prototype.wrap = function (document, isNew) {
/// <signature>
/// <summary>Wraps the given database object in this model's Instance wrapper</summary>
Expand Down Expand Up @@ -152,8 +183,7 @@ Model.prototype.find = function (conditions, callback) {
}

var $ = this;
if (!_.isPlainObject(conditions)) conditions = { _id: conditions };

if (!_.isPlainObject(conditions)) conditions = this.downstreamID(conditions);
this.toSource(conditions);

this.collection.find(conditions).toArray(function (err, results) {
Expand Down Expand Up @@ -185,7 +215,7 @@ Model.prototype.findOne = Model.prototype.get = function (conditions, callback)
}

var $ = this;
if (!_.isPlainObject(conditions)) conditions = { _id: conditions };
if (!_.isPlainObject(conditions)) conditions = this.downstreamID(conditions);

this.toSource(conditions);

Expand Down Expand Up @@ -342,7 +372,7 @@ Model.prototype.remove = function (conditions, callback) {
conditions = {};
}

if (!_.isPlainObject(conditions)) conditions = { _id: conditions };
if (!_.isPlainObject(conditions)) conditions = this.downstreamID(conditions);

this.toSource(conditions);

Expand Down

0 comments on commit 1bafab7

Please sign in to comment.