Skip to main content

字段和类型

本节涵盖你可以与 Prisma 客户端一起使用的各种特殊字段和类型。

¥This section covers various special fields and types you can use with Prisma Client.

Decimal 合作

¥Working with Decimal

Decimal 字段由 Decimal.js library 表示。以下示例演示了如何导入和使用 Prisma.Decimal

¥Decimal fields are represented by the Decimal.js library. The following example demonstrates how to import and use Prisma.Decimal:

import { PrismaClient, Prisma } from '@prisma/client'

const newTypes = await prisma.sample.create({
data: {
cost: new Prisma.Decimal(24.454545),
},
})

你还可以执行算术运算:

¥You can also perform arithmetic operations:

import { PrismaClient, Prisma } from '@prisma/client'

const newTypes = await prisma.sample.create({
data: {
cost: new Prisma.Decimal(24.454545).plus(1),
},
})

Prisma.Decimal 使用 Decimal.js,请参阅 Decimal.js 文档 了解更多信息。

¥Prisma.Decimal uses Decimal.js, see Decimal.js docs to learn more.

警告

使用 Decimal 字段 MongoDB 目前不支持

¥The use of the Decimal field is not currently supported in MongoDB.

BigInt 合作

¥Working with BigInt

概述

¥Overview

BigInt 字段由 BigInt 表示(需要 Node.js 10.4.0+)。以下示例演示了如何使用 BigInt 类型:

¥BigInt fields are represented by the BigInt type (Node.js 10.4.0+ required). The following example demonstrates how to use the BigInt type:

import { PrismaClient, Prisma } from '@prisma/client'

const newTypes = await prisma.sample.create({
data: {
revenue: BigInt(534543543534),
},
})

序列化 BigInt

¥Serializing BigInt

Prisma 客户端以纯 JavaScript 对象的形式返回记录。如果你尝试在包含 BigInt 字段的对象上使用 JSON.stringify,你将看到以下错误:

¥Prisma Client returns records as plain JavaScript objects. If you attempt to use JSON.stringify on an object that includes a BigInt field, you will see the following error:

Do not know how to serialize a BigInt

要解决此问题,请使用 JSON.stringify 的自定义实现:

¥To work around this issue, use a customized implementation of JSON.stringify:

JSON.stringify(
this,
(key, value) => (typeof value === 'bigint' ? value.toString() : value) // return everything else unchanged
)

Bytes 合作

¥Working with Bytes

Bytes 字段由 Uint8Array 类型表示。以下示例演示了如何使用 Uint8Array 类型:

¥Bytes fields are represented by the Uint8Array type. The following example demonstrates how to use the Uint8Array type:

import { PrismaClient, Prisma } from '@prisma/client'

const newTypes = await prisma.sample.create({
data: {
myField: new Uint8Array([1, 2, 3, 4]),
},
})

请注意,在 Prisma v6 之前,BytesBuffer 类型表示:

¥Note that before Prisma v6, Bytes were represented by the Buffer type:

import { PrismaClient, Prisma } from '@prisma/client'

const newTypes = await prisma.sample.create({
data: {
myField: Buffer.from([1, 2, 3, 4]),
},
})

升级到 v6 的指南 中了解更多信息。

¥Learn more in the upgrade guide to v6.

DateTime 合作

¥Working with DateTime

注意

目前有一个 bug,它不允许你将 DateTime 值作为字符串传递,并且在你这样做时会产生运行时错误。DateTime 值需要作为 Date 对象传递(即 new Date('2024-12-04') 而不是 '2024-12-04')。

¥There currently is a bug that doesn't allow you to pass in DateTime values as strings and produces a runtime error when you do. DateTime values need to be passed as Date objects (i.e. new Date('2024-12-04') instead of '2024-12-04').

在创建具有 DateTime 类型字段的记录时,Prisma Client 会接受符合 ISO 8601 标准的 Date 对象作为值。

¥When creating records that have fields of type DateTime, Prisma Client accepts values as Date objects adhering to the ISO 8601 standard.

考虑以下模式:

¥Consider the following schema:

model User {
id Int @id @default(autoincrement())
birthDate DateTime?
}

以下是创建新记录的一些示例:

¥Here are some examples for creating new records:

1998 年 1 月 1 日;00 小时 00 分钟 000 毫秒

¥Jan 01, 1998; 00 h 00 min and 000 ms

await prisma.user.create({
data: {
birthDate: new Date('1998')
}
})
1998 年 12 月 1 日;00 小时 00 分钟 000 毫秒

¥Dec 01, 1998; 00 h 00 min and 000 ms

await prisma.user.create({
data: {
birthDate: new Date('1998-12')
}
})
1998 年 12 月 24 日;00 小时 00 分钟 000 毫秒

¥Dec 24, 1998; 00 h 00 min and 000 ms

await prisma.user.create({
data: {
birthDate: new Date('1998-12-24')
}
})
1998 年 12 月 24 日;06 小时 22 分 33 秒 444 毫秒

¥Dec 24, 1998; 06 h 22 min 33s and 444 ms

await prisma.user.create({
data: {
birthDate: new Date('1998-12-24T06:22:33.444Z')
}
})

Json 合作

¥Working with Json

看:使用 Json 字段

¥See: Working with Json fields

使用标量列表/标量数组

¥Working with scalar lists / scalar arrays

看:使用标量列表/数组

¥See: Working with scalar lists / arrays

使用复合 ID 和复合唯一约束

¥Working with composite IDs and compound unique constraints

看:使用复合 ID 和复合唯一约束

¥See: Working with composite IDs and compound unique constraints