diff --git a/lib/Aggregate.ts b/lib/Aggregate.ts index a91bf6a..5efa97c 100644 --- a/lib/Aggregate.ts +++ b/lib/Aggregate.ts @@ -1,5 +1,3 @@ -/// - export interface Stage { } \ No newline at end of file diff --git a/lib/Cache.ts b/lib/Cache.ts index 6208825..7368bd1 100644 --- a/lib/Cache.ts +++ b/lib/Cache.ts @@ -1,4 +1,3 @@ -/// import Bluebird = require('bluebird'); export interface Cache { diff --git a/lib/CacheDirector.ts b/lib/CacheDirector.ts index 6173380..57bc318 100644 --- a/lib/CacheDirector.ts +++ b/lib/CacheDirector.ts @@ -1,4 +1,3 @@ -/// export interface CacheDirector { valid(object: T): boolean; buildKey(object: T): string; diff --git a/lib/Configuration.ts b/lib/Configuration.ts index 443d0ab..8aa1e48 100644 --- a/lib/Configuration.ts +++ b/lib/Configuration.ts @@ -1,4 +1,3 @@ -/// export interface Configuration { host?: string; port?: number; diff --git a/lib/Core.ts b/lib/Core.ts index a7a0028..4ecead8 100644 --- a/lib/Core.ts +++ b/lib/Core.ts @@ -1,4 +1,3 @@ -/// import Bluebird = require('bluebird'); import MongoDB = require('mongodb'); import _ = require('lodash'); diff --git a/lib/Cursor.ts b/lib/Cursor.ts index dedf099..99f23d9 100644 --- a/lib/Cursor.ts +++ b/lib/Cursor.ts @@ -1,5 +1,4 @@ -/// -import {Model} from './Model'; +import {Model} from './Model'; import General = require('./General'); import MongoDB = require('mongodb'); import Bluebird = require('bluebird'); @@ -8,7 +7,7 @@ import * as Index from './Index'; /** * An Iridium collection cursor which allows the itteration through documents * in the collection, automatically wrapping them in the correct instance type. - * + * * @param TDocument The interface representing the collection's documents * @param TInstance The interface or class used to represent the wrapped documents. */ diff --git a/lib/Decorators.ts b/lib/Decorators.ts index da97fb6..ffe3425 100644 --- a/lib/Decorators.ts +++ b/lib/Decorators.ts @@ -1,4 +1,3 @@ -/// import MongoDB = require('mongodb'); import _ = require('lodash'); import skmatc = require('skmatc'); @@ -6,11 +5,12 @@ import {Instance} from './Instance'; import {Index, IndexSpecification} from './Index'; import {Schema} from './Schema'; import {InstanceImplementation} from './InstanceInterface'; +import {Transforms} from './Transforms'; /** * Specifies the name of the collection to which this instance's documents should be sent. * @param name The name of the MongoDB collection to store the documents in. - * + * * This decorator replaces the use of the static collection property on instance implementation * classes. If your transpiler does not support decorators then you are free to make use of the * property instead. @@ -26,7 +26,7 @@ export function Collection(name: string) { * More than one instance of this decorator may be used if you wish to specify multiple indexes. * @param spec The formal index specification which defines the properties and ordering used in the index. * @param options The options dictating the way in which the index behaves. - * + * * This decorator replaces the use of the static indexes property on instance implementation * classes. If your transpiler does not support decorators then you are free to make use of the * property instead. @@ -42,7 +42,7 @@ export function Index(spec: IndexSpecification, options?: MongoDB.IndexOptions) * More than one instance of this decorator may be used if you wish to specify multiple validators. * @param forType The value in the schema which will be delegated to this function for validation. * @param validate A function which calls this.assert(condition) to determine whether a schema node is valid or not. - * + * * This decorator replaces the use of the static validators property on instance implementation * classes. If your transpiler does not support decorators then you are free to make use of the * property instead. @@ -57,57 +57,60 @@ export function Validate(forType: any, validate: (schema: any, data: any, path: * Specifies the schema type for the property this decorator is applied to. This can be used to replace the * static schema property on your instance. Multiple instances of this decorator can be applied, but no more * than one per property. - * + * * @param asType The schema validation type to make use of for this property - * @param required Whether this property is required to have a value or not, defaults to true. + * @param required Whether this property is required to have a value or not, defaults to true. */ -export function Property(asType: any, required?: boolean): (target: { constructor: Function }, name: string) => void; +export function Property(asType: any, required?: boolean): (target: Instance, name: string) => void; /** * Specifies the schema type for a property with the given name on the class this decorator is applied to. This * can either compliment or replace the static schema property on your instance class. - * + * * @param name The name of the property that is being targetted * @param asType The schema validation type to make use of for this property - * @param required Whether this property is required to have a value or not, defaults to true. + * @param required Whether this property is required to have a value or not, defaults to true. */ -export function Property(name: string, asType: any, required?: boolean): (target: Function) => void; -export function Property(...args: any[]): (target: any, name?: string) => void { +export function Property(name: string, asType: any, required?: boolean): (target: InstanceImplementation) => void; +export function Property(...args: any[]): (target: Instance | InstanceImplementation, name?: string) => void { let name = null, asType = false, required = true; - + if (args.length > 1 && typeof args[args.length - 1] === 'boolean') required = args.pop(); - - return function(target: any, property?: string) { + + return function(target: InstanceImplementation, property?: string) { + let staticTarget: InstanceImplementation = target; if (!property) name = args.shift(); else { name = property; - target = target.constructor; + staticTarget = >target.constructor; } asType = args.pop() || false; - - target.schema = _.clone(target.schema || {}); - if(!required && typeof asType !== 'boolean') target.schema[name] = { $required: required, $type: asType }; - else target.schema[name] = asType; + + staticTarget.schema = _.clone(staticTarget.schema || { _id: false }); + if(!required && typeof asType !== 'boolean') staticTarget.schema[name] = { $required: required, $type: asType }; + else staticTarget.schema[name] = asType; } } /** * Specifies a custom transform to be applied to the property this decorator is applied to. - * + * * @param fromDB The function used to convert values from the database for the application. * @param toDB The function used to convert values from the application to the form used in the database. - * + * * This decorator can either compliment or replace the static transforms property on your 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. */ export function Transform(fromDB: (value: any) => any, toDB: (value: any) => any) { - return function(target: any, property: string) { - target.constructor.transforms = _.clone(target.constructor.transforms || {}) - target.constructor.transforms[property] = { + return function(target: Instance, property: string) { + let staticTarget: InstanceImplementation = >(target.constructor || target); + + staticTarget.transforms = _.clone(staticTarget.transforms || {}) + staticTarget.transforms[property] = { fromDB: fromDB, toDB: toDB }; @@ -117,12 +120,12 @@ export function Transform(fromDB: (value: any) => any, toDB: (value: any) => any /** * Specifies that this property should be treated as an ObjectID, with the requisite validator and transforms. - * + * * This decorator applies an ObjectID validator to the property, which ensures that values sent to the database * are instances of the MongoDB ObjectID type, as well as applying a transform operation which converts ObjectIDs * to strings for your application, and then converts strings back to ObjectIDs for the database. */ -export function ObjectID(target: { constructor: typeof Instance }, name: string) { +export function ObjectID(target: Instance, name: string) { Property(MongoDB.ObjectID)(target, name); Transform( value => value && value._bsontype == 'ObjectID' ? new MongoDB.ObjectID(value.id).toHexString() : value, diff --git a/lib/General.ts b/lib/General.ts index d8a6728..173f630 100644 --- a/lib/General.ts +++ b/lib/General.ts @@ -1,5 +1,3 @@ -/// - /** * A method which is called once an asynchronous operation has completed, an alternative * to using Promises. diff --git a/lib/Hooks.ts b/lib/Hooks.ts index dbe3bb6..b0a247a 100644 --- a/lib/Hooks.ts +++ b/lib/Hooks.ts @@ -1,4 +1,3 @@ -/// import instance = require('./Instance'); export interface Hooks { diff --git a/lib/Index.ts b/lib/Index.ts index b296b7e..04746ec 100644 --- a/lib/Index.ts +++ b/lib/Index.ts @@ -1,5 +1,4 @@ -/// -import MongoDB = require('mongodb'); +import MongoDB = require('mongodb'); export interface Index { spec: IndexSpecification; diff --git a/lib/Instance.ts b/lib/Instance.ts index 643e49a..41fc47e 100644 --- a/lib/Instance.ts +++ b/lib/Instance.ts @@ -1,5 +1,4 @@ -/// -import {Core} from './Core'; +import {Core} from './Core'; import {Model} from './Model'; import {Plugin} from './Plugins'; import {CacheDirector} from './CacheDirector'; @@ -19,10 +18,10 @@ import skmatc = require('skmatc'); * removing the wrapped document from the collection, as well as integrating with Omnom, our * built in document diff processor which allows clean, atomic, document updates to be performed * without needing to write the update queries yourself. - * + * * @param TDocument The interface representing the structure of the documents in the collection. * @param TInstance The type of instance which wraps the documents, generally the subclass of this class. - * + * * This class will be subclassed automatically by Iridium to create a model specific instance * which takes advantage of some of v8's optimizations to boost performance significantly. * The instance returned by the model, and all of this instance's methods, will be of type @@ -70,24 +69,24 @@ export class Instance { * @param document The document which will be inserted into the database. */ static onCreating: (document: { _id?: any }) => void; - + /** * A function which is called whenever a document of this type is received from the database, prior to it being * wrapped by an Instance object. * @param document The document that was retrieved from the database. */ static onRetrieved: (document: { _id?: any }) => void; - + /** * A function which is called whenever a new instance has been created to wrap a document. * @param instance The instance which has been created. */ static onReady: (instance: Instance<{ _id?: any }, Instance<{ _id?: any }, any>>) => void; - + /** * A function which is called whenever an instance's save() method is called to allow you to interrogate and/or manipulate * the changes which are being made. - * + * * @param instance The instance to which the changes are being made * @param changes The MongoDB change object describing the changes being made to the document. */ @@ -125,12 +124,12 @@ export class Instance { * The cache director used to derive unique cache keys for documents of this type. */ static cache: CacheDirector; - + /** * The indexes which should be managed by Iridium for the collection used by this type. */ static indexes: (Index.Index | Index.IndexSpecification)[] = []; - + /** * Saves any changes to this instance, using the built in diff algorithm to write the update query. * @param {function(Error, IInstance)} callback A callback which is triggered when the save operation completes diff --git a/lib/InstanceInterface.ts b/lib/InstanceInterface.ts index a49a7c1..18ac53a 100644 --- a/lib/InstanceInterface.ts +++ b/lib/InstanceInterface.ts @@ -1,4 +1,3 @@ -/// import {Schema} from './Schema'; import {Model} from './Model'; import * as Index from './Index'; @@ -8,10 +7,10 @@ import {Transforms} from './Transforms'; /** * This interface dictates the format of an instance class which wraps documents received * from the database for a specific Iridium model. - * + * * @param TDocument The interface representing the documents stored in the database, after being passed through the transforms pipeline. * @param TInstance The type of object which is instantiated when calling this implementation's constructor. - * + * * It is important to note that, when implementing this interface, each of the properties and methods * should be exposed statically. That is, you would expose the collection property as a static variable * on the instance implementation, since prototype methods and variables become available to consumers of the @@ -26,7 +25,7 @@ export interface InstanceImplementation, doc: TDocument, isNew?: boolean, isPartial?: boolean): TInstance; - + /** * The name of the database collection from which documents are retrieved, and to which they are stored. */ @@ -41,32 +40,32 @@ export interface InstanceImplementation import {Core} from './Core'; /** diff --git a/lib/Model.ts b/lib/Model.ts index cdb5a08..7e66fb6 100644 --- a/lib/Model.ts +++ b/lib/Model.ts @@ -1,5 +1,4 @@ -/// -import MongoDB = require('mongodb'); +import MongoDB = require('mongodb'); import Bluebird = require('bluebird'); import util = require('util'); import _ = require('lodash'); @@ -30,10 +29,10 @@ import * as AggregationPipeline from './Aggregate'; * An Iridium Model which represents a structured MongoDB collection. * Models expose the methods you will generally use to query those collections, and ensure that * the results of those queries are returned as {TInstance} instances. - * + * * @param TDocument The interface used to determine the schema of documents in the collection. * @param TInstance The interface or class used to represent collection documents in the JS world. - * + * * @class */ export class Model { diff --git a/lib/ModelCache.ts b/lib/ModelCache.ts index cc335b0..eab93a5 100644 --- a/lib/ModelCache.ts +++ b/lib/ModelCache.ts @@ -1,5 +1,4 @@ -/// -import {Model} from './Model'; +import {Model} from './Model'; import Bluebird = require('bluebird'); /** diff --git a/lib/ModelHandlers.ts b/lib/ModelHandlers.ts index 79b7ad0..f4cee40 100644 --- a/lib/ModelHandlers.ts +++ b/lib/ModelHandlers.ts @@ -1,5 +1,4 @@ -/// -import {Core} from './Core'; +import {Core} from './Core'; import {Schema} from './Schema'; import {Model} from './Model'; import {ModelCache} from './ModelCache'; @@ -13,7 +12,7 @@ import Bluebird = require('bluebird'); * Provides a number of methods which are used to handle events that occur within * the Iridium workflow - such as what happens when a document is received from * the database, or how to handle the creation of new documents and saving of instances. - * + * * Mostly this is for cache support, wrapping and hook triggering. * @internal */ @@ -38,7 +37,7 @@ export class ModelHandlers { if (this.model.core.cache && options.cache && !options.fields) { this.model.cache.set(target); // Does not block execution pipeline - fire and forget } - + // Trigger the received hook if (this.model.hooks.onRetrieved) this.model.hooks.onRetrieved(target); @@ -58,7 +57,7 @@ export class ModelHandlers { document = this.model.helpers.convertToDB(document); let validation: Skmatc.Result = this.model.helpers.validate(document); if (validation.failed) return Bluebird.reject(validation.error); - + return document; }); })); diff --git a/lib/ModelHelpers.ts b/lib/ModelHelpers.ts index ad04677..811c1e8 100644 --- a/lib/ModelHelpers.ts +++ b/lib/ModelHelpers.ts @@ -1,5 +1,4 @@ -/// -import MongoDB = require('mongodb'); +import MongoDB = require('mongodb'); import {Model} from './Model'; import skmatc = require('skmatc'); import {Omnom} from './utils/Omnom'; @@ -39,7 +38,7 @@ export class ModelHelpers { wrapDocument(document: TDocument, isNew?: boolean, isPartial?: boolean): TInstance { return new this.model.Instance(document, isNew, isPartial); } - + /** * Converts the given document to its database form into a form * using the transforms defined on the model. @@ -52,7 +51,7 @@ export class ModelHelpers { document[property] = this.model.transforms[property].toDB(document[property]); return document; } - + /** * Converts the given document to its database form into a form * using the transforms defined on the model. diff --git a/lib/ModelInterfaces.ts b/lib/ModelInterfaces.ts index 0ead466..01fe5d9 100644 --- a/lib/ModelInterfaces.ts +++ b/lib/ModelInterfaces.ts @@ -1,14 +1,13 @@ -/// -/** +/** * The interface to which a prepared instance constructor should conform. When called with a document * object, it should instantiate a new instance of type TInstance which is associated with its parent * model. - * + * * This is primarily used internally for prepared model instance constructors. - * + * * @param TDocument The interface used to describe the structure of the documents found in the database collection. * @param TInstance The interface or class used to wrap the documents returned from the database. - * + * * @internal */ export interface ModelSpecificInstanceConstructor { diff --git a/lib/ModelOptions.ts b/lib/ModelOptions.ts index bc2cbd1..52931ef 100644 --- a/lib/ModelOptions.ts +++ b/lib/ModelOptions.ts @@ -1,5 +1,4 @@ -/// -import MongoDB = require('mongodb'); +import MongoDB = require('mongodb'); import * as Index from './Index'; import {Hooks} from './Hooks'; import {CacheDirector} from './CacheDirector'; @@ -26,44 +25,44 @@ export interface CreateOptions { * The write concern, can either be a number from 0 to the number of nodes within * the cluster, or 'majority' if you would like to wait for the majority of nodes * within the cluster to confirm the write before returning. - * + * * It is recommended that you set this to 'majority', however in all situations * where you mind if data is lost, you should set it to at least 1. */ w?: string | number; - + /** * The timeout in milliseconds before the write will be aborted by the MongoDB server * and an error response (if the write concern is non-zero) is returned to the client. */ wtimeout?: number; - + /** * Whether to wait for the write to be commited to the server's journal (flushed to disk) * or not. By specifying 1 here, you imply w:1 - howver this can be combined with w:'majority' * to give excellent write reliability within a cluster, even across failures. */ j?: number; - + /** * Whether or not to serialize JavaScript functions which are provided as values. For security * reasons it is probably best to set this to false, however it may come in handy under certain * circumstances. */ serializeFunctions?: boolean; - + /** * Whether to generate document ObjectIDs within the client library or on the server, it is recommended * that you leave this to default (false) unless you are making thousands of inserts per second from * a single node and experiencing _id collisions. */ forceServerObjectId?: boolean; - + /** * Whether to perform an upsert operation if the document already exists. */ upsert?: boolean; - + /** * Whether to store the resulting document in the Iridium document cache to boost later retrieval times. */ @@ -75,25 +74,25 @@ export interface UpdateOptions { * The write concern, can either be a number from 0 to the number of nodes within * the cluster, or 'majority' if you would like to wait for the majority of nodes * within the cluster to confirm the write before returning. - * + * * It is recommended that you set this to 'majority', however in all situations * where you mind if data is lost, you should set it to at least 1. */ w?: string | number; - + /** * The timeout in milliseconds before the write will be aborted by the MongoDB server * and an error response (if the write concern is non-zero) is returned to the client. */ wtimeout?: number; - + /** * Whether to wait for the write to be commited to the server's journal (flushed to disk) * or not. By specifying 1 here, you imply w:1 - howver this can be combined with w:'majority' * to give excellent write reliability within a cluster, even across failures. */ j?: boolean; - + /** * Whether to perform an upsert operation if the document already exists. This can be combined * with $setOnInsert to automatically create documents which do not exist in the database prior @@ -107,25 +106,25 @@ export interface RemoveOptions { * The write concern, can either be a number from 0 to the number of nodes within * the cluster, or 'majority' if you would like to wait for the majority of nodes * within the cluster to confirm the write before returning. - * + * * It is recommended that you set this to 'majority', however in all situations * where you mind if data is lost, you should set it to at least 1. */ w?: string | number; - + /** * The timeout in milliseconds before the write will be aborted by the MongoDB server * and an error response (if the write concern is non-zero) is returned to the client. */ wtimeout?: number; - + /** * Whether to wait for the write to be commited to the server's journal (flushed to disk) * or not. By specifying 1 here, you imply w:1 - howver this can be combined with w:'majority' * to give excellent write reliability within a cluster, even across failures. */ j?: number; - + /** * Whether to only remove the first document in the collection or not, by default this is false * and any document matching the conditions will be removed. diff --git a/lib/ModelSpecificInstance.ts b/lib/ModelSpecificInstance.ts index 21306e9..4b14bcc 100644 --- a/lib/ModelSpecificInstance.ts +++ b/lib/ModelSpecificInstance.ts @@ -1,5 +1,4 @@ -/// -import {Model} from './Model'; +import {Model} from './Model'; import {InstanceImplementation} from './InstanceInterface'; import {ModelSpecificInstanceConstructor} from './ModelInterfaces'; import util = require('util'); @@ -8,13 +7,13 @@ import _ = require('lodash'); /** * Creates a new subclass of the given instanceType which correctly performs property transforms * and associates the instance with the correct model when instantiated. - * + * * @param TDocument The interface representing the structure of the documents found in the database. * @param TInstance The interface or class representing the documents after they have been wrapped in an instance. - * + * * @param model The model which instances should be associated with when the resulting constructor is used. * @param instanceType The constructor used to create new instances of type TInstance. - * + * * @internal */ export function ModelSpecificInstance(model: Model, instanceType: InstanceImplementation): ModelSpecificInstanceConstructor { diff --git a/lib/Plugins.ts b/lib/Plugins.ts index 3697502..b7558e2 100644 --- a/lib/Plugins.ts +++ b/lib/Plugins.ts @@ -1,4 +1,3 @@ -/// import {Core} from './Core'; import {Model} from './Model'; diff --git a/lib/Schema.ts b/lib/Schema.ts index d96924f..84f8c31 100644 --- a/lib/Schema.ts +++ b/lib/Schema.ts @@ -1,4 +1,3 @@ -/// export interface Schema { _id: boolean | any; [key:string]: any; diff --git a/lib/Transforms.ts b/lib/Transforms.ts index 514cd1c..42ad555 100644 --- a/lib/Transforms.ts +++ b/lib/Transforms.ts @@ -1,4 +1,3 @@ -/// export interface Transforms { [property:string]: PropertyTransform; } @@ -14,7 +13,7 @@ export interface PropertyTransform { * @returns A derived value which is more useful to the application. */ fromDB(value: any): any; - + /** * Converts a property's value into a representation more suitable for * the database. diff --git a/lib/cacheControllers/IDDirector.ts b/lib/cacheControllers/IDDirector.ts index eab3166..7c1ddfb 100644 --- a/lib/cacheControllers/IDDirector.ts +++ b/lib/cacheControllers/IDDirector.ts @@ -1,4 +1,3 @@ -/// import {CacheDirector} from '../CacheDirector'; import MongoDB = require('mongodb'); diff --git a/lib/caches/MemoryCache.ts b/lib/caches/MemoryCache.ts index bda0c4c..de158d5 100644 --- a/lib/caches/MemoryCache.ts +++ b/lib/caches/MemoryCache.ts @@ -1,10 +1,9 @@ -/// import Bluebird = require('bluebird'); import {Cache} from '../Cache'; /** * A cache implementation which stores documents in an in-memory cache. - * + * * Be aware that this is an incredibly simplistic implementation which doesn't manage * memory usage at all and is very likely NOT suitable for production use. */ diff --git a/lib/caches/NoOpCache.ts b/lib/caches/NoOpCache.ts index a898871..86607c4 100644 --- a/lib/caches/NoOpCache.ts +++ b/lib/caches/NoOpCache.ts @@ -1,11 +1,10 @@ -/// import {Cache} from '../Cache'; import Bluebird = require('bluebird'); /** * A cache implementation which does not cache any received documents * and returns nothing when requested - mimicking an empty cache. - * + * * This is the default cache used if one is not supplied and should * not impose any significant performance overhead. */ diff --git a/lib/middleware/Express.ts b/lib/middleware/Express.ts index 8e4f8e3..e496db4 100644 --- a/lib/middleware/Express.ts +++ b/lib/middleware/Express.ts @@ -1,4 +1,3 @@ -/// import http = require('http'); import {MiddlewareFactory} from '../Middleware'; import {Core} from '../Core'; @@ -7,7 +6,7 @@ import {Core} from '../Core'; * A factory method which creates Express/Connect compatible middleware functions to inject * a 'db' field on your request objects as well as ensuring that the Iridium Core is connected * to a MongoDB database before handling any requests. - * + * * @internal */ export function ExpressMiddlewareFactory(core: Core): ExpressMiddleware { diff --git a/lib/utils/ObjectID.ts b/lib/utils/ObjectID.ts index cd6e8d3..203abee 100644 --- a/lib/utils/ObjectID.ts +++ b/lib/utils/ObjectID.ts @@ -1,15 +1,14 @@ -/// import MongoDB = require('mongodb'); /** * Converts a string to an ObjectID instance - a shortcut for require('mongodb').ObjectID.createFromHexString - * + * * @param value The string representation of the ObjectID you wish to create. * @returns A MongoDB ObjectID instance equivalent to the string you provided. - * + * * You should be aware that this method performs no validation on the received string, MongoDB's ObjectID requires * that it either be a 12 byte UTF8 string, or a 24 byte hexadecimal string in order to be converted correctly. - * + * * This method removes the need for your application to directly depend on MongoDB's Node.js client library, * which helps clean up your code a bit and reduces the headache of maintaining two different versions of the * library (since Iridium also has one). diff --git a/lib/utils/Omnom.ts b/lib/utils/Omnom.ts index 6ec13ab..1646bff 100644 --- a/lib/utils/Omnom.ts +++ b/lib/utils/Omnom.ts @@ -1,4 +1,3 @@ -/// import _ = require('lodash'); import MongoDB = require('mongodb');