跳到主要内容

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: "[email protected]",
person: {
create: {
first_name: "William",
last_name: "Albright",
+ hubspot_id: String
}
}
}
})

Argument `hubspot_id` must not be null.

以下是与切换到新协议相关的变更。如果您正在使用 jsonProtocol 预览功能,您很可能遇到过这些问题。

移除数组快捷方式

作为此重大更新的一部分,一些数组快捷方式被移除。这些快捷方式是将单个元素作为值添加到基于数组的运算符的一种方式。

OR 运算符

以下是在 Prisma ORM 4 及更低版本中的代码

prisma.user.findMany({
where: {
OR: { email: '[email protected]' },
},
})

需要在 Prisma ORM 5 中更改为以下代码

prisma.user.findMany({
where: {
OR: [{ email: '[email protected]' }],
},
})

OR 运算符将只接受数组值。

innotIn 运算符

OR 类似,innotIn 也需要数组值。

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],
},
},
})
针对单个元素的建议

如果您的 innotIn 值只有一个元素,您也可以更新您的代码,完全不使用这些运算符

prisma.user.findMany({
where: {
id: 123,
},
})

prisma.user.findMany({
where: {
id: { not: 123 },
},
})

在 PostgreSQL 中过滤 JSON 字段的 path 参数

当在 PostgreSQL 模型中过滤 JSON 字段时path 参数现在只接受数组。

当使用以下 schema 时

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 数据库不受影响,因为它们使用不同的语法。

标量列表

标量列表的值在所有操作中都必须是数组。

使用以下 schema

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)现在只接受数组值。

使用以下 schema

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' }],
},
})