Compare commits

..

9 Commits

Author SHA1 Message Date
ab1799f4f2 3.1.12 2019-01-08 18:59:36 +01:00
9985b849d1 3.1.11 2019-01-08 18:45:31 +01:00
1800273b25 fix(core): update 2019-01-08 18:45:30 +01:00
e715cc6d37 3.1.10 2019-01-08 18:22:48 +01:00
f7abc175aa fix(core): update 2019-01-08 18:22:48 +01:00
bbcb3a614e 3.1.9 2019-01-08 14:37:17 +01:00
941d2f0902 fix(core): update 2019-01-08 14:37:17 +01:00
5c78f83a28 3.1.8 2019-01-08 13:52:09 +01:00
124f117352 fix(core): update 2019-01-08 13:52:08 +01:00
5 changed files with 71 additions and 17 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartdata", "name": "@pushrocks/smartdata",
"version": "3.1.7", "version": "3.1.12",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartdata", "name": "@pushrocks/smartdata",
"version": "3.1.7", "version": "3.1.12",
"private": false, "private": false,
"description": "do more with data", "description": "do more with data",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -1,5 +1,5 @@
export interface IMongoDescriptor { export interface IMongoDescriptor {
connectionUrl: string; mongoDbName: string;
password: string; mongoDbUrl: string;
database: string; mongoDbPass: string;
} }

View File

@ -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());
};
}
}; };
} }
@ -99,7 +109,6 @@ export class SmartdataCollection<T> {
await this.checkDoc(dbDocArg); await this.checkDoc(dbDocArg);
this.markUniqueIndexes(dbDocArg.uniqueIndexes); this.markUniqueIndexes(dbDocArg.uniqueIndexes);
const saveableObject = await dbDocArg.createSavableObject(); const saveableObject = await dbDocArg.createSavableObject();
console.log(saveableObject);
const result = await this.mongoDbCollection.insertOne(saveableObject); const result = await this.mongoDbCollection.insertOne(saveableObject);
return result; return result;
} }
@ -107,11 +116,19 @@ export class SmartdataCollection<T> {
/** /**
* inserts object into the DbCollection * inserts object into the DbCollection
*/ */
async update(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> { public async update(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
await this.init(); await this.init();
await this.checkDoc(dbDocArg); await this.checkDoc(dbDocArg);
const identifiableObject = await dbDocArg.createIdentifiableObject();
const saveableObject = await dbDocArg.createSavableObject(); const saveableObject = await dbDocArg.createSavableObject();
this.mongoDbCollection.updateOne(saveableObject.dbDocUniqueId, saveableObject); this.mongoDbCollection.updateOne(identifiableObject, saveableObject);
}
public async delete (dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
await this.init();
await this.checkDoc(dbDocArg);
const identifiableObject = await dbDocArg.createIdentifiableObject();
this.mongoDbCollection.deleteOne(identifiableObject);
} }
/** /**
@ -119,7 +136,7 @@ export class SmartdataCollection<T> {
* if this.objectValidation is not set it passes. * if this.objectValidation is not set it passes.
*/ */
private checkDoc(docArg: T): Promise<void> { private checkDoc(docArg: T): Promise<void> {
let done = plugins.smartq.defer<void>(); const done = plugins.smartq.defer<void>();
let validationResult = true; let validationResult = true;
if (this.objectValidation) { if (this.objectValidation) {
validationResult = this.objectValidation(docArg); validationResult = this.objectValidation(docArg);

View File

@ -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':
@ -123,6 +139,13 @@ export class SmartDataDbDoc<T> {
} }
} }
/**
* deletes a document from the database
*/
async delete() {
}
/** /**
* also store any referenced objects to DB * also store any referenced objects to DB
* better for data consistency * better for data consistency
@ -141,11 +164,25 @@ export class SmartDataDbDoc<T> {
} }
} }
async createSavableObject() { /**
let saveableObject: any = {}; // is not exposed to outside, so any is ok here * creates a saveable object so the instance can be persisted as json in the database
for (let propertyNameString of this.saveableProperties) { */
public async createSavableObject() {
const saveableObject: any = {}; // is not exposed to outside, so any is ok here
for (const propertyNameString of this.saveableProperties) {
saveableObject[propertyNameString] = this[propertyNameString]; saveableObject[propertyNameString] = this[propertyNameString];
} }
return saveableObject; return saveableObject;
} }
/**
* creates an identifiable object for operations that require filtering
*/
public async createIdentifiableObject() {
const identifiableObject: any = {}; // is not exposed to outside, so any is ok here
for (const propertyNameString of this.uniqueIndexes) {
identifiableObject[propertyNameString] = this[propertyNameString];
}
return identifiableObject;
}
} }