You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在NestJS中注入Prisma部分API,prisma.tag的类型是什么?

在NestJS中注入Prisma单表API的正确类型

你要注入的prisma.tag对应的正确类型是Prisma.TagDelegate,更严谨的写法是带上它的泛型参数——也就是你VSCode自动推断出的Prisma.TagDelegate<Prisma.RejectOnNotFound | Prisma.RejectPerOperation>。

补充说明:

  • 这个类型是Prisma根据你的schema自动生成的,对应tag表的CRUD操作代理类,包含findMany、findUnique、create等所有针对该表的操作方法。
  • 如果你不需要自定义错误拒绝策略,也可以直接使用Prisma.TagDelegate(不带泛型),Prisma会应用默认的拒绝配置。

修正后的代码示例:

import { Injectable } from '@nestjs/common';
import { PrismaService } from './prisma.service';
import { Prisma } from '@prisma/client';

@Injectable()
export class TagService {
    private readonly tagApi: Prisma.TagDelegate<Prisma.RejectOnNotFound | Prisma.RejectPerOperation>;

    constructor(prisma: PrismaService) {
        this.tagApi = prisma.tag;
    }

    exampleUsageMethod(tagId: string) {
        return this.tagApi.findMany({ where: { id: tagId } });
    }
}

内容的提问来源于stack exchange,提问作者alyssa moon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 11:26:52