nestjs 세팅
nestjs docs - first steps (opens in a new tab)
git clone 링크
cd 프로젝트 폴더
nest new .
- 레포지토리를 만들때 README.md를 이미 만들어놔서 그냥 삭제하고 실행했다 (이미 만들어져 있는 README.md가 있으면 nest new . 실패)
pnpm i prisma @prisma/client
pnpm prisma init
pnpm prisma generate
pnpm prisma format
pnpm prisma migrate -dev --name init
pnpm prisma studiopnpm i -D husky lint-staged
pnpm husky init.husky/pre-commit
pnpm lint-stagedpackage.json
"lint-staged": {
"*.ts": [
"eslint --fix",
"prettier --write"
]
},.prettierrc
{
"singleQuote": true,
"trailingComma": "all",
"semi": true,
"tabWidth": 2,
"useTabs": false,
"printWidth": 120
}port 설정
prisma 설정
pnpm add -D prisma
pnpm prisma init생성된 .env 파일을 DB 이름에 맞춰 수정
model User {
id Int @default(autoincrement()) @id
email String @unique
nickname String @unique
passwordHash String
createdAt DateTime @default(now())
}테스트 모델 추가하고
pnpm prisma migrate dev --name init
pnpm add @prisma/client
pnpm prisma generatepnpm prisma studio로 확인 가능
pnpm add @prisma/adapter-pg 다운
apps / api / src / prisma.service.ts
import { Injectable } from '@nestjs/common'
import { PrismaPg } from '@prisma/adapter-pg'
import { PrismaClient } from '../generated/prisma/client'
@Injectable()
export class PrismaService extends PrismaClient {
constructor() {
const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL
})
super({ adapter })
}
}import { Injectable } from '@nestjs/common';
import { Prisma, User } from '../generated/prisma/client.js';
import { PrismaService } from './prisma.service';
@Injectable()
export class UserService {
constructor(private prisma: PrismaService) {}
async user(
userWhereUniqueInput: Prisma.UserWhereUniqueInput,
): Promise<User | null> {
return this.prisma.user.findUnique({
where: userWhereUniqueInput,
});
}
async users(params: {
skip?: number;
take?: number;
cursor?: Prisma.UserWhereUniqueInput;
where?: Prisma.UserWhereInput;
orderBy?: Prisma.UserOrderByWithRelationInput;
}): Promise<User[]> {
const { skip, take, cursor, where, orderBy } = params;
return this.prisma.user.findMany({
skip,
take,
cursor,
where,
orderBy,
});
}
async createUser(data: Prisma.UserCreateInput): Promise<User> {
return this.prisma.user.create({
data,
});
}
async updateUser(params: {
where: Prisma.UserWhereUniqueInput;
data: Prisma.UserUpdateInput;
}): Promise<User> {
const { where, data } = params;
return this.prisma.user.update({
data,
where,
});
}
async deleteUser(where: Prisma.UserWhereUniqueInput): Promise<User> {
return this.prisma.user.delete({
where,
});
}
}
- https: //www.prisma.io/docs/guides/frameworks/nestjs (opens in a new tab)
- https: //docs.nestjs.com/recipes/prisma nestjs에서 prisma 예시 코드와 prisma에서 nestjs 예시로 있는 코드가 조금 다름
import { Body, Controller, Get, Post } from '@nestjs/common';
import { UserModel } from '../generated/prisma/models';
import { AppService } from './app.service';
import { UserService } from './user.service';
@Controller()
export class AppController {
constructor(
private readonly userService: UserService,
private readonly appService: AppService,
) {}
@Get('message')
getMessage() {
return this.appService.getMessage();
}
@Post('user')
async signupUser(
@Body() userData: { nickname: string; email: string; passwordHash: string },
): Promise<UserModel> {
return this.userService.createUser(userData);
}
}
pnpm add @nestjs/swagger
- https://docs.nestjs.com/openapi/introduction (opens in a new tab)
- https://docs.nestjs.com/techniques/validation (opens in a new tab)
- https://docs.nestjs.com/openapi/mapped-types (opens in a new tab)
pnpm add bcrypt
pnpm add -D @types/bcrypt