This project tries to create a local database with CRD operations. The data is stored in form of key value pair in .json file. it can be imported as a library in any project and used using it's functional endpoints.
- data_store
- store.js: This file has the implementation of the local storage database that was assigned in the task.
- db.json: This file get created automatically when no file path for storage.
- index.js: This file has a sample javascript code which uses store.js as a library to store data. It contains a test code to showcase how to use the library.
- index.js file contains a sample implementation of the library. There are different functions already implemented to test the working of the database. Uncomment the function call checkCreate() to check createElement() function, checkRead() to check the readElement() function and checkDelete() to check the working of deleteElement() function. Appropriate data can be passed in their implementation by changing the corresponding variables.
- After uncommenting the required functions run node index.js to see the use that function.
- import store.js file in your project like:
const { LocalStore } = require("./data_store/store");
- LocaStore is the class which implements all the functions given above.
- An object can be instantiated as:
const database = new LocalStore(newFilePath);
- Here the newFilePath parameter shows the locations where you want to store the file which contains the data. This parameter is optional
- The below given functions can be accessed using the dot operator using the object created.
- The library contains a class named LocalStore
- The class implements CRD i.e. Create, Read, Delete operations using following functions
- createElement( key, value, timeToLive ) : This function create a new element and adds it to the local database. The database stores information in a json file.
- key - This is key for the data to be inserted
- value - Information required to be stored corresponding to the above key (in json format)
- timeToLive (optional parameter) - This defines the expiration time of the data being stored. If this value is not provided then the data being store never expires.
- readElement( key ) : This function returns the value corresponding to the key passed as parameter to the function.
- deleteElement( key ) : This function deletes the element, if exists, from the local database.
- createElement( key, value, timeToLive ) : This function create a new element and adds it to the local database. The database stores information in a json file.
- All these functions return an object of same structure, which is:
{data: null, errMessage: {message: "", systemMessage: ""}}
- If the function is a get type function like readElement then the returned object contains the required data in JSON form corresponding to the value of data property in this object.
- The error messages are returned in form of strings - message shows the custom generated message - systemMessage shows the message generated by the system in case any error occurs
- Note : First check the length if message property of errMessage in the returned object. If the length is not zero then the value corresponding to data property will be null and invalid. Use that message to debug the your code. ( The sample code in index.js demonstrates this approach ).
- Suppose obj is the variable which stores the returned object then:
- obj.data gives the data required in JSON form(if the function called was to fetch some data else null)
- obj.errMessage.message gives the custom error message in string form.
- obj.errMessage.message gives the message generated by the system
- If createElement() is called for a key which already exists then the error message displayed will be: "Data already exists for the corresponding key"
- if while inserting an element in the json file, an error occurs then the message will be : "An error occured while inserting the data"
- If readElement() is called for an empty file, error message displayed will be : "The file is empty"
- If readElement() or deleteElement() is called for a non existing key then : "No element with the corresponding key exists in the database"
- If readElement() or deleteElement() is called for a key which has expired according to it's timToLive property, then : "This key has expired and cannot be read"