From de1f1110b4f3497ed20d295d691e9ae45006d43a Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Tue, 17 May 2022 23:54:26 +0200 Subject: [PATCH] fix(core): update --- test/test.ts | 4 ++-- ts/00_commitinfo_data.ts | 2 +- ts/smartdata.classes.collection.ts | 10 +++++----- ts/smartdata.classes.cursor.ts | 20 ++++++++++++-------- ts/smartdata.classes.doc.ts | 9 ++++++--- ts/smartdata.classes.watcher.ts | 10 ++++++---- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/test/test.ts b/test/test.ts index 5a69e4c..833b8e3 100644 --- a/test/test.ts +++ b/test/test.ts @@ -92,7 +92,7 @@ tap.test('should save the car to the db', async () => { }); tap.test('expect to get instance of Car with shallow match', async () => { - const totalQueryCycles = totalCars / 2; + const totalQueryCycles = totalCars / 6; let counter = 0; do { const timeStart = Date.now(); @@ -113,7 +113,7 @@ tap.test('expect to get instance of Car with shallow match', async () => { }); tap.test('expect to get instance of Car with deep match', async () => { - const totalQueryCycles = totalCars / 4; + const totalQueryCycles = totalCars / 6; let counter = 0; do { const timeStart = Date.now(); diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 0679bd0..01b93b9 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@pushrocks/smartdata', - version: '5.0.0', + version: '5.0.1', description: 'do more with data' } diff --git a/ts/smartdata.classes.collection.ts b/ts/smartdata.classes.collection.ts index f982eac..d710fe0 100644 --- a/ts/smartdata.classes.collection.ts +++ b/ts/smartdata.classes.collection.ts @@ -180,10 +180,10 @@ export class SmartdataCollection { return result; } - public async getCursor(filterObject: any): Promise> { + public async getCursor(filterObjectArg: any, dbDocArg: typeof SmartDataDbDoc): Promise> { await this.init(); - const cursor = this.mongoDbCollection.find(filterObject); - return new SmartdataDbCursor(cursor); + const cursor = this.mongoDbCollection.find(filterObjectArg); + return new SmartdataDbCursor(cursor, dbDocArg); } /** @@ -200,7 +200,7 @@ export class SmartdataCollection { /** * watches the collection while applying a filter */ - public async watch(filterObject: any): Promise { + public async watch(filterObject: any, smartdataDbDocArg: typeof SmartDataDbDoc): Promise { await this.init(); const changeStream = this.mongoDbCollection.watch([ { @@ -209,7 +209,7 @@ export class SmartdataCollection { ], { fullDocument: 'updateLookup' }); - const smartdataWatcher = new SmartdataDbWatcher(changeStream); + const smartdataWatcher = new SmartdataDbWatcher(changeStream, smartdataDbDocArg); await smartdataWatcher.readyDeferred.promise; return smartdataWatcher; } diff --git a/ts/smartdata.classes.cursor.ts b/ts/smartdata.classes.cursor.ts index c94030a..3c232b7 100644 --- a/ts/smartdata.classes.cursor.ts +++ b/ts/smartdata.classes.cursor.ts @@ -1,3 +1,4 @@ +import { SmartDataDbDoc } from './smartdata.classes.doc.js'; import * as plugins from './smartdata.plugins.js'; /** @@ -8,26 +9,29 @@ export class SmartdataDbCursor { // INSTANCE public mongodbCursor: plugins.mongodb.FindCursor; - constructor(cursorArg: plugins.mongodb.FindCursor) { + private smartdataDbDoc: typeof SmartDataDbDoc; + constructor(cursorArg: plugins.mongodb.FindCursor, dbDocArg: typeof SmartDataDbDoc) { this.mongodbCursor = cursorArg; + this.smartdataDbDoc = dbDocArg; } public async next(closeAtEnd = true) { - const result = await this.mongodbCursor.next(); + const result = this.smartdataDbDoc.createInstanceFromMongoDbNativeDoc(await this.mongodbCursor.next()); if (!result && closeAtEnd) { await this.close(); } return result; } - public async forEach(forEachFuncArg: (itemArg: T) => Promise, closeCursorAtEnd = true) { - let currentValue: T; + public async forEach(forEachFuncArg: (itemArg: any) => Promise, closeCursorAtEnd = true) { + let nextDocument: any; do { - currentValue = await this.mongodbCursor.next(); - if (currentValue) { - await forEachFuncArg(currentValue); + nextDocument = await this.mongodbCursor.next(); + if (nextDocument) { + const nextClassInstance = this.smartdataDbDoc.createInstanceFromMongoDbNativeDoc(nextDocument); + await forEachFuncArg(nextClassInstance); } - } while (currentValue); + } while (nextDocument); if (closeCursorAtEnd) { await this.close(); } diff --git a/ts/smartdata.classes.doc.ts b/ts/smartdata.classes.doc.ts index 6835993..3052730 100644 --- a/ts/smartdata.classes.doc.ts +++ b/ts/smartdata.classes.doc.ts @@ -137,8 +137,10 @@ export class SmartDataDbDoc, filterArg: plugins.tsclass.typeFest.PartialDeep ) { - const cursor: SmartdataDbCursor = await (this as any).collection.getCursor( - convertFilterForMongoDb(filterArg) + const collection: SmartdataCollection = (this as any).collection; + const cursor: SmartdataDbCursor = await collection.getCursor( + convertFilterForMongoDb(filterArg), + this as any as typeof SmartDataDbDoc ); return cursor; } @@ -155,7 +157,8 @@ export class SmartDataDbDoc = (this as any).collection; const watcher: SmartdataDbWatcher = await collection.watch( - convertFilterForMongoDb(filterArg) + convertFilterForMongoDb(filterArg), + (this as any) ); return watcher; } diff --git a/ts/smartdata.classes.watcher.ts b/ts/smartdata.classes.watcher.ts index 3d0e4f6..a33d963 100644 --- a/ts/smartdata.classes.watcher.ts +++ b/ts/smartdata.classes.watcher.ts @@ -1,3 +1,4 @@ +import { SmartDataDbDoc } from './smartdata.classes.doc.js'; import * as plugins from './smartdata.plugins.js'; /** @@ -8,12 +9,13 @@ export class SmartdataDbWatcher { public readyDeferred = plugins.smartpromise.defer(); // INSTANCE - public changeStream: plugins.mongodb.ChangeStream; - public changeSubject = new plugins.smartrx.rxjs.Subject(); - constructor(changeStreamArg: plugins.mongodb.ChangeStream) { + private changeStream: plugins.mongodb.ChangeStream; + + public changeSubject = new plugins.smartrx.rxjs.Subject>(); + constructor(changeStreamArg: plugins.mongodb.ChangeStream, smartdataDbDocArg: typeof SmartDataDbDoc) { this.changeStream = changeStreamArg; this.changeStream.on('change', async (item: T) => { - this.changeSubject.next(item); + this.changeSubject.next(smartdataDbDocArg.createInstanceFromMongoDbNativeDoc(item)); }) plugins.smartdelay.delayFor(0).then(() => { this.readyDeferred.resolve();