npmextra/ts/npmextra.classes.keyvaluestore.ts

121 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

import * as plugins from './npmextra.plugins';
import * as paths from './npmextra.paths';
2016-08-28 12:51:04 +00:00
import { Task, TaskOnce } from '@pushrocks/taskbuffer';
2017-07-12 13:30:49 +00:00
export type TKeyValueStore = 'path' | 'gitProject' | 'custom';
2016-09-24 14:44:48 +00:00
2017-07-09 17:05:03 +00:00
/**
* kvStore is a simple key vlaue store to store data about projects between runs
*/
2016-09-24 14:44:48 +00:00
export class KeyValueStore {
dataObject: any;
deletedObject: any = {};
2017-07-12 15:13:29 +00:00
initialReadTask = new TaskOnce({
taskFunction: async () => {
this.dataObject = plugins.smartfile.fs.toObjectSync(this.filePath);
2017-07-12 15:13:29 +00:00
}
});
2017-07-12 15:13:29 +00:00
syncTask = new Task({
buffered: true,
bufferMax: 2,
execDelay: 500,
taskFunction: async () => {
this.dataObject = plugins.smartlodash.merge(
{},
plugins.smartfile.fs.toObjectSync(this.filePath),
this.dataObject
);
2017-07-12 15:22:22 +00:00
for (let key in this.deletedObject) {
delete this.dataObject[key];
2017-07-12 15:22:22 +00:00
}
this.deletedObject = {};
await plugins.smartfile.memory.toFs(JSON.stringify(this.dataObject), this.filePath);
2017-07-12 15:13:29 +00:00
},
name: 'syncTask'
});
type: TKeyValueStore; // the type of the kvStore
identity: string; // the identity of the kvStore
filePath: string; // the filePath of the kvStore
2017-07-12 15:13:29 +00:00
/**
* the constructor of keyvalue store
* @param typeArg
* @param customStringArg
*/
constructor(typeArg: TKeyValueStore, customStringArg: string) {
2017-03-18 15:23:47 +00:00
// set kvStoreType
this.type = typeArg;
this.identity = customStringArg;
this.initFilePath();
2017-03-18 15:23:47 +00:00
}
2016-09-24 19:49:53 +00:00
2017-03-18 15:23:47 +00:00
/**
2017-07-12 13:30:49 +00:00
* reads all keyValue pairs at once and returns them
2017-03-18 15:23:47 +00:00
*/
async readAll() {
await this.initialReadTask.trigger();
this.syncTask.trigger();
return this.dataObject;
2017-07-12 13:30:49 +00:00
}
2016-08-28 12:51:04 +00:00
2017-07-12 13:30:49 +00:00
/**
* reads a keyValueFile from disk
*/
async readKey(keyArg: string) {
let data = await this.readAll();
return data[keyArg];
2017-03-18 15:23:47 +00:00
}
2016-08-28 12:51:04 +00:00
2017-03-18 15:23:47 +00:00
/**
2017-08-16 16:25:45 +00:00
* writes a specific key to the keyValueStore
2017-03-18 15:23:47 +00:00
*/
async writeKey(keyArg: string, valueArg: any) {
let writeObject: any = {};
writeObject[keyArg] = valueArg;
this.writeAll(writeObject);
2017-08-16 16:25:45 +00:00
}
/**
* writes all keyValue pairs in the object argument
*/
async writeAll(keyValueObject) {
plugins.smartlodash.merge(this.dataObject, keyValueObject);
this.syncTask.trigger();
2017-03-18 15:23:47 +00:00
}
2016-08-28 12:51:04 +00:00
2017-03-18 15:23:47 +00:00
/**
* wipes a key value store from disk
*/
async wipe() {
2017-07-12 15:13:29 +00:00
for (let key in this.dataObject) {
this.deletedObject[key] = this.dataObject[key];
2017-07-12 15:13:29 +00:00
}
2017-03-18 15:23:47 +00:00
}
2016-09-24 19:49:53 +00:00
2017-03-18 15:23:47 +00:00
/**
* updates a value
*/
async update(keyObject) {}
2016-09-24 19:49:53 +00:00
2017-03-18 15:23:47 +00:00
/**
* computes the identity
*/
private initFilePath() {
2017-07-09 17:05:03 +00:00
// determine the right base directory
let baseDir: string;
2017-03-18 15:23:47 +00:00
if (this.type === 'custom') {
baseDir = paths.kvCustomDir;
2017-03-18 15:23:47 +00:00
} else if (this.type === 'gitProject') {
baseDir = paths.kvGitDir;
2017-03-18 15:23:47 +00:00
} else if (this.type === 'path') {
baseDir = paths.kvPathDir;
2016-09-24 19:49:53 +00:00
}
this.filePath = plugins.path.join(baseDir, this.identity + '.json');
plugins.smartfile.fs.ensureDirSync(paths.kvCustomDir);
plugins.smartfile.fs.ensureDirSync(paths.kvGitDir);
plugins.smartfile.fs.ensureDirSync(paths.kvPathDir);
plugins.smartfile.fs.ensureFileSync(this.filePath, '{}');
2017-03-18 15:23:47 +00:00
}
2016-09-16 20:28:38 +00:00
}