2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Prisma でモデル名を取得したい

Last updated at Posted at 2024-11-02

はじめに

Prisma Client で Model 名(文字列)を使いたい場面に遭遇した。
Prisma は node_modules/.prisma/client/index.d.ts に型定義が存在する

Model 名を取得する

以下に記載するような schema.prisma が定義されているとする。

schema.prisma
model User {
  id   String  @id
  name String
}

npx prisma generate 等何でも良いので node_modules/.prisma/client/index.d.ts を生成する。
下記のように記述すれば ModelName を取得できる。

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

prisma.user.name;
// -> User

全件欲しい場合は、以下のように記述すれば良い。

import { Prisma } from '@prisma/client';

Prisma.ModelName;

Ref

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?