Function that returns a store that can save/load sync/async data into a specified JSON file.
No dependencies at all.
To have this repeated pattern of code uploaded as an external module.
$ npm i -s json-store-factory
This tool gives you an interface to load
/save
/get
/set
/delete
data of a json
file, in a sync
or async
way.
- create store
- save store
- load store
- get store data
- set store data
- delete store
const jsonStoreFactory = require("json-store-factory");
const store = jsonStoreFactory("./store.json", { beautify: true });
Synchronously:
store.saveSync({ title: "My title", info: { author: "someone" } });
Asynchronously:
await store.save({ title: "My title", info: { author: "someone" } });
Synchronously:
const data = store.loadSync();
Asynchronously:
const data = await store.load();
Pass a selector (array of property names) indicating what you want to get from the store. You can pass a default value too.
Synchronously:
const item = store.getSync(["info", "author"], "Optional default value"); // returns: "someone"
Asynchronously:
const item = await store.get(["info", "author"], "Optional default value");
Pass a selector (array of property names) indicating what you want to set from the store, and the value for it. The resultant data is returned.
Synchronously:
store.setSync(["title"], "My new title");
Asynchronously:
await store.set(["title"], "My new title");
Synchronously:
store.deleteSync();
Asynchronously:
await store.delete();
You can also delete specific parts of the store by providing selectors:
Synchronously:
const store = store.deleteSync("title");
Asynchronously:
const store = await store.delete(["title"]);
The module is a simple function that takes 3 arguments:
-
store:String
: path to the store. -
settings:Object
: settings for the store. Currently, onlybeautify:Boolean
is allowed (which defaults totrue
). -
extensions:Object
: extensions for the store instance. Properties and methods that will be added to the store (which defaults to{}
).
The signatures of the methods of the store can be guessed by the Usage section of this document.
Synchronous operations can throw errors due to filesystem operations or JSON transformations.
Due to how very useful are the internal methods of the API, they are also available through the function the module is:
-
getter
: this function...- ...receives:
-
data:Object
: data to be altered. -
selector:Array|String
: selector of the data we want to get. -
defaultValue:any
: the value returned when it is not found.
-
- ...receives:
-
setter
: this function...- ...receives:
-
data:Object
: data to be altered. -
selector:Array|String
: selector of the data we want to set. -
value:any
: the value we want to assign to the provided selection. -
force:Boolean
: whether or not we want to force the creation of intermediate objects. Defaults tofalse
.
-
- ...receives:
-
deleter
: this function...- ...receives:
-
data:Object
: data to be altered. -
selector:Array|String
: selector of the data we want to delete.
-
- ...receives:
They all return the resulted data.
Note: This internal API is available for browser environments too.
Here you have an example of how to use it:
require("json-store-factory").getter({ some: { data: "text" }}, ["some", "data"]) === "text"; // returns true
To run tests, place the cmd
in the json-store-factory
project and run:
$ npm run test
As it does not hace external dependencies, it should work.
This project is released under WTFPL or What The Fuck Public License, which means do what you want, basically.
Please, address your issues here.