fix(core): update
This commit is contained in:
@ -36,11 +36,11 @@ export class SmartdataCollection<T> {
|
||||
/**
|
||||
* the collection that is used
|
||||
*/
|
||||
mongoDbCollection: plugins.mongodb.Collection;
|
||||
objectValidation: IDocValidationFunc<T> = null;
|
||||
collectionName: string;
|
||||
smartdataDb: SmartdataDb;
|
||||
uniqueIndexes: string[] = [];
|
||||
public mongoDbCollection: plugins.mongodb.Collection;
|
||||
public objectValidation: IDocValidationFunc<T> = null;
|
||||
public collectionName: string;
|
||||
public smartdataDb: SmartdataDb;
|
||||
public uniqueIndexes: string[] = [];
|
||||
|
||||
constructor(collectedClassArg: T & SmartDataDbDoc<T>, smartDataDbArg: SmartdataDb) {
|
||||
// tell the collection where it belongs
|
||||
@ -54,7 +54,7 @@ export class SmartdataCollection<T> {
|
||||
/**
|
||||
* makes sure a collection exists within MongoDb that maps to the SmartdataCollection
|
||||
*/
|
||||
async init() {
|
||||
public async init() {
|
||||
if (!this.mongoDbCollection) {
|
||||
// connect this instance to a MongoDB collection
|
||||
const availableMongoDbCollections = await this.smartdataDb.mongoDb.collections();
|
||||
@ -65,15 +65,15 @@ export class SmartdataCollection<T> {
|
||||
await this.smartdataDb.mongoDb.createCollection(this.collectionName);
|
||||
}
|
||||
this.mongoDbCollection = await this.smartdataDb.mongoDb.collection(this.collectionName);
|
||||
console.log(`Successfully initiated Collection ${this.collectionName}`);
|
||||
// console.log(`Successfully initiated Collection ${this.collectionName}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* mark unique index
|
||||
*/
|
||||
markUniqueIndexes(keyArrayArg: string[] = []) {
|
||||
for (let key of keyArrayArg) {
|
||||
public markUniqueIndexes(keyArrayArg: string[] = []) {
|
||||
for (const key of keyArrayArg) {
|
||||
if (!this.uniqueIndexes.includes(key)) {
|
||||
this.mongoDbCollection.createIndex(key, {
|
||||
unique: true
|
||||
@ -87,14 +87,14 @@ export class SmartdataCollection<T> {
|
||||
/**
|
||||
* adds a validation function that all newly inserted and updated objects have to pass
|
||||
*/
|
||||
addDocValidation(funcArg: IDocValidationFunc<T>) {
|
||||
public addDocValidation(funcArg: IDocValidationFunc<T>) {
|
||||
this.objectValidation = funcArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* finds an object in the DbCollection
|
||||
*/
|
||||
async find(filterObject: any): Promise<any> {
|
||||
public async find(filterObject: any): Promise<any> {
|
||||
await this.init();
|
||||
const result = await this.mongoDbCollection.find(filterObject).toArray();
|
||||
return result;
|
||||
@ -103,7 +103,7 @@ export class SmartdataCollection<T> {
|
||||
/**
|
||||
* create an object in the database
|
||||
*/
|
||||
async insert(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
|
||||
public async insert(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
|
||||
await this.init();
|
||||
await this.checkDoc(dbDocArg);
|
||||
this.markUniqueIndexes(dbDocArg.uniqueIndexes);
|
||||
@ -120,7 +120,17 @@ export class SmartdataCollection<T> {
|
||||
await this.checkDoc(dbDocArg);
|
||||
const identifiableObject = await dbDocArg.createIdentifiableObject();
|
||||
const saveableObject = await dbDocArg.createSavableObject();
|
||||
this.mongoDbCollection.updateOne(identifiableObject, saveableObject);
|
||||
console.log(identifiableObject);
|
||||
console.log(saveableObject);
|
||||
const updateableObject: any = {};
|
||||
for (const key of Object.keys(saveableObject)) {
|
||||
if (identifiableObject[key]) {
|
||||
continue;
|
||||
}
|
||||
updateableObject[key] = saveableObject[key];
|
||||
}
|
||||
console.log(updateableObject);
|
||||
this.mongoDbCollection.updateOne(identifiableObject, { $set: updateableObject }, {upsert: true});
|
||||
}
|
||||
|
||||
public async delete(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
|
||||
|
@ -44,7 +44,7 @@ export class SmartdataDb {
|
||||
/**
|
||||
* connects to the database that was specified during instance creation
|
||||
*/
|
||||
async init(): Promise<any> {
|
||||
public async init(): Promise<any> {
|
||||
let finalConnectionUrl = this.smartdataOptions.mongoDbUrl;
|
||||
if (this.smartdataOptions.mongoDbPass) {
|
||||
finalConnectionUrl = mongoHelpers.addPassword(
|
||||
@ -52,7 +52,7 @@ export class SmartdataDb {
|
||||
this.smartdataOptions.mongoDbPass
|
||||
);
|
||||
}
|
||||
console.log(finalConnectionUrl);
|
||||
console.log(`connection Url: ${finalConnectionUrl}`);
|
||||
this.mongoDbClient = await plugins.mongodb.MongoClient.connect(finalConnectionUrl, {
|
||||
useNewUrlParser: true
|
||||
});
|
||||
@ -64,7 +64,7 @@ export class SmartdataDb {
|
||||
/**
|
||||
* closes the connection to the databse
|
||||
*/
|
||||
async close(): Promise<any> {
|
||||
public async close(): Promise<any> {
|
||||
await this.mongoDbClient.close();
|
||||
this.status = 'disconnected';
|
||||
plugins.smartlog.defaultLogger.log(
|
||||
@ -75,7 +75,7 @@ export class SmartdataDb {
|
||||
|
||||
// handle table to class distribution
|
||||
|
||||
addTable(SmartdataCollectionArg: SmartdataCollection<any>) {
|
||||
public addTable(SmartdataCollectionArg: SmartdataCollection<any>) {
|
||||
this.smartdataCollectionMap.add(SmartdataCollectionArg);
|
||||
}
|
||||
|
||||
@ -84,8 +84,8 @@ export class SmartdataDb {
|
||||
* @param nameArg
|
||||
* @returns DbTable
|
||||
*/
|
||||
async getSmartdataCollectionByName<T>(nameArg: string): Promise<SmartdataCollection<T>> {
|
||||
let resultCollection = this.smartdataCollectionMap.find(dbTableArg => {
|
||||
public async getSmartdataCollectionByName<T>(nameArg: string): Promise<SmartdataCollection<T>> {
|
||||
const resultCollection = this.smartdataCollectionMap.find(dbTableArg => {
|
||||
return dbTableArg.collectionName === nameArg;
|
||||
});
|
||||
return resultCollection;
|
||||
|
@ -45,32 +45,32 @@ export class SmartDataDbDoc<T> {
|
||||
/**
|
||||
* the collection object an Doc belongs to
|
||||
*/
|
||||
collection: SmartdataCollection<T>;
|
||||
public collection: SmartdataCollection<T>;
|
||||
|
||||
/**
|
||||
* how the Doc in memory was created, may prove useful later.
|
||||
*/
|
||||
creationStatus: TDocCreation = 'new';
|
||||
public creationStatus: TDocCreation = 'new';
|
||||
|
||||
/**
|
||||
* unique indexes
|
||||
*/
|
||||
uniqueIndexes: string[];
|
||||
public uniqueIndexes: string[];
|
||||
|
||||
/**
|
||||
* an array of saveable properties of a doc
|
||||
*/
|
||||
saveableProperties: string[];
|
||||
public saveableProperties: string[];
|
||||
|
||||
/**
|
||||
* name
|
||||
*/
|
||||
name: string;
|
||||
public name: string;
|
||||
|
||||
/**
|
||||
* primary id in the database
|
||||
*/
|
||||
dbDocUniqueId: string;
|
||||
public dbDocUniqueId: string;
|
||||
|
||||
/**
|
||||
* class constructor
|
||||
@ -89,8 +89,8 @@ export class SmartDataDbDoc<T> {
|
||||
}
|
||||
}
|
||||
|
||||
static async getInstances<T>(filterArg): Promise<T[]> {
|
||||
let self: any = this; // fool typesystem
|
||||
public static async getInstances<T>(filterArg): Promise<T[]> {
|
||||
const self: any = this; // fool typesystem
|
||||
let referenceMongoDBCollection: SmartdataCollection<T>;
|
||||
|
||||
if (self.smartdataCollection) {
|
||||
@ -100,9 +100,10 @@ export class SmartDataDbDoc<T> {
|
||||
}
|
||||
const foundDocs = await referenceMongoDBCollection.find(filterArg);
|
||||
const returnArray = [];
|
||||
for (let item of foundDocs) {
|
||||
let newInstance = new this();
|
||||
for (let key in item) {
|
||||
for (const item of foundDocs) {
|
||||
const newInstance = new this();
|
||||
newInstance.creationStatus = 'db';
|
||||
for (const key in item) {
|
||||
if (key !== 'id') {
|
||||
newInstance[key] = item[key];
|
||||
}
|
||||
@ -113,7 +114,7 @@ export class SmartDataDbDoc<T> {
|
||||
}
|
||||
|
||||
static async getInstance<T>(filterArg): Promise<T> {
|
||||
let result = await this.getInstances<T>(filterArg);
|
||||
const result = await this.getInstances<T>(filterArg);
|
||||
if (result && result.length > 0) {
|
||||
return result[0];
|
||||
}
|
||||
@ -123,15 +124,15 @@ export class SmartDataDbDoc<T> {
|
||||
* saves this instance but not any connected items
|
||||
* may lead to data inconsistencies, but is faster
|
||||
*/
|
||||
async save() {
|
||||
public async save() {
|
||||
// tslint:disable-next-line: no-this-assignment
|
||||
let self: any = this;
|
||||
const self: any = this;
|
||||
switch (this.creationStatus) {
|
||||
case 'db':
|
||||
await this.collection.update(self);
|
||||
break;
|
||||
case 'new':
|
||||
let writeResult = await this.collection.insert(self);
|
||||
const writeResult = await this.collection.insert(self);
|
||||
this.creationStatus = 'db';
|
||||
break;
|
||||
default:
|
||||
@ -142,20 +143,20 @@ export class SmartDataDbDoc<T> {
|
||||
/**
|
||||
* deletes a document from the database
|
||||
*/
|
||||
async delete() {}
|
||||
public async delete() {}
|
||||
|
||||
/**
|
||||
* also store any referenced objects to DB
|
||||
* better for data consistency
|
||||
*/
|
||||
saveDeep(savedMapArg: Objectmap<SmartDataDbDoc<any>> = null) {
|
||||
public saveDeep(savedMapArg: Objectmap<SmartDataDbDoc<any>> = null) {
|
||||
if (!savedMapArg) {
|
||||
savedMapArg = new Objectmap<SmartDataDbDoc<any>>();
|
||||
}
|
||||
savedMapArg.add(this);
|
||||
this.save();
|
||||
for (let propertyKey in this) {
|
||||
let property: any = this[propertyKey];
|
||||
for (const propertyKey of Object.keys(this)) {
|
||||
const property: any = this[propertyKey];
|
||||
if (property instanceof SmartDataDbDoc && !savedMapArg.checkForObject(property)) {
|
||||
property.saveDeep(savedMapArg);
|
||||
}
|
||||
|
@ -4,5 +4,6 @@ import * as lodash from 'lodash';
|
||||
import * as mongodb from 'mongodb';
|
||||
import * as smartq from '@pushrocks/smartpromise';
|
||||
import * as smartstring from '@pushrocks/smartstring';
|
||||
import * as smartunique from '@pushrocks/smartunique';
|
||||
|
||||
export { assert, smartlog, lodash, smartq, mongodb, smartstring };
|
||||
export { assert, smartlog, lodash, smartq, mongodb, smartstring, smartunique };
|
||||
|
Reference in New Issue
Block a user