kvStore now working as intended
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import * as plugins from './npmextra.plugins'
|
||||
import * as paths from './npmextra.paths'
|
||||
|
||||
import { Task } from 'taskbuffer'
|
||||
import { Task, TaskOnce } from 'taskbuffer'
|
||||
|
||||
export type TKeyValueStore = 'path' | 'gitProject' | 'custom'
|
||||
|
||||
@ -10,40 +10,51 @@ export type TKeyValueStore = 'path' | 'gitProject' | 'custom'
|
||||
*/
|
||||
export class KeyValueStore {
|
||||
dataObject: any
|
||||
firstReadDeferred = plugins.smartq.defer()
|
||||
syncTask: plugins.taskbuffer.Task
|
||||
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
|
||||
)
|
||||
await plugins.smartfile.memory.toFs(
|
||||
JSON.stringify(this.dataObject),
|
||||
this.filePath
|
||||
)
|
||||
},
|
||||
name: 'syncTask'
|
||||
})
|
||||
type: TKeyValueStore // the type of the kvStore
|
||||
identity: string // the identity of the kvStore
|
||||
filePath: string // the filePath of the kvStore
|
||||
constructor (typeArg: TKeyValueStore, customStringArg?: string) {
|
||||
|
||||
/**
|
||||
* the constructor of keyvalue store
|
||||
* @param typeArg
|
||||
* @param customStringArg
|
||||
*/
|
||||
constructor(typeArg: TKeyValueStore, customStringArg: string) {
|
||||
// set kvStoreType
|
||||
this.type = typeArg
|
||||
this.identity = customStringArg
|
||||
this.initFilePath()
|
||||
|
||||
// set up the sync Task
|
||||
this.syncTask = new Task({
|
||||
buffered: true,
|
||||
bufferMax: 2,
|
||||
execDelay: 500,
|
||||
taskFunction: async () => {
|
||||
this.dataObject = plugins.smartlodash.merge(
|
||||
{},
|
||||
plugins.smartfile.fs.toObjectSync(this.filePath,
|
||||
this.dataObject
|
||||
)
|
||||
await plugins.smartfile.memory.toFs(JSON.stringify(this.dataObject), this.filePath)
|
||||
},
|
||||
name: 'syncTask'
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* reads all keyValue pairs at once and returns them
|
||||
*/
|
||||
async readAll () {
|
||||
await this.initialReadTask.trigger()
|
||||
this.syncTask.trigger()
|
||||
await this.firstReadDeferred.promise
|
||||
return this.dataObject
|
||||
}
|
||||
|
||||
@ -51,7 +62,7 @@ export class KeyValueStore {
|
||||
* reads a keyValueFile from disk
|
||||
*/
|
||||
async read (keyArg: string) {
|
||||
let data = this.readAll()
|
||||
let data = await this.readAll()
|
||||
return data[keyArg]
|
||||
}
|
||||
|
||||
@ -67,13 +78,15 @@ export class KeyValueStore {
|
||||
* wipes a key value store from disk
|
||||
*/
|
||||
async wipe () {
|
||||
this.dataObject.
|
||||
for (let key in this.dataObject) {
|
||||
this.deletedObject[key] = this.dataObject[key]
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* updates a value
|
||||
*/
|
||||
async update () {
|
||||
async update (keyObject) {
|
||||
|
||||
}
|
||||
|
||||
@ -92,6 +105,9 @@ export class KeyValueStore {
|
||||
baseDir = paths.kvPathDir
|
||||
}
|
||||
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, '{}')
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ export class Npmextra {
|
||||
} else {
|
||||
npmextraToolOptions = {}
|
||||
}
|
||||
let mergedOptions = plugins.lodash.merge({}, defaultOptionsArg, npmextraToolOptions)
|
||||
let mergedOptions = plugins.smartlodash.merge({}, defaultOptionsArg, npmextraToolOptions)
|
||||
return mergedOptions
|
||||
}
|
||||
|
||||
|
@ -8,10 +8,13 @@ export let packageDir = plugins.path.join(__dirname,'../')
|
||||
// keyValueStore specific
|
||||
// ----------------------
|
||||
|
||||
export let home = plugins.smartpath.get.home()
|
||||
|
||||
/**
|
||||
* keyValue base path
|
||||
*/
|
||||
export let kvBase = '~/.npmextra/kv'
|
||||
export let kvBase = plugins.path.join(home,'.npmextra/kv')
|
||||
|
||||
|
||||
/**
|
||||
* the base directory for custom string based key value store
|
||||
|
@ -3,6 +3,7 @@ import * as beautylog from 'beautylog'
|
||||
import * as path from 'path'
|
||||
import * as smartfile from 'smartfile'
|
||||
import smartlodash from 'smartlodash'
|
||||
import * as smartpath from 'smartpath'
|
||||
import * as smartq from 'smartq'
|
||||
import * as taskbuffer from 'taskbuffer'
|
||||
|
||||
@ -10,6 +11,7 @@ export {
|
||||
beautylog,
|
||||
path,
|
||||
smartfile,
|
||||
smartpath,
|
||||
smartq,
|
||||
smartlodash,
|
||||
taskbuffer
|
||||
|
Reference in New Issue
Block a user