Simple and secure electron store with template-based data reconciliation
npm install electron-datastoreSimple and secure electron store with template-based data reconciliation, dot notation access, and encryption support.
``bash`
npm install electron-datastore
`typescript
import { Store } from "electron-datastore";
interface FrecencyData {
count: number;
lastAccessed: number;
}
interface FrecencyStore {
commands: Record
settings: {
maxItems: number;
};
}
// Create a store instance with a template
const store = new Store
name: "commands-frecency",
template: {
commands: {},
settings: {
maxItems: 100,
},
},
// Optional: custom working directory
cwd: "/custom/path",
// Optional: encryption key
encryptionKey: "your-secret-key",
// Optional: enable/disable dot notation access (default: true)
accessPropertiesByDotNotation: true,
// Optional: enable/disable auto reconciliation on load (default: true)
autoReconcile: true,
});
// Set data (will be reconciled with template)
store.set("commands.some-command", {
count: 1,
lastAccessed: Date.now(),
});
// Get data
const someCommand = store.get("commands.some-command");
// Set multiple values at once
store.setAll({
commands: {
"command-1": { count: 1, lastAccessed: Date.now() },
"command-2": { count: 2, lastAccessed: Date.now() },
},
});
// Reset a key to template value
store.delete("settings.maxItems");
// Delete a field entirely
store.deleteField("commands.some-command");
// Reset entire store to template
store.clear();
// Manually reconcile with template
store.reconcile();
// Access entire store
console.log(store.store);
// Get store file path
console.log(store.path);
`
#### Constructor Options
- name (required): Name of the store file (without extension)template
- (required): Template object that defines the structure and default valuescwd
- (optional): Custom working directory. Defaults to app.getPath('userData')encryptionKey
- (optional): Key for encrypting the store dataaccessPropertiesByDotNotation
- (optional): Whether to allow accessing nested properties using dot notation. Defaults to trueautoReconcile
- (optional): Whether to automatically reconcile data with template on load. Defaults to true
#### Methods
- get(key: K): T[K]: Get value for a keyget
-
>(path: P): PathValue >(path: P, value: PathValue >(path: P): void The store uses the provided template to: 1. Define the initial state: Get value using dot notationset
- : Set value for a keyset
- : Set value using dot notationsetAll(data: Partial
- : Set multiple values at oncedelete
- : Reset key to template valuedelete
- : Reset value to template value using dot notationclear(): void
- : Reset entire store to templatereconcile(): void
- : Manually reconcile the current data with the templatestore: T
- : Get the entire store datapath: string
- : Get the store file pathTemplate-based Reconciliation
2. Validate and reconcile data structure
3. Provide default values for missing fields
4. Reset deleted keys to their template valuesDot Notation Access
When enabled, you can access and modify nested properties using dot notation:
`typescript``
store.get("settings.theme.primary");
store.set("settings.theme.primary", "#000000");
Data can be optionally encrypted using AES-256-CBC encryption. When encryption is enabled, the data is stored in an encrypted format and decrypted only when accessed through the API.
MIT