flatfile is a very simple library for storing, retrieving, and setting JSON data in a flatfile. The library focuses on ease of use and simplicity. Getting up and running is extremely simple and does not require installing any database software.
$ npm i flatfile --save
The module supports node v0.10, v0.12, and all major releases afterwards.
const flatfile = ;// Load a flatfile database based on file: myfile.jsonflatfile;
This is a very simple, asynchronous function for initiating the database. After reading the file and parsing to an object, it will return the object via the callback data paramater. If the file does not exist, an empty object will be returned, allowing you to save it to a new file with
data object that is passed from this function is not an exact copy from the file, but can be treated as such. The data object is given an extra value:
save is a function that can be called when the database should be saved.
datamyValue; // Returns the value of key myValue.
Getting a value from the
data object is truly the object representing the file, but it also contains the two special values for the library.
datamyValue = 'newValue';
Again, this is a very simple function for saving the changes to the database. You can repetitively call this function without having an issue. As a paramater, this function takes a callback function with an error paramater.
Warning: be aware that the data will not automatically save. It is necessary to call this function if you want to save the changes.