feat(add RethinkDB as main driver and revert to docs in README):
This commit is contained in:
@ -2,7 +2,6 @@ import * as plugins from './smartdata.plugins'
|
||||
import { Objectmap } from 'lik'
|
||||
|
||||
import { DbTable } from './smartdata.classes.dbcollection'
|
||||
import { getObjectDoc } from './smartdata.classes.dbobjectdoc'
|
||||
|
||||
import { Connection as dbConnection, ConnectionOptions } from 'rethinkdb'
|
||||
|
||||
@ -24,6 +23,21 @@ export class Db {
|
||||
this.status = 'initial'
|
||||
}
|
||||
|
||||
/**
|
||||
* supply additional SSl options
|
||||
*/
|
||||
setSsl (certificateStringArg: string, formatArg: 'base64' | 'clearText') {
|
||||
let certificateString: string
|
||||
if(formatArg = 'base64') {
|
||||
certificateString = plugins.smartstring.base64.decode(certificateStringArg)
|
||||
} else {
|
||||
certificateString = certificateStringArg
|
||||
}
|
||||
this.connectionOptions['ssl'] = {
|
||||
ca: Buffer.from(certificateString)
|
||||
}
|
||||
}
|
||||
|
||||
// basic connection stuff ----------------------------------------------
|
||||
|
||||
/**
|
||||
@ -31,6 +45,7 @@ export class Db {
|
||||
*/
|
||||
async connect (): Promise<any> {
|
||||
this.dbConnection = await plugins.rethinkDb.connect(this.connectionOptions)
|
||||
this.dbConnection.use(this.dbName)
|
||||
this.status = 'connected'
|
||||
plugins.beautylog.ok(`Connected to database ${this.dbName}`)
|
||||
}
|
||||
@ -41,7 +56,7 @@ export class Db {
|
||||
async close (): Promise<any> {
|
||||
await this.dbConnection.close()
|
||||
this.status = 'disconnected'
|
||||
plugins.beautylog.ok(`disconnected to database ${this.dbName}`)
|
||||
plugins.beautylog.ok(`disconnected from database ${this.dbName}`)
|
||||
}
|
||||
|
||||
// handle table to class distribution
|
||||
@ -53,7 +68,7 @@ export class Db {
|
||||
*/
|
||||
async getDbTableByName<T>(nameArg: string): Promise<DbTable<T>> {
|
||||
let resultCollection = this.dbTablesMap.find((dbCollectionArg) => {
|
||||
return dbCollectionArg.name === nameArg
|
||||
return dbCollectionArg.tableName === nameArg
|
||||
})
|
||||
return resultCollection
|
||||
}
|
||||
|
@ -2,6 +2,9 @@ import * as plugins from './smartdata.plugins'
|
||||
import { Db } from './smartdata.classes.db'
|
||||
import { DbDoc } from './smartdata.classes.dbdoc'
|
||||
|
||||
// RethinkDb Interfaces
|
||||
import { WriteResult, Cursor } from 'rethinkdb'
|
||||
|
||||
export interface IFindOptions {
|
||||
limit?: number
|
||||
}
|
||||
@ -23,21 +26,35 @@ export class DbTable<T> {
|
||||
*/
|
||||
table: plugins.rethinkDb.Table
|
||||
objectValidation: IDocValidation<T> = null
|
||||
name: string
|
||||
tableName: string
|
||||
db: Db
|
||||
|
||||
constructor (collectedClassArg: T & DbDoc<T>, dbArg: Db) {
|
||||
// tell the collection where it belongs
|
||||
this.name = collectedClassArg.name
|
||||
this.tableName = 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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a validation function that all newly inserted and updated objects have to pass
|
||||
*/
|
||||
@ -48,35 +65,39 @@ export class DbTable<T> {
|
||||
/**
|
||||
* finds an object in the DbCollection
|
||||
*/
|
||||
async find (docMatchArg: T | any, optionsArg?: IFindOptions): Promise<T[]> {
|
||||
|
||||
async find (): Promise<Cursor> {
|
||||
await this.init()
|
||||
return await plugins.rethinkDb.table(this.tableName).filter({
|
||||
/* TODO: */
|
||||
}).run(this.db.dbConnection)
|
||||
}
|
||||
|
||||
/**
|
||||
* create an object in the database
|
||||
*/
|
||||
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)
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts object into the DbCollection
|
||||
*/
|
||||
async insertOne (docArg: T): Promise<void> {
|
||||
await this.checkDoc(docArg)
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts many objects at once into the DbCollection
|
||||
*/
|
||||
insertMany (docArrayArg: T[]): Promise<void> {
|
||||
let done = plugins.smartq.defer<void>()
|
||||
let checkDocPromiseArray: Promise<void>[] = []
|
||||
for (let docArg of docArrayArg) {
|
||||
checkDocPromiseArray.push(this.checkDoc(docArg))
|
||||
}
|
||||
Promise.all(checkDocPromiseArray).then(() => {
|
||||
this.table.insertMany(docArrayArg)
|
||||
.then(() => { done.resolve() })
|
||||
})
|
||||
return done.promise
|
||||
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)
|
||||
}
|
||||
|
||||
/**
|
||||
* checks a Doc for constraints
|
||||
* if this.objectValidation is not set it passes.
|
||||
*/
|
||||
private checkDoc (docArg: T): Promise<void> {
|
||||
let done = plugins.smartq.defer<void>()
|
||||
@ -91,4 +112,8 @@ export class DbTable<T> {
|
||||
}
|
||||
return done.promise
|
||||
}
|
||||
|
||||
extractKey (writeResult: WriteResult) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ export class DbDoc<T> {
|
||||
/**
|
||||
* how the Doc in memory was created, may prove useful later.
|
||||
*/
|
||||
creationType: TDocCreation
|
||||
creationStatus: TDocCreation = 'new'
|
||||
|
||||
/**
|
||||
* an array of saveable properties of a doc
|
||||
@ -40,6 +40,11 @@ export class DbDoc<T> {
|
||||
*/
|
||||
name: string
|
||||
|
||||
/**
|
||||
* primary id in the database
|
||||
*/
|
||||
dbId: string
|
||||
|
||||
/**
|
||||
* class constructor
|
||||
*/
|
||||
@ -52,17 +57,18 @@ export class DbDoc<T> {
|
||||
* saves this instance but not any connected items
|
||||
* may lead to data inconsistencies, but is faster
|
||||
*/
|
||||
save () {
|
||||
let saveableObject: any = {} // is not exposed to outside, so any is ok here
|
||||
for (let propertyNameString of this.saveableProperties) {
|
||||
saveableObject[ propertyNameString ] = this[ propertyNameString ]
|
||||
}
|
||||
switch (this.creationType) {
|
||||
async save () {
|
||||
let self: any = this
|
||||
switch (this.creationStatus) {
|
||||
case 'db':
|
||||
this.collection // TODO implement collection.update()
|
||||
await this.collection.update(self)
|
||||
break
|
||||
case 'new':
|
||||
this.collection.insertOne(saveableObject)
|
||||
let writeResult = await this.collection.insert(self)
|
||||
this.creationStatus = 'db'
|
||||
break;
|
||||
default:
|
||||
console.error('neither new nor in db?')
|
||||
}
|
||||
}
|
||||
|
||||
@ -83,4 +89,12 @@ export class DbDoc<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
createSavableObject () {
|
||||
let saveableObject: any = {} // is not exposed to outside, so any is ok here
|
||||
for (let propertyNameString of this.saveableProperties) {
|
||||
saveableObject[ propertyNameString ] = this[ propertyNameString ]
|
||||
}
|
||||
return saveableObject
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,15 @@
|
||||
import 'typings-global'
|
||||
import * as assert from 'assert'
|
||||
import * as beautylog from 'beautylog'
|
||||
import * as lodash from 'lodash'
|
||||
import * as rethinkDb from 'rethinkdb'
|
||||
import * as smartq from 'smartq'
|
||||
import * as smartstring from 'smartstring'
|
||||
|
||||
export {
|
||||
assert,
|
||||
beautylog,
|
||||
lodash,
|
||||
smartq,
|
||||
rethinkDb
|
||||
rethinkDb,
|
||||
smartstring
|
||||
}
|
||||
|
Reference in New Issue
Block a user