Build a Nest.js App With TypeORM and Postgres | by Kevin Vogel | Feb, 2022

Create scalable apps with Nest.js & TypeORM

Kevin Vogel

It’s required to have a basic understanding of Node.js, TypeScript, and PostgreSQL which you have installed locally on your machine. I will choose Visual Studio Code as my code editor. You can use whatever you prefer.

First, we need to create our PostgreSQL database. I know, everybody handles that differently, some people use a GUI, but we are going to use our terminal. Again, you need to have PostgreSQL installed on your machine. If you have PostgreSQL installed, the following four commands will run on Linux, Mac, and Windows machines.

$ psql postgres
$ CREATE DATABASE nest_api;
$ l
$ q
  • CREATE DATABASE nest_api; creating the database we need
  • l list all databases
  • q CLI exit

Let’s continue with Nest.js! We are going to install the Nest.js CLI, so open the terminal of your choice and type:

$ npm i -g @nestjs/cli
$ nest new nest-postgres -p npm
$ cd nest-postgres
$ code .
$ git add .
$ git commit -m "chore(): init nest.js"
$ npm i @nestjs/config @nestjs/typeorm typeorm pg class-validator class-transformer
$ npm i -D @types/node
$ mkdir src/shared
$ mkdir src/shared/typeorm
$ mkdir src/common
$ mkdir src/common/envs
$ mkdir src/common/helper
$ touch src/shared/typeorm/typeorm.service.ts
$ touch src/common/envs/.env
$ touch src/common/envs/development.env
$ touch src/common/helper/env.helper.ts
nest-cli.json
nest-cli.json
src/common/helper/env.helper.ts
src/comon/envs/.env
src/comon/envs/development.env
src/app.module.ts
src/app.module.ts
src/shared/typeorm/typeorm.service.ts
src/app.module.ts
src/app.module.ts
$ npm run start:dev
$ nest g mo api
$ nest g mo api/user && nest g co api/user
--no-spec && nest g s api/user --no-spec
$ touch src/api/user/user.entity.ts
$ touch src/api/user/user.dto.ts
src/api/user/user.entity.ts
src/api/user/user.dto.ts
src/api/user/user.module.ts
src/api/user/user.module.ts
src/api/user/user.service.ts
src/api/user/user.service.ts
src/api/user/user.controller.ts
src/api/user/user.controller.ts
$ npm run start:dev
$ curl -X POST http://localhost:3000/user -H "Content-Type: application/json" -d '{"name": "Elon Musk", "email": "elon@gmail.com"}'Server Response:{"name":"Elon Musk","email":"elon@gmail.com","id":1,"isDeleted":false,"createdAt":"2022-02-18T14:12:13.672Z","updatedAt":"2022-02-18T14:12:13.672Z"}

Leave a Comment