增删改查生成器
nestjs-drizzle-crud-generator
by giuseppe-trisciuoglio
在 NestJS 中一键生成基于 Drizzle ORM 的完整 CRUD 模块,自动产出 endpoints、查询服务、Zod 校验 DTO 和单测,适合快速新增带数据库操作的后端实体功能。
安装
claude skill add --url github.com/giuseppe-trisciuoglio/developer-kit/tree/main/plugins/developer-kit-typescript/skills/nestjs-drizzle-crud-generator文档
NestJS Drizzle CRUD Generator
Overview
This skill automatically generates complete CRUD (Create, Read, Update, Delete) modules for NestJS applications using Drizzle ORM. It creates all necessary files following the zaccheroni-monorepo patterns.
When to Use
- Creating new entity modules with full CRUD endpoints
- Building database-backed features in NestJS
- Generating type-safe DTOs with Zod validation
- Adding services with Drizzle ORM queries
- Creating unit tests with mocked database
Instructions
Step 1: Identify Feature Requirements
Before generating, gather:
- Entity name (e.g.,
user,product,order) - List of fields with types
- Required fields vs optional fields
Step 2: Run the Generator
Execute the generation script:
python scripts/generate_crud.py --feature <name> --fields '<json-array>' --output <path>
Step 3: Field Definition Format
Fields must be defined as JSON array with name, type, and required properties.
Step 4: Integrate Module
After generation, integrate the module into your NestJS application.
Examples
Example 1: Generate a User module
python scripts/generate_crud.py \
--feature user \
--fields '[{"name": "name", "type": "string", "required": true}, {"name": "email", "type": "string", "required": true}, {"name": "password", "type": "string", "required": true}]' \
--output ./libs/server
Example 2: Generate a Product module
python scripts/generate_crud.py \
--feature product \
--fields '[{"name": "title", "type": "string", "required": true}, {"name": "price", "type": "number", "required": true}, {"name": "description", "type": "text", "required": false}, {"name": "inStock", "type": "boolean", "required": false, "default": true}]' \
--output ./libs/server
Quick Start
Step 1: Identify Feature Requirements
Before generating, gather:
- Entity name (e.g.,
user,product,order) - List of fields with types
- Required fields vs optional fields
Step 2: Run the Generator
Execute the generation script:
python scripts/generate_crud.py --feature <name> --fields '<json-array>' --output <path>
Step 3: Field Definition Format
Fields must be defined as JSON array:
[
{"name": "name", "type": "string", "required": true},
{"name": "email", "type": "string", "required": true},
{"name": "age", "type": "integer", "required": false},
{"name": "isActive", "type": "boolean", "required": false, "default": true},
{"name": "price", "type": "number", "required": true},
{"name": "description", "type": "text", "required": false},
{"name": "uuid", "type": "uuid", "required": false}
]
Step 4: Example Commands
Generate a User module:
python scripts/generate_crud.py \
--feature user \
--fields '[{"name": "name", "type": "string", "required": true}, {"name": "email", "type": "string", "required": true}, {"name": "password", "type": "string", "required": true}]' \
--output ./libs/server
Generate a Product module:
python scripts/generate_crud.py \
--feature product \
--fields '[{"name": "title", "type": "string", "required": true}, {"name": "price", "type": "number", "required": true}, {"name": "description", "type": "text", "required": false}, {"name": "inStock", "type": "boolean", "required": false, "default": true}]' \
--output ./libs/server
Generated Structure
The generator creates this directory structure:
libs/server/{feature-name}/
├── src/
│ ├── index.ts
│ └── lib/
│ ├── {feature}-feature.module.ts
│ ├── controllers/
│ │ ├── index.ts
│ │ └── {feature}.controller.ts
│ ├── services/
│ │ ├── index.ts
│ │ ├── {feature}.service.ts
│ │ └── {feature}.service.spec.ts
│ ├── dto/
│ │ ├── index.ts
│ │ └── {feature}.dto.ts
│ └── schema/
│ └── {feature}.table.ts
Supported Field Types
| Type | Drizzle Column | Zod Schema |
|---|---|---|
| string | text | z.string() |
| text | text | z.string() |
| number | real | z.number() |
| integer | integer | z.number().int() |
| boolean | boolean | z.boolean() |
| date | timestamp | z.date() |
| uuid | uuid | z.string().uuid() |
| text | z.string().email() |
Features
Module
- Uses
forRootAsyncpattern for lazy configuration - Exports generated service for other modules
- Imports DatabaseModule for feature tables
Controller
- Full CRUD endpoints: POST, GET, PATCH, DELETE
- Query parameter validation for pagination
- Zod validation pipe integration
Service
- Drizzle ORM query methods
- Soft delete support (via
deletedAtcolumn) - Pagination with limit/offset
- Filtering support
- Type-safe return types
DTOs
- Zod schemas for Create and Update
- Query parameter schemas for filtering
- NestJS DTO integration
Tests
- Jest test suite
- Mocked Drizzle database
- Test cases for all CRUD operations
Manual Integration
After generation, integrate into your app module:
// app.module.ts
import { {{FeatureName}}FeatureModule } from '@your-org/server-{{feature}}';
@Module({
imports: [
{{FeatureName}}FeatureModule.forRootAsync({
useFactory: () => ({
defaultPageSize: 10,
maxPageSize: 100,
}),
}),
],
})
export class AppModule {}
Field Options
Each field supports:
name: Field nametype: Data type (string, text, number, integer, boolean, date, uuid, email)required: Boolean for mandatory fieldsdefault: Default value for non-required fieldsmaxLength: Maximum length for stringsminLength: Minimum length for strings
Dependencies
The generated code requires:
@nestjs/common@nestjs/coredrizzle-ormdrizzle-zodzodnestjs-zod
Best Practices
- Verify generated code: Always review generated files before committing
- Run tests: Execute unit tests to verify the generated code works
- Customize as needed: Add business logic to services after generation
- Database migrations: Manually create migrations for the generated schema
- Type safety: Use the generated types in your application code
Constraints and Warnings
- Soft delete only: The generated delete method uses soft delete (sets
deletedAt). Hard deletes require manual modification - No authentication: Generated code does not include auth guards - add them separately
- Basic CRUD only: Complex queries or business logic must be implemented manually
- JSON field escaping: When passing fields JSON on command line, use single quotes around the JSON array
相关 Skills
技术栈评估
by alirezarezvani
对比框架、数据库和云服务,结合 5 年 TCO、安全风险、生态活力与迁移复杂度做量化评估,适合技术选型、栈升级和替换路线决策。
✎ 帮你系统比较技术栈优劣,不只看功能,还把TCO、安全性和生态健康度一起量化,选型和迁移决策更稳。
资深数据科学家
by alirezarezvani
覆盖实验设计、特征工程、预测建模、因果推断与模型评估,适合用 Python/R/SQL 做 A/B 测试、时序分析和生产级 ML 落地,支撑数据驱动决策。
✎ 从 A/B 测试、因果分析到预测建模一条龙搞定,既有硬核统计方法也懂业务沟通,特别适合把数据结论真正落地。
资深架构师
by alirezarezvani
适合系统设计评审、ADR记录和扩展性规划,分析依赖与耦合,权衡单体或微服务、数据库与技术栈选型,并输出Mermaid、PlantUML、ASCII架构图。
✎ 搞系统设计、技术选型和扩展规划时,用它能更快理清架构决策与依赖关系,还能直接产出 Mermaid/PlantUML 图,方案讨论效率很高。
相关 MCP 服务
PostgreSQL 数据库
编辑精选by Anthropic
PostgreSQL 是让 Claude 直接查询和管理你的数据库的 MCP 服务器。
✎ 这个服务器解决了开发者需要手动编写 SQL 查询的痛点,特别适合数据分析师或后端开发者快速探索数据库结构。不过,由于是参考实现,生产环境使用前务必评估安全风险,别指望它能处理复杂事务。
SQLite 数据库
编辑精选by Anthropic
SQLite 是让 AI 直接查询本地数据库进行数据分析的 MCP 服务器。
✎ 这个服务器解决了 AI 无法直接访问 SQLite 数据库的问题,适合需要快速分析本地数据集的开发者。不过,作为参考实现,它可能缺乏生产级的安全特性,建议在受控环境中使用。
Firecrawl 智能爬虫
编辑精选by Firecrawl
Firecrawl 是让 AI 直接抓取网页并提取结构化数据的 MCP 服务器。
✎ 它解决了手动写爬虫的麻烦,让 Claude 能直接访问动态网页内容。最适合需要实时数据的研究者或开发者,比如监控竞品价格或抓取新闻。但要注意,它依赖第三方 API,可能涉及隐私和成本问题。