Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
e715cc6d37 | |||
f7abc175aa | |||
bbcb3a614e | |||
941d2f0902 |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "3.1.8",
|
"version": "3.1.10",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "3.1.8",
|
"version": "3.1.10",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "do more with data",
|
"description": "do more with data",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
@ -13,13 +13,23 @@ export interface IDocValidationFunc<T> {
|
|||||||
(doc: T): boolean;
|
(doc: T): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type TDelayedDbCreation = () => SmartdataDb;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 db
|
* @param dbArg
|
||||||
*/
|
*/
|
||||||
export function Collection(db: SmartdataDb) {
|
export function Collection(dbArg: SmartdataDb | TDelayedDbCreation) {
|
||||||
return function(constructor) {
|
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['smartdataDelayedCollection'] = () => {
|
||||||
|
return new SmartdataCollection(constructor, dbArg());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,12 +77,27 @@ export class SmartDataDbDoc<T> {
|
|||||||
*/
|
*/
|
||||||
constructor() {
|
constructor() {
|
||||||
this.name = this.constructor['name'];
|
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['smartdataDelayedCollection'] === 'function') {
|
||||||
|
// tslint:disable-next-line: no-string-literal
|
||||||
|
this.collection = this.constructor['smartdataDelayedCollection']();
|
||||||
|
} else {
|
||||||
|
console.error('Could not determine collection for DbDoc');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getInstances<T>(filterArg): Promise<T[]> {
|
static async getInstances<T>(filterArg): Promise<T[]> {
|
||||||
let self: any = this; // fool typesystem
|
let self: any = this; // fool typesystem
|
||||||
let referenceMongoDBCollection: SmartdataCollection<T> = self.smartdataCollection;
|
let referenceMongoDBCollection: SmartdataCollection<T>;
|
||||||
|
|
||||||
|
if (self.smartdataCollection) {
|
||||||
|
referenceMongoDBCollection = self.smartdataCollection;
|
||||||
|
} else if (self.smartdataDelayedCollection) {
|
||||||
|
referenceMongoDBCollection = self.smartdataDelayedCollection();
|
||||||
|
};
|
||||||
const foundDocs = await referenceMongoDBCollection.find(filterArg);
|
const foundDocs = await referenceMongoDBCollection.find(filterArg);
|
||||||
const returnArray = [];
|
const returnArray = [];
|
||||||
for (let item of foundDocs) {
|
for (let item of foundDocs) {
|
||||||
@ -109,6 +124,7 @@ export class SmartDataDbDoc<T> {
|
|||||||
* may lead to data inconsistencies, but is faster
|
* may lead to data inconsistencies, but is faster
|
||||||
*/
|
*/
|
||||||
async save() {
|
async save() {
|
||||||
|
// tslint:disable-next-line: no-this-assignment
|
||||||
let self: any = this;
|
let self: any = this;
|
||||||
switch (this.creationStatus) {
|
switch (this.creationStatus) {
|
||||||
case 'db':
|
case 'db':
|
||||||
|
Reference in New Issue
Block a user