Serverless CRUD API Best Practices

Serverless Database creation, Class and connection handling………..

Shanira Perera
5 min readSep 21, 2022

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

1
2
3
4
5

Note : In that section DB public access is not a good idea but i put this to public is for only demonstrate the tables.

6

Ok lets go to Coding part………..

01. Create Entity class

Create student.entity.ts class under entity folder

Creating student.entity.ts

Im going to use typeORM framework for work with database

Run this in Terminal → npm i typeorm pg (pg means by postgres)

Entity Class → student.entity.ts

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

Database manager class

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

adding 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

Database config

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

Enter Server Name
Connection tab

Host Name should be the name that we copied from AWS.

Enter a password and tick the save password then hit SAVE.

Create database called studentdb

ok now we need to copy the parameters and fill our code.

  • DBHOST NAME : it is the endpoint name that we already copied.
Fill the Variables

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.

student directory > create.ts

3.2 Create index.ts under Student directory

student directory > index.ts

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!

student directory > schema.ts

3.4 Create student-service.ts

This includes the entire business logic of creating student

Student-service.ts

04. Create Response.ts

Response.ts

We can set the output message according to the statuscode.

05. Create Index.ts (global)

index.ts (Global)

06. RUN your Code — — — — — — — — — — — — —✅

command : serverless offline

Copy the endpoint URL : POST | http://localhost:3000/dev/students

6.1 open postman

Add to this on POSTMAN
add x-api-Key

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

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

--

--

Shanira Perera
Shanira Perera

Written by Shanira Perera

Hello, I’m Shanira, welcome to the world of technology. I am an Associate software engineer at virtusa. So let’s begin!

No responses yet