From 011d48c9b186f1237019af3ffe1612b86d7e3b37 Mon Sep 17 00:00:00 2001 From: Benjamin Pannell Date: Tue, 15 Dec 2015 18:34:16 +0200 Subject: [PATCH] refactor: Switch to using an options object for transform calls --- lib/ModelHandlers.ts | 4 +--- lib/ModelHelpers.ts | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/ModelHandlers.ts b/lib/ModelHandlers.ts index bb5e099..c1e9f9c 100644 --- a/lib/ModelHandlers.ts +++ b/lib/ModelHandlers.ts @@ -31,10 +31,8 @@ export class ModelHandlers { 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 // If onRetrieved returns a Bluebird promise then there is no significant performance overhead here .resolve(this.model.hooks.onRetrieved && this.model.hooks.onRetrieved(target)) diff --git a/lib/ModelHelpers.ts b/lib/ModelHelpers.ts index d9e08e2..d308b0e 100644 --- a/lib/ModelHelpers.ts +++ b/lib/ModelHelpers.ts @@ -46,10 +46,12 @@ export class ModelHelpers { * @returns {any} The result of having transformed the document. * @remarks This is only really called from insert/create - as */ - transformToDB(document: T, processProperties: boolean = true): T { - if(this.model.transforms.$document) + transformToDB(document: T, options: TransformOptions = { properties: true }): T { + if(options.document && this.model.transforms.$document) document = 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)) { @@ -68,10 +70,12 @@ export class ModelHelpers { * 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)) { @@ -89,9 +93,9 @@ export class ModelHelpers { * document level transforms. * @returns {any} A new document cloned from the original and transformed */ - convertToDB(document: T, processProperties: boolean = true): T { + convertToDB(document: T, options: TransformOptions = { properties: true }): T { var doc: T = this.cloneDocument(document); - return this.transformToDB(doc); + return this.transformToDB(doc, options); } /** @@ -130,3 +134,8 @@ export class ModelHelpers { return this.cloneDocument(original); } } + +export interface TransformOptions { + properties?: boolean; + document?: boolean; +} \ No newline at end of file