Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/privatenumber/reactive-json-file
💎 Reactively sync JSON mutations to disk
https://github.com/privatenumber/reactive-json-file
autosave file fs json reactive save vue
Last synced: 21 days ago
JSON representation
💎 Reactively sync JSON mutations to disk
- Host: GitHub
- URL: https://github.com/privatenumber/reactive-json-file
- Owner: privatenumber
- License: mit
- Created: 2020-08-19T03:02:03.000Z (over 4 years ago)
- Default Branch: develop
- Last Pushed: 2022-01-28T12:38:32.000Z (almost 3 years ago)
- Last Synced: 2024-10-19T01:11:29.750Z (about 1 month ago)
- Topics: autosave, file, fs, json, reactive, save, vue
- Language: TypeScript
- Homepage:
- Size: 609 KB
- Stars: 24
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Sync JSON mutations to disk using reactive magic!
Great for removing saving-to-disk concerns when updating a JSON object.
```js
import { openJson } from 'reactive-json-file'// Open a JSON file
const object = openJson('./data.json')// No need to save changes to disk, just update the object
object.name = 'John Doe'
```## :rocket: Install
```sh
npm i reactive-json-file
```## ⚙️ API
### openJson(filePath, options)
Open a file (eg. JSON file) and return the object#### Options
- `throttle` `` - Milliseconds to throttle saves by. Saves are already batched at the end of every event-loop, but this adds time-based throttling.
- `fs` `` ([fs](https://nodejs.org/api/fs.html)) - Pass in a custom file-system. Defaults to native Node.js fs
- `serialize`/`deserialize` `` - Functions to serialize/deserialize the object with. eg. to save output to YAML```js
import { openJson as openYaml } from 'reactive-json-file'
import yaml from 'js-yaml'const object = openYaml('./file.yaml', {
serialize: data => yaml.dump(data),
deserialize: string => yaml.load(string)
})object.message = 'YAML!'
```### closeJson(object)
Close a file to disable syncing## 🙋♀️ FAQ
### How does it work?
Arbitrary new changes are detected by using ES6 [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) behind the scenes.