Skip to content

wilrobg/NestJs-example-BE

Repository files navigation

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Description

Nest framework TypeScript starter repository.

Installation

$ npm install

Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Solution

Part1: SQL

--Who are the first 10 authors ordered by date_of_birth?
select * from author order by date_of_birth limit 10;
--What is the sales total for the author named “Lorelai Gilmore”?
select
  sum(quantity * item_price) sale
from
  sale_item s
  inner join book b on b.id = s.book_id
  inner join author a on a.id = b.author_id
where
  a.name ilike 'Lorelai Gilmore';
--What are the top 10 performing authors, ranked by sales revenue?	
select
  a.name,
  sum(quantity * item_price) sale
from
  sale_item s
  inner join book b on b.id = s.book_id
  inner join author a on a.id = b.author_id
group by
  a.name
order by
  sale desc
LIMIT
  10;

Part2: Basic API Endpoint

After configurate the .env file, hit the follow endpoint:

/bookstore?name=Maya%20Schamberger

name parameter is optional

Part3: API Performance

A layer was added to improve performances, it acts as a temporary data store providing high performance data access. For this API use built-in one, an in-memory data store.

Part4: Build Docker Container and steps to deploy

Creating a Dockerfile:

FROM node:12.19.0-alpine3.9 AS development

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install glob rimraf

RUN npm install --only=development

COPY . .

RUN npm run build

FROM node:12.19.0-alpine3.9 as production

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install --only=production

COPY . .

COPY --from=development /usr/src/app/dist ./dist

CMD ["node", "dist/main"]

Build Docker image

The first step to deploying the application to Kubernetes is to build a Docker images. Build and tag the Docker image for the app:

docker build -t ravn_challenge .

then upload image to Docker Hub

docker push <username>/ravn-be-challenge:1.0.0

Deploying the sample app to GKE

We are ready to to deploy the Docker image you built to your GKE cluster.

kubectl create deployment ravn-be-challenge --image=<username>/ravn-be-challenge:1.0.0

Set the baseline number of Deployment replicas to 3.

kubectl scale deployment ravn-be-challenge --replicas=3

Create a HorizontalPodAutoscaler resource for the api Deployment

kubectl autoscale deployment ravn-be-challenge --cpu-percent=80 --min=1 --max=5

To see the Pods created, run the following command:

kubectl get pods

Use the kubectl expose command to generate a Kubernetes Service for the ravn-be-challenge deployment

kubectl expose deployment ravn-be-challenge --name=ravn-be-challenge-service --type=LoadBalancer --port 80 --target-port 8080

Here, the --port flag specifies the port number configured on the Load Balancer, and the --target-port flag specifies the port number that the ravn-be-challenge container is listening on.

Run the following command to get the Service details for ravn-be-challenge-service:

kubectl get service

You get the external ip, to look up for the service

License

Nest is MIT licensed.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published