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'
|
2017-06-23 09:40:20 +00:00
|
|
|
import { getObjectDoc } from './smartdata.classes.dbobjectdoc'
|
2016-09-11 16:01:46 +00:00
|
|
|
|
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 {
|
2017-02-25 10:37:05 +00:00
|
|
|
dbUrl: string
|
|
|
|
db: plugins.mongodb.Db
|
|
|
|
status: TConnectionStatus
|
2017-06-23 09:40:20 +00:00
|
|
|
classCollections = new Objectmap<DbCollection<any>>()
|
|
|
|
objectCollections = new Objectmap<DbCollection<any>>()
|
2016-09-11 16:01:46 +00:00
|
|
|
|
2017-06-18 17:52:54 +00:00
|
|
|
constructor (dbUrlArg: string) {
|
2017-02-25 10:37:05 +00:00
|
|
|
this.dbUrl = dbUrlArg
|
|
|
|
}
|
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
|
|
|
|
*/
|
2017-06-18 17:52:54 +00:00
|
|
|
connect (): Promise<any> {
|
|
|
|
let done = plugins.smartq.defer()
|
|
|
|
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)
|
|
|
|
})
|
2017-02-25 10:37:05 +00:00
|
|
|
return done.promise
|
|
|
|
}
|
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()
|
|
|
|
this.db.close()
|
2017-02-25 10:37:05 +00:00
|
|
|
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`)
|
|
|
|
done.resolve()
|
|
|
|
return done.promise
|
|
|
|
}
|
2016-09-13 20:53:21 +00:00
|
|
|
|
2017-02-25 10:37:05 +00:00
|
|
|
// advanced communication with the database --------------------------------
|
2016-09-13 23:02:11 +00:00
|
|
|
|
2017-02-25 10:37:05 +00:00
|
|
|
/**
|
2017-06-23 09:40:20 +00:00
|
|
|
* gets a class based collection by name: string
|
2017-02-25 10:37:05 +00:00
|
|
|
*/
|
2017-06-23 09:40:20 +00:00
|
|
|
async getClassCollectionByName<T> (nameArg: string): Promise<DbCollection<T>> {
|
|
|
|
let resultCollection = this.classCollections.find((dbCollectionArg) => {
|
2017-02-25 10:37:05 +00:00
|
|
|
return dbCollectionArg.name === nameArg
|
|
|
|
})
|
2017-06-23 09:40:20 +00:00
|
|
|
return resultCollection
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets an object collection by name
|
|
|
|
*/
|
|
|
|
async getObjectCollectionByName<T> (nameArg: string, dbArg: Db , makeNewArg: boolean = false): Promise<DbCollection<T>> {
|
|
|
|
let resultCollection = this.objectCollections.find((dbCollectionArg) => {
|
|
|
|
return dbCollectionArg.name === nameArg
|
|
|
|
})
|
|
|
|
if (!resultCollection && makeNewArg) {
|
|
|
|
resultCollection = getObjectDoc(nameArg, this).collection
|
|
|
|
return resultCollection
|
|
|
|
} else {
|
|
|
|
return resultCollection
|
2016-09-13 23:02:11 +00:00
|
|
|
}
|
2017-06-18 17:52:54 +00:00
|
|
|
}
|
2017-02-25 10:37:05 +00:00
|
|
|
|
2017-06-18 17:52:54 +00:00
|
|
|
addCollection (dbCollectionArg: DbCollection<any>) {
|
2017-06-23 09:40:20 +00:00
|
|
|
this.classCollections.add(dbCollectionArg)
|
2017-02-25 10:37:05 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 16:01:46 +00:00
|
|
|
}
|