Skip to content

Commit

Permalink
Updated Model to support new preprocessor framework
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Dec 3, 2013
1 parent b5baa54 commit 7a54337
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
/// <reference path="utils/validation.js"/>

var _ = require('lodash');
var Database = require('./Database');
var Instance = require('./Instance');
var validate = require('./utils/validation');
var transform = require('./utils/transforms');
var Preprocessors = require('./preprocessors');
var ObjectID = require('mongodb').ObjectID;


Expand All @@ -20,15 +21,20 @@ function Model(database, collection, schema, options) {
/// <param name="options" type="Object">Additional options configuring the behaviour of this model's instances</param>

if (!options) options = {};
if (!options.hooks) options.hooks = {};

options.transforms = options.transforms || {};

_.defaults(options.transforms, {
_id: {
$down: function (value) { return new ObjectID(value.id).toHexString(); },
$up: function (value) { return value ? ObjectID.createFromHexString(value) : undefined; }
}
_.defaults(options, {
hooks: {},
preprocessors: [
new Preprocessors.Rename({
_id: 'id'
}),
new Preprocessors.Convert({
id: {
$down: function (value) { return new ObjectID(value.id).toHexString(); },
$up: function (value) { return value ? ObjectID.createFromHexString(value) : undefined; }
}
})
]
});

options.schema = schema;
Expand All @@ -50,6 +56,22 @@ function Model(database, collection, schema, options) {
});
}

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>

for(var i = 0; i < this.options.preprocessors.length; i++)
this.options.preprocessors[i].fromSource(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>

for(var i = this.options.preprocessors.length - 1; i >= 0; i--)
this.options.preprocessors[i].toSource(document);
};

Model.prototype.wrap = function (document, isNew) {
/// <signature>
/// <summary>Wraps the given database object in this model's Instance wrapper</summary>
Expand All @@ -63,7 +85,7 @@ Model.prototype.wrap = function (document, isNew) {
/// <returns type="Instance"/>
/// </signature>

return new Instance(this.collection, document, this.options, isNew);
return new Instance(this, document, isNew);
};

Model.prototype.find = function (conditions, callback) {
Expand Down Expand Up @@ -102,7 +124,7 @@ Model.prototype.find = function (conditions, callback) {
}
}

transform.up(this.options.transforms, conditions);
this.toSource(conditions);

this.collection.find(conditions).toArray(function (err, results) {
if (err) return callback(err);
Expand Down Expand Up @@ -147,7 +169,7 @@ Model.prototype.findOne = Model.prototype.get = function (conditions, callback)
}
}

transform.up(this.options.transforms, conditions);
this.toSource(conditions);

this.collection.findOne(conditions, function (err, results) {
if (err) return callback(err);
Expand Down Expand Up @@ -233,7 +255,7 @@ Model.prototype.insert = Model.prototype.create = function (object, callback) {
}

// Transform the object
transform.up($.options.transforms, obj);
$.toSource(obj);

prepped.push(obj);
return prepNext();
Expand Down Expand Up @@ -279,7 +301,7 @@ Model.prototype.update = function (conditions, changes, callback) {
}
}

transform.up(this.options.transforms, conditions);
this.toSource(conditions);

this.collection.update(conditions, changes, { w: callback ? 1 : 0, multi: true }, callback);
};
Expand Down Expand Up @@ -312,7 +334,7 @@ Model.prototype.count = function (conditions, callback) {
}
}

transform.up(this.options.transforms, conditions);
this.toSource(conditions);

this.collection.count(conditions, callback);
};
Expand Down Expand Up @@ -352,7 +374,7 @@ Model.prototype.remove = function (conditions, callback) {
}
}

transform.up(this.options.transforms, conditions);
this.toSource(conditions);

this.collection.remove(conditions, { w: callback ? 1 : 0 }, callback);
};
Expand Down

0 comments on commit 7a54337

Please sign in to comment.