@vanviolet/prisma-produce
v1.0.0
Published
A powerful CLI tool to generate DTOs, Entities, Interfaces, and Classes from Prisma schema
Downloads
15
Maintainers
Readme
Prisma Produce
🇬🇧 English | 🇮🇩 Bahasa Indonesia | 🇯🇵 日本語
🇬🇧 English
🚀 Features
- Multiple Output Formats: Generate DTOs, Entities, Interfaces, or Plain Classes
- Full Decorator Support: Automatic class-validator and class-transformer decorators
- Swagger Integration: ApiProperty decorators for @nestjs/swagger
- Interactive Mode: User-friendly CLI with prompts
- Watch Mode: Auto-regenerate on schema changes
- Flexible Configuration: Config file or CLI options
- Split Mode: Generate one file per model or single file
- Namespace Support: Wrap classes in namespaces
- Custom Decorators: Add your own field decorators
- TypeScript First: Full type safety and IntelliSense support
📦 Installation
npm install -g @vanviolet/prisma-produceOr use in your project:
npm install --save-dev @vanviolet/prisma-produce🎯 Quick Start
Interactive Mode
Simply run:
prisma-produceThe CLI will guide you through the configuration.
Initialize Config File
prisma-produce initThis creates prisma-produce.config.json in your project root.
Generate with Config
prisma-produce generateGenerate with CLI Options
prisma-produce generate --schema=prisma/schema.prisma --output=src/dto/database.dto.ts📖 Usage
Commands
generate (default)
Generate DTOs from your Prisma schema.
prisma-produce generate [options]Options:
--schema <path>- Path to Prisma schema file (default:prisma/schema.prisma)--output <path>- Output file path (default:app/common/interface/database.dto.ts)--output-dir <path>- Output directory (for split mode)--format <type>- Output format:dto|entity|interface|class(default:dto)--prefix <string>- Class name prefix (e.g.,Base→BaseUserDTO)--suffix <string>- Class name suffix (default:DTO)--namespace <string>- Wrap classes in namespace (e.g.,Database.DTO)--models <list>- Include only specific models (comma-separated)--exclude <list>- Exclude specific models (comma-separated)--relations <mode>- Relations mode:none|simple|full(default:none)--relations-depth <n>- Max depth for relations (default:1)--split-by-model- Generate one file per model-w, --watch- Watch mode (auto-regenerate on changes)--config <path>- Path to config file (default:prisma-produce.config.json)
init
Initialize configuration file.
prisma-produce init🔧 Configuration
Config File (prisma-produce.config.json)
{
"schemaPath": "prisma/schema.prisma",
"outputPath": "src/dto/database.dto.ts",
"prefix": "",
"suffix": "DTO",
"namespace": "",
"format": "dto",
"models": [],
"exclude": [],
"relations": "none",
"relationsDepth": 1,
"splitByModel": false,
"watch": false,
"customDecorators": {
"email": [
"@Transform(({ value }) => value.toLowerCase())",
"@Trim()"
]
}
}📋 Examples
Basic DTO Generation
prisma-produce generate --schema=prisma/schema.prisma --output=src/dto.tsInput (schema.prisma):
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
role Role @default(USER)
createdAt DateTime @default(now())
}
enum Role {
USER
ADMIN
}Output (src/dto.ts):
import { IsArray, IsEnum, IsNotEmpty, IsOptional, IsString, IsNumber, IsDate } from 'class-validator';
import { Expose, Transform, Type } from 'class-transformer';
import { ApiProperty } from '@nestjs/swagger';
import { Role } from '@prisma/client';
export class UserDTO {
@Expose()
@ApiProperty({ type: Number })
@IsNotEmpty()
@IsNumber()
id: number;
@Expose()
@ApiProperty({ type: String })
@IsNotEmpty()
@IsString()
email: string;
@Expose()
@ApiProperty({ type: String, required: false, nullable: true })
@IsOptional()
@IsString()
name?: string | null;
@Expose()
@ApiProperty({ enum: Role, enumName: 'Role' })
@IsNotEmpty()
@IsEnum(Role)
role: Role;
@Expose()
@ApiProperty({ type: Date })
@IsNotEmpty()
@IsDate()
@Type(() => Date)
@Transform(({ value }) => {
if (typeof value === 'string' || typeof value === 'number') {
return new Date(value);
}
return value;
})
createdAt: Date;
}Generate with Prefix and Namespace
prisma-produce generate --prefix=Base --namespace=Database.Modelsexport namespace Database {
export namespace Models {
export class BaseUserDTO {
// ...
}
}
}Generate Only Specific Models
prisma-produce generate --models=User,Post,CommentExclude Models
prisma-produce generate --exclude=AuditLog,SystemConfigSplit by Model
prisma-produce generate --split-by-model --output-dir=src/dtoGenerates:
src/dto/user.dto.tssrc/dto/post.dto.tssrc/dto/comment.dto.tssrc/dto/index.ts(exports all)
Watch Mode
prisma-produce generate --watchEntity Format
prisma-produce generate --format=entity --suffix=Entity --output=src/entities.tsGenerates entities without @ApiProperty decorators.
Interface Format
prisma-produce generate --format=interface --suffix="" --output=src/interfaces.tsGenerates plain TypeScript interfaces.
Plain Class Format
prisma-produce generate --format=class --suffix="" --output=src/models.tsGenerates plain classes with constructors.
🎨 Output Formats
1. DTO (Default)
- Full class-validator decorators
- Full class-transformer decorators
- ApiProperty decorators
- Best for NestJS APIs
2. Entity
- class-validator decorators
- class-transformer decorators
- No ApiProperty decorators
- Best for database entities
3. Interface
- Plain TypeScript interfaces
- No decorators
- Best for type definitions
4. Class
- Plain classes with constructors
- No decorators
- Best for models/domain objects
🛠️ Custom Decorators
Add custom decorators in genprisma.config.json:
{
"customDecorators": {
"email": [
"@Transform(({ value }) => value.toLowerCase())",
"@IsEmail()"
],
"phone": [
"@Transform(({ value }) => value.replace(/\\D/g, ''))"
]
}
}📦 NPM Scripts
Add to your package.json:
{
"scripts": {
"generate:dto": "prisma-produce generate",
"generate:dto:watch": "prisma-produce generate --watch",
"generate:dto:init": "prisma-produce init"
}
}🔄 Integration with Prisma
Automatically regenerate DTOs after Prisma migrations:
{
"scripts": {
"prisma:migrate": "prisma migrate dev && npm run generate:dto"
}
}📝 Required Dependencies
The CLI will check if these dependencies are installed in your project and prompt to install them if missing:
Dependencies:
class-validator- For validation decorators (DTO/Entity format)class-transformer- For transformation decorators (DTO/Entity format)@nestjs/swagger- For ApiProperty decorators (DTO format only)
Note: The CLI itself includes @prisma/internals and chokidar, so you don't need to install them in your project.
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
MIT © vanviolet
🐛 Issues
Report issues at GitHub Issues
📚 Documentation
For more information, visit GitHub Repository
🌟 Show Your Support
If this project helps you, please give it a ⭐️!
Made with ❤️ by vanviolet
Other languages: 🇮🇩 Bahasa Indonesia | 🇯🇵 日本語
