prepare v2 of smartdata

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

View File

@ -7,7 +7,7 @@ import { DbCollection } from './smartdata.classes.dbcollection';
export declare type TConnectionStatus = 'disconnected' | 'connected' | 'failed';
export declare class Db {
dbUrl: string;
db: plugins.mongodb.Db;
dbConnection: plugins.rethinkDb.Connection;
status: TConnectionStatus;
classCollections: Objectmap<DbCollection<any>>;
objectCollections: Objectmap<DbCollection<any>>;

View File

@ -22,24 +22,16 @@ class Db {
* connects to the database that was specified during instance creation
*/
connect() {
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 __awaiter(this, void 0, void 0, function* () {
this.dbConnection = yield plugins.rethinkDb.connect('test');
});
return done.promise;
}
/**
* closes the connection to the databse
*/
close() {
let done = plugins.smartq.defer();
this.db.close();
this.dbConnection.close();
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`);
done.resolve();
return done.promise;
@ -78,4 +70,4 @@ class Db {
}
}
exports.Db = Db;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsK0NBQThDO0FBQzlDLDZCQUErQjtBQUcvQixtRkFBOEQ7QUFPOUQ7SUFPRSxZQUFhLFFBQWdCO1FBSDdCLHFCQUFnQixHQUFHLElBQUksZUFBUyxFQUFxQixDQUFBO1FBQ3JELHNCQUFpQixHQUFHLElBQUksZUFBUyxFQUFxQixDQUFBO1FBR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DO0NBRUY7QUE5REQsZ0JBOERDIn0=

View File

@ -2,6 +2,6 @@ import 'typings-global';
import * as assert from 'assert';
import * as beautylog from 'beautylog';
import * as lodash from 'lodash';
import * as mongodb from 'mongodb';
import * as rethinkDb from 'rethinkdb';
import * as smartq from 'smartq';
export { assert, beautylog, lodash, mongodb, smartq };
export { assert, beautylog, lodash, smartq, rethinkDb };

View File

@ -7,8 +7,8 @@ const beautylog = require("beautylog");
exports.beautylog = beautylog;
const lodash = require("lodash");
exports.lodash = lodash;
const mongodb = require("mongodb");
exports.mongodb = mongodb;
const rethinkDb = require("rethinkdb");
exports.rethinkDb = rethinkDb;
const smartq = require("smartq");
exports.smartq = smartq;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLDBCQUF1QjtBQUN2QixpQ0FBZ0M7QUFPNUIsd0JBQU07QUFOVix1Q0FBc0M7QUFPbEMsOEJBQVM7QUFOYixpQ0FBZ0M7QUFPNUIsd0JBQU07QUFOVixtQ0FBa0M7QUFPOUIsMEJBQU87QUFOWCxpQ0FBZ0M7QUFPNUIsd0JBQU0ifQ==
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLDBCQUF1QjtBQUN2QixpQ0FBZ0M7QUFPNUIsd0JBQU07QUFOVix1Q0FBc0M7QUFPbEMsOEJBQVM7QUFOYixpQ0FBZ0M7QUFPNUIsd0JBQU07QUFOVix1Q0FBc0M7QUFRbEMsOEJBQVM7QUFQYixpQ0FBZ0M7QUFNNUIsd0JBQU0ifQ==

View File

@ -21,6 +21,7 @@
"dependencies": {
"@types/lodash": "^4.14.74",
"@types/mongodb": "^2.2.11",
"@types/rethinkdb": "^2.3.8",
"beautylog": "^6.1.10",
"lik": "^1.0.40",
"lodash": "^4.17.4",

View File

@ -37,7 +37,7 @@ tap.test('should create an extended class', async () => {
})
expect(testCarInstance.name).to.equal('TestCar')
expect(testCarInstance.saveableProperties[ 0 ]).equal('color')
expect(testCarInstance.collection).be.instanceof(smartdata.DbCollection)
expect(testCarInstance.collection).be.instanceof(smartdata.DbTable)
expect(testCarInstance).be.instanceof(smartdata.DbDoc)
if (!process.env.CI) { console.log(TestCar) }
})

View File

@ -29,7 +29,7 @@ tap.test('should establish a connection to mongodb', async () => {
// ------
// Collections
// ------
let testDbCollection: smartdata.DbCollection<ITestObject1>
let testDbCollection: smartdata.DbTable<ITestObject1>
tap.test('should give me a collection', async () => {
testDbCollection = await testDb.getObjectCollectionByName<ITestObject1>('TestValue', testDb, true)

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
}

View File

@ -44,6 +44,12 @@
version "8.0.53"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.53.tgz#396b35af826fa66aad472c8cb7b8d5e277f4e6d8"
"@types/rethinkdb@^2.3.8":
version "2.3.8"
resolved "https://registry.yarnpkg.com/@types/rethinkdb/-/rethinkdb-2.3.8.tgz#961f78f0e731668631891bd1199722bb4a2258a8"
dependencies:
"@types/node" "*"
"@types/shelljs@^0.7.4":
version "0.7.6"
resolved "https://registry.yarnpkg.com/@types/shelljs/-/shelljs-0.7.6.tgz#4ac7ca01c191ba65b8e2bf50543c5560084d8d27"