Compare commits

..

33 Commits

Author SHA1 Message Date
8301eb79a2 4.0.19 2021-11-12 16:36:25 +01:00
ad6366a294 fix(core): update 2021-11-12 16:36:25 +01:00
cb6c03ebfe 4.0.18 2021-11-12 16:23:27 +01:00
551916fe5c fix(core): update 2021-11-12 16:23:26 +01:00
9b3892c1e8 4.0.17 2021-10-18 14:37:17 +02:00
c1b1af9c5d fix(dbdoc manager access working again ): update 2021-10-18 14:37:17 +02:00
d3a3d5be9d 4.0.16 2021-10-16 23:33:22 +02:00
c9a734d879 fix(core): update 2021-10-16 23:33:21 +02:00
856e8e7d1f 4.0.15 2021-10-16 21:17:03 +02:00
7a4d557724 fix(core): update 2021-10-16 21:17:02 +02:00
7cbd0bd99b 4.0.14 2021-10-16 19:54:06 +02:00
e10f6585a5 fix(core): update 2021-10-16 19:54:05 +02:00
5c8dffdd9c 4.0.13 2021-10-02 15:03:35 +02:00
846996eeac fix(core): update 2021-10-02 15:03:35 +02:00
668df09ba0 4.0.12 2021-09-19 17:05:09 +02:00
03656f4ca0 fix(core): update 2021-09-19 17:05:08 +02:00
c4c612f3a9 4.0.11 2021-09-18 00:38:21 +02:00
e357f7581c fix(core): update 2021-09-18 00:38:20 +02:00
9697b1e48b 4.0.10 2021-09-17 22:34:15 +02:00
aeb35705d4 fix(core): update 2021-09-17 22:34:15 +02:00
236c8c6551 4.0.9 2021-09-16 19:49:55 +02:00
1f28db15e7 fix(core): update 2021-09-16 19:49:55 +02:00
86d600e287 4.0.8 2021-06-09 15:38:15 +02:00
bb81530dac fix(core): update 2021-06-09 15:38:14 +02:00
b9f9b36b87 4.0.7 2021-06-09 15:37:50 +02:00
df2fadfa01 fix(core): update 2021-06-09 15:37:49 +02:00
8b2beb3485 4.0.6 2021-06-09 14:55:56 +02:00
144a620f43 fix(core): update 2021-06-09 14:55:55 +02:00
c241247845 4.0.5 2021-06-09 14:32:14 +02:00
81e39d09e4 4.0.4 2021-06-09 14:10:08 +02:00
8e51b518b1 fix(core): update 2021-06-09 14:10:08 +02:00
8308d8d03b 4.0.3 2021-06-09 13:40:23 +02:00
97365ddf29 fix(core): update 2021-06-09 13:40:23 +02:00
15 changed files with 526 additions and 99 deletions

View File

@ -12,6 +12,10 @@ stages:
- release - release
- metadata - metadata
before_script:
- apt-get update && apt-get install -y libcurl3 libssl-dev openssl libssl1.0.0 mongodb
- npm install -g @shipzone/npmci
# ==================== # ====================
# security stage # security stage
# ==================== # ====================
@ -36,6 +40,7 @@ auditProductionDependencies:
- npmci command npm audit --audit-level=high --only=prod --production - npmci command npm audit --audit-level=high --only=prod --production
tags: tags:
- docker - docker
allow_failure: true
auditDevDependencies: auditDevDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci image: registry.gitlab.com/hosttoday/ht-docker-node:npmci

24
.vscode/launch.json vendored
View File

@ -2,28 +2,10 @@
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "current file", "command": "npm test",
"type": "node", "name": "Run npm test",
"request": "launch", "request": "launch",
"args": [ "type": "node-terminal"
"${relativeFile}"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": "test.ts",
"type": "node",
"request": "launch",
"args": [
"test/test.ts"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
} }
] ]
} }

26
package-lock.json generated
View File

@ -1,18 +1,19 @@
{ {
"name": "@pushrocks/smartdata", "name": "@pushrocks/smartdata",
"version": "4.0.2", "version": "4.0.19",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@pushrocks/smartdata", "name": "@pushrocks/smartdata",
"version": "4.0.2", "version": "4.0.19",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@pushrocks/lik": "^4.0.20", "@pushrocks/lik": "^4.0.20",
"@pushrocks/smartlog": "^2.0.39", "@pushrocks/smartlog": "^2.0.39",
"@pushrocks/smartpromise": "^3.1.5", "@pushrocks/smartpromise": "^3.1.5",
"@pushrocks/smartstring": "^3.0.24", "@pushrocks/smartstring": "^3.0.24",
"@pushrocks/smartunique": "^3.0.3",
"@tsclass/tsclass": "^3.0.33", "@tsclass/tsclass": "^3.0.33",
"@types/lodash": "^4.14.169", "@types/lodash": "^4.14.169",
"@types/mongodb": "^3.6.12", "@types/mongodb": "^3.6.12",
@ -24,7 +25,6 @@
"@gitzone/tsbuild": "^2.1.25", "@gitzone/tsbuild": "^2.1.25",
"@gitzone/tstest": "^1.0.54", "@gitzone/tstest": "^1.0.54",
"@pushrocks/qenv": "^4.0.10", "@pushrocks/qenv": "^4.0.10",
"@pushrocks/smartunique": "^3.0.3",
"@pushrocks/tapbundle": "^3.2.14", "@pushrocks/tapbundle": "^3.2.14",
"@types/mongodb-memory-server": "^2.3.0", "@types/mongodb-memory-server": "^2.3.0",
"@types/node": "^15.3.0", "@types/node": "^15.3.0",
@ -2622,7 +2622,6 @@
"version": "3.0.3", "version": "3.0.3",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartunique/-/smartunique-3.0.3.tgz", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartunique/-/smartunique-3.0.3.tgz",
"integrity": "sha512-f+c3s2WzzjASoRHyYTLU0kHDVWREg4sZVdi5L42bTA3CTUWNrcGUC62h4wP4U4BiPl3bopTr3LPhClZHJ738oA==", "integrity": "sha512-f+c3s2WzzjASoRHyYTLU0kHDVWREg4sZVdi5L42bTA3CTUWNrcGUC62h4wP4U4BiPl3bopTr3LPhClZHJ738oA==",
"dev": true,
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@types/shortid": "0.0.29", "@types/shortid": "0.0.29",
@ -3195,7 +3194,6 @@
"version": "0.0.29", "version": "0.0.29",
"resolved": "https://verdaccio.lossless.one/@types%2fshortid/-/shortid-0.0.29.tgz", "resolved": "https://verdaccio.lossless.one/@types%2fshortid/-/shortid-0.0.29.tgz",
"integrity": "sha1-gJPuBBam4r8qpjOBCRFLP7/6Dps=", "integrity": "sha1-gJPuBBam4r8qpjOBCRFLP7/6Dps=",
"dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/@types/through2": { "node_modules/@types/through2": {
@ -3239,7 +3237,6 @@
"version": "7.0.4", "version": "7.0.4",
"resolved": "https://verdaccio.lossless.one/@types%2fuuid/-/uuid-7.0.4.tgz", "resolved": "https://verdaccio.lossless.one/@types%2fuuid/-/uuid-7.0.4.tgz",
"integrity": "sha512-WGZCqBZZ0mXN2RxvLHL6/7RCu+OWs28jgQMP04LWfpyJlQUMTR6YU9CNJAKDgbw+EV/u687INXuLUc7FuML/4g==", "integrity": "sha512-WGZCqBZZ0mXN2RxvLHL6/7RCu+OWs28jgQMP04LWfpyJlQUMTR6YU9CNJAKDgbw+EV/u687INXuLUc7FuML/4g==",
"dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/@types/which": { "node_modules/@types/which": {
@ -9442,7 +9439,6 @@
"version": "2.1.11", "version": "2.1.11",
"resolved": "https://verdaccio.lossless.one/nanoid/-/nanoid-2.1.11.tgz", "resolved": "https://verdaccio.lossless.one/nanoid/-/nanoid-2.1.11.tgz",
"integrity": "sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA==", "integrity": "sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA==",
"dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/nanomatch": { "node_modules/nanomatch": {
@ -12714,7 +12710,6 @@
"version": "2.2.16", "version": "2.2.16",
"resolved": "https://verdaccio.lossless.one/shortid/-/shortid-2.2.16.tgz", "resolved": "https://verdaccio.lossless.one/shortid/-/shortid-2.2.16.tgz",
"integrity": "sha512-Ugt+GIZqvGXCIItnsL+lvFJOiN7RYqlGy7QE41O3YC1xbNSeDGIRO7xg2JJXIAj1cAGnOeC1r7/T9pgrtQbv4g==", "integrity": "sha512-Ugt+GIZqvGXCIItnsL+lvFJOiN7RYqlGy7QE41O3YC1xbNSeDGIRO7xg2JJXIAj1cAGnOeC1r7/T9pgrtQbv4g==",
"dev": true,
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"nanoid": "^2.1.0" "nanoid": "^2.1.0"
@ -14621,7 +14616,6 @@
"version": "7.0.3", "version": "7.0.3",
"resolved": "https://verdaccio.lossless.one/uuid/-/uuid-7.0.3.tgz", "resolved": "https://verdaccio.lossless.one/uuid/-/uuid-7.0.3.tgz",
"integrity": "sha512-DPSke0pXhTZgoF/d+WSt2QaKMCFSfx7QegxEWT+JOuHF5aWrKEn0G+ztjuJg/gG8/ItK+rbPCD/yNv8yyih6Cg==", "integrity": "sha512-DPSke0pXhTZgoF/d+WSt2QaKMCFSfx7QegxEWT+JOuHF5aWrKEn0G+ztjuJg/gG8/ItK+rbPCD/yNv8yyih6Cg==",
"dev": true,
"license": "MIT", "license": "MIT",
"bin": { "bin": {
"uuid": "dist/bin/uuid" "uuid": "dist/bin/uuid"
@ -17058,7 +17052,6 @@
"version": "3.0.3", "version": "3.0.3",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartunique/-/smartunique-3.0.3.tgz", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartunique/-/smartunique-3.0.3.tgz",
"integrity": "sha512-f+c3s2WzzjASoRHyYTLU0kHDVWREg4sZVdi5L42bTA3CTUWNrcGUC62h4wP4U4BiPl3bopTr3LPhClZHJ738oA==", "integrity": "sha512-f+c3s2WzzjASoRHyYTLU0kHDVWREg4sZVdi5L42bTA3CTUWNrcGUC62h4wP4U4BiPl3bopTr3LPhClZHJ738oA==",
"dev": true,
"requires": { "requires": {
"@types/shortid": "0.0.29", "@types/shortid": "0.0.29",
"@types/uuid": "^7.0.0", "@types/uuid": "^7.0.0",
@ -17527,8 +17520,7 @@
"@types/shortid": { "@types/shortid": {
"version": "0.0.29", "version": "0.0.29",
"resolved": "https://verdaccio.lossless.one/@types%2fshortid/-/shortid-0.0.29.tgz", "resolved": "https://verdaccio.lossless.one/@types%2fshortid/-/shortid-0.0.29.tgz",
"integrity": "sha1-gJPuBBam4r8qpjOBCRFLP7/6Dps=", "integrity": "sha1-gJPuBBam4r8qpjOBCRFLP7/6Dps="
"dev": true
}, },
"@types/through2": { "@types/through2": {
"version": "2.0.36", "version": "2.0.36",
@ -17565,8 +17557,7 @@
"@types/uuid": { "@types/uuid": {
"version": "7.0.4", "version": "7.0.4",
"resolved": "https://verdaccio.lossless.one/@types%2fuuid/-/uuid-7.0.4.tgz", "resolved": "https://verdaccio.lossless.one/@types%2fuuid/-/uuid-7.0.4.tgz",
"integrity": "sha512-WGZCqBZZ0mXN2RxvLHL6/7RCu+OWs28jgQMP04LWfpyJlQUMTR6YU9CNJAKDgbw+EV/u687INXuLUc7FuML/4g==", "integrity": "sha512-WGZCqBZZ0mXN2RxvLHL6/7RCu+OWs28jgQMP04LWfpyJlQUMTR6YU9CNJAKDgbw+EV/u687INXuLUc7FuML/4g=="
"dev": true
}, },
"@types/which": { "@types/which": {
"version": "1.3.2", "version": "1.3.2",
@ -22144,8 +22135,7 @@
"nanoid": { "nanoid": {
"version": "2.1.11", "version": "2.1.11",
"resolved": "https://verdaccio.lossless.one/nanoid/-/nanoid-2.1.11.tgz", "resolved": "https://verdaccio.lossless.one/nanoid/-/nanoid-2.1.11.tgz",
"integrity": "sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA==", "integrity": "sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA=="
"dev": true
}, },
"nanomatch": { "nanomatch": {
"version": "1.2.13", "version": "1.2.13",
@ -24584,7 +24574,6 @@
"version": "2.2.16", "version": "2.2.16",
"resolved": "https://verdaccio.lossless.one/shortid/-/shortid-2.2.16.tgz", "resolved": "https://verdaccio.lossless.one/shortid/-/shortid-2.2.16.tgz",
"integrity": "sha512-Ugt+GIZqvGXCIItnsL+lvFJOiN7RYqlGy7QE41O3YC1xbNSeDGIRO7xg2JJXIAj1cAGnOeC1r7/T9pgrtQbv4g==", "integrity": "sha512-Ugt+GIZqvGXCIItnsL+lvFJOiN7RYqlGy7QE41O3YC1xbNSeDGIRO7xg2JJXIAj1cAGnOeC1r7/T9pgrtQbv4g==",
"dev": true,
"requires": { "requires": {
"nanoid": "^2.1.0" "nanoid": "^2.1.0"
} }
@ -25976,8 +25965,7 @@
"uuid": { "uuid": {
"version": "7.0.3", "version": "7.0.3",
"resolved": "https://verdaccio.lossless.one/uuid/-/uuid-7.0.3.tgz", "resolved": "https://verdaccio.lossless.one/uuid/-/uuid-7.0.3.tgz",
"integrity": "sha512-DPSke0pXhTZgoF/d+WSt2QaKMCFSfx7QegxEWT+JOuHF5aWrKEn0G+ztjuJg/gG8/ItK+rbPCD/yNv8yyih6Cg==", "integrity": "sha512-DPSke0pXhTZgoF/d+WSt2QaKMCFSfx7QegxEWT+JOuHF5aWrKEn0G+ztjuJg/gG8/ItK+rbPCD/yNv8yyih6Cg=="
"dev": true
}, },
"v8-compile-cache": { "v8-compile-cache": {
"version": "2.3.0", "version": "2.3.0",

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartdata", "name": "@pushrocks/smartdata",
"version": "4.0.2", "version": "4.0.19",
"private": false, "private": false,
"description": "do more with data", "description": "do more with data",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
@ -25,6 +25,7 @@
"@pushrocks/smartlog": "^2.0.39", "@pushrocks/smartlog": "^2.0.39",
"@pushrocks/smartpromise": "^3.1.5", "@pushrocks/smartpromise": "^3.1.5",
"@pushrocks/smartstring": "^3.0.24", "@pushrocks/smartstring": "^3.0.24",
"@pushrocks/smartunique": "^3.0.3",
"@tsclass/tsclass": "^3.0.33", "@tsclass/tsclass": "^3.0.33",
"@types/lodash": "^4.14.169", "@types/lodash": "^4.14.169",
"@types/mongodb": "^3.6.12", "@types/mongodb": "^3.6.12",
@ -36,7 +37,6 @@
"@gitzone/tsbuild": "^2.1.25", "@gitzone/tsbuild": "^2.1.25",
"@gitzone/tstest": "^1.0.54", "@gitzone/tstest": "^1.0.54",
"@pushrocks/qenv": "^4.0.10", "@pushrocks/qenv": "^4.0.10",
"@pushrocks/smartunique": "^3.0.3",
"@pushrocks/tapbundle": "^3.2.14", "@pushrocks/tapbundle": "^3.2.14",
"@types/mongodb-memory-server": "^2.3.0", "@types/mongodb-memory-server": "^2.3.0",
"@types/node": "^15.3.0", "@types/node": "^15.3.0",

View File

@ -90,7 +90,7 @@ class MyObject extends smartdata.DbDoc<MyObject /* ,[an optional interface to im
const localObject = new MyObject({ const localObject = new MyObject({
property1: 'hi', property1: 'hi',
property2: { property2: {
deep: 3 deep: 3,
}, },
}); });
await localObject.save(); // saves the object to the database await localObject.save(); // saves the object to the database
@ -102,9 +102,9 @@ const myInstance = await MyObject.getInstance({
property1: 'hi', property1: 'hi',
property2: { property2: {
deep: { deep: {
$gt: 2 $gt: 2,
} as any } as any,
} },
}); // outputs a new instance of MyObject with the values from db assigned }); // outputs a new instance of MyObject with the values from db assigned
``` ```

59
test/test.easystore.ts Normal file
View File

@ -0,0 +1,59 @@
import { tap, expect } from '@pushrocks/tapbundle';
import { Qenv } from '@pushrocks/qenv';
const testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit/');
console.log(process.memoryUsage());
// the tested module
import * as smartdata from '../ts/index';
import * as mongoPlugin from 'mongodb-memory-server';
import { smartunique } from '../ts/smartdata.plugins';
// =======================================
// Connecting to the database server
// =======================================
let testDb: smartdata.SmartdataDb;
let smartdataOptions: smartdata.IMongoDescriptor;
let mongod: mongoPlugin.MongoMemoryServer;
tap.test('should create a testinstance as database', async () => {
mongod = new mongoPlugin.MongoMemoryServer({});
console.log('created mongod instance');
await mongod._startUpInstance().catch((err) => {
console.log(err);
});
console.log('mongod started');
smartdataOptions = {
mongoDbName: await mongod.getDbName(),
mongoDbPass: '',
mongoDbUrl: await mongod.getUri(),
};
console.log(smartdataOptions);
testDb = new smartdata.SmartdataDb(smartdataOptions);
await testDb.init();
});
let easyStore: smartdata.EasyStore<{
key1: string;
key2: string;
}>;
tap.test('should create an easystore', async () => {
easyStore = await testDb.createEasyStore('hellothere');
await easyStore.writeKey('key1', 'hello');
const retrievedKey = await easyStore.readKey('key1');
expect(retrievedKey).to.equal('hello');
});
tap.test('close', async () => {
testDb.close();
mongod.stop();
setTimeout(() => {
process.exit(0);
}, 1000);
});
tap.start();

View File

@ -134,7 +134,7 @@ tap.test('expect to get instance of Car with deep match', async () => {
const timeStart = Date.now(); const timeStart = Date.now();
const myCars2 = await Car.getInstances({ const myCars2 = await Car.getInstances({
deepData: { deepData: {
sodeep: 'yes' sodeep: 'yes',
}, },
}); });
if (counter % 10 === 0) { if (counter % 10 === 0) {
@ -200,19 +200,25 @@ class Truck extends smartdata.SmartDataDbDoc<Car, Car> {
tap.test('should store a new Truck', async () => { tap.test('should store a new Truck', async () => {
const truck = new Truck('blue', 'MAN'); const truck = new Truck('blue', 'MAN');
await truck.save(); await truck.save();
const myTruck = await Truck.getInstance({ color: 'blue' });
myTruck.id = 'foo';
await myTruck.save();
const myTruck2 = await Truck.getInstance({ color: 'blue' }); const myTruck2 = await Truck.getInstance({ color: 'blue' });
console.log(myTruck2); myTruck2.color = 'red';
await myTruck2.save();
const myTruck3 = await Truck.getInstance({ color: 'blue' });
console.log(myTruck3);
}); });
tap.test('should ', async () => {}) tap.test('should use a cursor', async () => {
const cursor = await Truck.getCursor({});
cursor.forEach(async truckArg => {
console.log(truckArg.id);
});
});
// ======================================= // =======================================
// close the database connection // close the database connection
// ======================================= // =======================================
tap.test('should close the database connection', async (tools) => { tap.test('should drop the db and close the database connection', async (tools) => {
await testDb.mongoDb.dropDatabase();
await testDb.close(); await testDb.close();
try { try {
await mongod.stop(); await mongod.stop();

110
test/test.typescript.ts Normal file
View File

@ -0,0 +1,110 @@
import { tap, expect } from '@pushrocks/tapbundle';
import { Qenv } from '@pushrocks/qenv';
const testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit/');
console.log(process.memoryUsage());
// the tested module
import * as smartdata from '../ts/index';
import * as mongoPlugin from 'mongodb-memory-server';
import { smartunique } from '../ts/smartdata.plugins';
// =======================================
// Connecting to the database server
// =======================================
let testDb: smartdata.SmartdataDb;
let smartdataOptions: smartdata.IMongoDescriptor;
let mongod: mongoPlugin.MongoMemoryServer;
const totalCars = 2000;
tap.skip.test('should create a testinstance as database', async () => {
mongod = new mongoPlugin.MongoMemoryServer({});
console.log('created mongod instance');
await mongod._startUpInstance().catch((err) => {
console.log(err);
});
console.log('mongod started');
smartdataOptions = {
mongoDbName: await mongod.getDbName(),
mongoDbPass: '',
mongoDbUrl: await mongod.getUri(),
};
console.log(smartdataOptions);
testDb = new smartdata.SmartdataDb(smartdataOptions);
});
tap.test('should connect to atlas', async (tools) => {
const databaseName = `test-smartdata-${smartunique.shortId()}`;
testDb = new smartdata.SmartdataDb({
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGO_URL'),
mongoDbName: databaseName,
});
});
tap.test('should establish a connection to mongod', async () => {
await testDb.init();
});
// =======================================
// The actual tests
// =======================================
// ------
// Collections
// ------
@smartdata.Manager()
class Car extends smartdata.SmartDataDbDoc<Car, Car> {
@smartdata.unI()
public index: string = smartunique.shortId();
@smartdata.svDb()
public color: string;
@smartdata.svDb()
public brand: string;
@smartdata.svDb()
deepData = {
sodeep: 'yes',
};
constructor(colorArg: string, brandArg: string) {
super();
this.color = colorArg;
this.brand = brandArg;
}
}
const createCarClass = (dbArg: smartdata.SmartdataDb) => {
smartdata.setDefaultManagerForDoc({ db: dbArg }, Car);
return Car;
};
tap.test('should produce a car', async () => {
const CarClass = createCarClass(testDb);
const carInstance = new CarClass('red', 'Mercedes');
await carInstance.save();
});
tap.test('should get a car', async () => {
const car = Car.getInstance({
color: 'red',
});
});
// =======================================
// close the database connection
// =======================================
tap.test('should close the database connection', async (tools) => {
await testDb.mongoDb.dropDatabase();
await testDb.close();
try {
await mongod.stop();
} catch (e) {}
});
tap.start({ throwOnError: true });

View File

@ -1,5 +1,6 @@
export * from './smartdata.classes.db'; export * from './smartdata.classes.db';
export * from './smartdata.classes.collection'; export * from './smartdata.classes.collection';
export * from './smartdata.classes.doc'; export * from './smartdata.classes.doc';
export * from './smartdata.classes.easystore';
export { IMongoDescriptor } from './interfaces'; export { IMongoDescriptor } from './interfaces';

View File

@ -1,5 +1,6 @@
import * as plugins from './smartdata.plugins'; import * as plugins from './smartdata.plugins';
import { SmartdataDb } from './smartdata.classes.db'; import { SmartdataDb } from './smartdata.classes.db';
import { SmartdataDbCursor } from './smartdata.classes.cursor';
import { SmartDataDbDoc } from './smartdata.classes.doc'; import { SmartDataDbDoc } from './smartdata.classes.doc';
import { CollectionFactory } from './smartdata.classes.collectionfactory'; import { CollectionFactory } from './smartdata.classes.collectionfactory';
@ -22,19 +23,86 @@ const collectionFactory = new CollectionFactory();
* This is a decorator that will tell the decorated class what dbTable to use * This is a decorator that will tell the decorated class what dbTable to use
* @param dbArg * @param dbArg
*/ */
export function Collection<TManager>(dbArg: SmartdataDb | TDelayed<SmartdataDb>, managerArg?: TDelayed<TManager>) { export function Collection(dbArg: SmartdataDb | TDelayed<SmartdataDb>) {
return function classDecorator<T extends { new (...args: any[]): {} }>(constructor: T) { return function classDecorator<T extends { new (...args: any[]): {} }>(constructor: T) {
return class extends constructor { return class extends constructor {
public static get collection() { public static get collection() {
if (!(dbArg instanceof SmartdataDb)) {
dbArg = dbArg();
}
return collectionFactory.getCollection(constructor.name, dbArg); return collectionFactory.getCollection(constructor.name, dbArg);
} }
public get collection() { public get collection() {
if (!(dbArg instanceof SmartdataDb)) {
dbArg = dbArg();
}
return collectionFactory.getCollection(constructor.name, dbArg); return collectionFactory.getCollection(constructor.name, dbArg);
} }
public get manager() { };
if (managerArg) { };
return managerArg(); }
export interface IManager {
db: SmartdataDb;
}
export const setDefaultManagerForDoc = <T>(managerArg: IManager, dbDocArg: T): T => {
(dbDocArg as any).prototype.defaultManager = managerArg;
return dbDocArg;
};
/**
* This is a decorator that will tell the decorated class what dbTable to use
* @param dbArg
*/
export function Manager<TManager extends IManager>(managerArg?: TManager | TDelayed<TManager>) {
return function classDecorator<T extends { new (...args: any[]): any }>(constructor: T) {
return class extends constructor {
public static get collection() {
let dbArg: SmartdataDb;
if (!managerArg) {
dbArg = this.prototype.defaultManager.db;
} else if (managerArg['db']) {
dbArg = (managerArg as TManager).db;
} else {
dbArg = (managerArg as TDelayed<TManager>)().db;
} }
return collectionFactory.getCollection(constructor.name, dbArg);
}
public get collection() {
let dbArg: SmartdataDb;
if (!managerArg) {
//console.log(this.defaultManager.db);
//process.exit(0)
dbArg = this.defaultManager.db;
} else if (managerArg['db']) {
dbArg = (managerArg as TManager).db;
} else {
dbArg = (managerArg as TDelayed<TManager>)().db;
}
return collectionFactory.getCollection(constructor.name, dbArg);
}
public static get manager() {
let manager: TManager;
if (!managerArg) {
manager = this.prototype.defaultManager
} else if (managerArg['db']) {
manager = managerArg as TManager;
} else {
manager = (managerArg as TDelayed<TManager>)();
}
return manager;
}
public get manager() {
let manager: TManager;
if (!managerArg) {
manager = this.defaultManager
} else if (managerArg['db']) {
manager = managerArg as TManager;
} else {
manager = (managerArg as TDelayed<TManager>)();
}
return manager;
} }
}; };
}; };
@ -103,9 +171,28 @@ export class SmartdataCollection<T> {
/** /**
* finds an object in the DbCollection * finds an object in the DbCollection
*/ */
public async find(filterObject: any): Promise<any> { public async findOne(filterObject: any): Promise<any> {
await this.init(); await this.init();
const result = await this.mongoDbCollection.find(filterObject).toArray(); const cursor = this.mongoDbCollection.find(filterObject);
const result = await cursor.next();
cursor.close();
return result;
}
public async getCursor(filterObject: any): Promise<SmartdataDbCursor<any>> {
await this.init();
const cursor = this.mongoDbCollection.find(filterObject);
return new SmartdataDbCursor(cursor);
}
/**
* finds an object in the DbCollection
*/
public async findAll(filterObject: any): Promise<any[]> {
await this.init();
const cursor = this.mongoDbCollection.find(filterObject);
const result = await cursor.toArray();
cursor.close();
return result; return result;
} }

View File

@ -5,18 +5,12 @@ import { SmartdataDb } from './smartdata.classes.db';
export class CollectionFactory { export class CollectionFactory {
public collections: { [key: string]: SmartdataCollection<any> } = {}; public collections: { [key: string]: SmartdataCollection<any> } = {};
public getCollection = ( public getCollection = (nameArg: string, dbArg: SmartdataDb): SmartdataCollection<any> => {
nameArg: string,
dbArg: SmartdataDb | (() => SmartdataDb)
): SmartdataCollection<any> => {
if (!this.collections[nameArg]) { if (!this.collections[nameArg]) {
this.collections[nameArg] = (() => { this.collections[nameArg] = (() => {
if (dbArg instanceof SmartdataDb) { if (dbArg instanceof SmartdataDb) {
// tslint:disable-next-line: no-string-literal // tslint:disable-next-line: no-string-literal
return new SmartdataCollection(nameArg, dbArg); return new SmartdataCollection(nameArg, dbArg);
} else {
dbArg = dbArg();
return new SmartdataCollection(nameArg, dbArg);
} }
})(); })();
} }

View File

@ -0,0 +1,39 @@
import * as plugins from './smartdata.plugins';
/**
* a wrapper for the native mongodb cursor. Exposes better
*/
export class SmartdataDbCursor<T = any> {
// STATIC
// INSTANCE
public mongodbCursor: plugins.mongodb.Cursor<T>;
constructor(cursorArg: plugins.mongodb.Cursor<T>) {
this.mongodbCursor = cursorArg
};
public async next(closeAtEnd = true) {
const result = 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;
do {
currentValue = await this.mongodbCursor.next();
if (currentValue) {
await forEachFuncArg(currentValue);
}
} while (currentValue);
if (closeCursorAtEnd) {
await this.close();
}
}
public async close() {
await this.mongodbCursor.close();
}
}

View File

@ -2,6 +2,7 @@ import * as plugins from './smartdata.plugins';
import { ObjectMap } from '@pushrocks/lik'; import { ObjectMap } from '@pushrocks/lik';
import { SmartdataCollection } from './smartdata.classes.collection'; import { SmartdataCollection } from './smartdata.classes.collection';
import { EasyStore } from './smartdata.classes.easystore';
import { logger } from './smartdata.logging'; import { logger } from './smartdata.logging';
import { IMongoDescriptor } from './interfaces'; import { IMongoDescriptor } from './interfaces';
@ -23,6 +24,12 @@ export class SmartdataDb {
this.status = 'initial'; this.status = 'initial';
} }
// easystore
public async createEasyStore(nameIdArg: string) {
const easyStore = new EasyStore(nameIdArg, this);
return easyStore;
}
// basic connection stuff ---------------------------------------------- // basic connection stuff ----------------------------------------------
/** /**

View File

@ -3,7 +3,8 @@ import * as plugins from './smartdata.plugins';
import { ObjectMap } from '@pushrocks/lik'; import { ObjectMap } from '@pushrocks/lik';
import { SmartdataDb } from './smartdata.classes.db'; import { SmartdataDb } from './smartdata.classes.db';
import { SmartdataCollection } from './smartdata.classes.collection'; import { SmartdataDbCursor } from './smartdata.classes.cursor';
import { IManager, SmartdataCollection } from './smartdata.classes.collection';
export type TDocCreation = 'db' | 'new' | 'mixed'; export type TDocCreation = 'db' | 'new' | 'mixed';
@ -41,12 +42,40 @@ export function unI() {
}; };
} }
export class SmartDataDbDoc<T extends TImplements, TImplements> { export const convertFilterForMongoDb = (filterArg: { [key: string]: any }) => {
const convertedFilter: { [key: string]: any } = {};
const convertFilterArgument = (keyPathArg2: string, filterArg2: any) => {
if (typeof filterArg2 === 'object') {
for (const key of Object.keys(filterArg2)) {
if (key.startsWith('$')) {
convertedFilter[keyPathArg2] = filterArg2;
return;
} else if (key.includes('.')) {
throw new Error('keys cannot contain dots');
}
}
for (const key of Object.keys(filterArg2)) {
convertFilterArgument(`${keyPathArg2}.${key}`, filterArg2[key]);
}
} else {
convertedFilter[keyPathArg2] = filterArg2;
}
};
for (const key of Object.keys(filterArg)) {
convertFilterArgument(key, filterArg[key]);
}
return convertedFilter;
};
export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends IManager = any> {
/** /**
* the collection object an Doc belongs to * the collection object an Doc belongs to
*/ */
public static collection: SmartdataCollection<any>; public static collection: SmartdataCollection<any>;
public collection: SmartdataCollection<any>; public collection: SmartdataCollection<any>;
public static defaultManager;
public static manager;
public manager: TManager;
/** /**
* how the Doc in memory was created, may prove useful later. * how the Doc in memory was created, may prove useful later.
@ -78,54 +107,81 @@ export class SmartDataDbDoc<T extends TImplements, TImplements> {
*/ */
constructor() {} constructor() {}
public static createInstanceFromMongoDbNativeDoc<T>(
this: plugins.tsclass.typeFest.Class<T>,
mongoDbNativeDocArg: any
): T {
const newInstance = new this();
(newInstance as any).creationStatus = 'db';
for (const key of Object.keys(mongoDbNativeDocArg)) {
newInstance[key] = mongoDbNativeDocArg[key];
}
return newInstance;
}
/**
* gets all instances as array
* @param this
* @param filterArg
* @returns
*/
public static async getInstances<T>( public static async getInstances<T>(
this: plugins.tsclass.typeFest.Class<T>, this: plugins.tsclass.typeFest.Class<T>,
filterArg: plugins.tsclass.typeFest.PartialDeep<T> filterArg: plugins.tsclass.typeFest.PartialDeep<T>
): Promise<T[]> { ): Promise<T[]> {
const convertedFilter: any = {}; const foundDocs = await (this as any).collection.findAll(convertFilterForMongoDb(filterArg));
const convertFilterArgument = (keyPathArg: string, filterArg2: any) => {
if (typeof filterArg2 === 'object') {
for (const key of Object.keys(filterArg2)) {
if (key.startsWith('$')) {
convertedFilter[keyPathArg] = filterArg2;
return;
} else if (key.includes('.')) {
throw new Error('keys cannot contain dots');
}
}
for (const key of Object.keys(filterArg2)) {
convertFilterArgument(`${keyPathArg}.${key}`, filterArg2[key]);
}
} else {
convertedFilter[keyPathArg] = filterArg2
}
}
for (const key of Object.keys(filterArg)) {
convertFilterArgument(key, filterArg[key]);
}
const foundDocs = await (this as any).collection.find(convertedFilter);
const returnArray = []; const returnArray = [];
for (const item of foundDocs) { for (const foundDoc of foundDocs) {
const newInstance = new this(); const newInstance: T = (this as any).createInstanceFromMongoDbNativeDoc(foundDoc);
(newInstance as any).creationStatus = 'db';
for (const key of Object.keys(item)) {
newInstance[key] = item[key];
}
returnArray.push(newInstance); returnArray.push(newInstance);
} }
return returnArray; return returnArray;
} }
/**
* gets the first matching instance
* @param this
* @param filterArg
* @returns
*/
public static async getInstance<T>( public static async getInstance<T>(
this: plugins.tsclass.typeFest.Class<T>, this: plugins.tsclass.typeFest.Class<T>,
filterArg: plugins.tsclass.typeFest.PartialDeep<T> filterArg: plugins.tsclass.typeFest.PartialDeep<T>
): Promise<T> { ): Promise<T> {
const result = await (this as any).getInstances(filterArg); const foundDoc = await (this as any).collection.findOne(convertFilterForMongoDb(filterArg));
if (result && result.length > 0) { if (foundDoc) {
return result[0]; const newInstance: T = (this as any).createInstanceFromMongoDbNativeDoc(foundDoc);
return newInstance;
} else {
return null;
} }
} }
/**
* get cursor
* @returns
*/
public static async getCursor<T>(
this: plugins.tsclass.typeFest.Class<T>,
filterArg: plugins.tsclass.typeFest.PartialDeep<T>
) {
const cursor: SmartdataDbCursor<T> = await (this as any).collection.getCursor(convertFilterForMongoDb(filterArg));
return cursor;
}
/**
* run a function for all instances
* @returns
*/
public static async forEach<T>(
this: plugins.tsclass.typeFest.Class<T>,
filterArg: plugins.tsclass.typeFest.PartialDeep<T>,
forEachFunction: (itemArg: T) => Promise<any>
) {
const cursor: SmartdataDbCursor<T> = await (this as any).getCursor(filterArg);
await cursor.forEach(forEachFunction)
}
/** /**
* saves this instance but not any connected items * saves this instance but not any connected items
* may lead to data inconsistencies, but is faster * may lead to data inconsistencies, but is faster

View File

@ -0,0 +1,93 @@
import * as plugins from './smartdata.plugins';
import { Collection } from './smartdata.classes.collection';
import { SmartdataDb } from './smartdata.classes.db';
import { SmartDataDbDoc, svDb, unI } from './smartdata.classes.doc';
/**
* EasyStore allows the storage of easy objects. It also allows easy sharing of the object between different instances
*/
export class EasyStore<T> {
// instance
public smartdataDbRef: SmartdataDb;
public nameId: string;
private easyStoreClass = (() => {
@Collection(() => this.smartdataDbRef)
class SmartdataEasyStore extends SmartDataDbDoc<SmartdataEasyStore, SmartdataEasyStore> {
@unI()
public nameId: string;
@svDb()
public data: Partial<T>;
}
return SmartdataEasyStore;
})();
constructor(nameIdArg: string, smnartdataDbRefArg: SmartdataDb) {
this.smartdataDbRef = smnartdataDbRefArg;
this.nameId = nameIdArg;
}
private async getEasyStore() {
let easyStore = await this.easyStoreClass.getInstance({
nameId: this.nameId,
});
if (!easyStore) {
easyStore = new this.easyStoreClass();
easyStore.nameId = this.nameId;
easyStore.data = {};
await easyStore.save();
}
return easyStore;
}
/**
* reads all keyValue pairs at once and returns them
*/
public async readAll() {
const easyStore = await this.getEasyStore();
return easyStore.data;
}
/**
* reads a keyValueFile from disk
*/
public async readKey(keyArg: keyof T) {
const easyStore = await this.getEasyStore();
return easyStore.data[keyArg];
}
/**
* writes a specific key to the keyValueStore
*/
public async writeKey(keyArg: keyof T, valueArg: any) {
const easyStore = await this.getEasyStore();
easyStore.data[keyArg] = valueArg;
await easyStore.save();
}
public async deleteKey(keyArg: keyof T) {
const easyStore = await this.getEasyStore();
delete easyStore.data[keyArg];
await easyStore.save();
}
/**
* writes all keyValue pairs in the object argument
*/
public async writeAll(keyValueObject: Partial<T>) {
const easyStore = await this.getEasyStore();
easyStore.data = { ...easyStore.data, ...keyValueObject };
await easyStore.save();
}
/**
* wipes a key value store from disk
*/
public async wipe() {
const easyStore = await this.getEasyStore();
easyStore.data = {};
await easyStore.save();
}
}