@web_dev_guy/jdb-js
v1.0.32
Published
NoSql Json DB
Downloads
42
Maintainers
Readme
WELCOME TO JDB.JS
this page will discuss how to operate with JDB.JS.
What is JDB.JS?
JDB.JS is meant to be a NoSQL JSON database that people could use.
Dependencies
fs
path
xml2js
toml
@iarna/toml
yaml
python-shell
Installation
npm install @web_dev_guy/jdb-js
Linking to your Project Using NPM
Import
import JDB from "@web_dev_guy/jdb-js";
Require
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
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"'
.
- this function writes the
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.
- this function will get the value of the key which has the same name as the
addData(key : String, value: String | Int)
-- this function appends data (
"{key}" : "{value}"
) to the file instead of deleteing the already existing contents.
- this function appends data (
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
ifreturnBool
is false, else it returns a bool value.
- returns the name of the key that contains the value that is equal to
setKey(name : String, newname : String)
-- changes the key's name from
name
tonewname
.
- changes the key's name from
replaceUrl(new_url : String)
-- changes the url of the JDB class to the value of
new_url
.
- changes the url of the JDB class to the value of
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 oftxt
which contains the content of the JDB.
- after reading the JDBF and returning the json it will run the
onWriteData(words : String | Int, callback: Function)
-- after writing the data it runs the
callback
function.
- after writing the data it runs the
onAddData(text : String | Int, callback : Function)
-- its
onWriteData
but it dosnt delete the existing contents of the text.
- its
onRemoveData(name : String, callback : Function)
-- when a key thats equal to
name
is removed, the thecallback
function triggers.
- when a key thats equal to
limitData(by : Int)
-- basically the
LIMIT
keyword in sql whereby
is the least amount of values returned. It returns the value in[{key : key_name}, {value : value_value }]
format.
- basically the
keyExists(key : String)
-- checks if
key
is a key in the JDBF, if its there than it returnstrue
, elsefalse
.
- checks if
valueExists(value : String | Int)
-- checks if
value
is a value in the JDBF, if its there than it returnstrue
, elsefalse
.
- checks if
countData()
-- returns the count of keys in a JDBF.
whereKey(key : String)
-- finds the
key
and returns its position in the JDBF.
- finds the
whereValue(value : String | Int)
-- finds the
value
and returns its position in the JDBF.
- finds the
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 thetype
param.
- Sorts the keys or values, you can choose by changing the
unionData(state1 : String, state2 : String, stringing : Boolean)
-- merges
state1
andstate2
together, if you want it stringifyed, setstringing
totrue
.
- merges
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
totrue
.
- takes the 2 states and returns the common values and it could be converted to json by setting
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
totrue
.
- takes the 2 states and returns the different values cand it could be converted into json by setting the
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.
- watches the data of the current file and runs the
watchDataInterval(callback : Function, interval : Int)
-- watches the data every
interval
in the current file and runscallback
-whenever the data is changed.
- watches the data every
runWithPythonFirst(func : Function, code : String | I/O Wrapper, isfile : Boolean, returnpython : Boolean)
-- first runs python code given in
code
and runs it depending on ifisfile
istrue
orfalse
, if true then it will readcode
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 setreturnpython
to true. After the python code it will run the js code, which the python func output can be used in.
- first runs python code given 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
CreateJDBF(name : String)
-
- This creates a new JDBF in the JDBF folder which has
{}
as the starting content.
DestroyJDBF(name : String)
-
- This destorys or deletes the JDBF from the JDBF folder.
IntegrateJDBF(oldf : String, newf : String)
- This moves the data from the file named
oldf
to the file namednewf
.
DuplicateJDBF(file : String, name : String)
-
- This duplicates the file named
file
. then the duplicated file name isname
.
HookJDBF(fileone : String, filetwo : String)
-
- This makes
filetwo
always have the contents offileone
.
ImportasJDBF(file : String, type : String, name : String)
-
- this function imports code from a file named
file
which you can specify its file type in thetype
param into a JDBF namedname
, 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].
ExportJDBF(file : String, type : String, name : String)
-
- export jdbf named
file
to a file named namename
which is atype
format.
BackupJDBF(file : String)
-
- 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.
RestoreJDBF(file : String)
-
- restores the
.bak.json
file namedfile
to a.json
file namedfile
.
MergeJDBF(fileone : String, filetwo : String, name : String)
-
- merges jdbf's named
fileone
andfiletwo
into one file that would be namedname
.
DiffJDBF(fileone : String, filetwo : String, name : String)
-
- The DiffJDBF() function takes in two JSON files (
fileone
andfiletwo
) 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.
PythonWithJDBF(file : String, var_name : String, code : String | I/O Wrapper, isfile : Boolean)
-
- This function will run python code specified in
code
and it will differently if it is a file, if yes, then setisfile
totrue
. you wont need to import json as the code will do that for you. the variable which will contain the json code from thefile
can be set using thevar_name
. Again YOU WILL NEED PYTHON LANG FOR THIS TO WORK.
Syntax
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:
// 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();
Developer Note
enjoy these new JDBFS funcs
Upcoming Features
- JDB CLI
- JDB IMPORTS