import * as plugins from './smartdata.plugins' import { Db } from './smartdata.classes.db' import { DbDoc } from './smartdata.classes.dbdoc' export interface IFindOptions { limit?: number } export interface IDocValidation { (doc: T): boolean } export function Collection (db: Db) { return function (constructor) { constructor[ 'dbCollection' ] = new DbTable(constructor, db) } } export class DbTable { /** * the collection that is used, defaults to mongodb collection, * can be nedb datastore (sub api of mongodb) */ table: plugins.rethinkDb.Table objectValidation: IDocValidation = null name: string db: Db constructor (collectedClassArg: T & DbDoc, dbArg: Db) { // tell the collection where it belongs this.name = collectedClassArg.name this.db = dbArg // connect this instance to a RethinkDB table this.table = plugins.rethinkDb.db(this.db.dbName).table(this.name) // tell the db class about it (important since Db uses different systems under the hood) this.db.addTable(this) } /** * adds a validation function that all newly inserted and updated objects have to pass */ addDocValidation (funcArg: IDocValidation) { this.objectValidation = funcArg } /** * finds an object in the DbCollection */ async find (docMatchArg: T | any, optionsArg?: IFindOptions): Promise { } /** * inserts object into the DbCollection */ async insertOne (docArg: T): Promise { await this.checkDoc(docArg) } /** * inserts many objects at once into the DbCollection */ insertMany (docArrayArg: T[]): Promise { let done = plugins.smartq.defer() let checkDocPromiseArray: Promise[] = [] for (let docArg of docArrayArg) { checkDocPromiseArray.push(this.checkDoc(docArg)) } Promise.all(checkDocPromiseArray).then(() => { this.table.insertMany(docArrayArg) .then(() => { done.resolve() }) }) return done.promise } /** * checks a Doc for constraints */ private checkDoc (docArg: T): Promise { let done = plugins.smartq.defer() 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 } }