Skip to content

Commit f2fabac

Browse files
authored
Merge pull request #23 from omar-dulaimi/feature/swagger-relation-splitting
Add Swagger and relation splitting features
2 parents 9d42533 + b6c184e commit f2fabac

File tree

9 files changed

+765
-11
lines changed

9 files changed

+765
-11
lines changed

CLAUDE.md

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,99 @@ generated/
6464
└── index.ts
6565
```
6666

67+
### Configuration Options
68+
6769
The generator is configured via Prisma schema:
6870
```prisma
71+
generator class_validator {
72+
provider = "prisma-class-validator-generator"
73+
output = "./generated" // optional, defaults to ./generated
74+
swagger = "true" // optional, adds @ApiProperty decorators
75+
separateRelationFields = "true" // optional, creates separate base/relation classes
76+
}
77+
```
78+
79+
#### Configuration Flags
80+
81+
**`swagger`** (optional, default: `false`)
82+
- Adds NestJS Swagger `@ApiProperty` decorators alongside class-validator decorators
83+
- Includes type information, examples, array handling, and enum values
84+
- Useful for automatic API documentation generation in NestJS applications
85+
86+
**`separateRelationFields`** (optional, default: `false`)
87+
- Splits models into separate base and relation classes for better NestJS integration
88+
- Creates `ModelBase` (scalar fields only), `ModelRelations` (relations only), and combined `Model` class
89+
- Enables use of NestJS mapped types like `PickType`, `PartialType`, etc.
90+
- Perfect for DTOs that need to exclude relations or work with specific field subsets
91+
92+
#### Example Usage
93+
94+
**Basic Usage (class-validator only):**
95+
```prisma
96+
generator class_validator {
97+
provider = "prisma-class-validator-generator"
98+
output = "./generated"
99+
}
100+
```
101+
Generates:
102+
```typescript
103+
export class User {
104+
@IsDefined()
105+
@IsInt()
106+
id!: number;
107+
108+
@IsDefined()
109+
@IsString()
110+
email!: string;
111+
}
112+
```
113+
114+
**With Swagger Support:**
115+
```prisma
69116
generator class_validator {
70117
provider = "prisma-class-validator-generator"
71-
output = "./generated" // optional, defaults to ./generated
118+
output = "./generated"
119+
swagger = "true"
120+
}
121+
```
122+
Generates:
123+
```typescript
124+
export class User {
125+
@IsDefined()
126+
@ApiProperty({ example: 'Generated by autoincrement', type: "integer" })
127+
@IsInt()
128+
id!: number;
129+
130+
@IsDefined()
131+
@ApiProperty({ type: "string" })
132+
@IsString()
133+
email!: string;
134+
}
135+
```
136+
137+
**With Relation Splitting:**
138+
```prisma
139+
generator class_validator {
140+
provider = "prisma-class-validator-generator"
141+
output = "./generated"
142+
separateRelationFields = "true"
143+
}
144+
```
145+
Generates:
146+
- `UserBase.model.ts` - Only scalar fields with decorators
147+
- `UserRelations.model.ts` - Only relation fields with decorators
148+
- `User.model.ts` - Combined class extending UserBase with relations
149+
150+
**Full NestJS Integration:**
151+
```prisma
152+
generator class_validator {
153+
provider = "prisma-class-validator-generator"
154+
output = "./generated"
155+
swagger = "true"
156+
separateRelationFields = "true"
72157
}
73158
```
159+
Perfect for NestJS APIs with automatic Swagger docs and flexible DTOs.
74160

75161
## Modern Development Setup (Prisma 6+)
76162

README.md

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,10 @@ Customize the generator behavior:
235235

236236
```prisma
237237
generator class_validator {
238-
provider = "prisma-class-validator-generator"
239-
output = "./src/models" // Output directory
238+
provider = "prisma-class-validator-generator"
239+
output = "./src/models" // Output directory
240+
swagger = "true" // Add Swagger decorators
241+
separateRelationFields = "true" // Split base/relation classes
240242
}
241243
```
242244

@@ -245,6 +247,53 @@ generator class_validator {
245247
| Option | Type | Default | Description |
246248
|--------|------|---------|-------------|
247249
| `output` | `string` | `"./generated"` | Output directory for generated models |
250+
| `swagger` | `string` | `"false"` | Add NestJS `@ApiProperty` decorators for Swagger docs |
251+
| `separateRelationFields` | `string` | `"false"` | Generate separate base and relation classes for flexible DTOs |
252+
253+
### 🌟 New in v6.0.0-beta.1: NestJS & Swagger Integration
254+
255+
#### Swagger Support (`swagger = "true"`)
256+
257+
Automatically generates NestJS Swagger decorators alongside class-validator decorators:
258+
259+
```typescript
260+
export class User {
261+
@IsDefined()
262+
@ApiProperty({ example: 'Generated by autoincrement', type: "integer" })
263+
@IsInt()
264+
id!: number;
265+
266+
@IsDefined()
267+
@ApiProperty({ type: "string" })
268+
@IsString()
269+
email!: string;
270+
271+
@IsOptional()
272+
@ApiProperty({ type: "string", required: false })
273+
@IsString()
274+
name?: string | null;
275+
}
276+
```
277+
278+
#### Relation Field Splitting (`separateRelationFields = "true"`)
279+
280+
Perfect for NestJS DTOs - generates separate classes for maximum flexibility:
281+
282+
- **`UserBase.model.ts`** - Only scalar fields with validation decorators
283+
- **`UserRelations.model.ts`** - Only relation fields
284+
- **`User.model.ts`** - Combined class extending UserBase
285+
286+
This enables powerful NestJS patterns:
287+
```typescript
288+
// Create DTO without relations using PickType
289+
export class CreateUserDto extends PickType(UserBase, ['email', 'name']) {}
290+
291+
// Update DTO with partial fields
292+
export class UpdateUserDto extends PartialType(UserBase) {}
293+
294+
// Full model with relations for responses
295+
export class UserResponseDto extends User {}
296+
```
248297

249298
## 📚 Advanced Usage
250299

@@ -457,7 +506,7 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
457506
---
458507

459508
<div align="center">
460-
<p>Made with ❤️ by the Prisma Class Validator Generator team</p>
509+
<p>Made with ❤️ by <strong>Omar Dulaimi</strong></p>
461510
<p>
462511
<a href="https://github.com/omar-dulaimi/prisma-class-validator-generator/stargazers">⭐ Star us on GitHub</a> •
463512
<a href="https://github.com/omar-dulaimi/prisma-class-validator-generator/issues">🐛 Report Issues</a> •

0 commit comments

Comments
 (0)