update rethink integration

This commit is contained in:
Philipp Kunz 2018-01-07 17:58:30 +01:00
parent 8101e49026
commit 1af51dd989
3 changed files with 34 additions and 34 deletions

View File

@ -18,7 +18,10 @@ interface ITestObject1 {
tap.test('should establish a connection to mongodb', async () => { tap.test('should establish a connection to mongodb', async () => {
// testDb = new smartdata.Db(`mongodb://${process.env.MONGO_USER}:${process.env.MONGO_PASS}@sandbox-shard-00-00-uyw7y.mongodb.net:27017,sandbox-shard-00-01-uyw7y.mongodb.net:27017,sandbox-shard-00-02-uyw7y.mongodb.net:27017/${process.env.MONGO_DATABASE}?ssl=true&replicaSet=sandbox-shard-0&authSource=admin`) // testDb = new smartdata.Db(`mongodb://${process.env.MONGO_USER}:${process.env.MONGO_PASS}@sandbox-shard-00-00-uyw7y.mongodb.net:27017,sandbox-shard-00-01-uyw7y.mongodb.net:27017,sandbox-shard-00-02-uyw7y.mongodb.net:27017/${process.env.MONGO_DATABASE}?ssl=true&replicaSet=sandbox-shard-0&authSource=admin`)
testDb = new smartdata.Db(`mongodb://localhost:27017/${process.env.MONGO_DATABASE}`) testDb = new smartdata.Db({
db: 'test',
host: ''
})
await testDb.connect() await testDb.connect()
}) })
@ -29,32 +32,32 @@ tap.test('should establish a connection to mongodb', async () => {
// ------ // ------
// Collections // Collections
// ------ // ------
let testDbCollection: smartdata.DbTable<ITestObject1> let testDbTable: smartdata.DbTable<ITestObject1>
tap.test('should give me a collection', async () => { tap.test('should give me a collection', async () => {
testDbCollection = await testDb.getObjectCollectionByName<ITestObject1>('TestValue', testDb, true) testDbTable = await testDb.getTableByName<ITestObject1>('testTable')
}) })
tap.test('should insert a doc into the collection', async () => { tap.test('should insert a doc into the collection', async () => {
await testDbCollection.insertOne({ value1: 'test' }) await testDbTable.insertOne({ value1: 'test' })
}) })
tap.test('should find all docs of testDbCollection', async () => { tap.test('should find all docs of testDbCollection', async () => {
await testDbCollection.find({}).then(async (resultArray) => { await testDbTable.find({}).then(async (resultArray) => {
console.log(resultArray) console.log(resultArray)
expect(resultArray[ 0 ].value1).equal('test') expect(resultArray[ 0 ].value1).equal('test')
}) })
}) })
tap.test('should insert many docs into the collection', async () => { tap.test('should insert many docs into the collection', async () => {
await testDbCollection.insertMany([ await testDbTable.insertMany([
{ value1: 'test2' }, { value1: 'test2' },
{ value1: 'test', value2: 3, value3: 'hi' } { value1: 'test', value2: 3, value3: 'hi' }
]) ])
}) })
tap.test('should find a specified doc', async () => { tap.test('should find a specified doc', async () => {
await testDbCollection.find({ 'value3': { '$exists': true } }).then((resultArray) => { await testDbTable.find({ 'value3': { '$exists': true } }).then((resultArray) => {
console.log(resultArray) console.log(resultArray)
expect(resultArray[ 0 ].value3).equal('hi') expect(resultArray[ 0 ].value3).equal('hi')
}) })

View File

@ -16,8 +16,9 @@ export class Db {
connectionOptions: plugins.rethinkDb.ConnectionOptions connectionOptions: plugins.rethinkDb.ConnectionOptions
dbConnection: plugins.rethinkDb.Connection dbConnection: plugins.rethinkDb.Connection
status: TConnectionStatus status: TConnectionStatus
dbTablesMap = new Objectmap<DbTable<any>>()
constructor (connectionOptionsArg: ConnectionOptions) { constructor(connectionOptionsArg: ConnectionOptions) {
this.dbName = connectionOptionsArg.db this.dbName = connectionOptionsArg.db
this.connectionOptions = connectionOptionsArg this.connectionOptions = connectionOptionsArg
} }
@ -43,4 +44,21 @@ export class Db {
return done.promise return done.promise
} }
// handle table to class distribution
/**
* Gets a table's name and returns smartdata's DbTable class
* @param nameArg
* @returns DbTable
*/
async getDbTableByName<T>(nameArg: string): Promise<DbTable<T>> {
let resultCollection = this.dbTablesMap.find((dbCollectionArg) => {
return dbCollectionArg.name === nameArg
})
return resultCollection
}
addTable (dbCollectionArg: DbTable<any>) {
this.dbTablesMap.add(dbCollectionArg)
}
} }

View File

@ -35,7 +35,7 @@ export class DbTable<T> {
this.table = plugins.rethinkDb.db(this.db.dbName).table(this.name) this.table = plugins.rethinkDb.db(this.db.dbName).table(this.name)
// tell the db class about it (important since Db uses different systems under the hood) // tell the db class about it (important since Db uses different systems under the hood)
this.db.addCollection(this) this.db.addTable(this)
} }
/** /**
@ -48,36 +48,15 @@ export class DbTable<T> {
/** /**
* finds an object in the DbCollection * finds an object in the DbCollection
*/ */
find (docMatchArg: T | any, optionsArg?: IFindOptions): Promise<T[]> { async find (docMatchArg: T | any, optionsArg?: IFindOptions): Promise<T[]> {
let done = plugins.smartq.defer<T[]>()
let findCursor = this.table.find(docMatchArg)
if (optionsArg) {
if (optionsArg.limit) { findCursor = findCursor.limit(1) }
}
findCursor.toArray((err, docs) => {
if (err) {
done.reject(err)
throw err
}
done.resolve(docs)
})
return done.promise
} }
/** /**
* inserts object into the DbCollection * inserts object into the DbCollection
*/ */
insertOne (docArg: T): Promise<void> { async insertOne (docArg: T): Promise<void> {
let done = plugins.smartq.defer<void>() await this.checkDoc(docArg)
this.checkDoc(docArg).then(
() => {
this.table.insertOne(docArg)
.then(() => { done.resolve() })
},
() => {
done.reject(new Error('one the docs did not pass validation'))
})
return done.promise
} }
/** /**