From ac636053c44634d97df8ae3c413d4b1280380785 Mon Sep 17 00:00:00 2001 From: Benjamin Pannell Date: Fri, 13 Dec 2013 13:40:52 +0200 Subject: [PATCH] Updated Model, Instance and Database constructors --- lib/Database.js | 2 ++ lib/Instance.js | 5 ++++- lib/Model.js | 8 +++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/Database.js b/lib/Database.js index fb761ca..52bcfaa 100644 --- a/lib/Database.js +++ b/lib/Database.js @@ -15,6 +15,8 @@ function Database(config) { "use strict"; + if(!(this instanceof Database)) return new Database(config); + this.connection = null; this.models = {}; this.plugins = []; diff --git a/lib/Instance.js b/lib/Instance.js index 12e0252..8482d44 100644 --- a/lib/Instance.js +++ b/lib/Instance.js @@ -23,6 +23,9 @@ function Instance(model, doc, isNew) { /// "use strict"; + + if(!(this instanceof Instance)) return new Instance(model, doc, isNew); + var $ = this; if(!isNew) model.fromSource(doc); @@ -130,7 +133,7 @@ function Instance(model, doc, isNew) { if (err) return callback(err); inserted = inserted[0]; - transform.down(options.transforms, inserted); + model.fromSource(inserted); oldDoc = newDoc = inserted; diff --git a/lib/Model.js b/lib/Model.js index 8847b91..c023600 100644 --- a/lib/Model.js +++ b/lib/Model.js @@ -20,6 +20,8 @@ function Model(database, collection, schema, options) { /// A JSON representation of the database schema /// Additional options configuring the behaviour of this model's instances + if(!(this instanceof Model)) return new Model(database, collection, schema, options); + if (!options) options = {}; _.defaults(options, { @@ -37,7 +39,7 @@ function Model(database, collection, schema, options) { ] }); - Object.defineProperty(this, 'preprocessor', { + Object.defineProperty(this, 'preprocessors', { value: new Concoction(options.preprocessors) }); @@ -88,14 +90,14 @@ Model.prototype.fromSource = function(document) { /// Applies the model's preprocessors to convert the document from the source /// The object to apply the preprocessors to - this.preprocessor.apply(document); + this.preprocessors.apply(document); }; Model.prototype.toSource = function(document) { /// Applies the model's preprocessors to convert the document from the source /// The object to apply the preprocessors to - this.preprocessor.reverse(document); + this.preprocessors.reverse(document); }; Model.prototype.wrap = function (document, isNew) {