# CRUD Application with TypeScript & PostgreSQL

Concepts of CRUD App

### Github Repository

[https://github.com/Labs-2021/crud-node-postgreSQL](https://github.com/Labs-2021/crud-node-postgreSQL)

Our CRUD APP Components:

1.  NodeJS
2.  ExpressJS
3.  TypeORM
4.  PostgreSQL
5.  Postgres installation with Docker

**Init Package.json**

yarn init -y

**Install Express**

yarn add express

**Install TypeScript**

yarn add typescript ts-node-dev [@types/express](http://twitter.com/types/express "Twitter profile for @types/express") -D

**Init TypeScript**

yarn tsc — init

tsconfig.json

“target”: “es2021”,

### 1\. Express

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174748458/8oQctvAe9.jpeg)

import express from “express”;

const app = express();

app.listen(3000, ()=>console.log(“Server is running”));

### 2\. TypeORM

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174749707/_feOxtq_F.png)

### [#](https://typeorm.io/#undefined/installation) Installation

yarn add typeorm reflect-metadata pg

1.  Install the npm package:
2.  `yarn install typeorm --save`
3.  You need to install `reflect-metadata` shim:
4.  `yarn install reflect-metadata --save`
5.  and import it somewhere in the global place of your app (for example in `app.ts`):
6.  `import "reflect-metadata";`
7.  You may need to install node typings:
8.  `npm install @types/node --save-dev`
9.  Install a database drive

*   for **PostgreSQL** or **CockroachDB**
*   `yarn install pg --save`

### [#](https://typeorm.io/#using-ormconfig/using-environment-variables) Using environment variables

Create `.env` or `ormconfig.env` in the project root (near `package.json`). It should have the following content:

```
TYPEORM_CONNECTION = mysql  
TYPEORM_HOST = localhost  
TYPEORM_USERNAME = root  
TYPEORM_PASSWORD = admin  
TYPEORM_DATABASE = test  
TYPEORM_PORT = 3000  
TYPEORM_SYNCHRONIZE = true  
TYPEORM_LOGGING = true  
TYPEORM_ENTITIES = entity/*.js,modules/**/entity/*.js
```

ist of available env variables you can set:

*   TYPEORM\_CACHE
*   TYPEORM\_CACHE\_ALWAYS\_ENABLED
*   TYPEORM\_CACHE\_DURATION
*   TYPEORM\_CACHE\_OPTIONS
*   TYPEORM\_CONNECTION
*   TYPEORM\_DATABASE
*   TYPEORM\_DEBUG
*   TYPEORM\_DRIVER\_EXTRA
*   TYPEORM\_DROP\_SCHEMA
*   TYPEORM\_ENTITIES
*   TYPEORM\_ENTITIES\_DIR
*   TYPEORM\_ENTITY\_PREFIX
*   TYPEORM\_HOST
*   TYPEORM\_LOGGER
*   TYPEORM\_LOGGING
*   TYPEORM\_MAX\_QUERY\_EXECUTION\_TIME
*   TYPEORM\_MIGRATIONS
*   TYPEORM\_MIGRATIONS\_DIR
*   TYPEORM\_MIGRATIONS\_RUN
*   TYPEORM\_MIGRATIONS\_TABLE\_NAME
*   TYPEORM\_PASSWORD
*   TYPEORM\_PORT
*   TYPEORM\_SCHEMA
*   TYPEORM\_SID
*   TYPEORM\_SUBSCRIBERS
*   TYPEORM\_SUBSCRIBERS\_DIR
*   TYPEORM\_SYNCHRONIZE
*   TYPEORM\_URL
*   TYPEORM\_USERNAME
*   TYPEORM\_UUID\_EXTENSION

.env

TYPEORM\_CONNECTION = postgres

TYPEORM\_HOST = localhost

TYPEORM\_USERNAME = admin

TYPEORM\_PASSWORD = admin

TYPEORM\_DATABASE = code\_drops\_crud

TYPEORM\_PORT = 5432

TYPEORM\_MIGRATIONS = src/database/migrations/\*.ts

TYPEORM\_MIGRATIONS\_DIR = src/database/migrations

docker run — name postgres -e POSTGRES\_PASSWORD=admin -p 5432:5432 -d postgres

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174751104/GjbInpw_I.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174752501/PW0Z5ijnaW.png)

### BeeKeeper Studio

[**Installation | Beekeeper Studio**  
*Beekeeper Studio can be installed on Mac, Windows, and Linux desktops. Download the dmg installer file from , then drag…*docs.beekeeperstudio.io](https://docs.beekeeperstudio.io/installation/#linux-installation "https://docs.beekeeperstudio.io/installation/#linux-installation")[](https://docs.beekeeperstudio.io/installation/#linux-installation)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174753957/CpOdn6gQ1E.jpeg)

### TypeORM Migrations

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174755528/Hhgl_MM-u.png)

**Migrations Commands**

yarn typeorm migration:create -n CreateCategories

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174757125/7SkK3uFb5bQ.png)

yarn typeorm migration:run

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174758613/Vpd8hov4z.png)

yarn typeorm migration:revert

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174760148/zHvocTQCk.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174761608/_j9ybqpY7.png)

### Migration Videos

yarn typeorm migration:create -n CreateVideos

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174762911/6uDc2yX12.png)

### UUID

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174764186/tEJrHFpWu.png)

yarn add uuid

yarn add @types/uuid -D

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174765506/fjo3yj0EW.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174766806/H1oFB8Gog.png)

Insomnia CRUD Github

[https://github.com/Labs-2021/crud-node-postgreSQL/blob/main/.insomnia/Workspace/wrk\_4b5e4a867e9c4195b058da96bfa7571b.yml](https://github.com/Labs-2021/crud-node-postgreSQL/blob/main/.insomnia/Workspace/wrk_4b5e4a867e9c4195b058da96bfa7571b.yml)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174768171/ltlx6z1u5.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174769584/UIxlz_tgd.png)

### Create Video

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174770914/FNVcPsJp1.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174772208/YFGQ6x36W.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174773734/niOoVoGpa.png)

### 5\. Postgres installation with Docker

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174775287/tdS-F-27S.jpeg)

*   [https://hub.docker.com/\_/postgres](https://hub.docker.com/_/postgres)

docker pull postgres

docker run --name postgresql -e POSTGRES\_PASSWORD=admin -p 5432:5432 -d postgres

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662174776901/9RdrX2Alt.png)

❯ docker run -d --name postgresql-dev -e POSTGRESQL\_USERNAME=postgres -e POSTGRESQL\_PASSWORD=docker -e POSTGRESQL\_DATABASE=gobarber -p 5432:5432  bitnami/postgresql:latest
