-
-
Notifications
You must be signed in to change notification settings - Fork 399
Expand file tree
/
Copy pathConfigStorage.ts
More file actions
55 lines (47 loc) · 1.35 KB
/
Copy pathConfigStorage.ts
File metadata and controls
55 lines (47 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import FileAsync from 'lowdb/adapters/FileAsync'
import fs from 'fs-extra'
import lowdb from 'lowdb'
import path from 'path'
import { Rpc } from '../../events/EventSystem/Rpc'
import { storageClearEvent, storageLoadEvent, storageStoreEvent } from '../../events/StorageEvents'
export default class ConfigStorage {
private file: string
private database: any
private rpc: Rpc
constructor(file: string, rpc: Rpc) {
this.file = file
this.rpc = rpc
}
private async getDb() {
const pathInfo = path.parse(this.file)
// Ensure that Settings dir exists
await fs.mkdirp(pathInfo.dir)
const adapter = new FileAsync(this.file)
if (!this.database) {
this.database = await lowdb(adapter)
}
return this.database
}
public async init() {
this.rpc.on(storageStoreEvent, async event => {
const db = await this.getDb()
await db.set(event.store, event.data).write()
return
})
this.rpc.on(storageLoadEvent, async event => {
const db = await this.getDb()
const data = await db.get(event.store).value()
return {
data,
store: event.store,
}
})
this.rpc.on(storageClearEvent, async event => {
const db = await this.getDb()
const keys = await db.keys().value()
for (const key of keys) {
await db.unset(key).write()
}
})
}
}