Store settings in lowdb
This commit is contained in:
41
backend/src/ConfigStorage.ts
Normal file
41
backend/src/ConfigStorage.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import * as FileAsync from 'lowdb/adapters/FileAsync'
|
||||
import * as lowdb from 'lowdb'
|
||||
import { backendEvents } from '../../events'
|
||||
import {
|
||||
makeStorageResponseEvent,
|
||||
storageClearEvent,
|
||||
storageLoadEvent,
|
||||
storageStoreEvent,
|
||||
makeStorageAcknoledgementEvent,
|
||||
} from '../../events/StorageEvents'
|
||||
|
||||
export default class ConfigStorage {
|
||||
private adapter: any
|
||||
constructor(file: string) {
|
||||
this.adapter = new FileAsync(file)
|
||||
}
|
||||
|
||||
public async init() {
|
||||
const database: lowdb.LoDashExplicitAsyncWrapper<any> = await lowdb(this.adapter)
|
||||
backendEvents.subscribe(storageStoreEvent, async (event) => {
|
||||
await database.set(event.store, event.data).write()
|
||||
backendEvents.emit(makeStorageAcknoledgementEvent(event.transactionId), undefined)
|
||||
})
|
||||
|
||||
backendEvents.subscribe(storageLoadEvent, async (event) => {
|
||||
const responseEvent = makeStorageResponseEvent(event.transactionId)
|
||||
try {
|
||||
const data = await database.get(event.store).value()
|
||||
backendEvents.emit(responseEvent, { data, transactionId: event.transactionId })
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
backendEvents.emit(responseEvent, { transactionId: event.transactionId })
|
||||
}
|
||||
})
|
||||
|
||||
backendEvents.subscribe(storageClearEvent, async (event) => {
|
||||
await database.drop()
|
||||
backendEvents.emit(makeStorageAcknoledgementEvent(event.transactionId), undefined)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
updateAvailable,
|
||||
} from '../../events'
|
||||
import { DataSource, MqttSource } from './DataSource'
|
||||
|
||||
import ConfigStorage from './ConfigStorage'
|
||||
import { UpdateInfo } from 'builder-util-runtime'
|
||||
|
||||
export class ConnectionManager {
|
||||
@@ -80,3 +80,6 @@ class UpdateNotifier {
|
||||
}
|
||||
|
||||
export const updateNotifier = new UpdateNotifier()
|
||||
|
||||
const configStorage = new ConfigStorage('blah.json')
|
||||
configStorage.init()
|
||||
|
||||
Reference in New Issue
Block a user