diff --git a/dist/lib/Decorators.js b/dist/lib/Decorators.js index 048a4ed..c87d87c 100644 --- a/dist/lib/Decorators.js +++ b/dist/lib/Decorators.js @@ -83,6 +83,11 @@ exports.Property = Property; * class, however only one transform can be applied to any property at a time. * If your transpiler does not support decorators then you are free to make use of the * property instead. + * + * If this decorator is applied to the instance class itself, as opposed to a property, then + * it will be treated as a $document transformer - and will receive the full document as opposed + * to individual property values. Similarly, it is expected to return a full document when either + * fromDB or toDB is called. */ function Transform(fromDB, toDB) { return function (target, property) { diff --git a/dist/lib/Decorators.js.map b/dist/lib/Decorators.js.map index b1a66d3..0dd580f 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","Binary"],"mappings":"AAAA,IAAO,OAAO,WAAW,SAAS,CAAC,CAAC;AACpC,IAAO,CAAC,WAAW,QAAQ,CAAC,CAAC;AAC7B,IAAO,MAAM,WAAW,QAAQ,CAAC,CAAC;AAMlC,2BAA4C,cAAc,CAAC,CAAA;AAE3D;;;;;;;GAOG;AACH,oBAA2B,IAAY;IACtCA,MAAMA,CAACA,UAASA,MAAwCA;QACvD,MAAM,CAAC,UAAU,GAAG,IAAI,CAAC;IAC1B,CAAC,CAACA;AACHA,CAACA;AAJe,kBAAU,aAIzB,CAAA;AAED;;;;;;;;;GASG;AACH,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;;;;;;;;;GASG;AACH,kBAAyB,OAAY,EAAE,QAAiE;IACv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AACA,MAAMA,CAACA,IAAIA,CAC7BA,CAACA,MAAMA,EAAEA,IAAIA,CAACA,CAACA;AACjBA,CAACA;AANe,cAAM,SAMrB,CAAA","file":"lib/Decorators.js","sourcesContent":["import MongoDB = require('mongodb');\r\nimport _ = require('lodash');\r\nimport Skmatc = require('skmatc');\r\nimport {Instance} from './Instance';\r\nimport {Model} from './Model';\r\nimport {Index, IndexSpecification} from './Index';\r\nimport {Schema} from './Schema';\r\nimport {InstanceImplementation} from './InstanceInterface';\r\nimport {Transforms, DefaultTransforms} from './Transforms';\r\n\r\n/**\r\n * Specifies the name of the collection to which this instance's documents should be sent.\r\n * @param name The name of the MongoDB collection to store the documents in.\r\n *\r\n * This decorator replaces the use of the static collection property on instance implementation\r\n * classes. If your transpiler does not support decorators then you are free to make use of the\r\n * property instead.\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\n/**\r\n * Specifies a MongoDB collection level index to be managed by Iridium for this instance type.\r\n * More than one instance of this decorator may be used if you wish to specify multiple indexes.\r\n * @param spec The formal index specification which defines the properties and ordering used in the index.\r\n * @param options The options dictating the way in which the index behaves.\r\n *\r\n * This decorator replaces the use of the static indexes property on instance implementation\r\n * classes. If your transpiler does not support decorators then you are free to make use of the\r\n * property instead.\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\n/**\r\n * Specifies a custom validator to be made available for this collection's schema.\r\n * More than one instance of this decorator may be used if you wish to specify multiple validators.\r\n * @param forType The value in the schema which will be delegated to this function for validation.\r\n * @param validate A function which calls this.assert(condition) to determine whether a schema node is valid or not.\r\n *\r\n * This decorator replaces the use of the static validators property on instance implementation\r\n * classes. If your transpiler does not support decorators then you are free to make use of the\r\n * property instead.\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\n/**\r\n * Specifies the schema type for the property this decorator is applied to. This can be used to replace the\r\n * static schema property on your instance. Multiple instances of this decorator can be applied, but no more\r\n * than one per property.\r\n *\r\n * @param asType The schema validation type to make use of for this property\r\n * @param required Whether this property is required to have a value or not, defaults to true.\r\n */\r\nexport function Property(asType: any, required?: boolean): (target: Instance, name: string) => void;\r\n/**\r\n * Specifies the schema type for a property with the given name on the class this decorator is applied to. This\r\n * can either compliment or replace the static schema property on your instance class.\r\n *\r\n * @param name The name of the property that is being targetted\r\n * @param asType The schema validation type to make use of for this property\r\n * @param required Whether this property is required to have a value or not, defaults to true.\r\n */\r\nexport function Property(name: string, asType: any, required?: boolean): (target: InstanceImplementation) => void;\r\nexport function Property(...args: any[]): (target: Instance | InstanceImplementation, name?: string) => void {\r\n\tlet name = null,\r\n\t\tasType = false,\r\n\t\trequired = true;\r\n\r\n\tif (args.length > 1 && typeof args[args.length - 1] === 'boolean')\r\n\t\trequired = args.pop();\r\n\r\n\treturn function(target: InstanceImplementation, property?: string) {\r\n\t\tlet staticTarget: InstanceImplementation = target;\r\n\t\tif (!property) name = args.shift();\r\n\t\telse {\r\n\t\t\tname = property;\r\n\t\t\tstaticTarget = >target.constructor;\r\n\t\t}\r\n\t\tasType = args.pop() || false;\r\n\r\n\t\tstaticTarget.schema = _.clone(staticTarget.schema || { _id: false });\r\n\t\tif(!required && typeof asType !== 'boolean') staticTarget.schema[name] = { $required: required, $type: asType };\r\n\t\telse staticTarget.schema[name] = asType;\r\n\t}\r\n}\r\n\r\n/**\r\n * Specifies a custom transform to be applied to the property this decorator is applied to.\r\n *\r\n * @param fromDB The function used to convert values from the database for the application.\r\n * @param toDB The function used to convert values from the application to the form used in the database.\r\n *\r\n * This decorator can either compliment or replace the static transforms property on your instance\r\n * class, however only one transform can be applied to any property at a time.\r\n * If your transpiler does not support decorators then you are free to make use of the\r\n * property instead.\r\n */\r\nexport function Transform(fromDB: (value: any, property: string, model: Model) => any, toDB: (value: any, property: string, model: Model) => any) {\r\n\treturn function(target: Instance, property: string = '$document') {\r\n\t\tlet staticTarget: InstanceImplementation = >(target.constructor || target);\r\n\r\n\t\tstaticTarget.transforms = _.clone(staticTarget.transforms || {})\r\n\t\tstaticTarget.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\n\r\n/**\r\n * Specifies that this property should be treated as an ObjectID, with the requisite validator and transforms.\r\n *\r\n * This decorator applies an ObjectID validator to the property, which ensures that values sent to the database\r\n * are instances of the MongoDB ObjectID type, as well as applying a transform operation which converts ObjectIDs\r\n * to strings for your application, and then converts strings back to ObjectIDs for the database.\r\n */\r\nexport function ObjectID(target: Instance, name: string) {\r\n\tProperty(MongoDB.ObjectID)(target, name);\r\n\tTransform(\r\n\t\tDefaultTransforms.ObjectID.fromDB,\r\n\t\tDefaultTransforms.ObjectID.toDB\r\n\t)(target, name);\r\n}\r\n\r\n/**\r\n * Specifies that this property should be stored using the MongoDB binary type and represented as a Buffer.\r\n * \r\n * This decorator applies a Buffer validator to the property, which ensures that values you send to the database\r\n * are well formatted Buffer objects represented using the BSON Binary datatype. In addition to this, it will\r\n * apply a transform which ensures you only work with Buffer objects and that data is always stored in Binary\r\n * format.\r\n */\r\nexport function Binary(target: Instance, name: string) {\r\n\tProperty(Buffer)(target, name);\r\n\tTransform(\r\n\t\tDefaultTransforms.Binary.fromDB,\r\n\t\tDefaultTransforms.Binary.toDB\r\n\t)(target, name);\r\n}"],"sourceRoot":"/source/"} \ No newline at end of file +{"version":3,"sources":["lib/Decorators.ts"],"names":["Collection","Index","Validate","Property","Transform","ObjectID","Binary"],"mappings":"AAAA,IAAO,OAAO,WAAW,SAAS,CAAC,CAAC;AACpC,IAAO,C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C,QAAQ,CAAC,GAAG;YACnC,MAAM,EAAE,MAAM;YACd,IAAI,EAAE,IAAI;SACV,CAAC;IACH,CAAC,CAACA;AACHA,CAACA;AAVe,iBAAS,YAUxB,CAAA;AAGD;;;;;;GAMG;AACH,kBAAyB,MAA0B,EAAE,IAAY;IAChEC,QAAQA,CAACA,OAAOA,CAACA,QAAQA,CAACA,CAACA,MAAMA,EAAEA,IAAIA,CAACA,CAACA;IACzCA,SAASA,CACRA,8BAAiBA,CAACA,QAAQA,CAACA,MAAMA,EACjCA,8BAAiBA,CAACA,QAAQA,CAACA,IAAIA,CAC/BA,CAACA,MAAMA,EAAEA,IAAIA,CAACA,CAACA;AACjBA,CAACA;AANe,gBAAQ,WAMvB,CAAA;AAED;;;;;;;GAOG;AACH,gBAAuB,MAA0B,EAAE,IAAY;IAC9DC,QAAQA,CAACA,MAAMA,CAACA,CAACA,MAAMA,EAAEA,IAAIA,CAACA,CAACA;IAC/BA,SAASA,CACRA,8BAAiBA,CAACA,MAAMA,CAACA,MAAMA,EAC/BA,8BAAiBA,CAACA,MAAMA,CAACA,IAAIA,CAC7BA,CAACA,MAAMA,EAAEA,IAAIA,CAACA,CAACA;AACjBA,CAACA;AANe,cAAM,SAMrB,CAAA","file":"lib/Decorators.js","sourcesContent":["import MongoDB = require('mongodb');\r\nimport _ = require('lodash');\r\nimport Skmatc = require('skmatc');\r\nimport {Instance} from './Instance';\r\nimport {Model} from './Model';\r\nimport {Index, IndexSpecification} from './Index';\r\nimport {Schema} from './Schema';\r\nimport {InstanceImplementation} from './InstanceInterface';\r\nimport {Transforms, DefaultTransforms} from './Transforms';\r\n\r\n/**\r\n * Specifies the name of the collection to which this instance's documents should be sent.\r\n * @param name The name of the MongoDB collection to store the documents in.\r\n *\r\n * This decorator replaces the use of the static collection property on instance implementation\r\n * classes. If your transpiler does not support decorators then you are free to make use of the\r\n * property instead.\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\n/**\r\n * Specifies a MongoDB collection level index to be managed by Iridium for this instance type.\r\n * More than one instance of this decorator may be used if you wish to specify multiple indexes.\r\n * @param spec The formal index specification which defines the properties and ordering used in the index.\r\n * @param options The options dictating the way in which the index behaves.\r\n *\r\n * This decorator replaces the use of the static indexes property on instance implementation\r\n * classes. If your transpiler does not support decorators then you are free to make use of the\r\n * property instead.\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\n/**\r\n * Specifies a custom validator to be made available for this collection's schema.\r\n * More than one instance of this decorator may be used if you wish to specify multiple validators.\r\n * @param forType The value in the schema which will be delegated to this function for validation.\r\n * @param validate A function which calls this.assert(condition) to determine whether a schema node is valid or not.\r\n *\r\n * This decorator replaces the use of the static validators property on instance implementation\r\n * classes. If your transpiler does not support decorators then you are free to make use of the\r\n * property instead.\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\n/**\r\n * Specifies the schema type for the property this decorator is applied to. This can be used to replace the\r\n * static schema property on your instance. Multiple instances of this decorator can be applied, but no more\r\n * than one per property.\r\n *\r\n * @param asType The schema validation type to make use of for this property\r\n * @param required Whether this property is required to have a value or not, defaults to true.\r\n */\r\nexport function Property(asType: any, required?: boolean): (target: Instance, name: string) => void;\r\n/**\r\n * Specifies the schema type for a property with the given name on the class this decorator is applied to. This\r\n * can either compliment or replace the static schema property on your instance class.\r\n *\r\n * @param name The name of the property that is being targetted\r\n * @param asType The schema validation type to make use of for this property\r\n * @param required Whether this property is required to have a value or not, defaults to true.\r\n */\r\nexport function Property(name: string, asType: any, required?: boolean): (target: InstanceImplementation) => void;\r\nexport function Property(...args: any[]): (target: Instance | InstanceImplementation, name?: string) => void {\r\n\tlet name = null,\r\n\t\tasType = false,\r\n\t\trequired = true;\r\n\r\n\tif (args.length > 1 && typeof args[args.length - 1] === 'boolean')\r\n\t\trequired = args.pop();\r\n\r\n\treturn function(target: InstanceImplementation, property?: string) {\r\n\t\tlet staticTarget: InstanceImplementation = target;\r\n\t\tif (!property) name = args.shift();\r\n\t\telse {\r\n\t\t\tname = property;\r\n\t\t\tstaticTarget = >target.constructor;\r\n\t\t}\r\n\t\tasType = args.pop() || false;\r\n\r\n\t\tstaticTarget.schema = _.clone(staticTarget.schema || { _id: false });\r\n\t\tif(!required && typeof asType !== 'boolean') staticTarget.schema[name] = { $required: required, $type: asType };\r\n\t\telse staticTarget.schema[name] = asType;\r\n\t}\r\n}\r\n\r\n/**\r\n * Specifies a custom transform to be applied to the property this decorator is applied to.\r\n *\r\n * @param fromDB The function used to convert values from the database for the application.\r\n * @param toDB The function used to convert values from the application to the form used in the database.\r\n *\r\n * This decorator can either compliment or replace the static transforms property on your instance\r\n * class, however only one transform can be applied to any property at a time.\r\n * If your transpiler does not support decorators then you are free to make use of the\r\n * property instead.\r\n * \r\n * If this decorator is applied to the instance class itself, as opposed to a property, then\r\n * it will be treated as a $document transformer - and will receive the full document as opposed\r\n * to individual property values. Similarly, it is expected to return a full document when either\r\n * fromDB or toDB is called.\r\n */\r\nexport function Transform(fromDB: (value: any, property: string, model: Model) => any, toDB: (value: any, property: string, model: Model) => any) {\r\n\treturn function(target: Instance, property: string = '$document') {\r\n\t\tlet staticTarget: InstanceImplementation = >(target.constructor || target);\r\n\r\n\t\tstaticTarget.transforms = _.clone(staticTarget.transforms || {})\r\n\t\tstaticTarget.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\n\r\n/**\r\n * Specifies that this property should be treated as an ObjectID, with the requisite validator and transforms.\r\n *\r\n * This decorator applies an ObjectID validator to the property, which ensures that values sent to the database\r\n * are instances of the MongoDB ObjectID type, as well as applying a transform operation which converts ObjectIDs\r\n * to strings for your application, and then converts strings back to ObjectIDs for the database.\r\n */\r\nexport function ObjectID(target: Instance, name: string) {\r\n\tProperty(MongoDB.ObjectID)(target, name);\r\n\tTransform(\r\n\t\tDefaultTransforms.ObjectID.fromDB,\r\n\t\tDefaultTransforms.ObjectID.toDB\r\n\t)(target, name);\r\n}\r\n\r\n/**\r\n * Specifies that this property should be stored using the MongoDB binary type and represented as a Buffer.\r\n * \r\n * This decorator applies a Buffer validator to the property, which ensures that values you send to the database\r\n * are well formatted Buffer objects represented using the BSON Binary datatype. In addition to this, it will\r\n * apply a transform which ensures you only work with Buffer objects and that data is always stored in Binary\r\n * format.\r\n */\r\nexport function Binary(target: Instance, name: string) {\r\n\tProperty(Buffer)(target, name);\r\n\tTransform(\r\n\t\tDefaultTransforms.Binary.fromDB,\r\n\t\tDefaultTransforms.Binary.toDB\r\n\t)(target, name);\r\n}"],"sourceRoot":"/source/"} \ No newline at end of file diff --git a/lib/Decorators.ts b/lib/Decorators.ts index 1f24376..59cb6f3 100644 --- a/lib/Decorators.ts +++ b/lib/Decorators.ts @@ -105,6 +105,11 @@ export function Property(...args: any[]): (target: Instance | Instance * class, however only one transform can be applied to any property at a time. * If your transpiler does not support decorators then you are free to make use of the * property instead. + * + * If this decorator is applied to the instance class itself, as opposed to a property, then + * it will be treated as a $document transformer - and will receive the full document as opposed + * to individual property values. Similarly, it is expected to return a full document when either + * fromDB or toDB is called. */ export function Transform(fromDB: (value: any, property: string, model: Model) => any, toDB: (value: any, property: string, model: Model) => any) { return function(target: Instance, property: string = '$document') {