Compare commits

..

6 Commits

Author SHA1 Message Date
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
13 changed files with 190 additions and 28 deletions

View File

@ -12,6 +12,10 @@ stages:
- release
- 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
# ====================
@ -36,6 +40,7 @@ auditProductionDependencies:
- npmci command npm audit --audit-level=high --only=prod --production
tags:
- docker
allow_failure: true
auditDevDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "@pushrocks/smartdata",
"version": "4.0.13",
"version": "4.0.16",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@pushrocks/smartdata",
"version": "4.0.13",
"version": "4.0.16",
"license": "MIT",
"dependencies": {
"@pushrocks/lik": "^4.0.20",

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartdata",
"version": "4.0.13",
"version": "4.0.16",
"private": false,
"description": "do more with data",
"main": "dist_ts/index.js",

View File

@ -90,7 +90,7 @@ class MyObject extends smartdata.DbDoc<MyObject /* ,[an optional interface to im
const localObject = new MyObject({
property1: 'hi',
property2: {
deep: 3
deep: 3,
},
});
await localObject.save(); // saves the object to the database
@ -102,9 +102,9 @@ const myInstance = await MyObject.getInstance({
property1: 'hi',
property2: {
deep: {
$gt: 2
} as any
}
$gt: 2,
} as any,
},
}); // 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 myCars2 = await Car.getInstances({
deepData: {
sodeep: 'yes'
sodeep: 'yes',
},
});
if (counter % 10 === 0) {
@ -207,7 +207,7 @@ tap.test('should store a new Truck', async () => {
console.log(myTruck2);
});
tap.test('should ', async () => {})
tap.test('should ', async () => {});
// =======================================
// close the database connection

View File

@ -80,7 +80,7 @@ class Car extends smartdata.SmartDataDbDoc<Car, Car> {
}
const createCarClass = (dbArg: smartdata.SmartdataDb) => {
smartdata.setDefaultManagerForDoc({db: dbArg}, Car);
smartdata.setDefaultManagerForDoc({ db: dbArg }, Car);
return Car;
};
@ -92,9 +92,9 @@ tap.test('should produce a car', async () => {
tap.test('should get a car', async () => {
const car = Car.getInstance({
color: 'red'
})
})
color: 'red',
});
});
// =======================================
// close the database connection

View File

@ -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';

View File

@ -42,19 +42,19 @@ export function Collection(dbArg: SmartdataDb | TDelayed<SmartdataDb>) {
}
export interface IManager {
db: SmartdataDb
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>) {
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() {
@ -62,7 +62,7 @@ export const setDefaultManagerForDoc = <T>(managerArg: IManager, dbDocArg: T):
if (!managerArg) {
dbArg = this.prototype.defaultManager.db;
} else if (managerArg['db']) {
dbArg = (managerArg as TManager).db
dbArg = (managerArg as TManager).db;
} else {
dbArg = (managerArg as TDelayed<TManager>)().db;
}
@ -75,7 +75,7 @@ export const setDefaultManagerForDoc = <T>(managerArg: IManager, dbDocArg: T):
//process.exit(0)
dbArg = this.defaultManager.db;
} else if (managerArg['db']) {
dbArg = (managerArg as TManager).db
dbArg = (managerArg as TManager).db;
} else {
dbArg = (managerArg as TDelayed<TManager>)().db;
}
@ -84,7 +84,7 @@ export const setDefaultManagerForDoc = <T>(managerArg: IManager, dbDocArg: T):
public static get manager() {
let manager: TManager;
if (managerArg['db']) {
manager = (managerArg as TManager);
manager = managerArg as TManager;
} else {
manager = (managerArg as TDelayed<TManager>)();
}
@ -93,7 +93,7 @@ export const setDefaultManagerForDoc = <T>(managerArg: IManager, dbDocArg: T):
public get manager() {
let manager: TManager;
if (managerArg['db']) {
manager = (managerArg as TManager);
manager = managerArg as TManager;
} else {
manager = (managerArg as TDelayed<TManager>)();
}

View File

@ -5,10 +5,7 @@ import { SmartdataDb } from './smartdata.classes.db';
export class CollectionFactory {
public collections: { [key: string]: SmartdataCollection<any> } = {};
public getCollection = (
nameArg: string,
dbArg: SmartdataDb
): SmartdataCollection<any> => {
public getCollection = (nameArg: string, dbArg: SmartdataDb): SmartdataCollection<any> => {
if (!this.collections[nameArg]) {
this.collections[nameArg] = (() => {
if (dbArg instanceof SmartdataDb) {

View File

@ -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 ----------------------------------------------
/**

View File

@ -100,9 +100,9 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
convertFilterArgument(`${keyPathArg}.${key}`, filterArg2[key]);
}
} else {
convertedFilter[keyPathArg] = filterArg2
}
convertedFilter[keyPathArg] = filterArg2;
}
};
for (const key of Object.keys(filterArg)) {
convertFilterArgument(key, filterArg[key]);
}

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();
}
}