A small library for manipulating json files and using them to save data.
import DB from "the-lopster";
const DB = require("the-lopster").default;
let db = new DB.DB(src, value);
-
src
- is the path to your JSON file. (A mandatory parameter, but the file does not have to exist, but if it exists, it should contain{}
as a minimum) -
value
- this is a preset, the value of which will be set in the file, but if the file already contains the data, the preference will be given to the data entered in this parameter (optional parameter)
That is, if in the file:
{
"age": 25
}
And in the code:
const db = new DB.DB("path/to/file.js", {age: 20});
Then the file will be overwritten:
{
"age": 20
}
This is a field in which the values from the file are stored, through it you can get the values of the file and also change them, but it is not recommended to change them.
Through this method, you can change the value of the file. Use exactly this method to change data.
const db = new DB.DB("path/to/file.json", {age: 20});
db.set(callback);
callback
- This is a function:
db.set(function () {
this.age = 25;
})
Or:
db.set((data) => {
data.age = 25;
})
This function enables synchronization with the file in the group will automatically change this.data
if there were any
variables in the file. Calling again will disable sync.
Any data changes are automatically written to the file. If the watch
function is enabled, when changing the file, the
variables from the file will be automatically pulled up.
If you need to use other data storage methods, you can use extensions based on the DB.Extension class.
At the moment, there is support for such files as JSON
(default), INI
, YAML
, TOML
, XML
.
To use the extension there is a function .use
Example:
import DB from "the-lopster";
DB.use(DB.XML);
This is an extension class that accepts three parameters ext
, parse
, stringify
.
-
ext
- file extension with a dot at the beginning. -
parse
- a function that takesstring
and returns anObject
-
stringify
- a function that takesObject
and returns anstring
import DB from "the-lopster";
function parse(value: string): Object {
}
function stringify(value: Object): string {
}
let EXT = new DB.Extension(".ext", parse, stringify);
Using:
DB.use(EXT);
This is a slightly truncated analogue of DB.DB
, it does not save data by itself, for this you need to call
method DB.Sherbet.save
.
Also, when closed due to the combination of CTRL+C
, when method DB.Sherbet.watch
works, data will be saved.
It reads data when an instance is created and when the program is closed, and all the rest of the time the data is
stored in the RAM, which is why it is called Sherbet.
Everything else works as in DB.DB
.