Try it online now: Demo Page and Interactive Playground!
Example: Add a post entry under users.tom and save it to the database.
db.get("users")
.get("tom")
.push({ title: "Post 1" })
.save();
Features
- 🏎️ Blazingly Fast Speeds - Fast read and write speeds, even when handling large data.
- 📦 Tiny Size - Tiny source code size allows for blazingly fast loading when speed matters.
- ⚡️ Versatile - Can be used with NodeJS, in the browser or in Electron.
Usage
Install StormDB through NPM:
$ npm i stormdb
Basic usage with NodeJS:
const StormDB = require("stormdb");
const engine = new StormDB.localFileEngine("./db.stormdb");
const db = new StormDB(engine);
db.default({ users: [] });
db.get("users").push({ name: "tom" });
db.get("users")
.get(0)
.get("name")
.set("jeff");
db.save();
The db.stormdb
database file is updated to:
{
"users": [
{"name":"jeff"}
]
}
Typescript Usage:
import StormDB from "stormdb";
const engine = new StormDB.localFileEngine("./db.stormdb");
const db = new StormDB(engine);
StormDB is designed to be flexible, and can be used in NodeJS, the browser or even Electron with very small adaptations to the code. Examples usages can be seen below:
Engine API
For expanding functionality, each database initialized can be expanded with the following options, in the format new Engine(path, options);
.
serialize
- function to serialize data before writing it to the database.deserialize
- function to deserialize data from the database.
Database Operations Examples
Change Value of Key in Database:
db.get("old").set("newData");
Return the Raw Value of a Selected Property:
db.get("list").value();
Set Key-Value Pair on Dictionary Property:
db.set("key", "value").save();
Delete Value:
db.get("key").delete();
If you delete a value from a list, it will leave a null value in the place of the deleted data:
db.get("key")
.get(1)
.delete();
If you don't want this behaviour, you can pass in true
to the .delete()
function to not leave a null value in place of the deleted data:
db.get("key")
.get(1)
.delete(true);
Set Key-Value Pair on dictionary with dot notation shorthand:
db.set("key.key2", "value").save();
Note, that the set
and get
functions allow for dot notation for the keys, if you don't want this behaviour, as you may have a key with a dot in it, you can disable it by providing an extra parameter:
db.get("one.two", false).set("three.four", "test", false);
Set Default Data for Empty Database:
db.default({ name: "tom" });
console.log(db.get("name"));
Push Item to Array Property:
db.get("list")
.push(1)
.save();
Filter Out All Elements under 5:
db.get("list").filter(i => i >= 5);
db.save();
Change Element with Highest Value:
db.get("users").sort((a, b) => b.value - a.value);
db.get("users")
.get(0)
.get("value")
.set("changed");
db.save();
Map List, Squaring Each Number in List:
db.get("data").map(x => x ** 2);
db.save();
Reduce List, Finding Value of All Values in List Summed:
db.get("data").reduce(
(accumulator, currentValue) => accumulator + currentValue
);
db.save();
Leverage Serialize and Deserialize functions to encrypt and decrypt data:
const engine = new StormDB.localFileEngine("./db.stormdb", {
serialize: data => {
return encrypt(JSON.stringify(data));
},
deserialize: data => {
return JSON.parse(decrypt(data));
}
});
const db = new StormDB(engine);
Credit
Author: Tom
License
MIT