lowdb is a simple database that stores data in .json files
instalation
npm i @fabrisoftware/lowdb
Jump To
require lowdb on you project
const lowdb = require('@fabrisoftware/lowdb');
const db = new lowdb.Database({
name: 'test', // name of json file
directory: './database' // directory to save the database
})
this generate this structure
node-projectroot
└── database
└── test.json
this method saves a value in the database
/*
key: key of value to store on database
value: value to store on database
*/
db.set(key, value)
// example
db.set("Jonh.age", 20);
db.set("Jonh.isAdult", true);
json file structure
{
"Jonn": {
"age": 20
"isAdult": true
}
}
this get data on database
db.get(key) // this return value of key
// example
if(db.get("Jonh.isAdult")){
console.log("Yes!");
}else{
console.log("No!");
}
// this print "Yes!"
this comprobe if key exists in database
db.has(key) // this returns bolean value
// example
if(db.has("jonh")){
console.log("this user exists!");
}
this delete data on database
db.delete(key) // this delete the data key
// example
// first you check if the key exists
if(db.has("jonh")){
db.delete("jonh");
}
json data structure
{} //void object