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
CURDobject 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
dbnamewhere you have to mention your database name you want to create. It is optional to pass any value toCURDobject. -
DB.createCollection(Collection Name)Create a new
collectionin your database by calling.createCollection()method on theDatabaseobject and store it in a variable.var firstCollection = DB.createCollection('firstCollection');It takes
collection nameas first parameter andDBas second parameter and return acollectionin which we can performinsertoperation. 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 returntrueorfalsebased on the collection exists or not. -
-
firstCollection.insertData({ Object of Data }, DB)Insert data to a collection by calling
.insertData()method on thecollectionobject we got from.createCollection(). It takes an Object containing all the data as first parameter and takesDBobject as a second parameter and returnIndexthat 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()takescollectionname 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
IDof specific data you can retrive the data by usingDB.readData()where you have to passCollection Nameas first parameter and theIDas 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 nameas first parameter and the object containaing all key value pairs as second parameter.
-
-
-
DB.getId( Data, Index );To know the
IDof a data inside collection we can usegetId()method onDBobject. It takes the object as first parameter and takes Index as an optional second parameter and returns theIDof the object. By defaultgetId()returns an array ofID's but if you want to get a specific one you can passIndexnumber as second parametervar Id = DB.getId( Data );We got the
Dataobject 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 theIDs 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
vto the object ifvis not exist in the object or else will update the value ofvto1if 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
vto the objects ifvis not exist in the objects or else will update the value ofvto1if it is already present in the objects.
-
-
-
DB.deleteCollection( Collection Name );To delete a
Collectionwe can usedeleteCollection()function it takescollection nameand delete it.DB.deleteCollection( 'firstCollection' );If we pass
IDof 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 namesecond theIDof the object and third thefieldyou want to delete.DB.deleteField('firstCollection', 1, 'v');It wll delete the field
vfrom1st 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.