fix(core): update

This commit is contained in:
Philipp Kunz 2021-09-17 22:34:15 +02:00
parent 236c8c6551
commit aeb35705d4
4 changed files with 27 additions and 31 deletions

24
.vscode/launch.json vendored
View File

@ -2,28 +2,10 @@
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "current file", "command": "npm test",
"type": "node", "name": "Run npm test",
"request": "launch", "request": "launch",
"args": [ "type": "node-terminal"
"${relativeFile}"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": "test.ts",
"type": "node",
"request": "launch",
"args": [
"test/test.ts"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
} }
] ]
} }

View File

@ -56,7 +56,8 @@ tap.test('should establish a connection to mongod', async () => {
// ------ // ------
// Collections // Collections
// ------ // ------
class CarTemplate extends smartdata.SmartDataDbDoc<CarTemplate, CarTemplate> { @smartdata.Manager()
class Car extends smartdata.SmartDataDbDoc<Car, Car> {
@smartdata.unI() @smartdata.unI()
public index: string = smartunique.shortId(); public index: string = smartunique.shortId();
@ -79,20 +80,22 @@ class CarTemplate extends smartdata.SmartDataDbDoc<CarTemplate, CarTemplate> {
} }
const createCarClass = (dbArg: smartdata.SmartdataDb) => { const createCarClass = (dbArg: smartdata.SmartdataDb) => {
console.log(this); smartdata.setDefaultManagerForDoc({db: dbArg}, Car);
@smartdata.Collection(() => {
return dbArg;
})
class Car extends CarTemplate {};
return Car; return Car;
}; };
tap.test('should prodice a car', async () => { tap.test('should produce a car', async () => {
const CarClass = createCarClass(testDb); const CarClass = createCarClass(testDb);
const carInstance = new CarClass('red', 'Mercedes'); const carInstance = new CarClass('red', 'Mercedes');
await carInstance.save(); await carInstance.save();
}); });
tap.test('should get a car', async () => {
const car = Car.getInstance({
color: 'red'
})
})
// ======================================= // =======================================
// close the database connection // close the database connection
// ======================================= // =======================================

View File

@ -45,16 +45,22 @@ export interface IManager {
db: SmartdataDb db: SmartdataDb
} }
export const setDefaultManagerForDoc = (managerArg: IManager, dbDoc: any) => {
dbDoc.prototype.defaultManager = managerArg;
}
/** /**
* 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 Manager<TManager extends IManager>(managerArg?: TManager | TDelayed<TManager>) { export function Manager<TManager extends IManager>(managerArg?: TManager | TDelayed<TManager>) {
return function classDecorator<T extends { new (...args: any[]): {} }>(constructor: T) { return function classDecorator<T extends { new (...args: any[]): any }>(constructor: T) {
return class extends constructor { return class extends constructor {
public static get collection() { public static get collection() {
let dbArg: SmartdataDb; let dbArg: SmartdataDb;
if (managerArg['db']) { if (!managerArg) {
dbArg = this.prototype.defaultManager.db;
} else if (managerArg['db']) {
dbArg = (managerArg as TManager).db dbArg = (managerArg as TManager).db
} else { } else {
dbArg = (managerArg as TDelayed<TManager>)().db; dbArg = (managerArg as TDelayed<TManager>)().db;
@ -63,7 +69,11 @@ export interface IManager {
} }
public get collection() { public get collection() {
let dbArg: SmartdataDb; let dbArg: SmartdataDb;
if (managerArg['db']) { if (!managerArg) {
//console.log(this.defaultManager.db);
//process.exit(0)
dbArg = this.defaultManager.db;
} else if (managerArg['db']) {
dbArg = (managerArg as TManager).db dbArg = (managerArg as TManager).db
} else { } else {
dbArg = (managerArg as TDelayed<TManager>)().db; dbArg = (managerArg as TDelayed<TManager>)().db;

View File

@ -47,6 +47,7 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
*/ */
public static collection: SmartdataCollection<any>; public static collection: SmartdataCollection<any>;
public collection: SmartdataCollection<any>; public collection: SmartdataCollection<any>;
public static defaultManager;
public static manager; public static manager;
public manager: TManager; public manager: TManager;