NodeJS Gateway — Part 2: Setting Up our Database (MongoDB)
Tags: mongoose, typescript, javascript, node
Published: 2024-06-26 06:08:56

---
title: NodeJS Gateway — Part 2: Setting Up our Database (MongoDB)
published: true
date: 2022-08-30 20:51:56 UTC
tags: mongoose,typescript,javascript,nodejs
canonical_url: 
---

### NodeJS Gateway — Part 2: Setting Up our Database (MongoDB)

![](https://cdn-images-1.medium.com/max/1024/1*_lp6l_eZVFnUAZVZ8V0_Iw.jpeg)
_Photo by Rubaitul Azad on Unsplash_

NoSQL databases make it easy to start developing your project or application with basic schemas, and since consistency can be jumped over with this type of database, expansion becomes easy as our variables and interactions increase and this is why we are using MongoDB for our gateway. This is also because the API application (refer to the link below about the project we are building) will be the one holding all the final transactional or interaction data (Postgres).

Just in case you are confused of what we are developing, please visit the link below:

[How I built a Payment Gateway using Javascript.](https://medium.com/@nkunzecaleb/how-i-build-a-payment-gateway-using-javascript-2e4f3e52b7cd)

In the previous post (Part 1), we were able to add the mongodb and mongoose npm modules to our gateway. In this post, we are going to concentrate on creating collections’ schemas, saving, retrieving, updating and deleting data from our database collections.

If you are wondering what collections are, you can relate them to tables or relations in SQL databases. Since the NoSQL databases are non-tabular databases which consist of JSON-like documents types of databases among other of which MongoDB is part. Therefore, the records (in SQL databases) or entries are documents in NoSQL, and a collection is a group of documents (records). I hope that is not confusing. You check out more broad explanations here [NoSQL Explained](https://www.mongodb.com/nosql-explained), [TechTarget](https://www.techtarget.com/searchdatamanagement/definition/NoSQL-Not-Only-SQL) and [NoSQL Databases](https://www.couchbase.com/resources/why-nosql).

We are going to look at inserting data for a basic demo internal (app) transfer transaction having transaction\_id transaction\_type sender\_account\_id recipient\_account\_id amount currency transaction\_note then date\_time .

First, we will start with creating a schema which in simple terms is the format of the data that is to be saved in the database. Let’s go ahead and create a model file transactionModel.ts for our transaction data and add the following code.

<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af9a5e596d29b884536a4a9890ee1185/href">https://medium.com/media/af9a5e596d29b884536a4a9890ee1185/href</a></iframe>

From the code, we see that all inputs are required apart from the transaction note which is optional. After defining the schema transactionSchema we create the transactionModel as on line 13 which will be used in searching, saving and retrieving transaction related data. Remember, this is basic and we will add on more values or variables as we go on.

Let’s go ahead and test our model to see if we can save and retrieve data from the database. And to do this, we are going to create 3 routes for saving and retrieving our transaction data.

- POST /api/transaction Create a transaction
- GET /api/transaction/:id Get transaction data for a single transaction
- GET /api/transactions Get All transactions

This part is more about database so we will not go deeper into routes. For now we can have basic routing to test our schema. For this, we are going to add a body-parser module that will help us make a middleware to convert our requests data to JSON format for storing. Use the command below.

```
npm install body-parser --save
```

Now we can add the code below to our app.ts file

```
const bodyParser = require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
```

So that we have the app.ts looking like this

<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf24dbf144c37c944e8402a9ebda76d1/href">https://medium.com/media/cf24dbf144c37c944e8402a9ebda76d1/href</a></iframe>

We then create a transactions route file transactions.ts in the routes folder. Finally we add the code below to it:

<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0259d5c56c2d71226309875d23263120/href">https://medium.com/media/0259d5c56c2d71226309875d23263120/href</a></iframe>

You can run npm run start to build and start our server.

From the above code save the transaction created by calling the mongoose start() function on the request body data received, and this sends back a response to the client when the saving process is successful or not. See the screenshot below from postman.

![](https://cdn-images-1.medium.com/max/1024/1*FSv_bbkyQwcgKLVsv0lU4A.png)
_The POST request for saving the transaction details (Successful)_

To get one transaction, we pass the id parameter in the route which is used to search for the transaction similar to the transactionId that it was generated during the transaction creation. See the screenshot below from postman.

![](https://cdn-images-1.medium.com/max/1024/1*YJ735zCcD_B801obC_-XjA.png)
_The GET request to retrieve a single transaction (Successful)_

Then we can also query for all transactions to be returned by passing no argument in the find() function of mongoose. See the screenshot below from postman.

![](https://cdn-images-1.medium.com/max/1024/1*dvzE14KL5MfN_VvS1r3OZg.png)
_The GET request to retrieve all transactions (Successful)_

A failed action will look as below in postman.

![](https://cdn-images-1.medium.com/max/1024/1*Jy1Gp57JvnYueS7C6Z352Q.png)
_A failed GET request for retrieval of a single transaction._

Well done for coming all this way, we have seen how we will be interacting with our database, and in other parts we will be looking at modifications and deleting of our data from the database.

I would like to thank you again for finishing this post, you can clap or even follow me for more posts like this as I traverse through my journey of creating a payment gateway. Please be free to rectify me on where I went wrong. Learning is the way to Go.

Next we will be looking at routes, middleware and hopefully controllers. I try to make these as short as possible to avoid having a full text book in one go 🤩.

In case you want to see the finished code (up-to-this stage), you can get it from the repo using the link below.

[GitHub - Cank256/payie: A payment solution project](https://github.com/Cank256/payie)

Till next post 😎.

* * *