implenting dbDoc

This commit is contained in:
2016-09-13 22:53:21 +02:00
parent eb54fbcd0d
commit df4346e0a2
16 changed files with 187 additions and 34 deletions

View File

@ -1,4 +1,5 @@
import * as plugins from './smartdata.plugins'
export * from './smartdata.classes.dbcollection'
export * from './smartdata.classes.db'
export * from './smartdata.classes.dbcollection'
export * from './smartdata.classes.dbdoc'

View File

@ -1,4 +1,7 @@
import * as plugins from './smartdata.plugins'
import { Objectmap } from 'lik'
import { DbCollection } from './smartdata.classes.dbcollection'
export type TConnectionStatus = 'disconnected' | 'connected' | 'failed'
@ -6,11 +9,17 @@ export class Db {
dbUrl: string
db: plugins.mongodb.Db
status: TConnectionStatus
collections = new Objectmap<DbCollection<any>>()
constructor(dbUrl: string) {
this.dbUrl = dbUrl
}
// basic connection stuff ----------------------------------------------
/**
* connects to the database that was specified during instance creation
*/
connect(): plugins.q.Promise<any> {
let done = plugins.q.defer()
plugins.mongodb.MongoClient.connect(this.dbUrl, (err, db) => {
@ -23,6 +32,9 @@ export class Db {
return done.promise
}
/**
* closes the connection to the databse
*/
close(): plugins.q.Promise<any> {
let done = plugins.q.defer()
this.db.close()
@ -30,4 +42,20 @@ export class Db {
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
};
}

View File

@ -1,9 +1,15 @@
import * as plugins from './smartdata.plugins'
import { Db } from './smartdata.classes.db'
export interface IFindOptions {
limit?: number
}
export class DbCollection<T> {
collection: plugins.mongodb.Collection
name: string
constructor(nameArg: string, dbArg: Db) {
this.name = nameArg
this.collection = dbArg.db.collection(nameArg)
}
@ -15,9 +21,13 @@ export class DbCollection<T> {
/**
* finds an object in the DbCollection
*/
find(docMatchArg: T | any): plugins.q.Promise<T[]> {
find(docMatchArg: T | any, optionsArg?: IFindOptions): plugins.q.Promise<T[]> {
let done = plugins.q.defer<T[]>()
this.collection.find(docMatchArg).toArray((err, docs) => {
let findCursor = this.collection.find(docMatchArg)
if (optionsArg) {
if ( optionsArg.limit ) { findCursor = findCursor.limit(1) }
}
findCursor.toArray((err, docs) => {
if (err) { throw err }
done.resolve(docs)
})

View File

@ -0,0 +1,20 @@
import * as plugins from './smartdata.plugins'
import { Db } from './smartdata.classes.db'
import { DbCollection } from './smartdata.classes.dbcollection'
export type TDocCreation = 'db' | 'data' | 'mixed'
export class DbDoc<T> {
collection: DbCollection<T>
creationType: TDocCreation
constructor(collectionNameArg: string, dbArg: Db ) {
this.collection = new DbCollection<T>(collectionNameArg, dbArg)
}
save() {
}
saveDeep() {
}
}