import { app } from 'electron' ; import { JSONFileSync , LowSync } from 'lowdb' ; import path from 'path' ; import type { Storage } from '../shared/state/reducer.storage' ; import { defaultStorage } from '../shared/state/reducer.storage' ; const keys = Object . keys as < T > ( o : T ) => Extract < keyof T , string > [ ] ; const STORAGE_FILE = path . join ( app . getPath ( 'userData' ) , 'store.json' ) ; const adapter = new JSONFileSync < Storage > ( STORAGE_FILE ) ; const lowdb = new LowSync < Storage > ( adapter ) ; lowdb . read ( ) ; lowdb . data ||= defaultStorage ; lowdb . write ( ) ; /**
 * Keyboard shortcuts
 */ export const database = { get : < Key extends keyof Storage > ( key ) : Storage [ Key ] => { return database . getAll ( ) [ key ] ; } , set : < Key extends keyof Storage > ( key , value ) : void => { lowdb . read ( ) ; if ( lowdb . data === null ) { lowdb . data = defaultStorage ; } lowdb . data [ key ] = value ; lowdb . write ( ) ; } , getAll : ( ) : Storage => { lowdb . read ( ) ; if ( lowdb . data === null ) { return defaultStorage ; } for ( const key of keys ( lowdb . data ) ) { if ( typeof defaultStorage [ key ] === 'undefined' ) { delete lowdb . data [ key ] ; } } return { ... defaultStorage , ... lowdb . data } ; } , setAll : ( value ) : void => { lowdb . data = value ; lowdb . write ( ) ; } } ;