added npmdocker for local development
This commit is contained in:
@ -14,69 +14,69 @@ export type TDbType = 'mongodb' | 'nedb'
|
||||
export type TConnectionStatus = 'disconnected' | 'connected' | 'failed'
|
||||
|
||||
export class Db {
|
||||
dbType: TDbType
|
||||
dbUrl: string
|
||||
db: plugins.mongodb.Db
|
||||
status: TConnectionStatus
|
||||
collections = new Objectmap<DbCollection<any>>()
|
||||
dbType: TDbType
|
||||
dbUrl: string
|
||||
db: plugins.mongodb.Db
|
||||
status: TConnectionStatus
|
||||
collections = new Objectmap<DbCollection<any>>()
|
||||
|
||||
constructor(dbUrlArg: string, dbTypeArg: TDbType = 'mongodb') {
|
||||
this.dbType = dbTypeArg
|
||||
this.dbUrl = dbUrlArg
|
||||
constructor(dbUrlArg: string, dbTypeArg: TDbType = 'mongodb') {
|
||||
this.dbType = dbTypeArg
|
||||
this.dbUrl = dbUrlArg
|
||||
}
|
||||
|
||||
// basic connection stuff ----------------------------------------------
|
||||
|
||||
/**
|
||||
* connects to the database that was specified during instance creation
|
||||
*/
|
||||
connect(): plugins.q.Promise<any> {
|
||||
let done = plugins.q.defer()
|
||||
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 = null
|
||||
}
|
||||
return done.promise
|
||||
}
|
||||
|
||||
// basic connection stuff ----------------------------------------------
|
||||
|
||||
/**
|
||||
* connects to the database that was specified during instance creation
|
||||
*/
|
||||
connect(): plugins.q.Promise<any> {
|
||||
let done = plugins.q.defer()
|
||||
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 = null
|
||||
}
|
||||
return done.promise
|
||||
/**
|
||||
* closes the connection to the databse
|
||||
*/
|
||||
close(): plugins.q.Promise<any> {
|
||||
let done = plugins.q.defer()
|
||||
if (this.dbType === 'mongodb') {
|
||||
this.db.close()
|
||||
}
|
||||
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`)
|
||||
done.resolve()
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
* closes the connection to the databse
|
||||
*/
|
||||
close(): plugins.q.Promise<any> {
|
||||
let done = plugins.q.defer()
|
||||
if (this.dbType === 'mongodb') {
|
||||
this.db.close()
|
||||
}
|
||||
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`)
|
||||
done.resolve()
|
||||
return done.promise
|
||||
// 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
|
||||
};
|
||||
|
||||
// advanced communication with the database --------------------------------
|
||||
addCollection(dbCollectionArg: DbCollection<any>) {
|
||||
this.collections.add(dbCollectionArg)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
};
|
||||
|
||||
addCollection(dbCollectionArg: DbCollection<any>) {
|
||||
this.collections.add(dbCollectionArg)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,13 +27,19 @@ export class DbCollection<T> {
|
||||
|
||||
|
||||
constructor(nameArg: string, dbArg: Db) {
|
||||
// tell the collection where it belongs
|
||||
this.name = nameArg
|
||||
this.db = dbArg
|
||||
|
||||
// make sure it actually exists
|
||||
if (this.db.dbType === 'mongodb') {
|
||||
this.collection = dbArg.db.collection(nameArg)
|
||||
} else {
|
||||
this.collection = new plugins.nedb()
|
||||
}
|
||||
|
||||
// tell the db class about it (important since Db uses different systems under the hood)
|
||||
this.db.addCollection(this)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,7 @@
|
||||
import * as plugins from './smartdata.plugins'
|
||||
|
||||
import { Objectmap } from 'lik'
|
||||
|
||||
import { Db } from './smartdata.classes.db'
|
||||
import { DbCollection } from './smartdata.classes.dbcollection'
|
||||
|
||||
@ -9,60 +11,70 @@ export type TDocCreation = 'db' | 'new' | 'mixed'
|
||||
* saveable - saveable decorator to be used on class properties
|
||||
*/
|
||||
export function svDb() {
|
||||
return (target: DbDoc<any>, key: string) => {
|
||||
console.log('called sva')
|
||||
if (!target.saveableProperties) { target.saveableProperties = [] }
|
||||
target.saveableProperties.push(key)
|
||||
}
|
||||
return (target: DbDoc<any>, key: string) => {
|
||||
console.log('called sva')
|
||||
if (!target.saveableProperties) { target.saveableProperties = [] }
|
||||
target.saveableProperties.push(key)
|
||||
}
|
||||
}
|
||||
|
||||
export class DbDoc<T> {
|
||||
|
||||
/**
|
||||
* the collection object an Doc belongs to
|
||||
*/
|
||||
collection: DbCollection<T>
|
||||
/**
|
||||
* the collection object an Doc belongs to
|
||||
*/
|
||||
collection: DbCollection<T>
|
||||
|
||||
/**
|
||||
* how the Doc in memory was created, may prove useful later.
|
||||
*/
|
||||
creationType: TDocCreation
|
||||
/**
|
||||
* how the Doc in memory was created, may prove useful later.
|
||||
*/
|
||||
creationType: TDocCreation
|
||||
|
||||
/**
|
||||
* an array of saveable properties of a doc
|
||||
*/
|
||||
saveableProperties: string[]
|
||||
/**
|
||||
* an array of saveable properties of a doc
|
||||
*/
|
||||
saveableProperties: string[]
|
||||
|
||||
/**
|
||||
* class constructor
|
||||
*/
|
||||
constructor() {
|
||||
this.collection = this.constructor['dbCollection']
|
||||
/**
|
||||
* class constructor
|
||||
*/
|
||||
constructor() {
|
||||
this.collection = this.constructor[ 'dbCollection' ]
|
||||
}
|
||||
|
||||
/**
|
||||
* saves this instance but not any connected items
|
||||
* may lead to data inconsistencies, but is faster
|
||||
*/
|
||||
save() {
|
||||
let saveableObject: any = {} // isn not exposed to outside, so any is ok here
|
||||
for (let propertyNameString of this.saveableProperties) {
|
||||
saveableObject[ propertyNameString ] = this[ propertyNameString ]
|
||||
}
|
||||
|
||||
/**
|
||||
* saves this instance but not any connected items
|
||||
* may lead to data inconsistencies, but is faster
|
||||
*/
|
||||
save() {
|
||||
let saveableObject = {}
|
||||
for (let propertyNameString of this.saveableProperties) {
|
||||
saveableObject[propertyNameString] = this[propertyNameString]
|
||||
}
|
||||
switch (this.creationType) {
|
||||
case 'db':
|
||||
this.collection // TODO implement collection.update()
|
||||
break
|
||||
case 'new':
|
||||
this.collection.insertOne(saveableObject)
|
||||
}
|
||||
switch (this.creationType) {
|
||||
case 'db':
|
||||
this.collection // TODO implement collection.update()
|
||||
break
|
||||
case 'new':
|
||||
this.collection.insertOne(saveableObject)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* also store any referenced objects to DB
|
||||
* better for data consistency
|
||||
*/
|
||||
saveDeep() {
|
||||
this.save()
|
||||
/**
|
||||
* also store any referenced objects to DB
|
||||
* better for data consistency
|
||||
*/
|
||||
saveDeep(savedMapArg: Objectmap<DbDoc<any>> = null) {
|
||||
if (!savedMapArg) {
|
||||
savedMapArg = new Objectmap<DbDoc<any>>()
|
||||
}
|
||||
savedMapArg.add(this)
|
||||
this.save()
|
||||
for (let propertyKey in this) {
|
||||
let property = this[ propertyKey ]
|
||||
if (property instanceof DbDoc && !savedMapArg.checkForObject(property)) {
|
||||
property.saveDeep(savedMapArg)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user