localDb.js is a simple Javascript
Library
with which you can perform some operations like a database do. It is not actually a database, but performs same as a database do. It stores data on browser's localStorage
. It gives some functionality like UPDATE
, DELETE
, INSERT
, SORT
, QUERY
etc which we used to do in SQL
like databases.
- Abinash Karmakar
- License : MIT ( Read License)
I made this library as easy to use as possible.
> npm i localstorage-db.js
You should have a basic knowledge of
Javascript
objects
.
It saves everything in JSON
format in browser's localStorage
. You can imagine a table or a container to get the concept of how it works. Go through the page to clear the concept.
At first you have to add a source of localDb.js in your web page before you use any javascript code of this library.
<script src="https://codeAbinash.github.io/localDb.js/localDb.min.js" type="module"></script>
<script src="https://codeAbinash.github.io/localDb.js/localDb.js" type="module"></script>
import localDb from "https://codeAbinash.github.io/localDb.js/localDb.min.js"
import localDb from "https://codeAbinash.github.io/localDb.js/localDb.js"
var StdS = new localDb("Students");
In this example it creates a database named "Students"
, and this database will be accessed by the StdS
variable. If the database stored any data previously by this name( "Students"
) in localStorage
, the previous data will still be available.
var StdS = new localDb("Students","new");
Creates a new database, Previous data will be deleted.
StdS.insVal( // insert data as multiple objects
{name:"Abinash",age:19,roll:10},
{name:"Ram",age:20,roll:11},
{name:"Sujan",age:21,roll:12},
{name:"Pithu",age:22,roll:13}
);
StdS.insVal( // insert data as multiple objects
{"name":"Abinash","age":19,"roll no":10},
{"name":"Ram","age":20,"roll no":11},
{"name":"Sujan","age":21,"roll no":12},
{"name":"Pithu","age":22,"roll no":13}
); // if you want to use multi word keys, you have to quote them (Concept of js objects)
Inserts all data as javascript
objects
in Students
database.
var data = [
{name:"Abinash",age:19,roll:10},
{name:"Ram",age:20,roll:11},
{name:"Sujan",age:21,roll:12},
{name:"Pithu",age:22,roll:13}
];
StdS.insVal(data);
// insert data as array of objects
Insert as array
of objects
. Here data
is a array
of objects
.
var allData = StdS.get();
// All data will be stored in the allData variable
console.log(allData);
var std = StdS.get(5);
// Returns the 5 indexed element.
var x = StdS.get(5,"first");
// Returns first 5 elements
var x = StdS.get(10,"last");
// Returns last 10 elements
var sujan20 = StdS.get("name=='Sujan' && age>20");
// Get from all elements where condition is true
var x = StdS.get(5,"name=='Abinash'");
// Get total 5 elements from first where condition is true
var x = StdS.get(5,"name=='Abinash'","last");
// Get total 5 elements from last where condition is true
var x = StdS.get(7,"name=='Abinash'","first");
// Get total 7 elements from first where condition is true
var x = StdS.get(5,"last","name=='Abinash'");
// Get total 5 elements from last where condition is true
var x = StdS.get(7,"first","name=='Abinash'");
// Get total 7 elements from first where condition is true
Results are always stored as array of objects
StdS.set("name='Ks'",2);
// Set name='Ks' of the second element and returns 1
//dbName.set(apply,condition)
StdS.set("name='K'","name=='Abinash' && age>20");
//Changes in all elements by condition and returns number of updated elements
StdS.set("name='S'","roll==25",10);
// checks first 10 elements, and updates where condition is true
StdS.set("age=25","name=='Abinash'","last",10);
// checks last 10 elements and updates where conditions is true
StdS.set("age=25","name=='Abinash'","first",20);
// checks first 20 elements and updates where conditions is true
StdS.set("age=25","name=='Abinash'",20,"first");
// checks first 20 elements and updates where conditions is true
StdS.set("age=25","name=='Abinash'",10,"last");
// checks last 10 elements and updates where conditions is true
set()
always returns how many elements are updated
StdS.delAll(); //Deletes all data in the database
StdS.del(5); //Deletes the 5 indexed (6th) element
StdS.del(2,"first");
//Deletes first 2 elements without any condition
StdS.del(3,"last");
//Deletes last 3 elements without any condition
StdS.del("name=='Ram' && age>=25");
// Delete where name is 'Ram' and age>=25 from all data
StdS.del(3,"last","name=='Abinash'");
StdS.del(3,"name=='Abinash'","last");
// delete where name is 'Abinash, from last 3 elements
StdS.del(3,"first","name=='Abinash'");
StdS.del(3,"name=='Abinash'","first");
// delete where name is 'Abinash, from first 3 elements
StdS.delDb(); // Deletes the database and it's all data
Delete functions always returns how many data are deleted
sort()
returns the localDb object after sorting
StdS.sort("age");
//sort() returns the localDb object after sorting
//Sorts all data in ascending order
StdS.sort("name");
// OR
StdS.sort("name","aSC");// the second argument is case insensitive
// OR
StdS.sort("name","ascending");
// Get the same result, Sorts ascending
StdS.sort("name","deS");
StdS.sort("name","dEscenDing");
// Sorts Descending
var y = StdS.sort("age").get(0);
//Get the 1st element after sorting
//Means the element with smallest age value
//Or this can also be done
StdS.sort("age");
var y = StdS.get(0);
//Get Same Result
var y = StdS.sort("age","des").get(0);
//Get the 1st element after sorting
//Means the element with maximum age value
If you save()
after sorting, the sorted Database will be saved in localStorage
StdS.sort("name").save();
// OR
StdS.sort("name");
StdS.save(); // Save all sorted data
You can "Sort after a Sorting" in a single line.
var y = StdS.sort("age","des").sort("name","asc");
// First sorts by age and then by name
var x = StdS.getName(); // Returns the name of the database
console.log(x);
It will return a string, all data is saved by this string(Name) in the localStorage.
var x = StdS.getKeyName();
console.log(x); // "lDark#4s5dStudentskey2sd55"
var d = StdS.setName("D");
// Changes the name of the database and returns the changed database
console.log(d.get());
// This can also be done
StdS.setName("D");
// All data will be copied to "D" and the StdS database will be deleted
var d = new localDb("D");
console.log(d.get());
console.log(d.length());
// Returns the number of elements in the Database
var s = new localDb("myDb");
s.copy(a);
// now s has the all values of a and previous s
s.delAll(); // Deletes everything is s
s.copy(a,b,c); // stores data of a,b,c into s
console.log(s.get());
// Output will be all of the data of a, b, c
- You can use a database in frontend too.
- Very useful for every kind of web apps or websites like Offline WebApp, PWA(Progressive Web App) etc.
- The main Advantage is it is very easy, maybe the easiest.
- Manages data easily.
- We know a normal browser gives us only 5mb storage for every site to use
localStorage
, so we can store up to 5MB data here. Well, think, you are using this DataBase in frontend, so you will have to store one(maybe 2 or 3) user's data. To store this little amount of data, 5MB storage is enough.
It may be used in all web applications like offline web applications or PWA or in a normal website.
For Example,
- Imagine you are making a Notes application, so you have to store every different note permanently in
localStorage
. Here you have to use a library to store all notes data otherwise you have to write very very long code. - Sometimes you have to store many website data, for example, if dark mode is on or off or to save where you quit the browser to start again the site in same condition, etc.