How to delete a record from your local/custom database in Node.js ?
The custom database signifies the local database in your file system. There are two types of database ‘SQL’ and ‘NoSQL’. In SQL database, data are stored as table manner and in Nosql database data are stored independently with some particular way to identify each record independently. We can also create our own database or datastore locally in Nosql manner.
There are some steps involve in creating the local database and create, delete information of it. These steps are as follows:
- Create package.json file in root of project directory using the following command:
npm init -y
- Install express and body-parser package using the following command:
npm install body-parser npm install express
- Create a POST route to delete a particular user record using id.
- Set the server to run on a specific port(Developer’s port – 3000).
- Create a repository file and add all the logic related to creating local database.
- Create a method in repository file to delete a record from database using id.
Example: This example illustrates how to delete a record from a local custom database.
Filename: index.js file
Filename: repository.js file This file contains all the logic to delete a record of custom database.
Filename: package.json file
Run index.js file using the following command:
Note: For the first time running the program database (datastore.json) file not exist in the project directory, it created dynamically after running the program. But here we try to delete a record from the database that means program suppose to have already run once and some records are added into the database that we try to delete.