Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
7cbd0bd99b | |||
e10f6585a5 | |||
5c8dffdd9c | |||
846996eeac | |||
668df09ba0 | |||
03656f4ca0 | |||
c4c612f3a9 | |||
e357f7581c |
26
package-lock.json
generated
26
package-lock.json
generated
@ -1,18 +1,19 @@
|
||||
{
|
||||
"name": "@pushrocks/smartdata",
|
||||
"version": "4.0.10",
|
||||
"version": "4.0.14",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@pushrocks/smartdata",
|
||||
"version": "4.0.10",
|
||||
"version": "4.0.14",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@pushrocks/lik": "^4.0.20",
|
||||
"@pushrocks/smartlog": "^2.0.39",
|
||||
"@pushrocks/smartpromise": "^3.1.5",
|
||||
"@pushrocks/smartstring": "^3.0.24",
|
||||
"@pushrocks/smartunique": "^3.0.3",
|
||||
"@tsclass/tsclass": "^3.0.33",
|
||||
"@types/lodash": "^4.14.169",
|
||||
"@types/mongodb": "^3.6.12",
|
||||
@ -24,7 +25,6 @@
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tstest": "^1.0.54",
|
||||
"@pushrocks/qenv": "^4.0.10",
|
||||
"@pushrocks/smartunique": "^3.0.3",
|
||||
"@pushrocks/tapbundle": "^3.2.14",
|
||||
"@types/mongodb-memory-server": "^2.3.0",
|
||||
"@types/node": "^15.3.0",
|
||||
@ -2622,7 +2622,6 @@
|
||||
"version": "3.0.3",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartunique/-/smartunique-3.0.3.tgz",
|
||||
"integrity": "sha512-f+c3s2WzzjASoRHyYTLU0kHDVWREg4sZVdi5L42bTA3CTUWNrcGUC62h4wP4U4BiPl3bopTr3LPhClZHJ738oA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@types/shortid": "0.0.29",
|
||||
@ -3195,7 +3194,6 @@
|
||||
"version": "0.0.29",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fshortid/-/shortid-0.0.29.tgz",
|
||||
"integrity": "sha1-gJPuBBam4r8qpjOBCRFLP7/6Dps=",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/through2": {
|
||||
@ -3239,7 +3237,6 @@
|
||||
"version": "7.0.4",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fuuid/-/uuid-7.0.4.tgz",
|
||||
"integrity": "sha512-WGZCqBZZ0mXN2RxvLHL6/7RCu+OWs28jgQMP04LWfpyJlQUMTR6YU9CNJAKDgbw+EV/u687INXuLUc7FuML/4g==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/which": {
|
||||
@ -9442,7 +9439,6 @@
|
||||
"version": "2.1.11",
|
||||
"resolved": "https://verdaccio.lossless.one/nanoid/-/nanoid-2.1.11.tgz",
|
||||
"integrity": "sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/nanomatch": {
|
||||
@ -12714,7 +12710,6 @@
|
||||
"version": "2.2.16",
|
||||
"resolved": "https://verdaccio.lossless.one/shortid/-/shortid-2.2.16.tgz",
|
||||
"integrity": "sha512-Ugt+GIZqvGXCIItnsL+lvFJOiN7RYqlGy7QE41O3YC1xbNSeDGIRO7xg2JJXIAj1cAGnOeC1r7/T9pgrtQbv4g==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"nanoid": "^2.1.0"
|
||||
@ -14621,7 +14616,6 @@
|
||||
"version": "7.0.3",
|
||||
"resolved": "https://verdaccio.lossless.one/uuid/-/uuid-7.0.3.tgz",
|
||||
"integrity": "sha512-DPSke0pXhTZgoF/d+WSt2QaKMCFSfx7QegxEWT+JOuHF5aWrKEn0G+ztjuJg/gG8/ItK+rbPCD/yNv8yyih6Cg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"uuid": "dist/bin/uuid"
|
||||
@ -17058,7 +17052,6 @@
|
||||
"version": "3.0.3",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartunique/-/smartunique-3.0.3.tgz",
|
||||
"integrity": "sha512-f+c3s2WzzjASoRHyYTLU0kHDVWREg4sZVdi5L42bTA3CTUWNrcGUC62h4wP4U4BiPl3bopTr3LPhClZHJ738oA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@types/shortid": "0.0.29",
|
||||
"@types/uuid": "^7.0.0",
|
||||
@ -17527,8 +17520,7 @@
|
||||
"@types/shortid": {
|
||||
"version": "0.0.29",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fshortid/-/shortid-0.0.29.tgz",
|
||||
"integrity": "sha1-gJPuBBam4r8qpjOBCRFLP7/6Dps=",
|
||||
"dev": true
|
||||
"integrity": "sha1-gJPuBBam4r8qpjOBCRFLP7/6Dps="
|
||||
},
|
||||
"@types/through2": {
|
||||
"version": "2.0.36",
|
||||
@ -17565,8 +17557,7 @@
|
||||
"@types/uuid": {
|
||||
"version": "7.0.4",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fuuid/-/uuid-7.0.4.tgz",
|
||||
"integrity": "sha512-WGZCqBZZ0mXN2RxvLHL6/7RCu+OWs28jgQMP04LWfpyJlQUMTR6YU9CNJAKDgbw+EV/u687INXuLUc7FuML/4g==",
|
||||
"dev": true
|
||||
"integrity": "sha512-WGZCqBZZ0mXN2RxvLHL6/7RCu+OWs28jgQMP04LWfpyJlQUMTR6YU9CNJAKDgbw+EV/u687INXuLUc7FuML/4g=="
|
||||
},
|
||||
"@types/which": {
|
||||
"version": "1.3.2",
|
||||
@ -22144,8 +22135,7 @@
|
||||
"nanoid": {
|
||||
"version": "2.1.11",
|
||||
"resolved": "https://verdaccio.lossless.one/nanoid/-/nanoid-2.1.11.tgz",
|
||||
"integrity": "sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA==",
|
||||
"dev": true
|
||||
"integrity": "sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA=="
|
||||
},
|
||||
"nanomatch": {
|
||||
"version": "1.2.13",
|
||||
@ -24584,7 +24574,6 @@
|
||||
"version": "2.2.16",
|
||||
"resolved": "https://verdaccio.lossless.one/shortid/-/shortid-2.2.16.tgz",
|
||||
"integrity": "sha512-Ugt+GIZqvGXCIItnsL+lvFJOiN7RYqlGy7QE41O3YC1xbNSeDGIRO7xg2JJXIAj1cAGnOeC1r7/T9pgrtQbv4g==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"nanoid": "^2.1.0"
|
||||
}
|
||||
@ -25976,8 +25965,7 @@
|
||||
"uuid": {
|
||||
"version": "7.0.3",
|
||||
"resolved": "https://verdaccio.lossless.one/uuid/-/uuid-7.0.3.tgz",
|
||||
"integrity": "sha512-DPSke0pXhTZgoF/d+WSt2QaKMCFSfx7QegxEWT+JOuHF5aWrKEn0G+ztjuJg/gG8/ItK+rbPCD/yNv8yyih6Cg==",
|
||||
"dev": true
|
||||
"integrity": "sha512-DPSke0pXhTZgoF/d+WSt2QaKMCFSfx7QegxEWT+JOuHF5aWrKEn0G+ztjuJg/gG8/ItK+rbPCD/yNv8yyih6Cg=="
|
||||
},
|
||||
"v8-compile-cache": {
|
||||
"version": "2.3.0",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartdata",
|
||||
"version": "4.0.10",
|
||||
"version": "4.0.14",
|
||||
"private": false,
|
||||
"description": "do more with data",
|
||||
"main": "dist_ts/index.js",
|
||||
@ -25,6 +25,7 @@
|
||||
"@pushrocks/smartlog": "^2.0.39",
|
||||
"@pushrocks/smartpromise": "^3.1.5",
|
||||
"@pushrocks/smartstring": "^3.0.24",
|
||||
"@pushrocks/smartunique": "^3.0.3",
|
||||
"@tsclass/tsclass": "^3.0.33",
|
||||
"@types/lodash": "^4.14.169",
|
||||
"@types/mongodb": "^3.6.12",
|
||||
@ -36,7 +37,6 @@
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tstest": "^1.0.54",
|
||||
"@pushrocks/qenv": "^4.0.10",
|
||||
"@pushrocks/smartunique": "^3.0.3",
|
||||
"@pushrocks/tapbundle": "^3.2.14",
|
||||
"@types/mongodb-memory-server": "^2.3.0",
|
||||
"@types/node": "^15.3.0",
|
||||
|
60
test/test.easystore.ts
Normal file
60
test/test.easystore.ts
Normal file
@ -0,0 +1,60 @@
|
||||
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();
|
@ -1,5 +1,6 @@
|
||||
export * from './smartdata.classes.db';
|
||||
export * from './smartdata.classes.collection';
|
||||
export * from './smartdata.classes.doc';
|
||||
export * from './smartdata.classes.easystore';
|
||||
|
||||
export { IMongoDescriptor } from './interfaces';
|
||||
|
@ -45,8 +45,9 @@ export interface IManager {
|
||||
db: SmartdataDb
|
||||
}
|
||||
|
||||
export const setDefaultManagerForDoc = (managerArg: IManager, dbDoc: any) => {
|
||||
dbDoc.prototype.defaultManager = managerArg;
|
||||
export const setDefaultManagerForDoc = <T>(managerArg: IManager, dbDocArg: T): T => {
|
||||
(dbDocArg as any).prototype.defaultManager = managerArg;
|
||||
return dbDocArg;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2,6 +2,7 @@ import * as plugins from './smartdata.plugins';
|
||||
import { ObjectMap } from '@pushrocks/lik';
|
||||
|
||||
import { SmartdataCollection } from './smartdata.classes.collection';
|
||||
import { EasyStore } from './smartdata.classes.easystore';
|
||||
|
||||
import { logger } from './smartdata.logging';
|
||||
import { IMongoDescriptor } from './interfaces';
|
||||
@ -23,6 +24,12 @@ export class SmartdataDb {
|
||||
this.status = 'initial';
|
||||
}
|
||||
|
||||
// easystore
|
||||
public async createEasyStore(nameIdArg: string) {
|
||||
const easyStore = new EasyStore(nameIdArg, this);
|
||||
return easyStore;
|
||||
}
|
||||
|
||||
// basic connection stuff ----------------------------------------------
|
||||
|
||||
/**
|
||||
|
93
ts/smartdata.classes.easystore.ts
Normal file
93
ts/smartdata.classes.easystore.ts
Normal 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;
|
||||
}
|
||||
|
||||
public 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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user