慣性聚合 高效追讀感興趣之博客、新聞、科技資訊
閱原文 以慣性聚合開啟

推薦訂閱源

L
LangChain Blog
宝玉的分享
宝玉的分享
酷 壳 – CoolShell
酷 壳 – CoolShell
N
Netflix TechBlog - Medium
F
Fortinet All Blogs
T
Tailwind CSS Blog
Google DeepMind News
Google DeepMind News
Jina AI
Jina AI
J
Java Code Geeks
Recent Announcements
Recent Announcements
The Cloudflare Blog
D
DataBreaches.Net
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
Vercel News
Vercel News
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
雷峰网
雷峰网
H
Help Net Security
博客园 - Franky
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
博客园_首页
C
Check Point Blog
腾讯CDC
美团技术团队
Martin Fowler
Martin Fowler
The GitHub Blog
The GitHub Blog
M
MIT News - Artificial intelligence
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
U
Unit 42
人人都是产品经理
人人都是产品经理
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Engineering at Meta
Engineering at Meta
M
Microsoft Research Blog - Microsoft Research
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
B
Blog
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
云风的 BLOG
云风的 BLOG
H
Hackread – Cybersecurity News, Data Breaches, AI and More
李成银的技术随笔
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

DEV Community

Local RAG: Chat With Your Documents (Open Source, Private) GGUF & Modelfile: The Power User's Guide to Local LLMs What Excited Me Most at Google I/O 2026 OSS assemble! Kilo Code is launching on Product Hunt. Join the launch! https://www.producthunt.com/products/kilocode Your Organizational AI Adoption Metrics Are Lying (Plus How to Measure Real Adoption) Building a Production-Grade MLOps Home Lab on Windows — K8s, LLM, RAG & GitLab CI The Moment I Realized AI Agents are Changing Software Forever I Spent a Month Testing Decentralized Poker Sites. Here's What Actually Works. DeepSeek-R1: The $0 o1 Alternative You Can Run Right Now The PHP Stack I Built TrustGate On — And Why I'd Do It Differently Today Building High-Throughput Data Pipelines: Why Chaining Encryption and Compression is a Performance Killer Optic is dead. A 2026 migration guide for OpenAPI breaking changes Smart Blind Stick, Mini Project The NSA just published an MCP security playbook. We created Agent Trust Transport Protocol ATTP - Implement today with MCPS Symfony 8 AWS Secrets Bundle Canlı TV Platformu Geliştirirken Öğrendiğim Teknik Dersler: Streaming, Flussonic ve Performans Gemma 4 Is Powerful — But Production AI Still Needs Governance What RepoSignal Surfaced in React — and Why Review Alone Doesn't Catch Everything LeetCode Solution: 1752. Check if Array Is Sorted and Rotated Breaking the Matrix at 15: How I Built a Cyber-Aesthetic AI Assistant Core Powered by Gemma 4 Разработка Android Kiosk приложения No More Manual Test Writing: How I Used Gemma 4 to Turn a GitHub Repo Into a Full Test Suite 🎯 Trafik Cezaları Platformları Geliştirirken Öğrendiğim Teknik Dersler The Myth of Low Latency: Why Event Meshes Make Your System Slow Building EIDOLON OS — A Local-First AI Cognitive Operating System qrrot - database with AI I Built a Local Gemma 4 Reviewer for Merchant Registry Evidence Compass v1.1.0 · we shipped a memory plugin that catches its own consumption drift How to build your first MCP server in 10 minutes Expo SDK 56 Is Out, and a Few Things Finally Clicked Into Place Building a 100ms Browser-Native WebSocket Clipboard Cómo solucionar `docker run` con `Exited (1)` en Raspberry Pi Why Claude Code Sessions Diverge: A Mechanism Catalog When One AI Agent Is Not Enough: A Practical Delegation Pattern for Enterprise Systems Cómo solucionar el bucle infinito en `useEffect` con objetos y arrays 🛢️ The Dangote Chain: What a Blockchain-Native Refinery IPO Would Look Like Build a "Where to Watch" feature in 50 lines with the StreamWatchHub API Gemma 4 on Android: Tricks for Faster On-Device Inference Your AI agent has amnesia. You've just normalized it. 🚀 Reviving My Women Safety System – From Idea to Real-Time Smart Safety Solution I built an AI that reviews every PR automatically (because nobody was reviewing mine) 🌿 Git Mastery: The Complete Developer Guide Bringing Gemma 4 E2B to the Edge: Building a Privacy-First Dream Analyzer with Flutter & LiteRT Google I/O 2026 Wasn’t About Features — It Was About AI Becoming the Developer Environment Building an AI Vedic Astrology App in 25 Days — What Actually Worked (and What Didn't) Hermes Agent Has Four Memories — And That's Why It Doesn't Forget You Pressure Isn't Killing You -Your Relationship With It Is 🐳 How to Run Any Project in Docker: A Complete Guide AccessLens — a blind person's lanyard, powered by Gemma 4 on-device Glyph v0.2: the release is the joinery
Prisma Generator NestJS DTO — 可插拔之 DTO,附注解与定制生成器
Tommaso Meli · 2026-05-24 · via DEV Community

若于Prisma之上构建NestJS之API,汝必感其碍:汝之架构乃真源,然汝之DTO、验证之则、Swagger之元数据,则栖于他处——而其渐离矣。

大抵Prisma之DTO生成器,能解其本(创建/更新/带装饰之实体类)。此诚有益,然汝需特制之输出时—审元数据、GraphQL类型、RBAC规范、汝自之验证者——汝复归手书之粘合码矣。

@tommasomeli/prisma-generator-nestjs-dto乃为彼第二阶段所建之Prisma生成器。其发乎寻常之NestJS DTO,复悄然退避——插件之API注解驱动之装饰器,复一类型安全配置文件 诚然,棱镜之架构不足。

箱中所得

每有棱镜之模型 User,行 npx prisma generate,则得:

src/generated/nestjs-dto/
  user/
    user.entity.ts
    create-user.dto.ts
    update-user.dto.ts
    index.ts
  index.ts

全屏模式入 全屏模式出

每文件俱载:

  • class-validator 装饰语 (@IsString@IsOptional,…)
  • @nestjs/swagger元数据@ApiProperty@ApiHideProperty,……)
  • 自足之输入— 关系数据传输对象,@DtoOverrideType目标既定,注解之辞亦自明矣。

直接于之配置之schema.prisma

generator nestjsDto {
  provider           = "prisma-generator-nestjs-dto"
  output             = "../src/generated/nestjs-dto"
  outputType         = "class"
  outputStructure    = "nestjs"
  fileNamingStrategy = "kebab"
  reExport           = "true"
  classValidator     = "true"
  swaggerDocs        = "true"
  prettier           = "true"
}

入全景模式 出全屏模式

安裝:

npm i -D @tommasomeli/prisma-generator-nestjs-dto

进入全屏模式 退出全屏模式

注释——无需触碰生成代码,即可控制可见性

模型与字段上方的三斜杠注释(///)驱动内置生成器。无需后处理,无需手动编辑.

model User {
  id           Int    @id @default(autoincrement())
  email        String @unique
  name         String
  /// @DtoHidden
  passwordHash String
  /// @DtoReadOnly
  createdAt    DateTime @default(now())
}

进入全屏模式 退出全屏模式

注释
@DtoHidden 隐于四处
@DtoReadOnly 除于创与更之DTO
@DtoEntityHidden 隐于实体(API应答)
@DtoCreateHidden / @DtoUpdateHidden 独隐于一DTO
@DtoOverrideType(MyType) 覆 TypeScript之型(自引)
@DtoIgnoreModel 尽弃此模

亦得系汝之自 配置以获验证者与装饰器 — 下文详述之。

插件之制 — 内置数据传输对象不足时

其别在于 extraGenerators:任取类之延 BaseGenerator,则并行于内置于同一流程。

插件所受者:

  • 解析之模型图(每字段皆注解)
  • 配置已解决(extraDecorators,extraValidators,extraImports……)
  • 导入合并辅助工具(addImport,formatImports,getTemplate)

此乃一简约插件,复用内置渲染器以无class-validator之状发实体:

import { isEntityHidden } from '@tommasomeli/prisma-generator-nestjs-dto';
import { BaseGenerator } from '@tommasomeli/prisma-generator-nestjs-dto';
import type { Field, File, Model } from '@tommasomeli/prisma-generator-nestjs-dto';

export default class EntityDtoGenerator extends BaseGenerator {
  filePrefix = '';
  fileSuffix = '.entity';
  classPrefix = '';
  classSuffix = '';

  async generate(): Promise<File[]> {
    return this.models.map((model) => {
      const filteredFields = model.fields.filter((f: Field) => !isEntityHidden(f));
      const processedModel: Model = { ...model, fields: filteredFields as Field[] };
      const outputPath = this.getPath(model);
      return {
        path: outputPath,
        content: this.getTemplate({ model: processedModel, classValidator: false, outputPath }),
      };
    });
  }
}

进入全屏模式 退出全屏模式

生命周期钩子

插件可钩取全程:

  • beforeAll(models) — 在任一生成器运行前,变更共享模型列表(共享预遍历)
  • afterAll(files) — 在所有生成器完成后,追加桶、审计报告或聚合索引

TypeScript插件,无需预编译 extraGenerators 指向 .ts 文件,生成器即加载之jiti,無需構建步驟以供汝之插件。

實例:自定義@Auditable註解

此倉庫內含一可運行之例於examples/blog/

模式 — 以自定義@Auditable名註解模型:

/// @Auditable("user_audit")
model User {
  id           Int      @id @default(autoincrement())
  email        String   @unique
  /// @DtoHidden
  passwordHash String
  posts        Post[]
}

/// @Auditable("post_audit")
model Post {
  id       Int    @id @default(autoincrement())
  title    String
  author   User   @relation(fields: [authorId], references: [id])
  authorId Int
}

進入全屏模式 退出全屏模式

配置 — 注解与插件之注册:

import { from, type GeneratorConfigFile } from '@tommasomeli/prisma-generator-nestjs-dto';

export default {
  extraAnnotations: ['Auditable'],
  extraGenerators: from('./generators/audit-generator.ts', ['AuditGenerator']),
} satisfies GeneratorConfigFile;

全屏模式开启 全屏模式关闭

输出 — 诸般DTO之外,复得模型专有之审计元数据,及聚合索引:

generated/
  user/user.audit.ts
  post/post.audit.ts
  audit-index.ts    ← emitted by AuditGenerator#afterAll

全屏模式开启 全屏模式关闭

— 类型安全之外部配置

Prisma之生成块适于简明标识,然不能表嵌套之物或多行数组。凡较繁者,当用configFile

generator nestjsDto {
  provider   = "prisma-generator-nestjs-dto"
  output     = "../generated"
  configFile = "../nestjs-dto.config.ts"
}

入全景模式 出全屏模式

"其"from()助者核验路径及命名导出于编译时(此导入闭包永不为运行时所调用)

import { from, fromNamespace, type GeneratorConfigFile } from '@tommasomeli/prisma-generator-nestjs-dto';

export default {
  extraValidators: from(() => import('src/common/validators'), ['IsUnique', 'IsStrongPassword']),
  extraDecorators: from(() => import('src/common/decorators'), ['Trim', 'Sanitize']),
  extraScalars: {
    Decimal: { ts: 'Decimal', from: 'decimal.js' },
    Json:    { ts: 'MyJson', from: 'src/json', apiType: 'Object' },
  },
} satisfies GeneratorConfigFile;

入全景模式 出全景模式

乃布注解于架构:

model User {
  /// @IsUnique()
  email    String @unique
  /// @IsStrongPassword({ minLength: 10 })
  password String
  /// @Trim()
  name     String
}

入全景模式 出全景模式

若定制校验者以名相冲突于内置者(IsBooleanApiProperty、…),则尔模块独胜于斯符。

可选之运行时说明书

启用 emitManifest = "true" 以得:

  • manifest.tsRecord<Prisma.ModelName, { primaryKey, entityFields, relations }> 为若干构建者、审计中间件、RBAC 字段列表之用
  • model-entity-map.ts — 模型名至实体类之类型映射

当需架构感知之运行时逻辑,而无需自解 Prisma DMMF 时,此尤便

其较之如何

此生成器 寻常之Prisma NestJS数据传输对象生成器
创制 / 更新 / 实体数据传输对象
Swagger(+) + class-validator
注解驱动之隐藏 / 只读 / 类型覆盖 偏颇
可插拔之次生成器
插件之定制注解
以名覆内置之导入
类型安全之外configFile
可选项运行时清单

试之

npm i -D @tommasomeli/prisma-generator-nestjs-dto
npx prisma generate

入全景模式 出全景模式

问题与补丁皆可,依MIT许可。

若此能省君于NestJS与Prisma之栈上时日,于仓中加一⭐(或一)咖啡 ☕其效甚远。