Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
c241247845 | |||
81e39d09e4 | |||
8e51b518b1 | |||
8308d8d03b | |||
97365ddf29 | |||
55d96fa68d | |||
54ec6accdf |
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "4.0.1",
|
"version": "4.0.5",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@pushrocks/smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "4.0.1",
|
"version": "4.0.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/lik": "^4.0.20",
|
"@pushrocks/lik": "^4.0.20",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "4.0.1",
|
"version": "4.0.5",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "do more with data",
|
"description": "do more with data",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
|
@ -14,7 +14,7 @@ export interface IDocValidationFunc<T> {
|
|||||||
(doc: T): boolean;
|
(doc: T): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TDelayedDbCreation = () => SmartdataDb;
|
export type TDelayed<TDelayedArg> = () => TDelayedArg;
|
||||||
|
|
||||||
const collectionFactory = new CollectionFactory();
|
const collectionFactory = new CollectionFactory();
|
||||||
|
|
||||||
@ -22,19 +22,67 @@ 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(dbArg: SmartdataDb | TDelayedDbCreation) {
|
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);
|
return collectionFactory.getCollection(constructor.name, dbArg);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export 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() {
|
||||||
|
let manager: TManager;
|
||||||
|
if (managerArg['db']) {
|
||||||
|
manager = (managerArg as TManager);
|
||||||
|
} else {
|
||||||
|
manager = (managerArg as TDelayed<TManager>)();
|
||||||
|
}
|
||||||
|
return manager;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// tslint:disable-next-line: max-classes-per-file
|
// tslint:disable-next-line: max-classes-per-file
|
||||||
export class SmartdataCollection<T> {
|
export class SmartdataCollection<T> {
|
||||||
/**
|
/**
|
||||||
|
@ -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);
|
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ import * as plugins from './smartdata.plugins';
|
|||||||
import { ObjectMap } from '@pushrocks/lik';
|
import { ObjectMap } from '@pushrocks/lik';
|
||||||
|
|
||||||
import { SmartdataDb } from './smartdata.classes.db';
|
import { SmartdataDb } from './smartdata.classes.db';
|
||||||
import { SmartdataCollection } from './smartdata.classes.collection';
|
import { IManager, SmartdataCollection } from './smartdata.classes.collection';
|
||||||
|
|
||||||
export type TDocCreation = 'db' | 'new' | 'mixed';
|
export type TDocCreation = 'db' | 'new' | 'mixed';
|
||||||
|
|
||||||
@ -41,12 +41,13 @@ export function unI() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SmartDataDbDoc<T extends TImplements, TImplements> {
|
export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends IManager = any> {
|
||||||
/**
|
/**
|
||||||
* the collection object an Doc belongs to
|
* the collection object an Doc belongs to
|
||||||
*/
|
*/
|
||||||
public static collection: SmartdataCollection<any>;
|
public static collection: SmartdataCollection<any>;
|
||||||
public collection: SmartdataCollection<any>;
|
public collection: SmartdataCollection<any>;
|
||||||
|
public manager: TManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* how the Doc in memory was created, may prove useful later.
|
* how the Doc in memory was created, may prove useful later.
|
||||||
|
Reference in New Issue
Block a user