2022-05-16 22:33:44 +00:00
|
|
|
import * as plugins from './smartdata.plugins.js';
|
|
|
|
import { Collection } from './smartdata.classes.collection.js';
|
|
|
|
import { SmartdataDb } from './smartdata.classes.db.js';
|
|
|
|
import { SmartDataDbDoc, svDb, unI } from './smartdata.classes.doc.js';
|
2021-10-16 17:54:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2023-02-06 10:43:11 +00:00
|
|
|
@svDb()
|
|
|
|
public ephermal: {
|
|
|
|
activated: boolean;
|
|
|
|
timeout: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
@svDb()
|
|
|
|
lastEdit: number;
|
|
|
|
|
2021-10-16 17:54:05 +00:00
|
|
|
@svDb()
|
|
|
|
public data: Partial<T>;
|
|
|
|
}
|
|
|
|
return SmartdataEasyStore;
|
|
|
|
})();
|
|
|
|
|
|
|
|
constructor(nameIdArg: string, smnartdataDbRefArg: SmartdataDb) {
|
|
|
|
this.smartdataDbRef = smnartdataDbRefArg;
|
|
|
|
this.nameId = nameIdArg;
|
|
|
|
}
|
|
|
|
|
2021-10-16 21:33:21 +00:00
|
|
|
private async getEasyStore() {
|
2021-10-16 17:54:05 +00:00
|
|
|
let easyStore = await this.easyStoreClass.getInstance({
|
2021-10-16 19:17:02 +00:00
|
|
|
nameId: this.nameId,
|
2021-10-16 17:54:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2023-02-06 10:43:11 +00:00
|
|
|
|
|
|
|
public async cleanUpEphermal() {
|
2023-06-24 23:27:09 +00:00
|
|
|
while (
|
|
|
|
(await this.smartdataDbRef.statusConnectedDeferred.promise) &&
|
|
|
|
this.smartdataDbRef.status === 'connected'
|
|
|
|
) {}
|
2023-02-06 10:43:11 +00:00
|
|
|
}
|
2021-10-16 17:54:05 +00:00
|
|
|
}
|