smartdata/ts/smartdata.classes.dbcollection.ts

120 lines
3.1 KiB
TypeScript
Raw Normal View History

2016-09-11 16:01:46 +00:00
import * as plugins from './smartdata.plugins'
2016-09-12 21:47:57 +00:00
import { Db } from './smartdata.classes.db'
2017-06-23 14:42:03 +00:00
import { DbDoc } from './smartdata.classes.dbdoc'
2016-09-11 16:01:46 +00:00
// RethinkDb Interfaces
import { WriteResult, Cursor } from 'rethinkdb'
2016-09-13 20:53:21 +00:00
export interface IFindOptions {
2017-06-18 17:52:54 +00:00
limit?: number
2016-09-13 20:53:21 +00:00
}
2016-11-17 21:36:12 +00:00
export interface IDocValidation<T> {
2017-06-18 17:52:54 +00:00
(doc: T): boolean
2016-11-17 21:36:12 +00:00
}
2017-06-18 17:52:54 +00:00
export function Collection (db: Db) {
return function (constructor) {
2018-01-07 16:26:34 +00:00
constructor[ 'dbCollection' ] = new DbTable(constructor, db)
2017-06-18 17:52:54 +00:00
}
}
2018-01-07 16:26:34 +00:00
export class DbTable<T> {
2017-06-18 17:52:54 +00:00
/**
* the collection that is used, defaults to mongodb collection,
* can be nedb datastore (sub api of mongodb)
*/
2018-01-07 16:26:34 +00:00
table: plugins.rethinkDb.Table
2017-06-18 17:52:54 +00:00
objectValidation: IDocValidation<T> = null
tableName: string
2017-06-18 17:52:54 +00:00
db: Db
2016-11-17 21:36:12 +00:00
2017-06-18 17:52:54 +00:00
constructor (collectedClassArg: T & DbDoc<T>, dbArg: Db) {
// tell the collection where it belongs
this.tableName = collectedClassArg.name
2017-06-18 17:52:54 +00:00
this.db = dbArg
2016-11-17 21:36:12 +00:00
2017-06-18 17:52:54 +00:00
// tell the db class about it (important since Db uses different systems under the hood)
2018-01-07 16:58:30 +00:00
this.db.addTable(this)
2017-06-18 17:52:54 +00:00
}
2017-02-25 10:37:05 +00:00
async init() {
if(!this.table) {
// connect this instance to a RethinkDB table
const availableTables = await plugins.rethinkDb
.db(this.db.dbName)
.tableList()
.run(this.db.dbConnection)
if(availableTables.indexOf(this.tableName)) {
await plugins.rethinkDb
.db(this.db.dbName)
.tableCreate(this.tableName)
.run(this.db.dbConnection)
}
}
this.table = plugins.rethinkDb.table(this.tableName)
}
2017-06-18 17:52:54 +00:00
/**
* adds a validation function that all newly inserted and updated objects have to pass
*/
addDocValidation (funcArg: IDocValidation<T>) {
this.objectValidation = funcArg
}
2016-09-11 16:01:46 +00:00
2017-06-18 17:52:54 +00:00
/**
* finds an object in the DbCollection
*/
async find (): Promise<Cursor> {
await this.init()
return await plugins.rethinkDb.table(this.tableName).filter({
/* TODO: */
}).run(this.db.dbConnection)
2017-06-18 17:52:54 +00:00
}
2016-09-11 16:01:46 +00:00
2017-06-18 17:52:54 +00:00
/**
* create an object in the database
2017-06-18 17:52:54 +00:00
*/
async insert (dbDocArg: T & DbDoc<T>): Promise<WriteResult> {
await this.init()
await this.checkDoc(dbDocArg)
return await plugins.rethinkDb.table(this.tableName).insert(
dbDocArg.createSavableObject()
).run(this.db.dbConnection)
2017-06-18 17:52:54 +00:00
}
2016-09-12 15:31:23 +00:00
2017-06-18 17:52:54 +00:00
/**
* inserts object into the DbCollection
2017-06-18 17:52:54 +00:00
*/
async update (dbDocArg: T & DbDoc<T>): Promise<WriteResult> {
await this.init()
await this.checkDoc(dbDocArg)
console.log(this.tableName, dbDocArg.createSavableObject())
return await plugins.rethinkDb.table(this.tableName).update(
dbDocArg.createSavableObject()
).run(this.db.dbConnection)
2017-06-18 17:52:54 +00:00
}
2016-09-11 16:01:46 +00:00
2017-06-18 17:52:54 +00:00
/**
* checks a Doc for constraints
* if this.objectValidation is not set it passes.
2017-06-18 17:52:54 +00:00
*/
private checkDoc (docArg: T): Promise<void> {
let done = plugins.smartq.defer<void>()
let validationResult = true
if (this.objectValidation) {
validationResult = this.objectValidation(docArg)
2016-09-12 16:14:01 +00:00
}
2017-06-18 17:52:54 +00:00
if (validationResult) {
done.resolve()
} else {
done.reject('validation of object did not pass')
2016-09-12 15:31:23 +00:00
}
2017-06-18 17:52:54 +00:00
return done.promise
}
extractKey (writeResult: WriteResult) {
}
2016-09-11 16:01:46 +00:00
}