fix(core): update

This commit is contained in:
Philipp Kunz 2021-06-09 13:40:23 +02:00
parent 55d96fa68d
commit 97365ddf29
2 changed files with 47 additions and 7 deletions

View File

@ -22,19 +22,62 @@ const collectionFactory = new CollectionFactory();
* This is a decorator that will tell the decorated class what dbTable to use * This is a decorator that will tell the decorated class what dbTable to use
* @param dbArg * @param dbArg
*/ */
export function Collection<TManager>(dbArg: SmartdataDb | TDelayed<SmartdataDb>, managerArg?: TDelayed<TManager>) { export function Collection(dbArg: SmartdataDb | TDelayed<SmartdataDb>) {
return function classDecorator<T extends { new (...args: any[]): {} }>(constructor: T) { return function classDecorator<T extends { new (...args: any[]): {} }>(constructor: T) {
return class extends constructor { return class extends constructor {
public static get collection() { public static get collection() {
if (!(dbArg instanceof SmartdataDb)) {
dbArg = dbArg();
}
return collectionFactory.getCollection(constructor.name, dbArg); return collectionFactory.getCollection(constructor.name, dbArg);
} }
public get collection() { public get collection() {
if (!(dbArg instanceof SmartdataDb)) {
dbArg = dbArg();
}
return collectionFactory.getCollection(constructor.name, dbArg);
}
};
};
}
interface IManager {
db: SmartdataDb
}
/**
* This is a decorator that will tell the decorated class what dbTable to use
* @param dbArg
*/
export function Manager<TManager extends IManager>(managerArg?: TManager | TDelayed<TManager>) {
return function classDecorator<T extends { new (...args: any[]): {} }>(constructor: T) {
return class extends constructor {
public static get collection() {
let dbArg: SmartdataDb;
if (managerArg['db']) {
dbArg = (managerArg as TManager).db
} else {
dbArg = (managerArg as TDelayed<TManager>)().db;
}
return collectionFactory.getCollection(constructor.name, dbArg);
}
public get collection() {
let dbArg: SmartdataDb;
if (managerArg['db']) {
dbArg = (managerArg as TManager).db
} else {
dbArg = (managerArg as TDelayed<TManager>)().db;
}
return collectionFactory.getCollection(constructor.name, dbArg); return collectionFactory.getCollection(constructor.name, dbArg);
} }
public get manager() { public get manager() {
if (managerArg) { let manager: TManager;
return managerArg(); if (managerArg['db']) {
manager = (managerArg as TManager);
} else {
manager = (managerArg as TDelayed<TManager>)();
} }
return manager;
} }
}; };
}; };

View File

@ -7,16 +7,13 @@ export class CollectionFactory {
public getCollection = ( public getCollection = (
nameArg: string, nameArg: string,
dbArg: SmartdataDb | (() => SmartdataDb) dbArg: SmartdataDb
): SmartdataCollection<any> => { ): SmartdataCollection<any> => {
if (!this.collections[nameArg]) { if (!this.collections[nameArg]) {
this.collections[nameArg] = (() => { this.collections[nameArg] = (() => {
if (dbArg instanceof SmartdataDb) { if (dbArg instanceof SmartdataDb) {
// tslint:disable-next-line: no-string-literal // tslint:disable-next-line: no-string-literal
return new SmartdataCollection(nameArg, dbArg); return new SmartdataCollection(nameArg, dbArg);
} else {
dbArg = dbArg();
return new SmartdataCollection(nameArg, dbArg);
} }
})(); })();
} }