fix(core): update
This commit is contained in:
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@pushrocks/smartdata',
|
||||
version: '5.0.8',
|
||||
version: '5.0.9',
|
||||
description: 'do more with data'
|
||||
}
|
||||
|
@ -7,4 +7,4 @@ export * from './smartdata.classes.cursor.js';
|
||||
// to be removed with the next breaking update
|
||||
import * as plugins from './smartdata.plugins.js';
|
||||
type IMongoDescriptor = plugins.tsclass.database.IMongoDescriptor;
|
||||
export type { IMongoDescriptor }
|
||||
export type { IMongoDescriptor };
|
||||
|
@ -179,7 +179,10 @@ export class SmartdataCollection<T> {
|
||||
return result;
|
||||
}
|
||||
|
||||
public async getCursor(filterObjectArg: any, dbDocArg: typeof SmartDataDbDoc): Promise<SmartdataDbCursor<any>> {
|
||||
public async getCursor(
|
||||
filterObjectArg: any,
|
||||
dbDocArg: typeof SmartDataDbDoc
|
||||
): Promise<SmartdataDbCursor<any>> {
|
||||
await this.init();
|
||||
const cursor = this.mongoDbCollection.find(filterObjectArg);
|
||||
return new SmartdataDbCursor(cursor, dbDocArg);
|
||||
@ -199,16 +202,22 @@ export class SmartdataCollection<T> {
|
||||
/**
|
||||
* watches the collection while applying a filter
|
||||
*/
|
||||
public async watch(filterObject: any, smartdataDbDocArg: typeof SmartDataDbDoc): Promise<SmartdataDbWatcher> {
|
||||
public async watch(
|
||||
filterObject: any,
|
||||
smartdataDbDocArg: typeof SmartDataDbDoc
|
||||
): Promise<SmartdataDbWatcher> {
|
||||
await this.init();
|
||||
const changeStream = this.mongoDbCollection.watch([
|
||||
const changeStream = this.mongoDbCollection.watch(
|
||||
[
|
||||
{
|
||||
$match: filterObject,
|
||||
},
|
||||
],
|
||||
{
|
||||
$match: filterObject
|
||||
fullDocument: 'updateLookup',
|
||||
}
|
||||
], {
|
||||
fullDocument: 'updateLookup'
|
||||
});
|
||||
const smartdataWatcher = new SmartdataDbWatcher(changeStream, smartdataDbDocArg);
|
||||
);
|
||||
const smartdataWatcher = new SmartdataDbWatcher(changeStream, smartdataDbDocArg);
|
||||
await smartdataWatcher.readyDeferred.promise;
|
||||
return smartdataWatcher;
|
||||
}
|
||||
|
@ -16,20 +16,23 @@ export class SmartdataDbCursor<T = any> {
|
||||
}
|
||||
|
||||
public async next(closeAtEnd = true) {
|
||||
const result = this.smartdataDbDoc.createInstanceFromMongoDbNativeDoc(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: any) => Promise<any>, closeCursorAtEnd = true) {
|
||||
public async forEach(forEachFuncArg: (itemArg: T) => Promise<any>, closeCursorAtEnd = true) {
|
||||
let nextDocument: any;
|
||||
do {
|
||||
nextDocument = await this.mongodbCursor.next();
|
||||
if (nextDocument) {
|
||||
const nextClassInstance = this.smartdataDbDoc.createInstanceFromMongoDbNativeDoc(nextDocument);
|
||||
await forEachFuncArg(nextClassInstance);
|
||||
const nextClassInstance =
|
||||
this.smartdataDbDoc.createInstanceFromMongoDbNativeDoc(nextDocument);
|
||||
await forEachFuncArg(nextClassInstance as any);
|
||||
}
|
||||
} while (nextDocument);
|
||||
if (closeCursorAtEnd) {
|
||||
|
@ -147,18 +147,18 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
|
||||
|
||||
/**
|
||||
* watch the collection
|
||||
* @param this
|
||||
* @param filterArg
|
||||
* @param forEachFunction
|
||||
* @param this
|
||||
* @param filterArg
|
||||
* @param forEachFunction
|
||||
*/
|
||||
public static async watch<T>(
|
||||
public static async watch<T>(
|
||||
this: plugins.tsclass.typeFest.Class<T>,
|
||||
filterArg: plugins.tsclass.typeFest.PartialDeep<T>
|
||||
) {
|
||||
const collection: SmartdataCollection<T> = (this as any).collection;
|
||||
const watcher: SmartdataDbWatcher<T> = await collection.watch(
|
||||
convertFilterForMongoDb(filterArg),
|
||||
(this as any)
|
||||
this as any
|
||||
);
|
||||
return watcher;
|
||||
}
|
||||
|
@ -12,11 +12,16 @@ export class SmartdataDbWatcher<T = any> {
|
||||
private changeStream: plugins.mongodb.ChangeStream<T>;
|
||||
|
||||
public changeSubject = new plugins.smartrx.rxjs.Subject<T>();
|
||||
constructor(changeStreamArg: plugins.mongodb.ChangeStream<T>, smartdataDbDocArg: typeof SmartDataDbDoc) {
|
||||
constructor(
|
||||
changeStreamArg: plugins.mongodb.ChangeStream<T>,
|
||||
smartdataDbDocArg: typeof SmartDataDbDoc
|
||||
) {
|
||||
this.changeStream = changeStreamArg;
|
||||
this.changeStream.on('change', async (item: T) => {
|
||||
this.changeSubject.next(smartdataDbDocArg.createInstanceFromMongoDbNativeDoc(item) as any as T);
|
||||
})
|
||||
this.changeSubject.next(
|
||||
smartdataDbDocArg.createInstanceFromMongoDbNativeDoc(item) as any as T
|
||||
);
|
||||
});
|
||||
plugins.smartdelay.delayFor(0).then(() => {
|
||||
this.readyDeferred.resolve();
|
||||
});
|
||||
|
@ -7,11 +7,21 @@ export { tsclass };
|
||||
import * as smartlog from '@pushrocks/smartlog';
|
||||
import * as lodash from 'lodash';
|
||||
import * as mongodb from 'mongodb';
|
||||
import * as smartdelay from '@pushrocks/smartdelay'
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartq from '@pushrocks/smartpromise';
|
||||
import * as smartrx from '@pushrocks/smartrx';
|
||||
import * as smartstring from '@pushrocks/smartstring';
|
||||
import * as smartunique from '@pushrocks/smartunique';
|
||||
|
||||
export { smartdelay, smartpromise, smartlog, lodash, smartq, smartrx, mongodb, smartstring, smartunique };
|
||||
export {
|
||||
smartdelay,
|
||||
smartpromise,
|
||||
smartlog,
|
||||
lodash,
|
||||
smartq,
|
||||
smartrx,
|
||||
mongodb,
|
||||
smartstring,
|
||||
smartunique,
|
||||
};
|
||||
|
Reference in New Issue
Block a user