datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model ExtendedProfile {
id Int @id @default(autoincrement())
biography String
user User @relation(fields: [userId], references: [id])
userId Int @unique
}
model User {
id Int @id @default(autoincrement())
name String?
email String @unique
profileViews Int @default(0)
role Role @default(USER)
coinflips Boolean[]
posts Post[]
profile ExtendedProfile?
}
model Post {
id Int @id @default(autoincrement())
title String
published Boolean @default(true)
author User @relation(fields: [authorId], references: [id])
authorId Int
comments Json?
views Int @default(0)
likes Int @default(0)
categories Category[]
}
model Category {
id Int @id @default(autoincrement())
name String @unique
posts Post[]
}
enum Role {
USER
ADMIN
}
const user = await prisma.user.create({
data: {
email: 'elsa@prisma.io',
name: 'Elsa Prisma',
},
})
/*
{
id: 22,
name: 'Elsa Prisma',
email: 'elsa@prisma.io',
profileViews: 0,
role: 'USER',
coinflips: []
}
*/
const includePosts = someCheckFunction();
if (includePosts) {
user = {
email: 'elsa@prisma.io',
name: 'Elsa Prisma',
posts: {
create: {
title: 'Include this post!',
},
},
}
} else {
user = {
email: 'elsa@prisma.io',
name: 'Elsa Prisma',
}
}
const createMany = await prisma.user.createMany({
data: [
{ name: 'Bob', email: 'bob@prisma.io' },
{ name: 'Bobo', email: 'bob@prisma.io' }, // unique key 중복
{ name: 'Yewande', email: 'yewande@prisma.io' },
{ name: 'Angelique', email: 'angelique@prisma.io' },
],
skipDuplicates: true, // 중복된 키가 있는경우 스킵을해주는 옵션
})
// By unique identifier
const user = await prisma.user.findUnique({
where: {
email: 'elsa@prisma.io',
},
})
// By ID
const user = await prisma.user.findUnique({
where: {
id: 99,
},
})
/*
model TimePeriod {
year Int
quarter Int
total Decimal
@@id([year, quarter])
}
*/
const timePeriod = await prisma.timePeriod.findUnique({
where: {
year_quarter: { //항목1_항목2 형식으로 이름 작성, 이름이 있을경우 이름 입력
quarter: 4,
year: 2020,
},
},
})
const users = await prisma.user.findMany()
// 게시글 리스트 중에 하나라도 100개이상의 좋아요를 받은 게시글 리스트 중 가장 나중에 생성된 posts 반환
const findUser = await prisma.user.findFirst({
where: {
posts: {
some: {
likes: {
gt: 100
}
}
}
},
orderBy: {
id: "desc"
}
})
}
const users = await prisma.user.findMany({
where: {
email: {
endsWith: 'prisma.io',
},
},
})
// 여러개의 쿼리를 중첩할 때에는 때에는 OR, AND 조건 사용
const users = await prisma.user.findMany({
where: {
OR: [
{
name: {
startsWith: 'E',
},
},
{
AND: {
profileViews: {
gt: 0,
},
role: {
equals: 'ADMIN',
},
},
},
],
},
})
const user = await prisma.user.findUnique({
where: {
email: 'emma@prisma.io',
},
select: {
email: true,
name: true,
},
})
/*
{ email: 'emma@prisma.io', name: "Emma" }
*/
// 하위 요소에 특정 필드만을 가져오는 경우 select를 중첩된 위치에 사용
const user = await prisma.user.findUnique({
where: {
email: 'emma@prisma.io',
},
select: {
email: true,
posts: {
select: {
likes: true,
},
},
},
})
/*
{ email: 'emma@prisma.io', posts: [ { likes: 0 }, { likes: 0 } ] }
*/
const users = await prisma.user.findMany({
where: {
role: 'ADMIN',
},
include: {
posts: true,
},
})
/*
{
"id": 38,
"name": "Maria",
"email": "maria@prisma.io",
"profileViews": 20,
"role": "ADMIN",
"coinflips": [
true,
false,
false
],
"posts": []
},
{
"id": 39,
"name": "Oni",
"email": "oni2@prisma.io",
"profileViews": 20,
"role": "ADMIN",
"coinflips": [
true,
false,
false
],
"posts": [
{
"id": 25,
"authorId": 39,
"title": "My awesome post",
"published": true,
"comments": null,
"views": 0,
"likes": 0
}
]
}
*/
const updateUser = await prisma.user.update({
where: {
email: 'viola@prisma.io',
},
data: {
name: 'Viola the Magnificent',
},
})
const updateUsers = await prisma.user.updateMany({
where: {
email: {
contains: 'prisma.io',
},
},
data: {
role: 'ADMIN',
},
})
const upsertUser = await prisma.user.upsert({
where: {
email: 'viola@prisma.io',
},
update: {
name: 'Viola the Magnificent',
},
create: {
email: 'viola@prisma.io',
name: 'Viola the Magnificent',
},
})
const updatePosts = await prisma.post.updateMany({
data: {
views: {
increment: 1,
},
likes: {
increment: 1,
},
},
})
const deleteUser = await prisma.user.delete({
where: {
email: 'bert@prisma.io',
},
})
const deleteUsers = await prisma.user.deleteMany({
where: {
email: {
contains: 'prisma.io',
},
},
})
const deleteUsers = await prisma.user.deleteMany({})
const deleteUser = await prisma.user.delete({
where: {
email: 'bert@prisma.io',
},
}) // The change you are trying to make would violate the required relation 'PostToUser' between the `Post` and `User` models.
model Post {
id Int @id @default(autoincrement())
author User? @relation(fields: [authorId], references: [id])
authorId Int?
author User @relation(fields: [authorId], references: [id])
authorId Int
}
const deletePosts = prisma.post.deleteMany({
where: {
authorId: 7,
},
})
const deleteUser = prisma.user.delete({
where: {
id: 7,
},
})
const transaction = await prisma.$transaction([deletePosts, deleteUser])
출처:
https://www.prisma.io/docs/concepts/components/prisma-client/crud