# Prisma 6 Bytes 字段类型与 Node Buffer 不直接兼容

> **日期**: 2026-05-13
> **类型**: 类型/接口陷阱

## 现象

Prisma 6 (v6.19.3) 生成的 Bytes 字段 TypeScript 类型是 `Uint8Array<ArrayBuffer>`。
传 Node 的 `Buffer` 给 Prisma 的 create/update 报：

```
TS2322: Type 'Buffer<ArrayBufferLike>' is not assignable to type 'Uint8Array<ArrayBuffer>'
```

虽然运行时 Buffer 继承自 Uint8Array，但 TS 看泛型参数不一致就拒。

## 解决（已验证）

**写入 Prisma**：用 `new Uint8Array(buffer)` 显式转换
```typescript
await this.prisma.foo.create({
  data: {
    encrypted: new Uint8Array(cipherBuffer), // 不是直接传 cipherBuffer
    iv: new Uint8Array(ivBuffer),
  },
});
```

**从 Prisma 读出再用 Buffer API**：用 `Buffer.from(uint8)`
```typescript
const row = await this.prisma.foo.findUnique(...);
const plaintext = decipher.update(Buffer.from(row.encrypted));
```

## 不该的做法

- ❌ `as Buffer` 强转 — 真正运行时一致，但 TS5 + Prisma 6 typedef 不一致，会过编译但跟未来 lib 更新冲突
- ❌ 改 tsconfig `skipLibCheck` 关 — 全局副作用太大

## 适用面

任何用 Prisma `Bytes` 类型 + Node crypto / 文件 IO 的代码。本项目以后做加密
(env / token / 文件附件等) 都会撞，沉淀此条避免重踩。
