Skip to content

Commit

Permalink
refactor: Remove references file from all files
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Sep 29, 2015
1 parent ba6c01c commit aabb131
Show file tree
Hide file tree
Showing 29 changed files with 93 additions and 120 deletions.
2 changes: 0 additions & 2 deletions lib/Aggregate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference path="../_references.d.ts" />

export interface Stage {

}
1 change: 0 additions & 1 deletion lib/Cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// <reference path="../_references.d.ts" />
import Bluebird = require('bluebird');

export interface Cache {
Expand Down
1 change: 0 additions & 1 deletion lib/CacheDirector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// <reference path="../_references.d.ts" />
export interface CacheDirector {
valid<T>(object: T): boolean;
buildKey<T>(object: T): string;
Expand Down
1 change: 0 additions & 1 deletion lib/Configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// <reference path="../_references.d.ts" />
export interface Configuration {
host?: string;
port?: number;
Expand Down
1 change: 0 additions & 1 deletion lib/Core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// <reference path="../_references.d.ts" />
import Bluebird = require('bluebird');
import MongoDB = require('mongodb');
import _ = require('lodash');
Expand Down
5 changes: 2 additions & 3 deletions lib/Cursor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// <reference path="../_references.d.ts" />
import {Model} from './Model';
import {Model} from './Model';
import General = require('./General');
import MongoDB = require('mongodb');
import Bluebird = require('bluebird');
Expand All @@ -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.
*/
Expand Down
55 changes: 29 additions & 26 deletions lib/Decorators.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/// <reference path="../_references.d.ts" />
import MongoDB = require('mongodb');
import _ = require('lodash');
import skmatc = require('skmatc');
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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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<any, any>, 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<any, any>) => void;
export function Property(...args: any[]): (target: Instance<any, any> | InstanceImplementation<any, any>, 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<any, any>, property?: string) {
let staticTarget: InstanceImplementation<any, any> = target;
if (!property) name = args.shift();
else {
name = property;
target = target.constructor;
staticTarget = <InstanceImplementation<any, any>>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<any, any>, property: string) {
let staticTarget: InstanceImplementation<any, any> = <InstanceImplementation<any, any>>(target.constructor || target);

staticTarget.transforms = _.clone(staticTarget.transforms || <Transforms>{})
staticTarget.transforms[property] = {
fromDB: fromDB,
toDB: toDB
};
Expand All @@ -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<any, any>, name: string) {
Property(MongoDB.ObjectID)(target, name);
Transform(
value => value && value._bsontype == 'ObjectID' ? new MongoDB.ObjectID(value.id).toHexString() : value,
Expand Down
2 changes: 0 additions & 2 deletions lib/General.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference path="../_references.d.ts" />

/**
* A method which is called once an asynchronous operation has completed, an alternative
* to using Promises.
Expand Down
1 change: 0 additions & 1 deletion lib/Hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// <reference path="../_references.d.ts" />
import instance = require('./Instance');

export interface Hooks<TDocument, TInstance> {
Expand Down
3 changes: 1 addition & 2 deletions lib/Index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// <reference path="../_references.d.ts" />
import MongoDB = require('mongodb');
import MongoDB = require('mongodb');

export interface Index {
spec: IndexSpecification;
Expand Down
19 changes: 9 additions & 10 deletions lib/Instance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// <reference path="../_references.d.ts" />
import {Core} from './Core';
import {Core} from './Core';
import {Model} from './Model';
import {Plugin} from './Plugins';
import {CacheDirector} from './CacheDirector';
Expand All @@ -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
Expand Down Expand Up @@ -70,24 +69,24 @@ export class Instance<TDocument extends { _id?: any }, TInstance> {
* @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.
*/
Expand Down Expand Up @@ -125,12 +124,12 @@ export class Instance<TDocument extends { _id?: any }, TInstance> {
* 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
Expand Down
21 changes: 10 additions & 11 deletions lib/InstanceInterface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// <reference path="../_references.d.ts" />
import {Schema} from './Schema';
import {Model} from './Model';
import * as Index from './Index';
Expand All @@ -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
Expand All @@ -26,7 +25,7 @@ export interface InstanceImplementation<TDocument extends { _id ?: any }, TInsta
* @param isPartial Whether the document which has been given to this instance had any field restrictions imposed on it during the query, and may therefore only contain partial data.
*/
new (model: Model<TDocument, TInstance>, doc: TDocument, isNew?: boolean, isPartial?: boolean): TInstance;

/**
* The name of the database collection from which documents are retrieved, and to which they are stored.
*/
Expand All @@ -41,32 +40,32 @@ export interface InstanceImplementation<TDocument extends { _id ?: any }, TInsta
* use of them as a result.
*/
schema: Schema;

/**
* Any additional indexes on the collection which should be managed by Iridium.
* This field is optional, but if provided allows you to make use of the model's ensureIndexes() method
* to automatically generate all specified indexes.
*/
indexes?: (Index.Index | Index.IndexSpecification)[];

/**
* An optional method which will be called whenever a document is about to be inserted into the database,
* allowing you to set default values and do any preprocessing you wish prior to the document being inserted.
*/
onCreating? (document: TDocument): void;

/**
* An optional method which is called whenever a new document is received from the model's collection and
* prior to the document being wrapped, can be used to perform preprocessing if necessary - however we recommend
* you rather make use of transforms for that task.
*/
onRetrieved? (document: TDocument): void;

/**
* An optional method which is called whenever a new document for this model has been wrapped in an instance.
*/
onReady? (instance: TInstance): void;

/**
* An optional method which is called prior to saving an instance, it is provided with the instance itself as
* well as the proposed changes to the instance. This allows you to make additional changes, such as updating
Expand All @@ -82,14 +81,14 @@ export interface InstanceImplementation<TDocument extends { _id ?: any }, TInsta
* properties instead.
*/
cache?: CacheDirector;

/**
* Any additional validation types you wish to make available for use within this model's database schema. This
* allows you to validate using conditions otherwise not available within skmatc itself. For more information
* on implementing a validator, take a look at the skmatc documentation on GitHub.
*/
validators?: Skmatc.Validator[];

/**
* Any transform operations you would like to perform on documents received from the database, or prior to being
* sent to the database. These may include things such as converting ObjectIDs to strings for the application, and
Expand Down
1 change: 0 additions & 1 deletion lib/Middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// <reference path="../_references.d.ts" />
import {Core} from './Core';

/**
Expand Down
7 changes: 3 additions & 4 deletions lib/Model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// <reference path="../_references.d.ts" />
import MongoDB = require('mongodb');
import MongoDB = require('mongodb');
import Bluebird = require('bluebird');
import util = require('util');
import _ = require('lodash');
Expand Down Expand Up @@ -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<TDocument extends { _id?: any }, TInstance> {
Expand Down
3 changes: 1 addition & 2 deletions lib/ModelCache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// <reference path="../_references.d.ts" />
import {Model} from './Model';
import {Model} from './Model';
import Bluebird = require('bluebird');

/**
Expand Down
Loading

0 comments on commit aabb131

Please sign in to comment.