added nedb to the mix

This commit is contained in:
2016-11-17 12:20:52 +01:00
parent 3a24f829b4
commit af8c41ad6c
16 changed files with 105 additions and 58 deletions

View File

@ -3,16 +3,26 @@ import { Objectmap } from 'lik'
import { DbCollection } from './smartdata.classes.dbcollection'
/**
* interface - indicates the database type
*/
export type TDbType = 'mongodb' | 'nedb'
/**
* interface - indicates the connection status of the db
*/
export type TConnectionStatus = 'disconnected' | 'connected' | 'failed'
export class Db {
dbType: TDbType
dbUrl: string
db: plugins.mongodb.Db
db: plugins.mongodb.Db | plugins.nedb
status: TConnectionStatus
collections = new Objectmap<DbCollection<any>>()
constructor(dbUrl: string) {
this.dbUrl = dbUrl
constructor(dbUrlArg: string, dbTypeArg: TDbType = 'mongodb') {
this.dbType = dbTypeArg
this.dbUrl = dbUrlArg
}
// basic connection stuff ----------------------------------------------
@ -22,13 +32,17 @@ export class Db {
*/
connect(): plugins.q.Promise<any> {
let done = plugins.q.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)
})
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') {
this.db = new plugins.nedb()
}
return done.promise
}

View File

@ -12,6 +12,9 @@ export function Collection(db: Db) {
}
export class DbCollection<T> {
/**
* the collection that is used
*/
collection: plugins.mongodb.Collection
name: string
constructor(nameArg: string, dbArg: Db) {

View File

@ -13,7 +13,7 @@ export class DbDoc<T> {
this.collection = this.constructor['dbCollection']
}
save() {
}
saveDeep() {

View File

@ -3,3 +3,4 @@ export import assert = require('assert')
export import beautylog = require('beautylog')
export import mongodb = require('mongodb')
export import q = require('q')
export import nedb = require('nedb')