Skip to content

Commit

Permalink
refactor: Switch to using an options object for transform calls
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Dec 15, 2015
1 parent 29a630a commit 011d48c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
4 changes: 1 addition & 3 deletions lib/ModelHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ export class ModelHandlers<TDocument extends { _id?: any }, TInstance> {
partial: false
});

this.model.helpers.transformFromDB(result, false);

let wrapped: TResult;
return Bluebird.resolve(result).then((target: any) => {
return Bluebird.resolve(this.model.helpers.transformFromDB(result, { document: true })).then((target: any) => {
return <Bluebird<TResult>>Bluebird
// If onRetrieved returns a Bluebird promise then there is no significant performance overhead here
.resolve(this.model.hooks.onRetrieved && this.model.hooks.onRetrieved(target))
Expand Down
21 changes: 15 additions & 6 deletions lib/ModelHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ export class ModelHelpers<TDocument extends { _id?: any }, TInstance> {
* @returns {any} The result of having transformed the document.
* @remarks This is only really called from insert/create - as
*/
transformToDB<T>(document: T, processProperties: boolean = true): T {
if(this.model.transforms.$document)
transformToDB<T>(document: T, options: TransformOptions = { properties: true }): T {
if(options.document && this.model.transforms.$document)
document = <any>this.model.transforms.$document.toDB(document, '$document', this.model);

if(!options.properties) return document;

for (var property in this.model.transforms)
if(property === '$document') continue;
else if(document.hasOwnProperty(property)) {
Expand All @@ -68,10 +70,12 @@ export class ModelHelpers<TDocument extends { _id?: any }, TInstance> {
* document level transforms, as property level transforms are applied in
* their relevant instance setters.
*/
transformFromDB(document: TDocument, processProperties: boolean = true): TDocument {
if(this.model.transforms.$document)
transformFromDB(document: TDocument, options: TransformOptions = { properties: true }): TDocument {
if(options.document && this.model.transforms.$document)
document = this.model.transforms.$document.fromDB(document, '$document', this.model);

if(!options.properties) return document;

for (var property in this.model.transforms)
if(property === '$document') continue;
else if(document.hasOwnProperty(property)) {
Expand All @@ -89,9 +93,9 @@ export class ModelHelpers<TDocument extends { _id?: any }, TInstance> {
* document level transforms.
* @returns {any} A new document cloned from the original and transformed
*/
convertToDB<T>(document: T, processProperties: boolean = true): T {
convertToDB<T>(document: T, options: TransformOptions = { properties: true }): T {
var doc: T = this.cloneDocument(document);
return this.transformToDB(doc);
return this.transformToDB(doc, options);
}

/**
Expand Down Expand Up @@ -130,3 +134,8 @@ export class ModelHelpers<TDocument extends { _id?: any }, TInstance> {
return this.cloneDocument(original);
}
}

export interface TransformOptions {
properties?: boolean;
document?: boolean;
}

0 comments on commit 011d48c

Please sign in to comment.