diff --git a/dist/lib/Decorators.js b/dist/lib/Decorators.js index b6cd4a0..c1d0be0 100644 --- a/dist/lib/Decorators.js +++ b/dist/lib/Decorators.js @@ -35,9 +35,9 @@ function Property() { name = property; target = target.constructor; } - asType = args.pop(); + asType = args.pop() || false; target.schema = _.clone(target.schema || {}); - if (!required) + if (!required && typeof asType !== 'boolean') target.schema[name] = { $required: required, $type: asType }; else target.schema[name] = asType; diff --git a/dist/lib/Decorators.js.map b/dist/lib/Decorators.js.map index f878d1f..cc8c35f 100644 --- a/dist/lib/Decorators.js.map +++ b/dist/lib/Decorators.js.map @@ -1 +1 @@ -{"version":3,"sources":["lib/Decorators.ts"],"names":["Collection","Index","Validate","Property","Transform","ObjectID"],"mappings":"AAAA,AACA,4CAD4C;AAC5C,IAAO,OAAO,WAAW,SAAS,CAAC,CAAC;AACpC,IAAO,CAAC,WAAW,QAAQ,CAAC,CAAC;AAC7B,IAAO,MAAM,WAAW,QAAQ,CAAC,CAAC;AAMlC,oBAA2B,IAAY;IACtCA,MAAMA,CAACA,UAASA,MAAwCA;QACvD,MAAM,CAAC,UAAU,GAAG,IAAI,CAAC;IAC1B,CAAC,CAACA;AACHA,CAACA;AAJe,kBAAU,aAIzB,CAAA;AAED,eAAsB,IAAwB,EAAE,OAA8B;IAC7EC,MAAMA,CAACA,UAASA,MAAuCA;QACtD,MAAM,CAAC,OAAO,GAAG,CAAC,MAAM,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC,MAAM,CAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,IAAI,EAAE,EAAE,CAAC,CAAC;IAC/F,CAAC,CAAAA;AACFA,CAACA;AAJe,aAAK,QAIpB,CAAA;AAED,kBAAyB,OAAY,EAAE,QAAiE;IACvGC,MAAMA,CAACA,UAASA,MAAuCA;QACtD,MAAM,CAAC,UAAU,GAAG,CAAC,MAAM,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,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B,MAA2B,EAAE,IAAyB;IAC/EC,MAAMA,CAACA,UAASA,MAAWA,EAAEA,QAAgBA;QAC5C,MAAM,CAAC,WAAW,CAAC,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,WAAW,CAAC,UAAU,IAAI,EAAE,CAAC,CAAA;QAC5E,MAAM,CAAC,WAAW,CAAC,UAAU,CAAC,QAAQ,CAAC,GAAG;YACzC,MAAM,EAAE,MAAM;YACd,IAAI,EAAE,IAAI;SACV,CAAC;IACH,CAAC,CAACA;AACHA,CAACA;AARe,iBAAS,YAQxB,CAAA;AAED,kBAAyB,MAAwC,EAAE,IAAY;IAC9EC,MAAMA,CAACA,WAAWA,CAACA,MAAMA,GAAWA,CAACA,CAACA,KAAKA,CAACA,MAAMA,CAACA,WAAWA,CAACA,MAAMA,IAAIA,EAAEA,CAACA,CAACA;IAC7EA,MAAMA,CAACA,WAAWA,CAACA,MAAMA,CAACA,IAAIA,CAACA,GAAGA,OAAOA,CAACA,QAAQA,CAACA;AACpDA,CAACA;AAHe,gBAAQ,WAGvB,CAAA","file":"lib/Decorators.js","sourcesContent":["/// \r\nimport MongoDB = require('mongodb');\r\nimport _ = require('lodash');\r\nimport skmatc = require('skmatc');\r\nimport Instance from './Instance';\r\nimport {Index, IndexSpecification} from './Index';\r\nimport {Schema} from './Schema';\r\nimport InstanceImplementation from './InstanceInterface';\r\n\r\nexport function Collection(name: string) {\r\n\treturn function(target: InstanceImplementation) {\r\n\t\ttarget.collection = name;\r\n\t};\r\n}\r\n\r\nexport function Index(spec: IndexSpecification, options?: MongoDB.IndexOptions) {\r\n\treturn function(target: InstanceImplementation) {\r\n\t\ttarget.indexes = (target.indexes || []).concat({ spec: spec, options: options || {} });\r\n\t}\r\n}\r\n\r\nexport function Validate(forType: any, validate: (schema: any, data: any, path: string) => Skmatc.Result) {\r\n\treturn function(target: InstanceImplementation) {\r\n\t\ttarget.validators = (target.validators || []).concat(skmatc.create(schema => schema === forType, validate));\r\n\t}\r\n}\r\n\r\nexport function Property(asType: any, required?: boolean): (target: { constructor: Function }, name: string) => void;\r\nexport function Property(name: string, asType: any, required?: boolean): (target: Function) => void;\r\nexport function Property(...args: any[]): (target: any, name?: string) => void {\r\n\tlet name = null,\r\n\t\tasType = false,\r\n\t\trequired = true;\r\n\t\r\n\tif (args.length > 1 && typeof args[args.length - 1] === 'boolean')\r\n\t\trequired = args.pop();\r\n\t\r\n\treturn function(target: any, property?: string) {\r\n\t\tif (!property) name = args.shift();\r\n\t\telse {\r\n\t\t\tname = property;\r\n\t\t\ttarget = target.constructor;\r\n\t\t}\r\n\t\tasType = args.pop();\r\n\t\t\r\n\t\ttarget.schema = _.clone(target.schema || {});\r\n\t\tif(!required) target.schema[name] = { $required: required, $type: asType };\r\n\t\telse target.schema[name] = asType;\r\n\t}\r\n}\r\n\r\nexport function Transform(fromDB: (value: any) => any, toDB: (value: any) => any) {\r\n\treturn function(target: any, property: string) {\r\n\t\ttarget.constructor.transforms = _.clone(target.constructor.transforms || {})\r\n\t\ttarget.constructor.transforms[property] = {\r\n\t\t\tfromDB: fromDB,\r\n\t\t\ttoDB: toDB\r\n\t\t};\r\n\t};\r\n}\r\n\r\nexport function ObjectID(target: { constructor: typeof Instance }, name: string) {\r\n\ttarget.constructor.schema = _.clone(target.constructor.schema || {});\r\n\ttarget.constructor.schema[name] = MongoDB.ObjectID;\r\n}"],"sourceRoot":"/source/"} \ No newline at end of file +{"version":3,"sources":["lib/Decorators.ts"],"names":["Collection","Index","Validate","Property","Transform","ObjectID"],"mappings":"AAAA,AACA,4CAD4C;AAC5C,IAAO,OAAO,WAAW,SAAS,CAAC,CAAC;AACpC,IAAO,CAAC,WAAW,QAAQ,CAAC,CAAC;AAC7B,IAAO,MAAM,WAAW,QAAQ,CAAC,CAAC;AAMlC,oBAA2B,IAAY;IACtCA,MAAMA,CAACA,UAASA,MAAwCA;QACvD,MAAM,CAAC,UAAU,GAAG,IAAI,CAAC;IAC1B,CAAC,CAACA;AACHA,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js","sourcesContent":["/// \r\nimport MongoDB = require('mongodb');\r\nimport _ = require('lodash');\r\nimport skmatc = require('skmatc');\r\nimport Instance from './Instance';\r\nimport {Index, IndexSpecification} from './Index';\r\nimport {Schema} from './Schema';\r\nimport InstanceImplementation from './InstanceInterface';\r\n\r\nexport function Collection(name: string) {\r\n\treturn function(target: InstanceImplementation) {\r\n\t\ttarget.collection = name;\r\n\t};\r\n}\r\n\r\nexport function Index(spec: IndexSpecification, options?: MongoDB.IndexOptions) {\r\n\treturn function(target: InstanceImplementation) {\r\n\t\ttarget.indexes = (target.indexes || []).concat({ spec: spec, options: options || {} });\r\n\t}\r\n}\r\n\r\nexport function Validate(forType: any, validate: (schema: any, data: any, path: string) => Skmatc.Result) {\r\n\treturn function(target: InstanceImplementation) {\r\n\t\ttarget.validators = (target.validators || []).concat(skmatc.create(schema => schema === forType, validate));\r\n\t}\r\n}\r\n\r\nexport function Property(asType: any, required?: boolean): (target: { constructor: Function }, name: string) => void;\r\nexport function Property(name: string, asType: any, required?: boolean): (target: Function) => void;\r\nexport function Property(...args: any[]): (target: any, name?: string) => void {\r\n\tlet name = null,\r\n\t\tasType = false,\r\n\t\trequired = true;\r\n\t\r\n\tif (args.length > 1 && typeof args[args.length - 1] === 'boolean')\r\n\t\trequired = args.pop();\r\n\t\r\n\treturn function(target: any, property?: string) {\r\n\t\tif (!property) name = args.shift();\r\n\t\telse {\r\n\t\t\tname = property;\r\n\t\t\ttarget = target.constructor;\r\n\t\t}\r\n\t\tasType = args.pop() || false;\r\n\t\t\r\n\t\ttarget.schema = _.clone(target.schema || {});\r\n\t\tif(!required && typeof asType !== 'boolean') target.schema[name] = { $required: required, $type: asType };\r\n\t\telse target.schema[name] = asType;\r\n\t}\r\n}\r\n\r\nexport function Transform(fromDB: (value: any) => any, toDB: (value: any) => any) {\r\n\treturn function(target: any, property: string) {\r\n\t\ttarget.constructor.transforms = _.clone(target.constructor.transforms || {})\r\n\t\ttarget.constructor.transforms[property] = {\r\n\t\t\tfromDB: fromDB,\r\n\t\t\ttoDB: toDB\r\n\t\t};\r\n\t};\r\n}\r\n\r\nexport function ObjectID(target: { constructor: typeof Instance }, name: string) {\r\n\ttarget.constructor.schema = _.clone(target.constructor.schema || {});\r\n\ttarget.constructor.schema[name] = MongoDB.ObjectID;\r\n}"],"sourceRoot":"/source/"} \ No newline at end of file diff --git a/dist/lib/ModelHelpers.js b/dist/lib/ModelHelpers.js index 100fd27..a8d4e9c 100644 --- a/dist/lib/ModelHelpers.js +++ b/dist/lib/ModelHelpers.js @@ -3,8 +3,10 @@ var Omnom_1 = require('./utils/Omnom'); var _ = require('lodash'); var ModelHelpers = (function () { function ModelHelpers(model) { + var _this = this; this.model = model; this._validator = new skmatc(model.schema); + model.validators.forEach(function (validator) { return _this._validator.register(validator); }); } /** * Validates a document to ensure that it matches the model's ISchema requirements @@ -36,18 +38,6 @@ var ModelHelpers = (function () { document[property] = this.model.transforms[property].toDB(document[property]); return document; }; - /** - * Converts the given document from its database form into a form for local consumption - * using the transforms defined on the model. - * @param {any} document The document to be converted - * @returns {any} A new document cloned from the original and transformed - */ - ModelHelpers.prototype.transformFromDB = function (document) { - for (var property in this.model.transforms) - if (document.hasOwnProperty(property)) - document[property] = this.model.transforms[property].fromDB(document[property]); - return document; - }; /** * Converts the given document to its database form into a form * using the transforms defined on the model. @@ -58,16 +48,6 @@ var ModelHelpers = (function () { var doc = _.cloneDeep(document); return this.transformToDB(doc); }; - /** - * Converts the given document from its database form into a form for local consumption - * using the transforms defined on the model. - * @param {any} document The document to be converted - * @returns {any} A new document cloned from the original and transformed - */ - ModelHelpers.prototype.convertFromDB = function (document) { - var doc = _.cloneDeep(document); - return this.transformFromDB(doc); - }; /** * Performs a diff operation between two documents and creates a MongoDB changes object to represent the differences * @param {any} original The original document prior to changes being made diff --git a/dist/lib/ModelHelpers.js.map b/dist/lib/ModelHelpers.js.map index b2e9e5d..051c1b1 100644 --- a/dist/lib/ModelHelpers.js.map +++ b/dist/lib/ModelHelpers.js.map @@ -1 +1 @@ -{"version":3,"sources":["lib/ModelHelpers.ts"],"names":["ModelHelpers","ModelHelpers.constructor","ModelHelpers.validate","ModelHelpers.wrapDocument","ModelHelpers.transformToDB","ModelHelpers.transformFromDB","ModelHelpers.convertToDB","ModelHelpers.convertFromDB","ModelHelpers.diff"],"mappings":"AAGA,IAAO,MAAM,WAAW,QAAQ,CAAC,CAAC;AAClC,sBAAkB,eAAe,CAAC,CAAA;AAClC,IAAO,CAAC,WAAW,QAAQ,CAAC,CAAC;AAG7B;IACIA,sBAAmBA,KAAkCA;QAAlCC,UAAKA,GAALA,KAAKA,CAA6BA;QACjDA,IAAIA,CAACA,UAAUA,GAAGA,IAAIA,MAAMA,CAACA,KAAKA,CAACA,MAAMA,CAACA,CAACA;IAC/CA,CAACA;IAIDD;;;;OAIGA;IACHA,+BAAQA,GAARA,UAASA,QAAmBA;QACxBE,MAAMA,CAACA,IAAIA,CAACA,UAAUA,CAACA,QAAQA,CAACA,QAAQA,CAACA,CAACA;IAC9CA,CAACA;IAEDF;;;;;;OAMGA;IACHA,mCAAYA,GAAZA,UAAaA,QAAmBA,EAAEA,KAAeA,EAAEA,SAAmBA;QAClEG,MAAMA,CAACA,IAAIA,IAAIA,CAACA,KAAKA,CAACA,QAAQA,CAACA,QAAQA,EAAEA,KAAKA,EAAEA,SAASA,CAACA,CAACA;IAC/DA,CAACA;IAEDH;;;;;OAKGA;IACHA,oCAAaA,GAAbA,UAAiBA,QAAWA;QACxBI,GAAGA,CAACA,CAACA,GAAGA,CAACA,QAAQA,IAAIA,IAAIA,CAACA,KAAKA,CAACA,UAAUA,CAACA;YACvCA,EAAEA,CAAAA,CAACA,QAAQA,CAACA,cAAcA,CAACA,QAAQA,CAACA,CAACA;gBACjCA,QAAQA,CAACA,QAAQA,CAACA,GAAGA,IAAIA,CAACA,KAAKA,CAACA,UAAUA,CAACA,QAAQA,CAACA,CAACA,IAAIA,CAACA,QAAQA,CAACA,QAAQA,CAACA,CAACA,CAACA;QACtFA,MAAMA,CAACA,QAAQA,CAACA;IACpBA,CAACA;IAEDJ;;;;;OAKGA;IACHA,sCAAeA,GAAfA,UAAmBA,QAAWA;QAC1BK,GAAGA,CAACA,CAACA,GAAGA,CAACA,QAAQA,IAAIA,IAAIA,CAACA,KAAKA,CAACA,UAAUA,CAACA;YACvCA,EAAEA,CAACA,CAACA,QAAQA,CAACA,cAAcA,CAACA,QAAQA,CAACA,CAACA;gBAClCA,QAAQA,CAACA,QAAQA,CAACA,GAAGA,IAAIA,CAACA,KAAKA,CAACA,UAAUA,CAACA,QAAQA,CAACA,CAACA,MAAMA,CAACA,QAAQA,CAACA,QAAQA,CAACA,CAACA,CAACA;QACxFA,MAAMA,CAACA,QAAQA,CAACA;IACpBA,CAACA;IAEDL;;;;;OAKGA;IACHA,kCAAWA,GAAXA,UAAeA,QAAWA;QACtBM,IAAIA,GAAGA,GAAMA,CAACA,CAACA,SAASA,CAACA,QAAQA,CAACA,CAACA;QACnCA,MAAMA,CAACA,IAAIA,CAACA,aAAaA,CAACA,GAAGA,CAACA,CAACA;IACnCA,CAACA;IAEDN;;;;;OAKGA;IACHA,oCAAaA,GAAbA,UAAiBA,QAAWA;QACxBO,IAAIA,GAAGA,GAAMA,CAACA,CAACA,SAASA,CAACA,QAAQA,CAACA,CAACA;QACnCA,MAAMA,CAACA,IAAIA,CAACA,eAAeA,CAACA,GAAGA,CAACA,CAACA;IACrCA,CAACA;IAEDP;;;;OAIGA;IACHA,2BAAIA,GAAJA,UAAKA,QAAmBA,EAAEA,QAAmBA;QACzCQ,IAAIA,KAAKA,GAAGA,IAAIA,eAAKA,EAAEA,CAACA;QACxBA,KAAKA,CAACA,IAAIA,CAACA,QAAQA,EAAEA,QAAQA,CAACA,CAACA;QAC/BA,MAAMA,CAACA,KAAKA,CAACA,OAAOA,CAACA;IACzBA,CAACA;IACLR,mBAACA;AAADA,CArFA,AAqFCA,IAAA;AArFD,8BAqFC,CAAA","file":"lib/ModelHelpers.js","sourcesContent":["/// \r\nimport MongoDB = require('mongodb');\r\nimport Model from './Model';\r\nimport skmatc = require('skmatc');\r\nimport Omnom from './utils/Omnom';\r\nimport _ = require('lodash');\r\nimport Bluebird = require('bluebird');\r\n\r\nexport default class ModelHelpers {\r\n constructor(public model: Model) {\r\n this._validator = new skmatc(model.schema);\r\n }\r\n\r\n private _validator: Skmatc.Skmatc;\r\n\r\n /**\r\n * Validates a document to ensure that it matches the model's ISchema requirements\r\n * @param {any} document The document to validate against the ISchema\r\n * @returns {SkmatcCore.IResult} The result of the validation\r\n */\r\n validate(document: TDocument): Skmatc.Result {\r\n return this._validator.validate(document);\r\n }\r\n\r\n /**\r\n * Wraps the given document in an instance wrapper for use throughout the application\r\n * @param {any} document The document to be wrapped as an instance\r\n * @param {Boolean} isNew Whether the instance originated from the database or was created by the application\r\n * @param {Boolean} isPartial Whether the document supplied contains all information present in the database\r\n * @returns {any} An instance which wraps this document\r\n */\r\n wrapDocument(document: TDocument, isNew?: boolean, isPartial?: boolean): TInstance {\r\n return new this.model.Instance(document, isNew, isPartial);\r\n }\r\n \r\n /**\r\n * Converts the given document to its database form into a form\r\n * using the transforms defined on the model.\r\n * @param {any} document The document to be converted\r\n * @returns {any} A new document cloned from the original and transformed\r\n */\r\n transformToDB(document: T): T {\r\n for (var property in this.model.transforms)\r\n if(document.hasOwnProperty(property))\r\n document[property] = this.model.transforms[property].toDB(document[property]);\r\n return document;\r\n }\r\n \r\n /**\r\n * Converts the given document from its database form into a form for local consumption\r\n * using the transforms defined on the model.\r\n * @param {any} document The document to be converted\r\n * @returns {any} A new document cloned from the original and transformed\r\n */\r\n transformFromDB(document: T): T {\r\n for (var property in this.model.transforms)\r\n if (document.hasOwnProperty(property))\r\n document[property] = this.model.transforms[property].fromDB(document[property]);\r\n return document;\r\n }\r\n \r\n /**\r\n * Converts the given document to its database form into a form\r\n * using the transforms defined on the model.\r\n * @param {any} document The document to be converted\r\n * @returns {any} A new document cloned from the original and transformed\r\n */\r\n convertToDB(document: T): T {\r\n var doc: T = _.cloneDeep(document);\r\n return this.transformToDB(doc);\r\n }\r\n \r\n /**\r\n * Converts the given document from its database form into a form for local consumption\r\n * using the transforms defined on the model.\r\n * @param {any} document The document to be converted\r\n * @returns {any} A new document cloned from the original and transformed\r\n */\r\n convertFromDB(document: T): T {\r\n var doc: T = _.cloneDeep(document);\r\n return this.transformFromDB(doc);\r\n }\r\n\r\n /**\r\n * Performs a diff operation between two documents and creates a MongoDB changes object to represent the differences\r\n * @param {any} original The original document prior to changes being made\r\n * @param {any} modified The document after changes were made\r\n */\r\n diff(original: TDocument, modified: TDocument): any {\r\n var omnom = new Omnom();\r\n omnom.diff(original, modified);\r\n return omnom.changes;\r\n }\r\n}\r\n"],"sourceRoot":"/source/"} \ No newline at end of file +{"version":3,"sources":["lib/ModelHelpers.ts"],"names":["ModelHelpers","ModelHelpers.constructor","ModelHelpers.validate","ModelHelpers.wrapDocument","ModelHelpers.transformToDB","ModelHelpers.convertToDB","ModelHelpers.diff"],"mappings":"AAGA,IAAO,MAAM,WAAW,QAAQ,CAAC,CAAC;AAClC,sBAAkB,eAAe,CAAC,CAAA;AAClC,IAAO,CAAC,WAAW,QAAQ,CAAC,CAAC;AAG7B;IACIA,sBAAmBA,KAAkCA;QADzDC,iBA8DCA;QA7DsBA,UAAKA,GAALA,KAAKA,CAA6BA;QACjDA,IAAIA,CAACA,UAAUA,GAAGA,IAAIA,MAAMA,CAACA,KAAKA,CAACA,MAAMA,CAACA,CAACA;QAC3CA,KAAKA,CAACA,UAAUA,CAACA,OAAOA,CAACA,UAAAA,SAASA,IAAIA,OAAAA,KAAIA,CAACA,UAAUA,CAACA,QAAQA,CAACA,SAASA,CAACA,EAAnCA,CAAmCA,CAACA,CAACA;IAC/EA,CAACA;IAIDD;;;;OAIGA;IACHA,+BAAQA,GAARA,UAASA,QAAmBA;QACxBE,MAAMA,CAACA,IAAIA,CAACA,UAAUA,CAACA,QAAQA,CAACA,QAAQA,CAACA,CAACA;IAC9CA,CAACA;IAEDF;;;;;;OAMGA;IACHA,mCAAYA,GAAZA,UAAaA,QAAmBA,EAAEA,KAAeA,EAAEA,SAAmBA;QAClEG,MAAMA,CAACA,IAAIA,IAAIA,CAACA,KAAKA,CAACA,QAAQA,CAACA,QAAQA,EAAEA,KAAKA,EAAEA,SAASA,CAACA,CAACA;IAC/DA,CAACA;IAEDH;;;;;OAKGA;IACHA,oCAAaA,GAAbA,UAAiBA,QAAWA;QACxBI,GAAGA,CAACA,CAACA,GAAGA,CAACA,QAAQA,IAAIA,IAAIA,CAACA,KAAKA,CAACA,UAAUA,CAACA;YACvCA,EAAEA,CAAAA,CAACA,QAAQA,CAACA,cAAcA,CAACA,QAAQA,CAACA,CAACA;gBACjCA,QAAQA,CAACA,QAAQA,CAACA,GAAGA,IAAIA,CAACA,KAAKA,CAACA,UAAUA,CAACA,QAAQA,CAACA,CAACA,IAAIA,CAACA,QAAQA,CAACA,QAAQA,CAACA,CAACA,CAACA;QACtFA,MAAMA,CAACA,QAAQA,CAACA;IACpBA,CAACA;IAEDJ;;;;;OAKGA;IACHA,kCAAWA,GAAXA,UAAeA,QAAWA;QACtBK,IAAIA,GAAGA,GAAMA,CAACA,CAACA,SAASA,CAACA,QAAQA,CAACA,CAACA;QACnCA,MAAMA,CAACA,IAAIA,CAACA,aAAaA,CAACA,GAAGA,CAACA,CAACA;IACnCA,CAACA;IAEDL;;;;OAIGA;IACHA,2BAAIA,GAAJA,UAAKA,QAAmBA,EAAEA,QAAmBA;QACzCM,IAAIA,KAAKA,GAAGA,IAAIA,eAAKA,EAAEA,CAACA;QACxBA,KAAKA,CAACA,IAAIA,CAACA,QAAQA,EAAEA,QAAQA,CAACA,CAACA;QAC/BA,MAAMA,CAACA,KAAKA,CAACA,OAAOA,CAACA;IACzBA,CAACA;IACLN,mBAACA;AAADA,CA9DA,AA8DCA,IAAA;AA9DD,8BA8DC,CAAA","file":"lib/ModelHelpers.js","sourcesContent":["/// \r\nimport MongoDB = require('mongodb');\r\nimport Model from './Model';\r\nimport skmatc = require('skmatc');\r\nimport Omnom from './utils/Omnom';\r\nimport _ = require('lodash');\r\nimport Bluebird = require('bluebird');\r\n\r\nexport default class ModelHelpers {\r\n constructor(public model: Model) {\r\n this._validator = new skmatc(model.schema);\r\n model.validators.forEach(validator => this._validator.register(validator));\r\n }\r\n\r\n private _validator: Skmatc.Skmatc;\r\n\r\n /**\r\n * Validates a document to ensure that it matches the model's ISchema requirements\r\n * @param {any} document The document to validate against the ISchema\r\n * @returns {SkmatcCore.IResult} The result of the validation\r\n */\r\n validate(document: TDocument): Skmatc.Result {\r\n return this._validator.validate(document);\r\n }\r\n\r\n /**\r\n * Wraps the given document in an instance wrapper for use throughout the application\r\n * @param {any} document The document to be wrapped as an instance\r\n * @param {Boolean} isNew Whether the instance originated from the database or was created by the application\r\n * @param {Boolean} isPartial Whether the document supplied contains all information present in the database\r\n * @returns {any} An instance which wraps this document\r\n */\r\n wrapDocument(document: TDocument, isNew?: boolean, isPartial?: boolean): TInstance {\r\n return new this.model.Instance(document, isNew, isPartial);\r\n }\r\n \r\n /**\r\n * Converts the given document to its database form into a form\r\n * using the transforms defined on the model.\r\n * @param {any} document The document to be converted\r\n * @returns {any} A new document cloned from the original and transformed\r\n */\r\n transformToDB(document: T): T {\r\n for (var property in this.model.transforms)\r\n if(document.hasOwnProperty(property))\r\n document[property] = this.model.transforms[property].toDB(document[property]);\r\n return document;\r\n }\r\n \r\n /**\r\n * Converts the given document to its database form into a form\r\n * using the transforms defined on the model.\r\n * @param {any} document The document to be converted\r\n * @returns {any} A new document cloned from the original and transformed\r\n */\r\n convertToDB(document: T): T {\r\n var doc: T = _.cloneDeep(document);\r\n return this.transformToDB(doc);\r\n }\r\n\r\n /**\r\n * Performs a diff operation between two documents and creates a MongoDB changes object to represent the differences\r\n * @param {any} original The original document prior to changes being made\r\n * @param {any} modified The document after changes were made\r\n */\r\n diff(original: TDocument, modified: TDocument): any {\r\n var omnom = new Omnom();\r\n omnom.diff(original, modified);\r\n return omnom.changes;\r\n }\r\n}\r\n"],"sourceRoot":"/source/"} \ No newline at end of file