id: 8ba2ec1a
参考記事
今回やること
- NestJSの初期化(hello worldまで)
- prisma の初期化
- postgreSQLの初期化
- prisma と postgreSQLの接続
NestJSの初期化
bash
$ npm i -g @nestjs/cli
$ nest new project-name
$ cd project-name
- tsconfigの修正
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": true,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
"strict": true
}
}
- listen portの変更(3000->3005)
main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3005);
}
bootstrap();
- hello world
bash
$ yarn start
localhost:3005でhello world が表示されればok
prisma の初期化
bash
$ yarn add -D prisma
$ yarn add @prisma/client
$ npx prisma init
postgreSQLの初期化
- postgreSQL用のdocker-compose.ymlの作成
docker-compose.yml
version: '3.8'
services:
dev-postgres:
image: postgres:14.4-alpine
ports:
- 5434:5432
environment:
POSTGRES_USER: qiita
POSTGRES_PASSWORD: qiita
POSTGRES_DB: qiita
restart: always
networks:
- lesson
networks:
lesson:
- DBコンテナの起動
bash
$ docker-compose up -d
prisma schemaの定義
schema.prisma
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
email String @unique
hashedPassword String
nickName String?
tasks Task[]
}
model Task {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String
description String?
userId Int
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
- schemaのmigration
bash
$ npx prisma migrate dev
- DBの状態確認
bash
$ npx prisma studio
- prisma client の更新
Whenever you make changes to your database that are reflected in the Prisma schema, you need to manually re-generate Prisma Client to update the generated code in the node_modules/.prisma/client directory:
bash
$ npx prisma generate