- 게시일
Prisma 시작하기

date
Oct 13, 2022
slug
prisma-시작하기
status
Published
tags
Prisma
ORM
summary
Prisma v4.4.0
type
Post
Prisma
Node.js 및 Typescript ORM(Object Relational Mapping)
Prisma 설치
npm
$ npm i -D prisma
yarn
$ yarn add -D prisma
Prisma 프로젝트 생성
$ npx prisma init
DB 설정
# .env # DB URL 입력 DATABASE_URL=postgresql://<사용자>:<비밀번호>@<주소>:5432/<DB명>
DB 테이블 작성
Visual Studio Code를 사용하는 경우 확장 프로그램 설치하기
// schema.prisma generator client { provider = "prisma-client-js" } datasource db { // 사용하는 DB에 맞게 provider 설정 provider = "postgresql" url = env("DATABASE_URL") } // 테이블 작성 model User { id Int @id @default(autoincrement()) createdAt DateTime @default(now()) email String @unique name String? }
Scalar types
type | mapping |
String | PostgreSQL - text
MySQL - varchar(191) |
Boolean | PostgreSQL - boolean
MySQL - TINYINT(1) |
Int | PostgreSQL - integer
MySQL - INT |
BigInt | PostgreSQL - bigint
MySQL - INT |
Float | PostgreSQL - double precision
MySQL - DOUBLE |
Decimal | PostgreSQL - decimal(65,30)
MySQL - DECIMAL(65,30) |
DateTime | PostgreSQL - timestamp(3)
MySQL - DATETIME(3) |
Json | PostgreSQL - jsonb
MySQL - JSON |
Bytes | PostgreSQL - bytea
MySQL - LONGBLOB |
DB에 적용
$ npx prisma db push
Prisma Client
Prisma Client는 데이터에 맞게 자동 생성되고 type-safe한 쿼리 빌더입니다.
DB에 쿼리 보내기
import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); // create user const newUser = await prisma.user.create({ data: { name: 'Alice', email: '[email protected]', }, }); // get users const users = await prisma.user.findMany();
Prisma Studio
Prisma Studio는 데이터베이스의 데이터를 위한 시각적 편집기입니다.
$ npx prisma studio