diff --git a/test/test.easystore.ts b/test/test.easystore.ts new file mode 100644 index 0000000..86f9754 --- /dev/null +++ b/test/test.easystore.ts @@ -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(); \ No newline at end of file diff --git a/ts/index.ts b/ts/index.ts index fb24f5d..274345d 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -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'; diff --git a/ts/smartdata.classes.db.ts b/ts/smartdata.classes.db.ts index ccf3f4f..687e6af 100644 --- a/ts/smartdata.classes.db.ts +++ b/ts/smartdata.classes.db.ts @@ -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 ---------------------------------------------- /** diff --git a/ts/smartdata.classes.easystore.ts b/ts/smartdata.classes.easystore.ts new file mode 100644 index 0000000..3a0ad7c --- /dev/null +++ b/ts/smartdata.classes.easystore.ts @@ -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 { + // instance + public smartdataDbRef: SmartdataDb; + public nameId: string; + + private easyStoreClass = (() => { + @Collection(() => this.smartdataDbRef) + class SmartdataEasyStore extends SmartDataDbDoc { + @unI() + public nameId: string; + + @svDb() + public data: Partial; + } + 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) { + 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(); + } +}