prepare v2 of smartdata

This commit is contained in:
2018-01-07 17:26:34 +01:00
parent 3ba2bc8446
commit 2d97ab9dc5
12 changed files with 44 additions and 81 deletions

View File

@ -1,23 +1,25 @@
import * as plugins from './smartdata.plugins'
import { Objectmap } from 'lik'
import { DbCollection } from './smartdata.classes.dbcollection'
import { DbTable } from './smartdata.classes.dbcollection'
import { getObjectDoc } from './smartdata.classes.dbobjectdoc'
import { Connection as dbConnection, ConnectionOptions } from 'rethinkdb'
/**
* interface - indicates the connection status of the db
*/
export type TConnectionStatus = 'disconnected' | 'connected' | 'failed'
export class Db {
dbUrl: string
db: plugins.mongodb.Db
dbName: string
connectionOptions: plugins.rethinkDb.ConnectionOptions
dbConnection: plugins.rethinkDb.Connection
status: TConnectionStatus
classCollections = new Objectmap<DbCollection<any>>()
objectCollections = new Objectmap<DbCollection<any>>()
constructor (dbUrlArg: string) {
this.dbUrl = dbUrlArg
constructor (connectionOptionsArg: ConnectionOptions) {
this.dbName = connectionOptionsArg.db
this.connectionOptions = connectionOptionsArg
}
// basic connection stuff ----------------------------------------------
@ -25,16 +27,9 @@ export class Db {
/**
* connects to the database that was specified during instance creation
*/
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)
})
return done.promise
async connect (): Promise<any> {
this.dbConnection = await plugins.rethinkDb.connect(this.connectionOptions)
plugins.beautylog.ok(`Connected to database ${this.dbName}`)
}
/**
@ -42,41 +37,10 @@ export class Db {
*/
close (): Promise<any> {
let done = plugins.smartq.defer()
this.db.close()
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`)
this.dbConnection.close()
plugins.beautylog.ok(`disconnected to database ${this.dbName}`)
done.resolve()
return done.promise
}
// advanced communication with the database --------------------------------
/**
* gets a class based collection by name: string
*/
async getClassCollectionByName<T> (nameArg: string): Promise<DbCollection<T>> {
let resultCollection = this.classCollections.find((dbCollectionArg) => {
return dbCollectionArg.name === nameArg
})
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
}
}
addCollection (dbCollectionArg: DbCollection<any>) {
this.classCollections.add(dbCollectionArg)
}
}

View File

@ -12,16 +12,16 @@ export interface IDocValidation<T> {
export function Collection (db: Db) {
return function (constructor) {
constructor[ 'dbCollection' ] = new DbCollection(constructor, db)
constructor[ 'dbCollection' ] = new DbTable(constructor, db)
}
}
export class DbCollection<T> {
export class DbTable<T> {
/**
* the collection that is used, defaults to mongodb collection,
* can be nedb datastore (sub api of mongodb)
*/
collection: plugins.mongodb.Collection
table: plugins.rethinkDb.Table
collectedClass: T & DbDoc<T>
objectValidation: IDocValidation<T> = null
name: string
@ -34,7 +34,7 @@ export class DbCollection<T> {
this.db = dbArg
// make sure it actually exists
this.collection = dbArg.db.collection(this.name)
this.table = dbArg.dbConnection.collection(this.name)
// tell the db class about it (important since Db uses different systems under the hood)
this.db.addCollection(this)
@ -52,7 +52,7 @@ export class DbCollection<T> {
*/
find (docMatchArg: T | any, optionsArg?: IFindOptions): Promise<T[]> {
let done = plugins.smartq.defer<T[]>()
let findCursor = this.collection.find(docMatchArg)
let findCursor = this.table.find(docMatchArg)
if (optionsArg) {
if (optionsArg.limit) { findCursor = findCursor.limit(1) }
}
@ -73,7 +73,7 @@ export class DbCollection<T> {
let done = plugins.smartq.defer<void>()
this.checkDoc(docArg).then(
() => {
this.collection.insertOne(docArg)
this.table.insertOne(docArg)
.then(() => { done.resolve() })
},
() => {
@ -92,7 +92,7 @@ export class DbCollection<T> {
checkDocPromiseArray.push(this.checkDoc(docArg))
}
Promise.all(checkDocPromiseArray).then(() => {
this.collection.insertMany(docArrayArg)
this.table.insertMany(docArrayArg)
.then(() => { done.resolve() })
})
return done.promise

View File

@ -3,7 +3,7 @@ import * as plugins from './smartdata.plugins'
import { Objectmap } from 'lik'
import { Db } from './smartdata.classes.db'
import { DbCollection } from './smartdata.classes.dbcollection'
import { DbTable } from './smartdata.classes.dbcollection'
export type TDocCreation = 'db' | 'new' | 'mixed'
@ -23,7 +23,7 @@ export class DbDoc<T> {
/**
* the collection object an Doc belongs to
*/
collection: DbCollection<T>
collection: DbTable<T>
/**
* how the Doc in memory was created, may prove useful later.

View File

@ -2,11 +2,11 @@ import * as plugins from './smartdata.plugins'
import { Db } from './smartdata.classes.db'
import { DbDoc } from './smartdata.classes.dbdoc'
import { DbCollection } from './smartdata.classes.dbcollection'
import { DbTable } from './smartdata.classes.dbcollection'
export let getObjectDoc = (nameArg,dbArg: Db) => {
let objectDoc = new DbDoc()
objectDoc.name = nameArg
objectDoc.collection = new DbCollection(objectDoc, dbArg)
objectDoc.collection = new DbTable(objectDoc, dbArg)
return objectDoc
}