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'
|
|
|
|
|
|
|
|
import { DbCollection } from './smartdata.classes.dbcollection'
|
2016-09-11 16:01:46 +00:00
|
|
|
|
2016-11-17 11:20:52 +00:00
|
|
|
/**
|
|
|
|
* interface - indicates the database type
|
|
|
|
*/
|
|
|
|
export type TDbType = 'mongodb' | 'nedb'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2016-11-17 11:20:52 +00:00
|
|
|
dbType: TDbType
|
2016-09-11 16:01:46 +00:00
|
|
|
dbUrl: string
|
2016-11-17 21:36:12 +00:00
|
|
|
db: plugins.mongodb.Db
|
2016-09-12 21:47:57 +00:00
|
|
|
status: TConnectionStatus
|
2016-09-13 20:53:21 +00:00
|
|
|
collections = new Objectmap<DbCollection<any>>()
|
2016-09-11 16:01:46 +00:00
|
|
|
|
2016-11-17 11:20:52 +00:00
|
|
|
constructor(dbUrlArg: string, dbTypeArg: TDbType = 'mongodb') {
|
|
|
|
this.dbType = dbTypeArg
|
|
|
|
this.dbUrl = dbUrlArg
|
2016-09-11 16:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 20:53:21 +00:00
|
|
|
// basic connection stuff ----------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* connects to the database that was specified during instance creation
|
|
|
|
*/
|
2016-09-11 16:01:46 +00:00
|
|
|
connect(): plugins.q.Promise<any> {
|
|
|
|
let done = plugins.q.defer()
|
2016-11-17 11:20:52 +00:00
|
|
|
if (this.dbType === 'mongodb') {
|
|
|
|
plugins.mongodb.MongoClient.connect(this.dbUrl, (err, db) => {
|
|
|
|
if (err) { console.log(err) }
|
|
|
|
plugins.assert.equal(null, err)
|
|
|
|
this.db = db
|
|
|
|
plugins.beautylog.success(`connected to database at ${this.dbUrl}`)
|
|
|
|
done.resolve(this.db)
|
|
|
|
})
|
|
|
|
} else if (this.dbType === 'nedb') {
|
2016-11-17 21:36:12 +00:00
|
|
|
this.db = null
|
2016-11-17 11:20:52 +00:00
|
|
|
}
|
2016-09-11 16:01:46 +00:00
|
|
|
return done.promise
|
|
|
|
}
|
|
|
|
|
2016-09-13 20:53:21 +00:00
|
|
|
/**
|
|
|
|
* closes the connection to the databse
|
|
|
|
*/
|
2016-09-11 16:01:46 +00:00
|
|
|
close(): plugins.q.Promise<any> {
|
|
|
|
let done = plugins.q.defer()
|
2016-11-17 21:36:12 +00:00
|
|
|
if (this.dbType === 'mongodb') {
|
|
|
|
this.db.close()
|
|
|
|
}
|
2016-09-11 16:01:46 +00:00
|
|
|
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`)
|
|
|
|
done.resolve()
|
|
|
|
return done.promise
|
|
|
|
}
|
2016-09-13 20:53:21 +00:00
|
|
|
|
|
|
|
// advanced communication with the database --------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets a collection by name: string
|
|
|
|
*/
|
|
|
|
getCollectionByName<T>(nameArg: string): plugins.q.Promise<DbCollection<T>> {
|
|
|
|
let done = plugins.q.defer<DbCollection<any>>()
|
|
|
|
let resultCollection = this.collections.find((dbCollectionArg) => {
|
|
|
|
return dbCollectionArg.name === nameArg
|
|
|
|
})
|
|
|
|
if (resultCollection !== null) {
|
|
|
|
done.resolve(resultCollection)
|
|
|
|
}
|
|
|
|
return done.promise
|
|
|
|
};
|
2016-09-13 23:02:11 +00:00
|
|
|
|
|
|
|
addCollection(dbCollectionArg: DbCollection<any>) {
|
|
|
|
this.collections.add(dbCollectionArg)
|
|
|
|
}
|
2016-11-17 21:36:12 +00:00
|
|
|
|
2016-09-11 16:01:46 +00:00
|
|
|
}
|