https://github.com/robojones/save-on-change
Automatically save objects into JSON files
https://github.com/robojones/save-on-change
automatically autosave file json nodejs save
Last synced: 30 days ago
JSON representation
Automatically save objects into JSON files
- Host: GitHub
- URL: https://github.com/robojones/save-on-change
- Owner: robojones
- License: mit
- Created: 2017-01-18T15:55:16.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-02-04T11:56:39.000Z (about 8 years ago)
- Last Synced: 2025-03-17T07:43:03.321Z (about 1 month ago)
- Topics: automatically, autosave, file, json, nodejs, save
- Language: JavaScript
- Homepage:
- Size: 6.84 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# save-on-change
This module allows you to read and write JSON files.
It overwrites the JSON file as soon as you change the object in your Node.js application.
When you are restarting your Node.js app, the object will automatically be loaded from the JSON file.## Example
```javascript
const autoSave = require('save-on-change')// load a JSON file
const a = autoSave('config.json')// change anything
a.hello = true
a.foo = 'baz'
```
The file `config.json` should now contain:
```json
{"hello": true, "foo": "baz"}
```## Installation
```
npm i save-on-change --save
```## Function: autoSave(filename[, onSave])
- __filename__ \ filename of the JSON file
- __onSave__ \ function that gets called every time the object gets saved__returns__ object representing the parsed contents of the file (Parser: JSON.parse())
If the JSON file does not exist, an empty object is returned.
Once you modify the object, the file will be created or overwritten.