smartdata/ts/smartdata.classes.collection.ts

164 lines
4.9 KiB
TypeScript
Raw Permalink Normal View History

import * as plugins from './smartdata.plugins';
import { SmartdataDb } from './smartdata.classes.db';
2018-07-09 22:02:04 +00:00
import { SmartDataDbDoc } from './smartdata.classes.doc';
export interface IFindOptions {
limit?: number;
}
/**
*
*/
export interface IDocValidationFunc<T> {
(doc: T): boolean;
}
2019-01-08 13:37:17 +00:00
export type TDelayedDbCreation = () => SmartdataDb;
/**
* This is a decorator that will tell the decorated class what dbTable to use
2019-01-08 13:37:17 +00:00
* @param dbArg
*/
2019-01-08 13:37:17 +00:00
export function Collection(dbArg: SmartdataDb | TDelayedDbCreation) {
2020-08-18 12:01:46 +00:00
return function (constructor) {
2019-01-08 13:37:17 +00:00
if (dbArg instanceof SmartdataDb) {
// tslint:disable-next-line: no-string-literal
constructor['smartdataCollection'] = new SmartdataCollection(constructor, dbArg);
} else {
2019-01-08 17:22:48 +00:00
constructor['smartdataDelayedCollection'] = () => {
2019-02-13 21:08:58 +00:00
return new SmartdataCollection(constructor, dbArg());
2019-01-08 13:37:17 +00:00
};
}
};
}
export class SmartdataCollection<T> {
/**
* the collection that is used
*/
2019-09-02 14:42:29 +00:00
public mongoDbCollection: plugins.mongodb.Collection;
public objectValidation: IDocValidationFunc<T> = null;
public collectionName: string;
public smartdataDb: SmartdataDb;
public uniqueIndexes: string[] = [];
2020-02-19 18:30:34 +00:00
constructor(collectedClassArg: T & SmartDataDbDoc<T, unknown>, smartDataDbArg: SmartdataDb) {
// tell the collection where it belongs
this.collectionName = collectedClassArg.name;
this.smartdataDb = smartDataDbArg;
// tell the db class about it (important since Db uses different systems under the hood)
this.smartdataDb.addTable(this);
}
/**
* makes sure a collection exists within MongoDb that maps to the SmartdataCollection
*/
2019-09-02 14:42:29 +00:00
public async init() {
if (!this.mongoDbCollection) {
// connect this instance to a MongoDB collection
const availableMongoDbCollections = await this.smartdataDb.mongoDb.collections();
2020-08-18 12:01:46 +00:00
const wantedCollection = availableMongoDbCollections.find((collection) => {
return collection.collectionName === this.collectionName;
});
if (!wantedCollection) {
await this.smartdataDb.mongoDb.createCollection(this.collectionName);
2020-09-09 03:51:21 +00:00
console.log(`Successfully initiated Collection ${this.collectionName}`);
}
2020-09-09 03:51:21 +00:00
this.mongoDbCollection = this.smartdataDb.mongoDb.collection(this.collectionName);
}
}
/**
* mark unique index
*/
2019-09-02 14:42:29 +00:00
public markUniqueIndexes(keyArrayArg: string[] = []) {
for (const key of keyArrayArg) {
2019-01-07 01:41:38 +00:00
if (!this.uniqueIndexes.includes(key)) {
this.mongoDbCollection.createIndex(key, {
2020-08-18 12:01:46 +00:00
unique: true,
});
// make sure we only call this once and not for every doc we create
this.uniqueIndexes.push(key);
}
}
}
/**
* adds a validation function that all newly inserted and updated objects have to pass
*/
2019-09-02 14:42:29 +00:00
public addDocValidation(funcArg: IDocValidationFunc<T>) {
this.objectValidation = funcArg;
}
/**
* finds an object in the DbCollection
*/
2019-09-02 14:42:29 +00:00
public async find(filterObject: any): Promise<any> {
await this.init();
2018-07-09 22:02:04 +00:00
const result = await this.mongoDbCollection.find(filterObject).toArray();
return result;
}
/**
* create an object in the database
*/
2020-02-19 18:30:34 +00:00
public async insert(dbDocArg: T & SmartDataDbDoc<T, unknown>): Promise<any> {
await this.init();
await this.checkDoc(dbDocArg);
this.markUniqueIndexes(dbDocArg.uniqueIndexes);
const saveableObject = await dbDocArg.createSavableObject();
const result = await this.mongoDbCollection.insertOne(saveableObject);
return result;
}
/**
* inserts object into the DbCollection
*/
2020-02-19 18:30:34 +00:00
public async update(dbDocArg: T & SmartDataDbDoc<T, unknown>): Promise<any> {
await this.init();
await this.checkDoc(dbDocArg);
2019-01-08 17:45:30 +00:00
const identifiableObject = await dbDocArg.createIdentifiableObject();
const saveableObject = await dbDocArg.createSavableObject();
2019-09-02 14:42:29 +00:00
const updateableObject: any = {};
for (const key of Object.keys(saveableObject)) {
if (identifiableObject[key]) {
continue;
}
updateableObject[key] = saveableObject[key];
}
2019-09-02 14:51:22 +00:00
this.mongoDbCollection.updateOne(
identifiableObject,
{ $set: updateableObject },
{ upsert: true }
);
2019-01-08 17:45:30 +00:00
}
2020-02-19 18:30:34 +00:00
public async delete(dbDocArg: T & SmartDataDbDoc<T, unknown>): Promise<any> {
2019-01-08 17:45:30 +00:00
await this.init();
await this.checkDoc(dbDocArg);
const identifiableObject = await dbDocArg.createIdentifiableObject();
2019-09-11 10:36:02 +00:00
await this.mongoDbCollection.deleteOne(identifiableObject, {
2020-08-18 12:01:46 +00:00
w: 1,
2019-09-11 10:36:02 +00:00
});
}
/**
* checks a Doc for constraints
* if this.objectValidation is not set it passes.
*/
private checkDoc(docArg: T): Promise<void> {
2019-01-08 17:45:30 +00:00
const done = plugins.smartq.defer<void>();
let validationResult = true;
if (this.objectValidation) {
validationResult = this.objectValidation(docArg);
}
if (validationResult) {
done.resolve();
} else {
done.reject('validation of object did not pass');
}
return done.promise;
}
}