json-db-manager v1.1.0
json-db-manager
A simple json manager like "data base (DB)"
local to your application, very advanced and malleable to use as you want.
- Compatible with several EcmaScript (ES) technologies
- Allowed to use
"require()"
and"import"
New features or changes
- A new parameter in the class, which is for the creation of the JSON file that was defined in "Path" and that if no existing will be created a file.
- "Request" events now have more items that return to their object.
Bugs fixed from the previous version
- The "read" event was not responding.
How to use
const db = new JsonDB();
You can choose the spacing by placing the number of spaces inside the
JsonDB
class, thedefault being: 0
new JsonDB(2); // Espaçamento adicional
You can also choose between a
json
storing"base64"
or"utf-8"
which is the default. For"uft-8"
do not enter a second parameter.
new JsonDB(0, "utf-8"); // Codificação opicional
The third parameter is responsible for management if your
json
file defined in"path"
does not exist, can automatically create the file, being the standard:"false"
new JsonDB(0, "utf-8", true); // opicional para para criar arquivo.
Metodos
GET
path is a function that will provide you with the objects as soon as you enter the json file path
console.log(db.path("./dir").get())
// db.path("./dir").get("KeyExample") Para procurar por um item especifico
Resultado exemplo
{
"KeyExample": {"velueExamples": [0, "1", [2, 2.5]]},
"KeyExample1": 3,
}
SET
Pass the "key" of your choice and the "value"
db.path("./dir").set("key", "value"); // Criando por parametro.
db.path("./dir").set({"key1": "value"}); // Formatação objeto.
db.path("./dir").set({"key2": "value", "key3": "value"}); // Formatação com objetos.
Resultado exemplo
{
"key": "value", // Criando por parametro.
"key1": "value", // Formatação objeto.
"key2": "value", // Formatação com objetos.
"key3": "value", // Formatação com objetos.
}
DELETE
Pass the "key" you want to delete
db.path("./dir").delete("keyExample");
Resultado exemplo
{
//keyExample is deleted
"KeyExample1": 8,
}
CLEAR
Delete all json and leave it ready for new values
db.path("./dir").clear();
Resultado exemplo
{} // Void
hostView
To create a server with your JSON file
- The
"port"
method is responsible for listing the server port. - The
"update"
method allows when the page is reloaded to update the information based on the"json file"
. - The
"update"
is optional and the default is"false"
db.path("./test.json").hostView({ port: 3000, update: true })
"hostView" has two methods, "on"
and "start"
.
The "on" method is responsible for performing manipulations on the "read" and "request" events.
While the "on"
event is activated when the server is turned on, the "request"
event is activated every time the server is accessed, such as when reloading the page or opening it for the first time.
Object made available by the event "read"
{
"settings": {
"port": "number",
"update": "boolean"
}
}
- port - Refers to the door chosen by you to the server.
- update - To indicate if the server will be updating the page.
Object made available by the event "request"
{
"url": "string",
"method": "string",
"ip": "number"
}
- url - The route used to display
"JSON"
. - method - The requisition method that was made on the server.
- ip - It is the
"IP"
of the device that accessed the server on the JSON file route.
Object made available by the event "request"
url: string, method: string, ip: number
let num = 1;
const server = db.path("./test.json").hostView({ port: 3000, update: true })
server.on("read", (req) => {
console.log(`Project started.\nPort: ${req.settings.port}\n`);
})
server.on("request", (req) => {
console.log(`Project acessed: ${num} time\nURL acess: "${req.url}"\nAccessed by IP: ${req.ip}`);
num++
})
server.start()//server initialization
Resultado exemplo
// Your browser | example URL: http://localhost:3000/data
1 {
2 "KeyExample1": 8,
3 }
// Your logs | example
Project started.
Port: 3000
Project acessed: 1 time
URL acess: "/data"
Accessed by IP: 192.168.0.17
To connect other devices to the server, simply be on the same network and use the
"IPv4"
of your project machine along with the"port"
listed.
Tip for "Windows"
users:
- Open "cmd" and run
"ipconfig"
. - Look for the numbers in the line that says
"IPv4 Address"
.
Resultado exemplo
# URL in your browser | example
http://192.168.0.22:3000