如何在 Prisma ORM 中在运行时获取当前模型名称或使用其他方法

huangapple go评论46阅读模式
英文:

How to get the current model name or use other method at runtime in Prisma ORM

问题

以下是您要求的代码的翻译部分:

 prisma ORM 版本 `4.9.0` 中已包含新功能 [文档链接](https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions/model)

在更新 `schema.prisma` 如下后我们如何在运行时获取模型名称并在运行时使用其他方法

```js
const { PrismaClient, Prisma } = require("@prisma/client");

const db = new PrismaClient().$extends({
  client: {
    log: (s) => console.log(s),
    async useLog() {
      // 如何在这里使用 log 函数
    },
  },
  model: {
    $allModels: {
      log: (s) => console.log(s),
      async find(id, option = {}) {
        // 如何在运行时获取当前模型名称
        
        // 或者在这里使用其他函数
      },
    },
  },
});

module.exports = { db };

希望这有助于您理解代码的翻译。如果您有任何其他问题,请随时提问。

英文:

New feature has been included In prisma ORM version 4.9.0 Doc Link

After update schema.prisma as follow

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["clientExtensions"]
}

How do we get model name at runtime and use other method also at runtime

const { PrismaClient, Prisma } = require("@prisma/client");

const db = new PrismaClient().$extends({
  client: {
    log: (s) => console.log(s),
    async useLog() {
      // how to use log function
      // here

    },
  },
  model: {
    $allModels: {
      log: (s) => console.log(s),
      async find(id, option = {}) {
        // how to get current model name in runtime
        
       // or use other function here
      },
    },
  },
});

module.exports = { db };

答案1

得分: 0

这是解决方案:

const { PrismaClient, Prisma } = require("@prisma/client");

const db = new PrismaClient().$extends({
  client: {
    log: (s) => console.log(s),
    async useLog() {
      const ctx = Prisma.getExtensionContext(this);
      ctx.log("test"); // 在控制台上打印 "test" 
    },
  },
  model: {
    $allModels: {
      log: (s) => console.log(s),
      async find(id, option = {}) {
        const ctx = Prisma.getExtensionContext(this);
        // 这里记录当前运行时模型名称
        console.log(ctx.name);

        // 在这里我们可以使用 prisma 模型的其他方法
        return ctx.findUniqueOrThrow({
          where: {
            id,
          },
          ...option,
        });
      },
    },
  },
});

module.exports = { db };

现在我们可以像下面这样在所有模型中使用 find 方法:

const { db } = require("@app/src/utils/db");

const teacher = await db.teacher.find(teacherId);
const student = await db.student.find(studentId);
英文:

Here is the solution

const { PrismaClient, Prisma } = require("@prisma/client");

const db = new PrismaClient().$extends({
  client: {
    log: (s) => console.log(s),
    async useLog() {
      const ctx = Prisma.getExtensionContext(this);
      ctx.log("test"); // it will print "test" on console 
    },
  },
  model: {
    $allModels: {
      log: (s) => console.log(s),
      async find(id, option = {}) {
        const ctx = Prisma.getExtensionContext(this);
        // here is log the current runtime model name
        console.log(ctx.name);

        // here we can use other method of prisma model
        return ctx.findUniqueOrThrow({
          where: {
            id,
          },
          ...option,
        });
      },
    },
  },
});

module.exports = { db };

Now we can use find method in all model just like

const { db } = require("@app/src/utils/db");

const teacher = await db.teacher.find(teacherId);
const student = await db.student.find(studentId);

huangapple
  • 本文由 发表于 2023年2月10日 03:23:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定