jsonProtocol 變更
從 Prisma ORM 5.0.0 版本開始,新的 jsonProtocol
已成為預設協定。有些變更直接由此變更產生,另有一些變更與新協定相關。
Prisma ORM 5 的完整變更列表請見我們的發行說明。
jsonProtocol
特定變更
以下是 jsonProtocol
功能在 Prisma ORM 5 中成為預設協定後直接產生的變更。
移除 jsonProtocol
預覽功能
在 Prisma ORM 5 中,jsonProtocol
是 Prisma Client 的預設且唯一協定。不再需要 jsonProtocol
預覽功能。
Prisma ORM 4 及更低版本
generator client {
provider = "prisma-client-js"
previewFeatures = ["jsonProtocol"]
}
Prisma ORM 5
generator client {
provider = "prisma-client-js"
}
改善的錯誤訊息
由於切換至新協定,多個錯誤訊息已獲得改善。例如,Prisma ORM 4 及更低版本中的以下錯誤訊息
Failed to validate the query: `Unable to match input value to any allowed input type for the field. Parse errors: [Query parsing/validation error at `Mutation.createOneUser.data.UserCreateInput.person.PersonCreateNestedOneWithoutUserInput.create`: Unable to match input value to any allowed input type for the field. Parse errors: [Query parsing/validation error at `Mutation.createOneUser.data.UserCreateInput.person.PersonCreateNestedOneWithoutUserInput.create.PersonCreateWithoutUserInput.hubspot_id`: A value is required but not set., Query parsing/validation error at `Mutation.createOneUser.data.UserCreateInput.person.PersonCreateNestedOneWithoutUserInput.create.PersonUncheckedCreateWithoutUserInput.hubspot_id`: A value is required but not set.], Query parsing/validation error at `Mutation.createOneUser.data.UserUncheckedCreateInput.person`: Field does not exist on enclosing type.]` at `Mutation.createOneUser.data`
在 Prisma ORM 5 中變為以下訊息
Invalid `prisma.user.create()` invocation in
/Users/prismo/projects/prisma/reproductions/workbench/index.ts:21:36
18 const prisma = new PrismaClient()
19
20 for (const u of userData) {
→ 21 const user = await prisma.user.create({
data: {
email: "eugene.albright@gallaudet.edu",
person: {
create: {
first_name: "William",
last_name: "Albright",
+ hubspot_id: String
}
}
}
})
Argument `hubspot_id` must not be null.
jsonProtocol
相關變更
以下是與切換至新協定相關的變更。如果您曾使用 jsonProtocol
預覽功能,您很可能遇到過這些問題。
移除陣列快捷方式
作為此重大更新的一部分,多個陣列快捷方式已被移除。這些快捷方式是一種將單一元素作為值新增至基於陣列的運算子的方法。
OR
運算子
Prisma ORM 4 及更低版本中的以下程式碼
prisma.user.findMany({
where: {
OR: { email: 'foo@example.com' },
},
})
在 Prisma ORM 5 中需要變更為以下程式碼
prisma.user.findMany({
where: {
OR: [{ email: 'foo@example.com' }],
},
})
OR
運算子將僅接受陣列值。
in
和 notIn
運算子
與 OR
類似,in
和 notIn
也需要陣列值。
Prisma ORM 4 及更低版本
prisma.user.findMany({
where: {
id: { in: 123 },
},
})
prisma.user.findMany({
where: {
id: { notIn: 123 },
},
})
Prisma ORM 5
prisma.user.findMany({
where: {
id: {
in: [123],
},
},
})
prisma.user.findMany({
where: {
id: {
notIn: [123],
},
},
})
針對單一元素的建議
如果您的 in
和 notIn
值僅有一個元素,您也可以更新您的程式碼以完全不使用這些運算子
prisma.user.findMany({
where: {
id: 123,
},
})
prisma.user.findMany({
where: {
id: { not: 123 },
},
})
PostgreSQL 中篩選 JSON 欄位的 path
引數
當在 PostgreSQL 模型中篩選 JSON 欄位時,path
引數現在僅接受陣列。
當使用以下結構描述時
model User {
id String @id
settings Json
}
Prisma ORM 4 及更低版本
prisma.user.findMany({
where: {
settings: {
path: 'someSetting',
equals: someValue,
},
},
})
Prisma ORM 5
prisma.user.findMany({
where: {
settings: {
path: ['someSetting'],
equals: someValue,
},
},
})
注意:此 path
引數變更僅影響 PostgreSQL 資料庫。MySQL 資料庫不受影響,因為它們使用不同的語法。
純量列表
純量列表值在所有操作中都必須是陣列。
使用以下結構描述
model Post {
id String @id @default(uuid())
tags String[]
}
Prisma ORM 4 及更低版本
prisma.post.create({
data: {
tags: 'databases',
},
})
prisma.post.findMany({
where: {
tags: 'databases',
},
})
Prisma ORM 5
prisma.post.create({
data: {
tags: ['databases'],
},
})
prisma.post.findMany({
where: {
tags: ['databases'],
},
})
複合列表
對複合類型列表 (適用於 MongoDB) 的操作現在僅接受陣列值。
使用以下結構描述
model Post {
id String @id @default(uuid())
commentsList Comment[]
}
type Comment {
text String
}
Prisma ORM 4 及更低版本
prisma.post.findMany({
where: {
commentsList: {
equals: { text: 'hello' },
},
},
})
Prisma ORM 5
prisma.post.findMany({
where: {
commentsList: {
equals: [{ text: 'hello' }],
},
},
})
速記法用法
如果您使用速記法並排除 equals
,您仍然必須為複合列表欄位提供陣列值。
Prisma 4 及更低版本
prisma.post.create({
data: {
commentsList: { text: 'hello' },
},
})
prisma.post.findMany({
where: {
commentsList: { text: 'hello' },
},
})
Prisma 5
prisma.post.create({
data: {
commentsList: [{ text: 'hello' }],
},
})
prisma.post.findMany({
where: {
commentsList: [{ text: 'hello' }],
},
})