Files
smartconfig/ts/npmextra.classes.keyvaluestore.ts

127 lines
3.0 KiB
TypeScript
Raw Permalink Normal View History

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