Skip to content

Latest commit

 

History

History
240 lines (176 loc) · 3.62 KB

03-Add-PostgreSQL-with-TypeORM.md

File metadata and controls

240 lines (176 loc) · 3.62 KB

[Kamil Mysliwiec, Mark Pieszak] NestJS Fundamentals Course [ENG, 2020]


03. Add PostgreSQL with TypeORM


020. Before we Get Started


021. Prerequisite Install Docker


022. Running PostgreSQL

$ docker-compose up -d

023. Introducing the TypeORM Module

$ npm install @nestjs/typeorm typeorm pg

024. Creating a TypeORM Entity


025. Using Repository to Access Database

$ curl -d '{
    "name": "Coffee #1",
    "brand": "Nesti",
    "flavors": ["chocolate"]
}' \
-H "Content-Type: application/json" \
-X POST http://localhost:3000/coffees/ \
| python -m json.tool

$ curl \
    -H "Content-Type: application/json" \
    -X GET http://localhost:3000/coffees \
    | python3 -m json.tool

$ curl \
    -H "Content-Type: application/json" \
    -X GET http://localhost:3000/coffees/1 \
    | python3 -m json.tool

026. Create a Relation between two Entities

$ nest generate class coffees/entities/flavor.entity --no-spec

027. Retrieve Entities with their Relations


$ curl \
    -H "Content-Type: application/json" \
    -X GET http://localhost:3000/coffees \
    | python3 -m json.tool

returns:

[
    {
        "id": 2,
        "name": "Coffee #2",
        "brand": "Nesti",
        "flavors": []
    },
    {
        "id": 1,
        "name": "Coffee #1",
        "brand": "Nesti",
        "flavors": []
    }
]


028. Using Cascading Inserts and Updates

$ curl -d '{
    "name": "Coffee #4",
    "brand": "Nesti",
    "flavors": ["caramel", "chocolate"]
}' \
-H "Content-Type: application/json" \
-X POST http://localhost:3000/coffees/ \
| python -m json.tool

{
    "brand": "Nesti",
    "flavors": [
        {
            "id": 1,
            "name": "caramel"
        },
        {
            "id": 2,
            "name": "chocolate"
        }
    ],
    "id": 3,
    "name": "Coffee #4"
}

$ curl \
    -H "Content-Type: application/json" \
    -X GET http://localhost:3000/coffees \
    | python3 -m json.tool

[
    {
        "id": 3,
        "name": "Coffee #4",
        "brand": "Nesti",
        "flavors": [
            {
                "id": 1,
                "name": "caramel"
            },
            {
                "id": 2,
                "name": "chocolate"
            }
        ]
    },
    {
        "id": 2,
        "name": "Coffee #2",
        "brand": "Nesti",
        "flavors": []
    },
    {
        "id": 1,
        "name": "Coffee #1",
        "brand": "Nesti",
        "flavors": []
    }
]


029. Adding Pagination

$ nest generate class common/dto/pagination-query.dto --no-spec

$ curl \
    -H "Content-Type: application/json" \
    -X GET http://localhost:3000/coffees?limit=1 \
    | python3 -m json.tool

$ curl \
    -H "Content-Type: application/json" \
    -X GET http://localhost:3000/coffees?offset=1 \
    | python3 -m json.tool

030. Use Transactions

$ nest generate class events/enities/event.entity --no-spec

031. Adding Indexes to Entities


032. Setting up Migrations

$ npx typeorm migration:create -n CoffeeRefactor

write migrations

$ npm run build
$ npx typeorm migration:run
$ npx typeorm migration:revert

// Not Works for me
$ npx typeorm migration:generate -n SchemaSync



Marley

Any questions in english: Telegram Chat
Любые вопросы на русском: Телеграм чат