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
* @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 class extends constructor {
public static get collection() {
if (!(dbArg instanceof SmartdataDb)) {
dbArg = dbArg();
}
return collectionFactory.getCollection(constructor.name, dbArg);
}
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);
}
public get manager() {
if (managerArg) {
return managerArg();
let manager: TManager;
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 = (
nameArg: string,
dbArg: SmartdataDb | (() => SmartdataDb)
dbArg: SmartdataDb
): SmartdataCollection<any> => {
if (!this.collections[nameArg]) {
this.collections[nameArg] = (() => {
if (dbArg instanceof SmartdataDb) {
// tslint:disable-next-line: no-string-literal
return new SmartdataCollection(nameArg, dbArg);
} else {
dbArg = dbArg();
return new SmartdataCollection(nameArg, dbArg);
}
})();
}