首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Prisma中导入prisma模型

如何在Prisma中导入prisma模型
EN

Stack Overflow用户
提问于 2021-09-20 09:29:46
回答 1查看 365关注 0票数 0

我正在使用包prisma-offset-pagination应用分页。为此,我必须在我的代码中使用Prisma模型,这是怎么可能的:检查行: 02

代码语言:javascript
复制
const result = prismaOffsetPagination({
    model: user,   // How to access prisma model like in this example.
    cursor: <cursor>,
    size: 5,
    buttonNum: 7,
    orderBy: 'id',
    orderDirection: 'desc',
    prisma: prisma,
});

我试过使用下面的代码,但它没有工作,并引发了一个错误: UnhandledPromiseRejectionWarning: TypeError:无法读取未定义的prismaOffsetPagination属性'toLowerCase‘

代码语言:javascript
复制
import pClient from '@prisma/client'
const { PrismaClient } = pClient
const { prisma } = new PrismaClient()

  const result = prismaOffsetPagination({
    model: prisma.user,
    cursor: 2,
    size: 5,
    buttonNum: 3,
    orderBy: 'id',
    orderDirection: 'desc',
  })
EN

回答 1

Stack Overflow用户

发布于 2021-12-11 00:55:16

prismaOffsetPagination函数是这样的:

代码语言:javascript
复制
export async function prismaOffsetPagination({
  model,
  cursor,
  size,
  buttonNum,
  orderBy,
  orderDirection,
  include,
  where,
  prisma,
}: Props<typeof model>): Promise<PaginationType> {
  // totalCount
  const prismaModel = prisma[model.name.toLowerCase()];
  const totalCount = await prismaModel.count({
    where: {
      ...where,
    },
  });

如您所见,model.name.toLowerCase()建议该模型是一个具有name属性的对象:model:{name:string}

基于此,对我来说,它可以在对象中传递模型的名称,如下所示:

代码语言:javascript
复制
const result = prismaOffsetPagination({
    model: {name: 'user'}
    cursor: <cursor>,
    size: 5,
    buttonNum: 7,
    orderBy: 'id',
    orderDirection: 'desc',
    prisma: prisma,
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69252076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档