1.0.1 • Published 3 years ago
heimsk-simple-db v1.0.1
heimsk-simple-db
A very simple, yet lightweight, useful and easy-to-use database, the database uses JSON files to store data.
Installation
npm install heimsk-simple-db
Creating a new database
const Database = require("heimsk-simple-db");
const database = new Database("./database" /* The path to the database folder */, "mydb" /* The name of the database (this will be used in the JSON file name));
// The first argument is the path to the database folder (if not it will be created automatically).
// The second argument is the database name (if there is no file with the name, a file with the name will be created inside the folder of the first argument).
Getting data
let data = database.get("table-name" /* The table name */);
/*
It will return an object like this:
{
value: "",
set: [Function set],
push: [Function push],
find: [Function find],
filter: [Function filter],
add: [Function add],
remove [Function remove],
assign: [Function assign],
deassing: [Function deassing],
size: {},
del: [Function del]
}
*/
console.log(data.value);
// If the table exists it will return an object with some functions and value.
Setting data
database.set("my-name" /* Table name */, "heimsk" /* The value that will be setted */);
// Or
let myname = database.get("my-name");
myname.set("heimsk");
Pushing data into a array
// The data type must be array.
database.set("my-pets", ["Dog"]);
database.push("my-pets" /* Table Name */, "Cat" /* The value that will be pushed */);
// Pushing more than 1 value.
database.push("my-pets", ["Bird", "Bunny", "Turtle"]);
// Or
let mypets = database.get("my-pets");
mypets.push("Fish");
Finding data inside an array
// The data type must be array
database.set("users", [
{
id: 1,
name: "Heimsk"
},
{
id: 2,
name: "QueenBee"
}
]);
database.find("users" /* The table name */, (user) => user.id === 2 /* The filter */);
/*
Returns
{
id: 2,
name: "QueenBee"
}
*/
// Or
let users = database.get("users")
users.find((user) => user.id === 1);
Filtering the data of an array
database.set("games", [
{
name: "Minecraft",
price: 100
},
{
name: "GTA V",
price: 200
},
{
name: "Skyrim",
price: 50
},
]);
let cheapgames = database.filter("games", (game) => game.price <= 100);
console.log(cheapgames)
/*
[
{
name: "Minecraft",
price: 100
},
{
name: "Skyrim",
price: 50
}
]
*/
// Or
let games = database.get("games");
let cheapgamess = games.filter((game) => game.price <= 100);
Adding values
database.set("heimsk_coins", 10);
database.add("heimsk_coins", 10);
// New value will be 20
// Or
let coins = database.get("heimsk_coins");
database.add(10);
Removing values
/*
Actual value: 20
*/
database.remove("heimsk_coins", 5);
// New value will be 15
// Or
let coinss = database.get("heimsk_coins");
coinss.remove(5);
// New value will be 10
Assign values
database.set("heimsk_profile", {
name: "Heimsk",
id: 1,
avatar: null
});
database.assign("heimsk_profile", {
background: null
});
/*
New value:
{
name: "Heimsk",
id: 1,
avatar: null,
background: null
}
*/
// Or
let profile = database.get("heimsk_profile");
profile.assign({ coins: 0 });
/*
New value:
{
name: "Heimsk",
id: 1,
avatar: null,
background: null,
coins: 0
}
*/
Updating an object's key
let profile = database.get("heimsk_profile");
/*
Actual value:
{
name: "Heimsk",
id: 1,
avatar: null,
background: null,
coins: 0
}
*/
profile.assign({ avatar: "avatar_url" })
/*
New value:
{
name: "Heimsk",
id: 1,
avatar: "avatar_url",
background: null,
coins: 0
}
*/
Unassign a key from an object
let profile = database.get("heimsk_profile");
/*
Actual value:
{
name: "Heimsk",
id: 1,
avatar: "avatar_url",
background: null,
coins: 0
}
*/
profile.deassign("coins");
// Or
database.deassign("heimsk_profile", "coins");
/*
New value:
{
name: "Heimsk",
id: 1,
avatar: "avatar_url",
background: null
}
*/
Check if a table exists
if(database.exists("user_profile")) {
console.log(database.get("user_profile").value);
} else {
database.set("user_profile", {
name: "user"
});
}
// Or
let user_profile = database.get("user_profile");
if(user_profile.value) {
console.log(user_profile.value);
} else {
user_profile.set({
name: "user"
});
}
Deleting a table
database.delete("user_profile");
// Or
let user_profile = database.get("user_profile");
user_profile.del();
Getting the entire data
console.log(database.allData);
Getting the size of an table in the memory
console.log(database.getTableSize("heimsk_profile"));
/*
{
bytes: 0,
megabytes: 0
}
*/
// Or
let profile = database.get("heimsk_profile");
console.log(profile.size);
/*
{
bytes: 0,
megabytes: 0
}
*/
Getting the entire data size in the memory
console.log(database.getTotalSize())
/*
{
bytes: 0,
megabytes: 0
}
*/
Deleting the Database
database.deleteDatabase();