跳到主要內容

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' }],
},
})
© . This site is unofficial and not affiliated with Prisma Data, Inc.