跳到主要内容

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 预览功能,很可能已经遇到过这些问题。

移除数组快捷方式

作为本次主要更新的一部分,一些数组快捷方式被移除。这些快捷方式曾是向基于数组的操作符添加单个元素值的方式。

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 操作符将只接受数组值。

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