Skip to content

A local Storage wrapper that allows to use local Storage as document database.

License

Notifications You must be signed in to change notification settings

detronetdip/CURD.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

GitHub license made-with-javascript

CURD.js

CURD.js gives you wing in your localStorage.

It is a javascript library that allows you to use your localStorage as a document database.

Table of Contents

Overview

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.

Installation

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.

Structure

It's good to have a clear concept of the structure of your database.

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
            },
            ...
        },
        ...
    }

Functions

  • Initialization

    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 to CURD object.

  • Create Collection

    DB.createCollection(Collection Name)
    

    Create a new collection in your database by calling .createCollection() method on the Database object and store it in a variable.

    var firstCollection = DB.createCollection('firstCollection');
    

    It takes collection name as first parameter and DB as second parameter and return a collection in which we can perform insert operation. Now we can performe insert operations on this collection.

  • Collection Exists Or Not

    DB.existsCollection( Collection Name );
    

    To check a collection exists or not we can use DB.existsCollection(). It will return true or false based on the collection exists or not.

  • Insert Data

    • Insert One Data

      firstCollection.insertData({ Object of Data }, DB)
      

      Insert data to a collection by calling .insertData() method on the collection object we got from .createCollection(). It takes an Object containing all the data as first parameter and takes DB object as a second parameter and return Index 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"

    • Insert Many Data

      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 of Id's

      var arrayOfObjects = [
          {
              key1: your first data,
              key2: your second data,
              ...
          },
          {
              key1: your first data,
              key2: your second data,
              ...
          },
          ...
      ]
      var arrayOfindices = firstCollection.insertMany(
                              arrayOfObjects,
                              DB
                          );
      
      
  • Read Data

    • Read All Collections

      DB.readData();
      

      DB.readData() returns all the data of all collection.

      var allData = DB.readData();
      console.log(allData);
      
    • Read One Collection

      DB.readData(Collection Name);
      

      DB.readData() takes collection name and returns all the data of specific collection.

      var singleCollection = DB.readData('firstCollection');
      console.log(singleCollection);
      
    • Read Specific Data of One Collection

      DB.readData(Collection Name, ID of Data);
      

      If you know the ID of specific data you can retrive the data by using DB.readData() where you have to pass Collection Name as first parameter and the ID as second parameter.

      var singleData = DB.readData('firstCollection',1);
      console.log(singleData);
      
    • Read Data With Specific Values

      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.

  • Get ID

    • Get ID Of A Data

      DB.getId( Data, Index );
      

      To know the ID of a data inside collection we can use getId() method on DB object. It takes the object as first parameter and takes Index as an optional second parameter and returns the ID of the object. By default getId() returns an array of ID's but if you want to get a specific one you can pass Index number as second parameter

      var Id = DB.getId( Data );
      

      We got the Data object from readData()

      we also can use it in another way by calling readData() inside getId().

      var Id = DB.getId(
          DB.readData(
            Collection Name,
            {
              key1:value1,
              key2 : value2
            }
          )
       );
      
    • Get All Id's Of A Collection

      DB.getAllId(Collection Name);
      

      This function returns an array of ID's containing all the IDs of the objects inside the collection.

  • Update Data

    • Update Single Data

      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 if v is not exist in the object or else will update the value of v to 1 if it is already present in the object.

    • Update Multiple Data

      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 if v is not exist in the objects or else will update the value of v to 1 if it is already present in the objects.

  • Delete Data

    • Delete Collection

    • Delete Object of An Collection

      DB.deleteCollection( Collection Name );
      

      To delete a Collection we can use deleteCollection() function it takes collection 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 );
      
    • Delete A Field of An Object

      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 a collection name second the ID of the object and third the field you want to delete.

      DB.deleteField('firstCollection', 1, 'v');
      

      It wll delete the field v from 1 st object of firstCollection.

  • Drawbacks

    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.

About

A local Storage wrapper that allows to use local Storage as document database.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published