nestjs-drizzle-crud-generator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNestJS Drizzle CRUD Generator
NestJS Drizzle CRUD 生成器
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.
该工具会自动为使用Drizzle ORM的NestJS应用生成完整的CRUD(创建、读取、更新、删除)模块。它会遵循zaccheroni-monorepo的项目结构创建所有必要文件。
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
- 创建带有完整CRUD接口的新实体模块
- 在NestJS中构建数据库支持的功能
- 生成带Zod验证的类型安全DTO
- 添加包含Drizzle ORM查询的服务
- 创建带模拟数据库的单元测试
Instructions
使用步骤
Step 1: Identify Feature Requirements
步骤1:明确功能需求
Before generating, gather:
- Entity name (e.g., ,
user,product)order - List of fields with types
- Required fields vs optional fields
生成前,请收集以下信息:
- 实体名称(例如:、
user、product)order - 带类型的字段列表
- 必填字段与可选字段
Step 2: Run the Generator
步骤2:运行生成器
Execute the generation script:
bash
python scripts/generate_crud.py --feature <name> --fields '<json-array>' --output <path>执行生成脚本:
bash
python scripts/generate_crud.py --feature <name> --fields '<json-array>' --output <path>Step 3: Field Definition Format
步骤3:字段定义格式
Fields must be defined as JSON array with name, type, and required properties.
字段必须定义为包含name、type和required属性的JSON数组。
Step 4: Integrate Module
步骤4:集成模块
After generation, integrate the module into your NestJS application.
生成完成后,将模块集成到你的NestJS应用中。
Examples
示例
Example 1: Generate a User module
示例1:生成用户模块
bash
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/serverbash
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/serverExample 2: Generate a Product module
示例2:生成产品模块
bash
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/serverbash
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/serverQuick Start
快速开始
Step 1: Identify Feature Requirements
步骤1:明确功能需求
Before generating, gather:
- Entity name (e.g., ,
user,product)order - List of fields with types
- Required fields vs optional fields
生成前,请收集以下信息:
- 实体名称(例如:、
user、product)order - 带类型的字段列表
- 必填字段与可选字段
Step 2: Run the Generator
步骤2:运行生成器
Execute the generation script:
bash
python scripts/generate_crud.py --feature <name> --fields '<json-array>' --output <path>执行生成脚本:
bash
python scripts/generate_crud.py --feature <name> --fields '<json-array>' --output <path>Step 3: Field Definition Format
步骤3:字段定义格式
Fields must be defined as JSON array:
json
[
{"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}
]字段必须定义为JSON数组:
json
[
{"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
步骤4:示例命令
Generate a User module:
bash
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/serverGenerate a Product module:
bash
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生成用户模块:
bash
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生成产品模块:
bash
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/serverGenerated 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生成器会创建如下目录结构:
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.tsSupported 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() |
| 类型 | Drizzle 列类型 | Zod 校验规则 |
|---|---|---|
| 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 pattern for lazy configuration
forRootAsync - Exports generated service for other modules
- Imports DatabaseModule for feature tables
- 使用模式实现延迟配置
forRootAsync - 导出生成的服务供其他模块使用
- 导入DatabaseModule以支持功能表
Controller
控制器
- Full CRUD endpoints: POST, GET, PATCH, DELETE
- Query parameter validation for pagination
- Zod validation pipe integration
- 完整的CRUD接口:POST、GET、PATCH、DELETE
- 分页查询参数校验
- 集成Zod验证管道
Service
服务
- Drizzle ORM query methods
- Soft delete support (via column)
deletedAt - Pagination with limit/offset
- Filtering support
- Type-safe return types
- Drizzle ORM查询方法
- 软删除支持(通过字段)
deletedAt - 基于limit/offset的分页
- 过滤支持
- 类型安全的返回类型
DTOs
DTO
- Zod schemas for Create and Update
- Query parameter schemas for filtering
- NestJS DTO integration
- 用于创建和更新的Zod规则
- 用于过滤的查询参数规则
- 集成NestJS DTO
Tests
测试
- Jest test suite
- Mocked Drizzle database
- Test cases for all CRUD operations
- Jest测试套件
- 模拟Drizzle数据库
- 覆盖所有CRUD操作的测试用例
Manual Integration
手动集成
After generation, integrate into your app module:
typescript
// app.module.ts
import { {{FeatureName}}FeatureModule } from '@your-org/server-{{feature}}';
@Module({
imports: [
{{FeatureName}}FeatureModule.forRootAsync({
useFactory: () => ({
defaultPageSize: 10,
maxPageSize: 100,
}),
}),
],
})
export class AppModule {}生成完成后,将模块集成到你的应用模块中:
typescript
// 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:
- : Field name
name - : Data type (string, text, number, integer, boolean, date, uuid, email)
type - : Boolean for mandatory fields
required - : Default value for non-required fields
default - : Maximum length for strings
maxLength - : Minimum length for strings
minLength
每个字段支持以下配置:
- :字段名称
name - :数据类型(string、text、number、integer、boolean、date、uuid、email)
type - :布尔值,标记是否为必填字段
required - :非必填字段的默认值
default - :字符串类型的最大长度
maxLength - :字符串类型的最小长度
minLength
Dependencies
依赖项
The generated code requires:
@nestjs/common@nestjs/coredrizzle-ormdrizzle-zodzodnestjs-zod
生成的代码需要以下依赖:
@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 ). Hard deletes require manual modification
deletedAt - 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
- 仅支持软删除:生成的删除方法使用软删除(设置字段),硬删除需要手动修改
deletedAt - 无认证逻辑:生成的代码不包含认证守卫,需单独添加
- 仅基础CRUD:复杂查询或业务逻辑需手动实现
- JSON字段转义:在命令行传递JSON字段时,需用单引号包裹JSON数组