CRUD Operations with TypeORM
Learn how to perform basic CRUD operations using NestJS as the API library.
Overview
In this lesson, we’ll look at CRUD operations. We’ll be using NestJS as the API library. It includes three basic files:
app.controller.ts
: This file handles all the routes, for example,/get/employees
.app.service.ts
: This file has the algorithms and implementations that connect with databases and third-party APIs.app.module.ts
: This module registers other modules (for example, entities) that are used in theapp.service.ts
orapp.controller.ts
files.
Migration
Whenever we update or add an entity, it’s essential to run a migration. A migration updates the SQL table to fit the entity or the model.
Open package.json
, and add two more scripts to generate and run a migration, as shown below:
Press + to interact
"scripts": {"migrate:generate": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:generate -d data-source.ts ./src/migrations/$npm_config_name","migrate:revert": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:revert -d data-source.ts","migrate:run": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:run -d data-source.ts"}
Next, at the root directory, create a data-source.ts
file. This ...