Compare commits

..

16 Commits

Author SHA1 Message Date
da0c9873eb 5.0.6 2022-06-14 22:02:57 +02:00
2fcd3f1550 fix(core): update 2022-06-14 22:02:57 +02:00
f726cf4c5b 5.0.5 2022-06-05 17:19:12 +02:00
c198969fae fix(core): update 2022-06-05 17:19:12 +02:00
be1badeb23 5.0.4 2022-06-05 17:18:13 +02:00
fe065b966f fix(core): update 2022-06-05 17:18:13 +02:00
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
16 changed files with 1787 additions and 1183 deletions

2794
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +1,14 @@
{
"name": "@pushrocks/smartdata",
"version": "4.0.28",
"version": "5.0.6",
"private": false,
"description": "do more with data",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"type": "module",
"scripts": {
"test": "(tstest test/)",
"testLocal": "(npmdocker)",
"build": "(tsbuild --web)"
"test": "tstest test/",
"build": "tsbuild --web --allowimplicitany"
},
"repository": {
"type": "git",
@ -22,25 +21,27 @@
},
"homepage": "https://gitlab.com/pushrocks/smartdata#README",
"dependencies": {
"@pushrocks/lik": "^5.0.4",
"@pushrocks/lik": "^6.0.0",
"@pushrocks/smartdelay": "^2.0.13",
"@pushrocks/smartlog": "^2.0.44",
"@pushrocks/smartmongo": "^1.0.8",
"@pushrocks/smartmongo": "^2.0.7",
"@pushrocks/smartpromise": "^3.1.7",
"@pushrocks/smartrx": "^2.0.25",
"@pushrocks/smartstring": "^4.0.2",
"@pushrocks/smartunique": "^3.0.3",
"@tsclass/tsclass": "^4.0.2",
"@tsclass/tsclass": "^4.0.8",
"@types/lodash": "^4.14.182",
"@types/mongodb": "^4.0.7",
"lodash": "^4.17.21",
"mongodb": "^4.6.0"
"mongodb": "^4.7.0"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.61",
"@gitzone/tsbuild": "^2.1.63",
"@gitzone/tsrun": "^1.2.37",
"@gitzone/tstest": "^1.0.71",
"@pushrocks/qenv": "^4.0.10",
"@pushrocks/tapbundle": "^5.0.3",
"@types/node": "^17.0.33",
"@types/node": "^17.0.42",
"@types/shortid": "0.0.29"
},
"files": [

View File

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

View File

@ -55,6 +55,9 @@ class Car extends smartdata.SmartDataDbDoc<Car, Car> {
@smartdata.svDb()
public brand: string;
@smartdata.svDb()
public testBuffer = Buffer.from('hello');
@smartdata.svDb()
deepData = {
sodeep: 'yes',
@ -67,7 +70,7 @@ class Car extends smartdata.SmartDataDbDoc<Car, Car> {
}
}
tap.test('should save the car to the db', async () => {
tap.test('should save the car to the db', async (toolsArg) => {
const myCar = new Car('red', 'Volvo');
await myCar.save();
@ -75,6 +78,9 @@ tap.test('should save the car to the db', async () => {
await myCar2.save();
let counter = 0;
const gottenCarInstance = await Car.getInstance({});
console.log(gottenCarInstance.testBuffer);
process.memoryUsage();
do {
const myCar3 = new Car('red', 'Renault');
@ -92,7 +98,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 +119,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();
@ -205,12 +211,11 @@ tap.test('should use a cursor', async () => {
// close the database connection
// =======================================
tap.test('close', async () => {
await testDb.mongoDb.dropDatabase();
await testDb.close();
if (smartmongoInstance) {
await smartmongoInstance.stop();
} else {
await testDb.mongoDb.dropDatabase();
}
await testDb.close();
});
tap.start({ throwOnError: true });

View File

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

View File

@ -25,8 +25,20 @@ tap.test('should create a testinstance as database', async () => {
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)
class House extends smartdata.SmartDataDbDoc<House, House> {
@smartdata.unI()
public id: string = smartunique.shortId();
@smartdata.svDb()
public data = {
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 watcher = await House.watch({});
watcher.changeSubject.subscribe(houseArg => {
watcher.changeSubject.subscribe(async houseArg => {
console.log('hey there, we observed a house');
watcher.close();
await watcher.close();
done.resolve();
});
const newHouse = new House();
await newHouse.save();
console.log('saved a house');
await done.promise;
})
@ -51,12 +64,11 @@ tap.skip.test('should watch a collection', async (toolsArg) => {
// close the database connection
// =======================================
tap.test('close', async () => {
await testDb.mongoDb.dropDatabase();
await testDb.close();
if (smartmongoInstance) {
await smartmongoInstance.stop();
} else {
await testDb.mongoDb.dropDatabase();
}
await testDb.close();
});
tap.start({ throwOnError: true });

View File

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

View File

@ -4,4 +4,7 @@ export * from './smartdata.classes.doc.js';
export * from './smartdata.classes.easystore.js';
export * from './smartdata.classes.cursor.js';
export type { IMongoDescriptor } from './interfaces/index.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 }

View File

@ -1 +0,0 @@
export * from './mongodescriptor.js';

View File

@ -1,22 +0,0 @@
export interface IMongoDescriptor {
/**
* the URL to connect to
*/
mongoDbUrl: string;
/**
* the db to use for the project
*/
mongoDbName?: string;
/**
* a username to use to connect to the database
*/
mongoDbUser?: string;
/**
* an optional password that will be replace <PASSWORD> in the connection string
*/
mongoDbPass?: string;
}

View File

@ -180,10 +180,10 @@ export class SmartdataCollection<T> {
return result;
}
public async getCursor(filterObject: any): Promise<SmartdataDbCursor<any>> {
public async getCursor(filterObjectArg: any, dbDocArg: typeof SmartDataDbDoc): Promise<SmartdataDbCursor<any>> {
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,14 +200,18 @@ export class SmartdataCollection<T> {
/**
* 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();
const changeStream = this.mongoDbCollection.watch([
{
$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';
/**
@ -8,26 +9,29 @@ export class SmartdataDbCursor<T = any> {
// INSTANCE
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.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<any>, closeCursorAtEnd = true) {
let currentValue: T;
public async forEach(forEachFuncArg: (itemArg: any) => Promise<any>, 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();
}

View File

@ -5,7 +5,6 @@ import { SmartdataCollection } from './smartdata.classes.collection.js';
import { EasyStore } from './smartdata.classes.easystore.js';
import { logger } from './smartdata.logging.js';
import { IMongoDescriptor } from './interfaces/index.js';
/**
* interface - indicates the connection status of the db
@ -13,13 +12,13 @@ import { IMongoDescriptor } from './interfaces/index.js';
export type TConnectionStatus = 'initial' | 'disconnected' | 'connected' | 'failed';
export class SmartdataDb {
smartdataOptions: IMongoDescriptor;
smartdataOptions: plugins.tsclass.database.IMongoDescriptor;
mongoDbClient: plugins.mongodb.MongoClient;
mongoDb: plugins.mongodb.Db;
status: TConnectionStatus;
smartdataCollectionMap = new ObjectMap<SmartdataCollection<any>>();
constructor(smartdataOptions: IMongoDescriptor) {
constructor(smartdataOptions: plugins.tsclass.database.IMongoDescriptor) {
this.smartdataOptions = smartdataOptions;
this.status = 'initial';
}

View File

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

View File

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

View File

@ -7,9 +7,11 @@ 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 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 { smartlog, lodash, smartq, smartrx, mongodb, smartstring, smartunique };
export { smartdelay, smartpromise, smartlog, lodash, smartq, smartrx, mongodb, smartstring, smartunique };