split into class files
This commit is contained in:
39
ts/index.ts
39
ts/index.ts
@ -1,39 +1,4 @@
|
||||
import * as plugins from './smartdata.plugins'
|
||||
|
||||
export class DbCollection<T> {
|
||||
constructor(nameArg: string, db: plugins.mongodb.Db) {
|
||||
let collection = db.collection(nameArg)
|
||||
}
|
||||
}
|
||||
|
||||
export type TDbConnectionStatus = 'disconnected' | 'connected' | 'failed'
|
||||
|
||||
export class DbConnection {
|
||||
dbUrl: string
|
||||
db: plugins.mongodb.Db
|
||||
status: TDbConnectionStatus
|
||||
|
||||
constructor(dbUrl: string) {
|
||||
this.dbUrl = dbUrl
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
close(): plugins.q.Promise<any> {
|
||||
let done = plugins.q.defer()
|
||||
this.db.close()
|
||||
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`)
|
||||
done.resolve()
|
||||
return done.promise
|
||||
}
|
||||
}
|
||||
export * from './smartdata.classes.dbcollection'
|
||||
export * from './smartdata.classes.dbconnection'
|
||||
|
24
ts/smartdata.classes.dbcollection.ts
Normal file
24
ts/smartdata.classes.dbcollection.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import * as plugins from './smartdata.plugins'
|
||||
import { DbConnection } from './smartdata.classes.dbconnection'
|
||||
|
||||
export class DbCollection<T> {
|
||||
collection: plugins.mongodb.Collection
|
||||
constructor(nameArg: string, dbConnectionArg: DbConnection) {
|
||||
this.collection = dbConnectionArg.db.collection(nameArg)
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a validation function that all newly inserted and updated objects have to pass
|
||||
*/
|
||||
addObjectValidation(){}
|
||||
|
||||
/**
|
||||
* inserts am object into the DbCollection
|
||||
*/
|
||||
insert(objectArg: T) {}
|
||||
|
||||
/**
|
||||
* inserts many objects at once into the DbCollection
|
||||
*/
|
||||
insertMany(objectArrayArg: T[]) {}
|
||||
}
|
33
ts/smartdata.classes.dbconnection.ts
Normal file
33
ts/smartdata.classes.dbconnection.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import * as plugins from './smartdata.plugins'
|
||||
|
||||
export type TDbConnectionStatus = 'disconnected' | 'connected' | 'failed'
|
||||
|
||||
export class DbConnection {
|
||||
dbUrl: string
|
||||
db: plugins.mongodb.Db
|
||||
status: TDbConnectionStatus
|
||||
|
||||
constructor(dbUrl: string) {
|
||||
this.dbUrl = dbUrl
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
close(): plugins.q.Promise<any> {
|
||||
let done = plugins.q.defer()
|
||||
this.db.close()
|
||||
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`)
|
||||
done.resolve()
|
||||
return done.promise
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user