Serverless CRUD API Best Practices
Serverless Database creation, Class and connection handling………..
What is the benefit of Serverless Database?
When you use the regular database you have to pay for the entire 24 hours, which doesn’t work because you have to pay for your idle time as well. So if we use server less database we have to pay only for the time we use the database.
Create Amazon Aurora Database
Path Amazon RDS > Databases > Create Database
Note : In that section DB public access is not a good idea but i put this to public is for only demonstrate the tables.
Ok lets go to Coding part………..
01. Create Entity class
Create student.entity.ts class under entity folder
Im going to use typeORM framework for work with database
Run this in Terminal → npm i typeorm pg (pg means by postgres)
you need to add this “experimentalDecorators”: true, to tsconfig.json file otherwise you get experimental support error
02. Create service to do the transaction between the entity and the database
Create database.manager.ts class under libs
In this section you can see i use process.env.variables why i used this it because when it was deployed these are put as environment variables. so we can change it.
NOTE: If someone had a access to the console he can see these variables think we store password in their it also visible to him. but it also have a solution i will talk it from another article.
add the variables to environment
Now we need those values from our database first of all we have to check whether our db is work or not.
Copy this endpoint name
Now we need a tool to handle our DB so we use pgAdmin
Download link : https://www.pgadmin.org/download/pgadmin-4-windows/
Steps to register a server
Host Name should be the name that we copied from AWS.
Enter a password and tick the save password then hit SAVE.
ok now we need to copy the parameters and fill our code.
- DBHOST NAME : it is the endpoint name that we already copied.
ok now we complete the connection of db now we have to create the employee so that we create a class called employee
03. Create Student Directory
3.1 Create class called create.ts
important of using middyfy ==> using middyfy is it do the validations separately therefore code will not be cluttered and code only focus on the business logic.
3.2 Create index.ts under Student directory
Usage of Index file → One directory you have set of functions so it hard to go one by one and check those functions therefore it has a method of using index file and it will export all the functions in the directory to the global index file. because it well organized!
3.3 Create schema.ts under student directory
This will use to validate your incoming requests.
Ex: First Name is required!
3.4 Create student-service.ts
This includes the entire business logic of creating student
04. Create Response.ts
We can set the output message according to the statuscode.
05. Create Index.ts (global)
06. RUN your Code — — — — — — — — — — — — —✅
command : serverless offline
Copy the endpoint URL : POST | http://localhost:3000/dev/students
6.1 open postman
Header x-api-Key is on the console log — — — — — -
— — — — — — — — — — — — — — — You re Done ! — — — — — — — — — — — — — — — —
Please follow and give a small clap if you learned something ☺☺☺☺
Reference : https://www.youtube.com/watch?v=nwMPpCQyMeY&t=1448s
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —