Skip to content

privatenumber/reactive-json-file

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

26 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

reactive-json-file

Sync JSON mutations to disk using reactive magic!

Great for removing saving-to-disk concerns when updating a JSON object.

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'

๐Ÿš€ Install

npm i reactive-json-file

โš™๏ธ API

openJson(filePath, options)

Open a file (eg. JSON file) and return the object

Options

  • throttle <Number> - Milliseconds to throttle saves by. Saves are already batched at the end of every event-loop, but this adds time-based throttling.

  • fs <FileSystemInterface> (fs) - Pass in a custom file-system. Defaults to native Node.js fs

  • serialize/deserialize <Function> - Functions to serialize/deserialize the object with. eg. to save output to YAML

    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 behind the scenes.