filestore-json
Version:
Easily sync JSON objects of any shape with the filesystem.
86 lines (65 loc) • 2.75 kB
Markdown
//github.com/JoshMerlino/filestore-json/actions/workflows/build.yml/badge.svg)](https://github.com/JoshMerlino/filestore-json/actions/workflows/build.yml)
* [](https://github.com/JoshMerlino/filestore-json/actions/workflows/code-style-analysis.yml)
* [](https://github.com/JoshMerlino/filestore-json/actions/workflows/test-ci.yml)



Easily sync JSON objects of any shape with the filesystem.
* Read & write to JSON files on a storage device without the need to interact with the filesystem.
* Persistant data between process restarts.
* Ability to determine age (ms since last write).
* Cache JSON responses from network requests.
* Automaticly updates internal value when JSON file is modified.
* Strong type internal value with type annotations.
# Examples
## Creating a store
```ts
import JSONStore from "filestore-json";
import path from "path";
// Initialize the store.
const store = JSONStore.from(path.resolve("path/to/your/store.json"));
// With types
type Type = Record<string, any>;
const store = JSONStore.from<Type>(path.resolve("path/to/your/store.json"));
```
```ts
// Read value of store.
console.log(store.value); // -> {}
// Update store value.
store.value = { myObject: false };
// Read new value of store.
console.log(store.value); // -> { myObject: false }
```
```ts
// Create object with store defaults
const defaults = { defaultValue: true };
// Initialize store.
const store = JSONStore.from(path.resolve("path/to/your/store.json"), defaults);
// Read value of store.
console.log(store.value); // -> { defaultValue: true }
// Update store value.
store.value = { myObject: false };
// Read new value of store.
console.log(store.value); // -> { myObject: false, defaultValue: true }
```
```ts
// Update store value.
store.value = { myObject: false };
// Read new value of store.
console.log(store.value); // -> { myObject: false }
// Clear value
store.clear();
// Read value of store.
console.log(store.value); // -> {}
```
```ts
// Read age of store.
console.log(store.age); // -> 0
```
* [![Build](https: