Compare commits

..

10 Commits

Author SHA1 Message Date
811e2490b8 5.0.3 2022-05-19 16:15:28 +02:00
206ccd40e9 fix(watcher.changeSubject): now emits correct type into observer functions 2022-05-19 16:15:28 +02:00
055298172f 5.0.2 2022-05-18 18:17:12 +02:00
278f3c8169 fix(tests): now uses @pushrocks/smartmongo backed by wiredTiger 2022-05-18 18:17:11 +02:00
0709ba921b 5.0.1 2022-05-17 23:54:26 +02:00
de1f1110b4 fix(core): update 2022-05-17 23:54:26 +02:00
30f4254428 5.0.0 2022-05-17 21:30:53 +02:00
1c4b03e647 BREAKING CHANGE(core): switch to esm 2022-05-17 21:30:53 +02:00
27cc7651ba 4.0.29 2022-05-17 21:26:17 +02:00
355a2a3f2b fix(core): update 2022-05-17 21:26:17 +02:00
12 changed files with 635 additions and 546 deletions

1055
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +1,14 @@
{ {
"name": "@pushrocks/smartdata", "name": "@pushrocks/smartdata",
"version": "4.0.28", "version": "5.0.3",
"private": false, "private": false,
"description": "do more with data", "description": "do more with data",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
"type": "module", "type": "module",
"scripts": { "scripts": {
"test": "(tstest test/)", "test": "tstest test/",
"testLocal": "(npmdocker)", "build": "tsbuild --web --allowimplicitany"
"build": "(tsbuild --web)"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -23,8 +22,9 @@
"homepage": "https://gitlab.com/pushrocks/smartdata#README", "homepage": "https://gitlab.com/pushrocks/smartdata#README",
"dependencies": { "dependencies": {
"@pushrocks/lik": "^5.0.4", "@pushrocks/lik": "^5.0.4",
"@pushrocks/smartdelay": "^2.0.13",
"@pushrocks/smartlog": "^2.0.44", "@pushrocks/smartlog": "^2.0.44",
"@pushrocks/smartmongo": "^1.0.8", "@pushrocks/smartmongo": "^2.0.1",
"@pushrocks/smartpromise": "^3.1.7", "@pushrocks/smartpromise": "^3.1.7",
"@pushrocks/smartrx": "^2.0.25", "@pushrocks/smartrx": "^2.0.25",
"@pushrocks/smartstring": "^4.0.2", "@pushrocks/smartstring": "^4.0.2",
@ -40,7 +40,7 @@
"@gitzone/tstest": "^1.0.71", "@gitzone/tstest": "^1.0.71",
"@pushrocks/qenv": "^4.0.10", "@pushrocks/qenv": "^4.0.10",
"@pushrocks/tapbundle": "^5.0.3", "@pushrocks/tapbundle": "^5.0.3",
"@types/node": "^17.0.33", "@types/node": "^17.0.34",
"@types/shortid": "0.0.29" "@types/shortid": "0.0.29"
}, },
"files": [ "files": [

View File

@ -45,12 +45,11 @@ tap.test('should create an easystore', async () => {
}); });
tap.test('close', async () => { tap.test('close', async () => {
await testDb.mongoDb.dropDatabase();
await testDb.close();
if (smartmongoInstance) { if (smartmongoInstance) {
await smartmongoInstance.stop(); await smartmongoInstance.stop();
} else {
await testDb.mongoDb.dropDatabase();
} }
await testDb.close();
}); });
tap.start(); tap.start();

View File

@ -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 () => { tap.test('expect to get instance of Car with shallow match', async () => {
const totalQueryCycles = totalCars / 2; const totalQueryCycles = totalCars / 6;
let counter = 0; let counter = 0;
do { do {
const timeStart = Date.now(); 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 () => { tap.test('expect to get instance of Car with deep match', async () => {
const totalQueryCycles = totalCars / 4; const totalQueryCycles = totalCars / 6;
let counter = 0; let counter = 0;
do { do {
const timeStart = Date.now(); const timeStart = Date.now();
@ -205,12 +205,11 @@ tap.test('should use a cursor', async () => {
// close the database connection // close the database connection
// ======================================= // =======================================
tap.test('close', async () => { tap.test('close', async () => {
await testDb.mongoDb.dropDatabase();
await testDb.close();
if (smartmongoInstance) { if (smartmongoInstance) {
await smartmongoInstance.stop(); await smartmongoInstance.stop();
} else {
await testDb.mongoDb.dropDatabase();
} }
await testDb.close();
}); });
tap.start({ throwOnError: true }); tap.start({ throwOnError: true });

View File

@ -85,12 +85,11 @@ tap.test('should get a car', async () => {
// close the database connection // close the database connection
// ======================================= // =======================================
tap.test('close', async () => { tap.test('close', async () => {
await testDb.mongoDb.dropDatabase();
await testDb.close();
if (smartmongoInstance) { if (smartmongoInstance) {
await smartmongoInstance.stop(); await smartmongoInstance.stop();
} else {
await testDb.mongoDb.dropDatabase();
} }
await testDb.close();
}); });
tap.start({ throwOnError: true }); tap.start({ throwOnError: true });

View File

@ -25,8 +25,20 @@ tap.test('should create a testinstance as database', async () => {
await testDb.init(); await testDb.init();
}); });
tap.skip.test('should connect to atlas', async (tools) => {
const databaseName = `test-smartdata-${smartunique.shortId()}`;
testDb = new smartdata.SmartdataDb({
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGO_URL'),
mongoDbName: databaseName,
});
await testDb.init();
});
@smartdata.Collection(() => testDb) @smartdata.Collection(() => testDb)
class House extends smartdata.SmartDataDbDoc<House, House> { class House extends smartdata.SmartDataDbDoc<House, House> {
@smartdata.unI()
public id: string = smartunique.shortId();
@smartdata.svDb() @smartdata.svDb()
public data = { public data = {
id: smartunique.shortId(), id: smartunique.shortId(),
@ -34,16 +46,17 @@ class House extends smartdata.SmartDataDbDoc<House, House> {
} }
} }
tap.skip.test('should watch a collection', async (toolsArg) => { tap.test('should watch a collection', async (toolsArg) => {
const done = toolsArg.defer(); const done = toolsArg.defer();
const watcher = await House.watch({}); const watcher = await House.watch({});
watcher.changeSubject.subscribe(houseArg => { watcher.changeSubject.subscribe(async houseArg => {
console.log('hey there, we observed a house'); console.log('hey there, we observed a house');
watcher.close(); await watcher.close();
done.resolve(); done.resolve();
}); });
const newHouse = new House(); const newHouse = new House();
await newHouse.save(); await newHouse.save();
console.log('saved a house');
await done.promise; await done.promise;
}) })
@ -51,12 +64,11 @@ tap.skip.test('should watch a collection', async (toolsArg) => {
// close the database connection // close the database connection
// ======================================= // =======================================
tap.test('close', async () => { tap.test('close', async () => {
await testDb.mongoDb.dropDatabase();
await testDb.close();
if (smartmongoInstance) { if (smartmongoInstance) {
await smartmongoInstance.stop(); await smartmongoInstance.stop();
} else {
await testDb.mongoDb.dropDatabase();
} }
await testDb.close();
}); });
tap.start({ throwOnError: true }); tap.start({ throwOnError: true });

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@pushrocks/smartdata', name: '@pushrocks/smartdata',
version: '4.0.28', version: '5.0.3',
description: 'do more with data' description: 'do more with data'
} }

View File

@ -180,10 +180,10 @@ export class SmartdataCollection<T> {
return result; return result;
} }
public async getCursor(filterObject: any): Promise<SmartdataDbCursor<any>> { public async getCursor(filterObjectArg: any, dbDocArg: typeof SmartDataDbDoc): Promise<SmartdataDbCursor<any>> {
await this.init(); await this.init();
const cursor = this.mongoDbCollection.find(filterObject); const cursor = this.mongoDbCollection.find(filterObjectArg);
return new SmartdataDbCursor(cursor); return new SmartdataDbCursor(cursor, dbDocArg);
} }
/** /**
@ -200,14 +200,18 @@ export class SmartdataCollection<T> {
/** /**
* watches the collection while applying a filter * watches the collection while applying a filter
*/ */
public async watch(filterObject: any): Promise<SmartdataDbWatcher> { public async watch(filterObject: any, smartdataDbDocArg: typeof SmartDataDbDoc): Promise<SmartdataDbWatcher> {
await this.init(); await this.init();
const changeStream = this.mongoDbCollection.watch([ const changeStream = this.mongoDbCollection.watch([
{ {
$match: filterObject $match: filterObject
} }
]); ], {
return new SmartdataDbWatcher(changeStream); fullDocument: 'updateLookup'
});
const smartdataWatcher = new SmartdataDbWatcher(changeStream, smartdataDbDocArg);
await smartdataWatcher.readyDeferred.promise;
return smartdataWatcher;
} }
/** /**

View File

@ -1,3 +1,4 @@
import { SmartDataDbDoc } from './smartdata.classes.doc.js';
import * as plugins from './smartdata.plugins.js'; import * as plugins from './smartdata.plugins.js';
/** /**
@ -8,26 +9,29 @@ export class SmartdataDbCursor<T = any> {
// INSTANCE // INSTANCE
public mongodbCursor: plugins.mongodb.FindCursor<T>; public mongodbCursor: plugins.mongodb.FindCursor<T>;
constructor(cursorArg: plugins.mongodb.FindCursor<T>) { private smartdataDbDoc: typeof SmartDataDbDoc;
constructor(cursorArg: plugins.mongodb.FindCursor<T>, dbDocArg: typeof SmartDataDbDoc) {
this.mongodbCursor = cursorArg; this.mongodbCursor = cursorArg;
this.smartdataDbDoc = dbDocArg;
} }
public async next(closeAtEnd = true) { public async next(closeAtEnd = true) {
const result = await this.mongodbCursor.next(); const result = this.smartdataDbDoc.createInstanceFromMongoDbNativeDoc(await this.mongodbCursor.next());
if (!result && closeAtEnd) { if (!result && closeAtEnd) {
await this.close(); await this.close();
} }
return result; return result;
} }
public async forEach(forEachFuncArg: (itemArg: T) => Promise<any>, closeCursorAtEnd = true) { public async forEach(forEachFuncArg: (itemArg: any) => Promise<any>, closeCursorAtEnd = true) {
let currentValue: T; let nextDocument: any;
do { do {
currentValue = await this.mongodbCursor.next(); nextDocument = await this.mongodbCursor.next();
if (currentValue) { if (nextDocument) {
await forEachFuncArg(currentValue); const nextClassInstance = this.smartdataDbDoc.createInstanceFromMongoDbNativeDoc(nextDocument);
await forEachFuncArg(nextClassInstance);
} }
} while (currentValue); } while (nextDocument);
if (closeCursorAtEnd) { if (closeCursorAtEnd) {
await this.close(); await this.close();
} }

View File

@ -137,8 +137,10 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
this: plugins.tsclass.typeFest.Class<T>, this: plugins.tsclass.typeFest.Class<T>,
filterArg: plugins.tsclass.typeFest.PartialDeep<T> filterArg: plugins.tsclass.typeFest.PartialDeep<T>
) { ) {
const cursor: SmartdataDbCursor<T> = await (this as any).collection.getCursor( const collection: SmartdataCollection<T> = (this as any).collection;
convertFilterForMongoDb(filterArg) const cursor: SmartdataDbCursor<T> = await collection.getCursor(
convertFilterForMongoDb(filterArg),
this as any as typeof SmartDataDbDoc
); );
return cursor; return cursor;
} }
@ -155,7 +157,8 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
) { ) {
const collection: SmartdataCollection<T> = (this as any).collection; const collection: SmartdataCollection<T> = (this as any).collection;
const watcher: SmartdataDbWatcher<T> = await collection.watch( const watcher: SmartdataDbWatcher<T> = await collection.watch(
convertFilterForMongoDb(filterArg) convertFilterForMongoDb(filterArg),
(this as any)
); );
return watcher; return watcher;
} }

View File

@ -1,3 +1,4 @@
import { SmartDataDbDoc } from './smartdata.classes.doc.js';
import * as plugins from './smartdata.plugins.js'; import * as plugins from './smartdata.plugins.js';
/** /**
@ -5,15 +6,20 @@ import * as plugins from './smartdata.plugins.js';
*/ */
export class SmartdataDbWatcher<T = any> { export class SmartdataDbWatcher<T = any> {
// STATIC // STATIC
public readyDeferred = plugins.smartpromise.defer();
// INSTANCE // INSTANCE
public changeStream: plugins.mongodb.ChangeStream<T>; private changeStream: plugins.mongodb.ChangeStream<T>;
public changeSubject = new plugins.smartrx.rxjs.Subject<T>(); public changeSubject = new plugins.smartrx.rxjs.Subject<T>();
constructor(changeStreamArg: plugins.mongodb.ChangeStream<T>) { constructor(changeStreamArg: plugins.mongodb.ChangeStream<T>, smartdataDbDocArg: typeof SmartDataDbDoc) {
this.changeStream = changeStreamArg; this.changeStream = changeStreamArg;
this.changeStream.on('change', (item: T) => { this.changeStream.on('change', async (item: T) => {
this.changeSubject.next(item); this.changeSubject.next(smartdataDbDocArg.createInstanceFromMongoDbNativeDoc(item) as any as T);
}) })
plugins.smartdelay.delayFor(0).then(() => {
this.readyDeferred.resolve();
});
} }
public async close() { public async close() {

View File

@ -7,9 +7,11 @@ export { tsclass };
import * as smartlog from '@pushrocks/smartlog'; import * as smartlog from '@pushrocks/smartlog';
import * as lodash from 'lodash'; import * as lodash from 'lodash';
import * as mongodb from 'mongodb'; import * as mongodb from 'mongodb';
import * as smartdelay from '@pushrocks/smartdelay'
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartq from '@pushrocks/smartpromise'; import * as smartq from '@pushrocks/smartpromise';
import * as smartrx from '@pushrocks/smartrx'; import * as smartrx from '@pushrocks/smartrx';
import * as smartstring from '@pushrocks/smartstring'; import * as smartstring from '@pushrocks/smartstring';
import * as smartunique from '@pushrocks/smartunique'; import * as smartunique from '@pushrocks/smartunique';
export { smartlog, lodash, smartq, smartrx, mongodb, smartstring, smartunique }; export { smartdelay, smartpromise, smartlog, lodash, smartq, smartrx, mongodb, smartstring, smartunique };