NoSql Json DB
npm install @web_dev_guy/jdb-js fs ` ###
- ### ` path ` ###
- ### ` xml2js ` ###
- ### ` toml ` ###
- ### ` @iarna/toml ` ###
- ### ` yaml ` ###
- ### ` python-shell ` ###
Installation ##
- ## ` npm install @web_dev_guy/jdb-js ` ##
Linking to your Project Using NPM ##
- ### Import ###
* `node
import JDB from "@web_dev_guy/jdb-js";
`
- ### Require ###
* `node
const { JDB } = require("@web_dev_guy/jdb-js");
`
JDB Class - ##
This is the class which contains all the functions that you can use
the first parameter is the name of your file you will be preforming CRUD on
`node
const { JDB } = require("./JDB/JDB.js") or require("@web_dev_guy/jdb-js");
const jdb = new JDB("jsonfile");
// links to "jsonfile.json" in the JDBF folder
`
now we will disscuss the functions in the JDB class
- ` readData() `-
* this function reads the file that you have put as the parameter in the class's constructor.
- ` writeData(data : String) `-
* this function writes the ` data ` into the JDBF (Json DataBase Files) , this WILL delete the rest of the contents in the file. The syntax to writing the json data in this function and other like functions is ` '"key1" : "value1", "key2" : "value2"' `.
- ` setData(key : String, value : String | Int) `-
* this is basically writeData, but it dosnt delete the existing contents and can only write new data once. It can also change the value of any key you put as the first param any time.
- ` getData(key : String) `-
* this function will get the value of the key which has the same name as the ` key ` param.
- `addData(key : String, value: String | Int) `-
* this function appends data (` "{key}" : "{value}" `) to the file instead of deleteing the already existing contents.
- ` addBigData(code : String | Int) `-
* this is just add function but you could add lines of JSON code.
- ` removeData(key : String) `-
* this function removes the key and its value from the JDBF code.
- ` removeAllData(brackets : Boolean) `-
* removes all the code from the file, if brackets is true, then the brackets will also be delete, else the brackets will.
- ` hasData(value : String | Int, returnBool : Boolean) `-
* returns the name of the key that contains the value that is equal to ` value ` if ` returnBool ` is false, else it returns a bool value.
- ` setKey(name : String, newname : String) `-
* changes the key's name from ` name ` to ` newname `.
- ` replaceUrl(new_url : String) `-
* changes the url of the JDB class to the value of ` new_url `.
- ` terminate() `-
* terminates the JDB code
- ` onReadData(callback(txt : String) : Function) `-
* after reading the JDBF and returning the json it will run the ` callback ` function that has the param of ` txt ` which contains the content of the JDB.
- ` onWriteData(words : String | Int, callback: Function) `-
* after writing the data it runs the ` callback ` function.
- ` onAddData(text : String | Int, callback : Function) `-
* its ` onWriteData ` but it dosnt delete the existing contents of the text.
- ` onRemoveData(name : String, callback : Function) `-
* when a key thats equal to ` name ` is removed, the the ` callback ` function triggers.
- ` limitData(by : Int) `-
* basically the ` LIMIT ` keyword in sql where ` by ` is the least amount of values returned. It returns the value in ` [{key : key_name}, {value : value_value }] ` format.
- ` keyExists(key : String) `-
* checks if ` key ` is a key in the JDBF, if its there than it returns ` true `, else ` false `.
- ` valueExists(value : String | Int) `-
* checks if ` value ` is a value in the JDBF, if its there than it returns ` true `, else ` false `.
- ` countData() `-
* returns the count of keys in a JDBF.
- ` whereKey(key : String) `-
* finds the ` key ` and returns its position in the JDBF.
- ` whereValue(value : String | Int) `-
* finds the ` value ` and returns its position in the JDBF.
- ` sortData(by : String, type : String) `-
* Sorts the keys or values, you can choose by changing the ` by ` param, in the JSON data based on the specified criteria thats in the ` type ` param.
- ` unionData(state1 : String, state2 : String, stringing : Boolean) `-
* merges ` state1 ` and ` state2 ` together, if you want it stringifyed, set ` stringing ` to ` true `.
- ` intersectData(state1 : String, state2 : String, stringing : Boolean) `-
* takes the 2 states and returns the common values and it could be converted to json by setting ` stringing ` to ` true `.
- ` differenceData(state1 : String, state2 : String, stringing : Boolean) `-
* takes the 2 states and returns the different values cand it could be converted into json by setting the ` stringing ` to ` true `.
- ` lenData() `-
* returns the length of the JDBF.
- ` watchData(callback : Function) `-
* watches the data of the current file and runs the ` callback ` whenever the data is changed.
- ` watchDataInterval(callback : Function, interval : Int) `-
* watches the data every ` interval ` in the current file and runs ` callback ` -whenever the data is changed.
- ` runWithPythonFirst(func : Function, code : String | I/O Wrapper, isfile : Boolean, returnpython : Boolean) `-
* first runs python code given in ` code ` and runs it depending on if ` isfile` is ` true ` or ` false `, if true then it will read ` code ` as a I/O, meaning read from file. This function WILL NOT run if you just give the file path, you must use node filesystem for this. If you want the function to return the python value, then set ` returnpython ` to true. After the python code it will run the js code, which the python func output can be used in.
- ` runWithPythonLast(func : Function, code : String | I/O Wrapper, isfile : Boolean) `-
* same as ` runWithPythonFirst ` but js runs first and that the js function wont get the return value from the python code to its advantage.
* note that these 2 python functions require the actual python language to be ran correctly.
JDBFS Class - ##
there are also functions can help you do stuff with files, rather than keys and value in the JDB class
$3
- This creates a new JDBF in the JDBF folder which has ` {} ` as the starting content.
$3
- This destorys or deletes the JDBF from the JDBF folder.
$3
- This moves the data from the file named ` oldf ` to the file named ` newf `.
$3
- This duplicates the file named ` file `. then the duplicated file name is ` name `.
$3
- This makes ` filetwo ` always have the contents of ` fileone `.
$3
- this function imports code from a file named ` file ` which you can specify its file type in the ` type ` param into a JDBF named ` name `, Note: if you have the file in a folde in the root dir, you should add the folders name to your url e.g - [FOLDER_NAME]/[FILE_NAME].
$3
- export jdbf named ` file ` to a file named name ` name ` which is a ` type ` format.
$3
- makes a backup jdbf of ` file ` with the ` .bak.json ` file format which will be in the place of the main jdbf if the main one isnt found.
$3
- restores the ` .bak.json ` file named ` file ` to a ` .json ` file named ` file `.
$3
- merges jdbf's named ` fileone ` and ` filetwo ` into one file that would be named ` name `.
$3
- The DiffJDBF() function takes in two JSON files ( ` fileone ` and ` filetwo `) and a new filename ( `name `). It reads the content of these JSON files, parses the data into JavaScript objects, and then compares the keys of the first file with the keys of the second file.
$3
- This function will run python code specified in ` code ` and it will differently if it is a file, if yes, then set ` isfile ` to ` true `. you wont need to import json as the code will do that for you. the variable which will contain the json code from the ` file ` can be set using the ` var_name `. Again YOU WILL NEED PYTHON LANG FOR THIS TO WORK.
$3
`node
const { JDBFS } = require("./JDB/JDB.js") or require("@web_dev_guy/jdb-js");
const jdbfs = new JDBFS();
jdbfs.CreateJDBF("example"); // creates new JDBF called example
// ...
jdbfs.IntegrateJDBF("example", "main"); // moves data from example.json to main.json
jdbfs.HookJDBF("main", "main2"); // main is now hooked to main
jdbfs.Duplicate("main", "main_dup"); // duplicated main and renamed the duplicate to main_dup
jdbfs.DestroyJDBF("example"); // destroys the JDBF called example
`
Example Usage: #
`javascript
// Create a new JDBFS instance
const jdbfs = new JDBFS();
// Create a JDB file
jdbfs.CreateJDBF("example");
// Create a new JDB instance
const jdb = new JDB("example");
// Write data to the JDB file
jdb.writeData('"name" : "john", "age" : "twenty-two", "month_of_birth" : "jan"');
// Set a new value for a key in the JSON data
jdb.setData("name", "alex");
// Read and log data from the JDB file asynchronously
jdb.onReadData((data) => {
console.log(data);
});
// Delete the JDB file
jdbfs.DestroyJDBF("example");
// Terminate the JDB instance
jdb.terminate();
``