CURD.js
gives you wing in your localStorage.
It is a javascript library that allows you to use your localStorage as a document database.
It is a javascript library that allows you to use your localStorage as a document database. With CURD.js
you can performe each and every opperation that a normal document database has.
Which mean now you can use your localStorage as a temporay database to store some data temporarily which will be synced with your server letter on.
It can also enhance your PWA's performance by giving it a offline database.
It is really easy and simple to install and use CURD.js
. Copy and Paste the following link to your code.
https://res.cloudinary.com/dxqq6aqn8/raw/upload/v1632910286/CURD/curd.min_urp5x3.js
You can also download and add the CURD.min.js
file to your code.
And you are good to go with superpowers.
Main database is an object which has all the collections containing all the data
MainDatabase : {
firstCollection{
key1:{
Data Object
},
key2:{
Data Object
},
...
},
secondCollection{
key1:{
Data Object
},
key2:{
Data Object
},
...
},
...
}
-
Create a
CURD
object and store it in a variable. Database will be created by this method if it's not present in the localStorage and if present it will do nothing.var DB = new CURD({ dbname:"mydb" })
It takes one parameter as object containing
dbname
where you have to mention your database name you want to create. It is optional to pass any value toCURD
object. -
DB.createCollection(Collection Name)
Create a new
collection
in your database by calling.createCollection()
method on theDatabase
object and store it in a variable.var firstCollection = DB.createCollection('firstCollection');
It takes
collection name
as first parameter andDB
as second parameter and return acollection
in which we can performinsert
operation. Now we can performe insert operations on thiscollection
. -
DB.existsCollection( Collection Name );
To check a collection exists or not we can use
DB.existsCollection()
. It will returntrue
orfalse
based on the collection exists or not. -
-
firstCollection.insertData({ Object of Data }, DB)
Insert data to a collection by calling
.insertData()
method on thecollection
object we got from.createCollection()
. It takes an Object containing all the data as first parameter and takesDB
object as a second parameter and returnIndex
that has been assigned to the currently inserted data.var Index = firstCollection.insertData( { key1: your first data, key2: your second data, ... }, DB );
First parameter must be an object or else it will give an error as "Expected Object Got None"
-
firstCollection.insertMany([array of objects], DB)
We can insert multiple data at once by calling
.insertMany()
on the collection object. But we have to pass an array of objects as a parameter insted of a single object and it returns array ofId
'svar arrayOfObjects = [ { key1: your first data, key2: your second data, ... }, { key1: your first data, key2: your second data, ... }, ... ] var arrayOfindices = firstCollection.insertMany( arrayOfObjects, DB );
-
-
-
DB.readData();
DB.readData()
returns all the data of all collection.var allData = DB.readData(); console.log(allData);
-
DB.readData(Collection Name);
DB.readData()
takescollection
name and returns all the data of specific collection.var singleCollection = DB.readData('firstCollection'); console.log(singleCollection);
-
DB.readData(Collection Name, ID of Data);
If you know the
ID
of specific data you can retrive the data by usingDB.readData()
where you have to passCollection Name
as first parameter and theID
as second parameter.var singleData = DB.readData('firstCollection',1); console.log(singleData);
-
var Data = DB.readData( Collection Name, { key1:value1, key2 : value2 } );
You can search data by specific key value pairs. Here you have to pass
collection name
as first parameter and the object containaing all key value pairs as second parameter.
-
-
-
DB.getId( Data, Index );
To know the
ID
of a data inside collection we can usegetId()
method onDB
object. It takes the object as first parameter and takes Index as an optional second parameter and returns theID
of the object. By defaultgetId()
returns an array ofID
's but if you want to get a specific one you can passIndex
number as second parametervar Id = DB.getId( Data );
We got the
Data
object fromreadData()
we also can use it in another way by calling
readData()
insidegetId()
.var Id = DB.getId( DB.readData( Collection Name, { key1:value1, key2 : value2 } ) );
-
DB.getAllId(Collection Name);
This function returns an array of
ID
's containing all theID
s of the objects inside thecollection
.
-
-
-
DB.updateData( Collection Name , Index, { key: value } );
updateData()
Update an object of a collection based on index, we can pass array of indices to update multiple objects.DB.updateData( 'firstCollection' , 1, { v: 1 } );
This will update 1st object of the collection. It will add
v
to the object ifv
is not exist in the object or else will update the value ofv
to1
if it is already present in the object. -
We can pass array of indices to update multiple objects at a time.
DB.updateData( 'firstCollection', [1,2,3,4,5], { v: 1 } );
This will update all the objects of the collection respect to the array of indices. It will add
v
to the objects ifv
is not exist in the objects or else will update the value ofv
to1
if it is already present in the objects.
-
-
-
DB.deleteCollection( Collection Name );
To delete a
Collection
we can usedeleteCollection()
function it takescollection name
and delete it.DB.deleteCollection( 'firstCollection' );
If we pass
ID
of an object as a second parameter then it will delete the object of the collection.DB.deleteCollection( 'firstCollection', 1 );
-
DB.deleteField( Collection Name, ID, Field);
To delete a field of an object we can use
deleteField()
function it takes 3 parameter, first it takes acollection name
second theID
of the object and third thefield
you want to delete.DB.deleteField('firstCollection', 1, 'v');
It wll delete the field
v
from1
st object offirstCollection
.
-
Though it works like a document database but it has it's own drawbacks. As it is using localStorage for storing the data it is not secure that enough though you can store encrypted data, and localStorage gives only 5MB of storage so you can't store more than 5MB data.
So if you are using it to store data, not in encrypted format, please do not store crusial information.