使用变量访问模型
Accessing models by using a variable
我如何动态访问 prisma 中的模型?
return await prisma[modelName].create({ data })
...好像不行
我正在寻找一种使用变量来访问我的模型的方法。如何做到这一点?
更新:
存在打字错误:Unable to compile TypeScript
。
error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'PrismaClient<PrismaClientOptions, never, RejectOnNotFound | RejectPerOperation | undefined>'.
No index signature with a parameter of type 'string' was found on type 'PrismaClient<PrismaClientOptions, never, RejectOnNotFound | RejectPerOperation | undefined>'.
这有点老套,但您可以在该行上方使用 // @ts-ignore
来避免您提到的错误。
这应该可以正常工作:
// @ts-ignore
return await prisma[modelName].create({ data });
我完全理解应该有更好的方法来做到这一点。然而,这是迄今为止最简单的方法,无需借助私有 API。
我如何动态访问 prisma 中的模型?
return await prisma[modelName].create({ data })
...好像不行
我正在寻找一种使用变量来访问我的模型的方法。如何做到这一点?
更新:
存在打字错误:Unable to compile TypeScript
。
error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'PrismaClient<PrismaClientOptions, never, RejectOnNotFound | RejectPerOperation | undefined>'.
No index signature with a parameter of type 'string' was found on type 'PrismaClient<PrismaClientOptions, never, RejectOnNotFound | RejectPerOperation | undefined>'.
这有点老套,但您可以在该行上方使用 // @ts-ignore
来避免您提到的错误。
这应该可以正常工作:
// @ts-ignore
return await prisma[modelName].create({ data });
我完全理解应该有更好的方法来做到这一点。然而,这是迄今为止最简单的方法,无需借助私有 API。