add NeDB support
This commit is contained in:
@ -16,7 +16,7 @@ export type TConnectionStatus = 'disconnected' | 'connected' | 'failed'
|
||||
export class Db {
|
||||
dbType: TDbType
|
||||
dbUrl: string
|
||||
db: plugins.mongodb.Db | plugins.nedb
|
||||
db: plugins.mongodb.Db
|
||||
status: TConnectionStatus
|
||||
collections = new Objectmap<DbCollection<any>>()
|
||||
|
||||
@ -41,7 +41,7 @@ export class Db {
|
||||
done.resolve(this.db)
|
||||
})
|
||||
} else if (this.dbType === 'nedb') {
|
||||
this.db = new plugins.nedb()
|
||||
this.db = null
|
||||
}
|
||||
return done.promise
|
||||
}
|
||||
@ -51,7 +51,9 @@ export class Db {
|
||||
*/
|
||||
close(): plugins.q.Promise<any> {
|
||||
let done = plugins.q.defer()
|
||||
this.db.close()
|
||||
if (this.dbType === 'mongodb') {
|
||||
this.db.close()
|
||||
}
|
||||
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`)
|
||||
done.resolve()
|
||||
return done.promise
|
||||
@ -76,4 +78,5 @@ export class Db {
|
||||
addCollection(dbCollectionArg: DbCollection<any>) {
|
||||
this.collections.add(dbCollectionArg)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,53 +5,96 @@ export interface IFindOptions {
|
||||
limit?: number
|
||||
}
|
||||
|
||||
export interface IDocValidation<T> {
|
||||
(doc: T): boolean
|
||||
}
|
||||
|
||||
export function Collection(db: Db) {
|
||||
return function(constructor){
|
||||
return function (constructor) {
|
||||
constructor['dbCollection'] = new DbCollection(constructor.name, db)
|
||||
}
|
||||
}
|
||||
|
||||
export class DbCollection<T> {
|
||||
/**
|
||||
* the collection that is used
|
||||
* the collection that is used, defaults to mongodb collection,
|
||||
* can be nedb datastore (sub api of mongodb)
|
||||
*/
|
||||
collection: plugins.mongodb.Collection
|
||||
name: string
|
||||
db: Db
|
||||
objectValidation: IDocValidation<T> = null
|
||||
|
||||
|
||||
constructor(nameArg: string, dbArg: Db) {
|
||||
this.name = nameArg
|
||||
this.collection = dbArg.db.collection(nameArg)
|
||||
this.db = dbArg
|
||||
if (this.db.dbType === 'mongodb') {
|
||||
this.collection = dbArg.db.collection(nameArg)
|
||||
} else {
|
||||
this.collection = new plugins.nedb()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a validation function that all newly inserted and updated objects have to pass
|
||||
*/
|
||||
addObjectValidation(funcArg) { }
|
||||
addDocValidation(funcArg: IDocValidation<T>) {
|
||||
this.objectValidation = funcArg
|
||||
}
|
||||
|
||||
/**
|
||||
* finds an object in the DbCollection
|
||||
*/
|
||||
find(docMatchArg: T | any, optionsArg?: IFindOptions): plugins.q.Promise<T[]> {
|
||||
let done = plugins.q.defer<T[]>()
|
||||
let findCursor = this.collection.find(docMatchArg)
|
||||
if (optionsArg) {
|
||||
if ( optionsArg.limit ) { findCursor = findCursor.limit(1) }
|
||||
if (this.db.dbType === 'mongodb') {
|
||||
let findCursor = this.collection.find(docMatchArg)
|
||||
if (optionsArg) {
|
||||
if (optionsArg.limit) { findCursor = findCursor.limit(1) }
|
||||
}
|
||||
findCursor.toArray((err, docs) => {
|
||||
if (err) {
|
||||
done.reject(err)
|
||||
throw err
|
||||
}
|
||||
done.resolve(docs)
|
||||
})
|
||||
} else if (this.db.dbType === 'nedb') {
|
||||
this.collection.find(docMatchArg, (err, docs) => {
|
||||
if (err) {
|
||||
done.reject(err)
|
||||
throw err
|
||||
}
|
||||
done.resolve(docs)
|
||||
})
|
||||
}
|
||||
findCursor.toArray((err, docs) => {
|
||||
if (err) { throw err }
|
||||
done.resolve(docs)
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts object into the DbCollection
|
||||
* inserts object into the DbCollection
|
||||
*/
|
||||
insertOne(docArg: T): plugins.q.Promise<void> {
|
||||
let done = plugins.q.defer<void>()
|
||||
this.checkDoc(docArg).then(() => {
|
||||
this.collection.insertOne(docArg)
|
||||
.then(() => { done.resolve() })
|
||||
})
|
||||
this.checkDoc(docArg).then(
|
||||
() => {
|
||||
if (this.db.dbType === 'mongodb') {
|
||||
this.collection.insertOne(docArg)
|
||||
.then(() => { done.resolve() })
|
||||
} else if (this.db.dbType === 'nedb') {
|
||||
this.collection.insert(docArg, (err, newDoc) => {
|
||||
if (err) {
|
||||
done.reject(err)
|
||||
throw err
|
||||
}
|
||||
done.resolve()
|
||||
})
|
||||
}
|
||||
},
|
||||
() => {
|
||||
done.reject(new Error('one the docs did not pass validation'))
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
@ -61,19 +104,41 @@ export class DbCollection<T> {
|
||||
insertMany(docArrayArg: T[]): plugins.q.Promise<void> {
|
||||
let done = plugins.q.defer<void>()
|
||||
let checkDocPromiseArray: plugins.q.Promise<void>[] = []
|
||||
for (let docArg of docArrayArg){
|
||||
for (let docArg of docArrayArg) {
|
||||
checkDocPromiseArray.push(this.checkDoc(docArg))
|
||||
}
|
||||
plugins.q.all(checkDocPromiseArray).then(() => {
|
||||
this.collection.insertMany(docArrayArg)
|
||||
.then(() => { done.resolve() })
|
||||
if (this.db.dbType === 'mongodb') {
|
||||
this.collection.insertMany(docArrayArg)
|
||||
.then(() => { done.resolve() })
|
||||
} else if (this.db.dbType === 'nedb') {
|
||||
let paramArray = plugins.lodash.concat<any>(docArrayArg, (err, newDoc) => {
|
||||
if (err) {
|
||||
done.reject(err)
|
||||
throw err
|
||||
}
|
||||
done.resolve()
|
||||
})
|
||||
this.collection.insert.apply(null, paramArray)
|
||||
}
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
private checkDoc(doc: T): plugins.q.Promise<void> {
|
||||
/**
|
||||
* checks a Doc for constraints
|
||||
*/
|
||||
private checkDoc(docArg: T): plugins.q.Promise<void> {
|
||||
let done = plugins.q.defer<void>()
|
||||
done.resolve()
|
||||
let validationResult = true
|
||||
if (this.objectValidation) {
|
||||
validationResult = this.objectValidation(docArg)
|
||||
}
|
||||
if (validationResult) {
|
||||
done.resolve()
|
||||
} else {
|
||||
done.reject('validation of object did not pass')
|
||||
}
|
||||
return done.promise
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,16 @@
|
||||
import 'typings-global'
|
||||
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')
|
||||
import * as assert from 'assert'
|
||||
import * as beautylog from 'beautylog'
|
||||
import * as lodash from 'lodash'
|
||||
import * as mongodb from 'mongodb'
|
||||
import * as q from 'q'
|
||||
let nedb = require('nedb')
|
||||
|
||||
export {
|
||||
assert,
|
||||
beautylog,
|
||||
lodash,
|
||||
mongodb,
|
||||
q,
|
||||
nedb
|
||||
}
|
||||
|
Reference in New Issue
Block a user