Skip to content

Repository files navigation

StormDB logo

🌩️ StormDB is a tiny, lightweight, 0 dependency, easy-to-use JSON-based database that allows users to quickly and easily achieve data persistence by provided an engine to store and access JSON data, for NodeJS the browser or Electron.


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:

constStormDB=require("stormdb");// start db with "./db.stormdb" storage locationconstengine=newStormDB.localFileEngine("./db.stormdb");constdb=newStormDB(engine);// set default db value if db is emptydb.default({users: []});// add new users entrydb.get("users").push({name: "tom"});// update username of first userdb.get("users").get(0).get("name").set("jeff");// save changes to dbdb.save();

The db.stormdb database file is updated to:

{"users": [{"name":"jeff"}]}

Typescript Usage:

importStormDBfrom"stormdb";// start db with "./db.stormdb" storage locationconstengine=newStormDB.localFileEngine("./db.stormdb");constdb=newStormDB(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");// before: {"old": "oldData"}// after: {"old": "newData"}

Set Key-Value Pair on Dictionary Property:

db.set("key","value").save();// before: {}// after: {"key": "value"}

Delete Value:

db.delete("key");// before: {'key': 'value', 'key2': 'value2'}// after: {'key2': 'value2'}

Set Key-Value Pair on Dictionary with Shorthand Syntax:

db.set("key.key2","value").save();// before: {}// after: {"key": {"key2": "value"}}

Set Default Data for Empty Database:

db.default({name: "tom"});// actual db: {}console.log(db.get("name"));// prints "tom"

Push Item to Array Property:

db.get("list").push(1).save();// before: {'list': []}// after: {'list': [1]}

Filter Out All Elements under 5:

// before = {'list': [1,2,6,1]}// output = {'list': [6]}db.get("list").filter(i=>i>=5);// save dbdb.save();

Change Element with Highest Value:

// before = {'users': [{value: 10}, {value: 5}, {value: 6}]}// after = {'users': [{value: "changed"}, {value: 6}, {value: 5}]}db.get("users").sort((a,b)=>b.value-a.value);// change value of highest elementdb.get("users").get(0).get("value").set("changed");// save dbdb.save();

Map List, Squaring Each Number in List:

// before = {'data': [1,2,3,4,5]}// after = {'data': [1,4,9,16,25]}// square each number in the listdb.get("data").map(x=>x**2);// save dbdb.save();

Reduce List, Finding Value of All Values in List Summed:

// before = {'data': [1,2,3,4,5]}// after = {'data': 15}// find value of all numbers in list summed togetherdb.get("data").reduce((accumulator,currentValue)=>accumulator+currentValue);// save dbdb.save();

Leverage Serialize and Deserialize functions to encrypt and decrypt data:

constengine=newStormDB.localFileEngine("./db.stormdb",{serialize: data=>{// ecrypt and serialize datareturnencrypt(JSON.stringify(data));},deserialize: data=>{// decrypt and deserialize datareturnJSON.parse(decrypt(data));}});constdb=newStormDB(engine);

Credit

Author: Tom

License

MIT

About

🌩️ StormDB is a tiny, lightweight, 0 dependency, easy-to-use JSON-based database for NodeJS, the browser or Electron.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages