Skip to content

EvanHahn/node-persisted-json-object

Repository files navigation

Persisted JSON objects for Node

This is an object that functions like a plain JavaScript object, but it saves itself to the file system after every change. Uses Proxy.

Usage

Use it just like a regular JavaScript object:

let jsonObject = require("persisted-json-object");

let obj = jsonObject({ file: "data.json" });

obj.foo = "boo";

obj.temporary = "soon to be deleted";
delete obj.temporary;

obj.grue = "you";

obj.coolNumbers = [420, 666, 69, 12];

After these changes, data.json will look like this (some formatting added):

{
  "foo": "boo",
  "grue": "you",
  "coolNumbers": [420, 666, 69, 12]
}

A few notes:

  • If data.json exists, it will load that data.
  • If data.json doesn't exist, it will be written as soon as you set a property.
  • Errors will be thrown if your object cannot be serialized as JSON.

About

make a normal object persist as JSON

Resources

License

Stars

Watchers

Forks

Packages