NodeJS Gateway — Part 2: Setting Up our Database (MongoDB)
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.
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, TechTarget and NoSQL Databases.
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.
https://medium.com/media/af9a5e596d29b884536a4a9890ee1185/hrefFrom 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
https://medium.com/media/cf24dbf144c37c944e8402a9ebda76d1/href
We then create a transactions route file transactions.ts in the routes folder. Finally we add the code below to it:
https://medium.com/media/0259d5c56c2d71226309875d23263120/href
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.
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.
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.
The GET request to retrieve all transactions (Successful)
A failed action will look as below in postman.
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
Till next post 😎.
Top comments (0)