diff --git a/ts/smartdata.classes.collection.ts b/ts/smartdata.classes.collection.ts index ba6660c..6cd8beb 100644 --- a/ts/smartdata.classes.collection.ts +++ b/ts/smartdata.classes.collection.ts @@ -13,13 +13,23 @@ export interface IDocValidationFunc { (doc: T): boolean; } +export type TDelayedDbCreation = () => SmartdataDb; + /** * This is a decorator that will tell the decorated class what dbTable to use - * @param db + * @param dbArg */ -export function Collection(db: SmartdataDb) { +export function Collection(dbArg: SmartdataDb | TDelayedDbCreation) { return function(constructor) { - constructor['smartdataCollection'] = new SmartdataCollection(constructor, db); + if (dbArg instanceof SmartdataDb) { + // tslint:disable-next-line: no-string-literal + constructor['smartdataCollection'] = new SmartdataCollection(constructor, dbArg); + } else { + constructor['smartdataDelayedDatabase'] = () => { + return new SmartdataCollection(constructor, dbArg()); + }; + } + }; } diff --git a/ts/smartdata.classes.doc.ts b/ts/smartdata.classes.doc.ts index 514e0b4..74e9803 100644 --- a/ts/smartdata.classes.doc.ts +++ b/ts/smartdata.classes.doc.ts @@ -77,7 +77,16 @@ export class SmartDataDbDoc { */ constructor() { this.name = this.constructor['name']; - this.collection = this.constructor['smartdataCollection']; + if(this.constructor['smartdataCollection']) { + // tslint:disable-next-line: no-string-literal + this.collection = this.constructor['smartdataCollection']; + // tslint:disable-next-line: no-string-literal + } else if (typeof this.constructor['smartdataDelayedDatabase'] === 'function') { + // tslint:disable-next-line: no-string-literal + this.collection = this.constructor['smartdataDelayedDatabase'](); + } else { + console.error('Could not determine collection for DbDoc'); + } } static async getInstances(filterArg): Promise { @@ -109,6 +118,7 @@ export class SmartDataDbDoc { * may lead to data inconsistencies, but is faster */ async save() { + // tslint:disable-next-line: no-this-assignment let self: any = this; switch (this.creationStatus) { case 'db':