@custom-generators/simple-prisma-dto-gen
v1.2.1
Published
Prisma generator for NestJS DTOs, enums and service scaffolding
Maintainers
Readme
Simple Prisma DTO Gen
Prisma generator focused on NestJS projects.
It generates:
- DTO classes for Prisma models
- Insert DTO classes
- Enum files
- A
DatabaseGenServicewith CRUD helpers - An optional
PrismaService - A
model.jsonfile with the Prisma DMMF
Scope
This package is not a generic Prisma code generator.
Current assumptions:
- DTOs import
@nestjs/swagger - The optional generated
PrismaServiceimports@nestjs/common - The optional generated
PrismaServiceuses@prisma/adapter-mariadb(or@prisma/adapter-postgresql) - The generated service layer is designed for NestJS-style usage
Because of that, the package fits best in NestJS + Prisma projects.
Generated Output
Given an output directory, the generator creates:
<output>/
dtos/
insert.dtos/
enums/
service/
database.gen.ts
prisma.service.ts # only when generatePrismaService = true
model.jsonInstallation
Install the generator and Prisma:
npm install --save-dev prisma @custom-generators/simple-prisma-dto-gen
npm install --save @prisma/client @nestjs/swaggerInstall these only if you want the generated PrismaService for MariaDB/MySQL:
npm install --save @nestjs/common @prisma/adapter-mariadb mariadb dotenvInstall these only if you want the generated PrismaService for PostgreSQL:
npm install --save @nestjs/common @prisma/adapter-postgresql pg dotenvInstall this only if you want generated InsertDto classes to include class-validator decorators:
npm install --save class-validator class-transformerPrisma Schema Configuration
Example schema.prisma:
generator client {
provider = "prisma-client"
output = "../src/generated/prisma"
engineType = "library"
}
generator classGenerator {
provider = "npx simple-prisma-dto-gen"
output = "../src/gen.dto"
customPrismaClientImportPath = "src/generated/prisma/client"
generatePrismaService = true
databaseType = "postgresql"
activeField = "active"
useClassValidator = "true"
}Generator Options
output: target directory for generated filescustomPrismaClientImportPath: import path used in generated service filesgeneratePrismaService: whentrue, generatesservice/prisma.service.ts; whenfalse,DatabaseGenServicedepends directly onPrismaClientdatabaseType: database adapter used by generatedPrismaService; supported values aremariadbandpostgresqlactiveField: when set, generatedfindOne*andfindMany*methods filter by this field set totrue, and generateddelete*methods perform soft delete by setting this field tofalseuseClassValidator: whentrue, generatedInsertDtoclasses includeclass-validatordecorators
If activeField is not configured, generated delete* methods call Prisma delete().
If databaseType is not configured, the generated PrismaService uses the MariaDB adapter.
JSON Fields
Prisma Json fields are generated with Prisma's own JSON types.
For regular DTOs:
payload: Prisma.JsonValue
metadata?: Prisma.JsonValue | nullFor InsertDto classes:
payload: Prisma.InputJsonValue
metadata?: Prisma.InputJsonValue | Prisma.NullableJsonNullValueInputGenerated files import Prisma only when the model has at least one Json field:
import { Prisma } from "src/generated/prisma/client"Swagger metadata for Json fields is generated as type: () => Object. JSON arrays also receive isArray: true.
Class Validator
When useClassValidator = "true", generated InsertDto classes include decorators from class-validator.
Example output:
import { ApiProperty } from "@nestjs/swagger"
import { IsNotEmpty, IsOptional, IsString } from "class-validator"
export class UserInsertDto {
@ApiProperty()
@IsString()
@IsNotEmpty()
name: string
@ApiProperty({required: false, nullable: true})
@IsOptional()
@IsString()
nickname?: string
}Current validator mapping:
String:IsString; required strings without defaults also receiveIsNotEmptyIntandBigInt:IsIntFloatandDecimal:IsNumberBoolean:IsBooleanDateTime:IsDateJson:IsObjectfor non-array fieldsEnum:IsEnum- list fields:
IsArrayand item validation witheach: truewhen applicable - optional fields and fields with defaults:
IsOptional
Usage
Initialize Prisma if needed:
npx prisma initConfigure your database connection, then introspect or maintain your schema as usual:
npx prisma db pullRun generators:
npx prisma generateThe provider value must be an executable command that Prisma can run. For this package, use the published bin command, not the npm package name. Using @custom-generators/simple-prisma-dto-gen directly will fail because Prisma tries to execute that exact string in the shell.
If you prefer, you can still use the explicit executable path as a fallback:
generator classGenerator {
provider = "node node_modules/@custom-generators/simple-prisma-dto-gen/dist/bin.js"
}Release
Before publishing, make sure you are logged in to npm:
npm loginRun the build locally:
npm run buildUpdate the package version with one of npm's version commands:
npm version patchUse minor or major instead of patch when the release contains broader changes:
npm version minor
npm version majorPublish the package:
npm publishThe package is configured with:
{
"files": [
"dist"
],
"prepublishOnly": "npm run build",
"publishConfig": {
"access": "public"
}
}Because of that, only the compiled dist directory is published, npm publish runs the build automatically before publishing, and the scoped package is published with public access.
After publishing, install or update the package in the consuming project:
npm install --save-dev @custom-generators/simple-prisma-dto-gen@latestThen regenerate Prisma artifacts in the consuming project:
npx prisma generateNestJS Integration
If you generate PrismaService, load environment variables before the Nest app bootstraps:
import 'dotenv/config'Example wrapper service:
import { Injectable } from "@nestjs/common"
import { DatabaseGenService } from "./gen.dto/service/database.gen"
import { PrismaService } from "./gen.dto/service/prisma.service"
@Injectable()
export class DatabaseService extends DatabaseGenService {
constructor(prisma: PrismaService) {
super(prisma)
}
}Register services in your module:
providers: [DatabaseService, PrismaService]Example
For a Prisma model like:
model User {
id Int @id @default(autoincrement())
name String
active Boolean @default(true)
}The generator will produce files similar to:
dtos/user.dto.tsinsert.dtos/user.insert.dto.tsservice/database.gen.ts
And DatabaseGenService will include methods like:
findOneUserfindManyUsercreateUserupdateUserdeleteUser
Limitations
- The generated DTOs are tailored to NestJS Swagger usage
- This package is intentionally opinionated and project-style driven
