简体   繁体   English

如何从 Prisma Schema 导出模型的属性类型?

[英]How can I export my model's properties types from Prisma Schema?

Lets say I have this Prisma Schema:假设我有这个 Prisma Schema:

model User {
  id             String   @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  email          String   @unique
  username       String   
}

Is there a way I can get lets say email's type in my app?有没有办法让我说我的应用程序中的电子邮件类型

  • One options I can come with is a file that will export types but that way everytime I make a change to my prisma schema I will have to go there and manually edit the type.我可以提供的一个选项是导出类型的文件,但是每次我更改我的 prisma 模式时,我都必须在那里 go 并手动编辑类型。

Prisma by default generates types for all your models. Prisma 默认为所有模型生成类型。 You could import it like this:你可以像这样导入它:

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

Your models will always be in sync with your schema file, anytime you change the schema file, you could execute npx prisma generate to update the PrismaClient and the generated types for your models.您的模型将始终与您的架构文件同步,无论何时更改架构文件,您都可以执行 npx npx prisma generate来更新 PrismaClient 和为您的模型生成的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM