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 fetch records from it using key-value pair information of record. These steps are as follows:
- Create package.json file in the root of the project directory using the following command:
npm init -y
- Install express and body-parser package using the following command:
npm install express npm install body-parser
- Create a POST route to request for a particular user from database using key-value information.
- 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 a local database.
- Create a method in the repository file to fetch a record from the database using key-value properties.
Note: If a record satisfied all the provided key-value properties then only the record is fetched(record object is returned) otherwise if any of the properties are not satisfied by the record then an empty object is returned.
Example: This example illustrates how to fetch a record from a local custom database using key-value pair properties.
Filename: fetchUser.js This js file has a method to display fetched user information to webpage.
Filename: repository.js This file contains all the logic to find a record using key-value information.
Run index.js file using the following command:
After Clicking the button
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 fetch information from the database using key-value pair information that means program suppose to have already run once and some records are added into the database that we try to fetch.
- How to add unique Id to each record in your local/custom database in Node.js ?
- How to update a record in your local/custom database in Node.js?
- How to delete a record from your local/custom database in Node.js ?
- Nodejs | Automatic restart NodeJs server with nodemon
- How to find record by Id from local/custom database in Node.js ?
- How to add records in your own local/custom database in Node.js ?
- How to store password securely in your local/custom database in Node.js ?
- How to hide or show one record from an ng-repeat within a table based on ng-click?
- Nodejs - Connect MongoDB with Node app using MongooseJS
- Cowsay in Nodejs using Requests library
- Nodejs | Web Crawling using Cheerio
- Signup Form Using Nodejs and MongoDB
- Nodejs | authentication using Passportjs and passport-local-mongoose
- NodeJs CRUD Operations Using Mongoose and MongoDB Atlas
- Generating Lighthouse Scores using NodeJS
- PHP | Get PHP configuration information using phpinfo()
- How to define style information of a document using HTML5 ?
- Web-Socket in NodeJS
- Dockerizing a simple Nodejs app
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.