2016-09-11 16:01:46 +00:00
|
|
|
import * as plugins from './smartdata.plugins'
|
2016-09-13 20:53:21 +00:00
|
|
|
import { Objectmap } from 'lik'
|
|
|
|
|
2018-01-07 16:26:34 +00:00
|
|
|
import { DbTable } from './smartdata.classes.dbcollection'
|
2017-06-23 09:40:20 +00:00
|
|
|
import { getObjectDoc } from './smartdata.classes.dbobjectdoc'
|
2016-09-11 16:01:46 +00:00
|
|
|
|
2018-01-07 16:26:34 +00:00
|
|
|
import { Connection as dbConnection, ConnectionOptions } from 'rethinkdb'
|
|
|
|
|
2016-11-17 11:20:52 +00:00
|
|
|
/**
|
|
|
|
* interface - indicates the connection status of the db
|
|
|
|
*/
|
2016-09-12 21:47:57 +00:00
|
|
|
export type TConnectionStatus = 'disconnected' | 'connected' | 'failed'
|
2016-09-11 16:01:46 +00:00
|
|
|
|
2016-09-12 21:47:57 +00:00
|
|
|
export class Db {
|
2018-01-07 16:26:34 +00:00
|
|
|
dbName: string
|
|
|
|
connectionOptions: plugins.rethinkDb.ConnectionOptions
|
|
|
|
dbConnection: plugins.rethinkDb.Connection
|
2017-02-25 10:37:05 +00:00
|
|
|
status: TConnectionStatus
|
2016-09-11 16:01:46 +00:00
|
|
|
|
2018-01-07 16:26:34 +00:00
|
|
|
constructor (connectionOptionsArg: ConnectionOptions) {
|
|
|
|
this.dbName = connectionOptionsArg.db
|
|
|
|
this.connectionOptions = connectionOptionsArg
|
2017-02-25 10:37:05 +00:00
|
|
|
}
|
2016-09-11 16:01:46 +00:00
|
|
|
|
2017-02-25 10:37:05 +00:00
|
|
|
// basic connection stuff ----------------------------------------------
|
2016-09-13 20:53:21 +00:00
|
|
|
|
2017-02-25 10:37:05 +00:00
|
|
|
/**
|
|
|
|
* connects to the database that was specified during instance creation
|
|
|
|
*/
|
2018-01-07 16:26:34 +00:00
|
|
|
async connect (): Promise<any> {
|
|
|
|
this.dbConnection = await plugins.rethinkDb.connect(this.connectionOptions)
|
|
|
|
plugins.beautylog.ok(`Connected to database ${this.dbName}`)
|
2017-02-25 10:37:05 +00:00
|
|
|
}
|
2016-09-11 16:01:46 +00:00
|
|
|
|
2017-02-25 10:37:05 +00:00
|
|
|
/**
|
|
|
|
* closes the connection to the databse
|
|
|
|
*/
|
2017-06-18 17:52:54 +00:00
|
|
|
close (): Promise<any> {
|
|
|
|
let done = plugins.smartq.defer()
|
2018-01-07 16:26:34 +00:00
|
|
|
this.dbConnection.close()
|
|
|
|
plugins.beautylog.ok(`disconnected to database ${this.dbName}`)
|
2017-02-25 10:37:05 +00:00
|
|
|
done.resolve()
|
|
|
|
return done.promise
|
|
|
|
}
|
2016-09-13 20:53:21 +00:00
|
|
|
|
2016-09-11 16:01:46 +00:00
|
|
|
}
|