Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prisma-class-generator",
"version": "0.2.9",
"name": "prisma-class-generator-custom",
"version": "1.2.5",
"description": "Class generator from Prisma schema",
"main": "dist/index.js",
"license": "MIT",
Expand All @@ -10,10 +10,10 @@
"typescript",
"generator"
],
"homepage": "https://github.com/kimjbstar/prisma-class-generator",
"homepage": "https://github.com/ABHwan/prisma-class-generator",
"repository": {
"type": "git",
"url": "git://github.com/kimjbstar/prisma-class-generator.git"
"url": "git://github.com/ABHwan/prisma-class-generator.git"
},
"scripts": {
"format": "prettier --write \"src/**/*.ts\"",
Expand All @@ -29,8 +29,8 @@
"node": ">=14"
},
"author": {
"name": "kimjbstar",
"email": "kimjbstar@gmail.com"
"name": "ABHwan",
"email": "say5131@gmail.com"
},
"dependencies": {
"@prisma/client": "^5.5.2",
Expand Down
2 changes: 2 additions & 0 deletions prisma/postgresql.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ generator prismaClassGenerator {
output = "../src/_gen/prisma-class"
dryRun = "false"
separateRelationFields = "false"
useValidator = "false"
rewrite = "false"
}

enum ProductType {
Expand Down
2 changes: 1 addition & 1 deletion src/components/field.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class FieldComponent extends BaseComponent implements Echoable {

let defaultValue = ''
if (this.default) {
if (!isNaN(Date.parse(this.default))) {
if (this.type === 'Date' && !isNaN(Date.parse(this.default))) {
defaultValue = `= new Date('${this.default}')`
} else {
defaultValue = `= ${this.default}`
Expand Down
57 changes: 49 additions & 8 deletions src/convertor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ import { ClassComponent } from './components/class.component'
import { DecoratorComponent } from './components/decorator.component'
import { FieldComponent } from './components/field.component'
import { PrismaClassGeneratorConfig } from './generator'
import {
arrayify,
capitalizeFirst,
uniquify,
wrapArrowFunction,
wrapQuote,
} from './util'
import { arrayify, capitalizeFirst, uniquify, wrapArrowFunction, wrapQuote } from './util'

/** BigInt, Boolean, Bytes, DateTime, Decimal, Float, Int, JSON, String, $ModelName */
type DefaultPrismaFieldType =
Expand Down Expand Up @@ -185,6 +179,44 @@ export class PrismaConvertor {
return decorator
}

extractValidatorDecoratorsFromField = (
dmmfField: DMMF.Field,
): DecoratorComponent[] => {
const decorators: DecoratorComponent[] = []
const importFrom = 'class-validator'
let name: string;
switch (dmmfField.type) {
case 'Int':
name = 'IsInt'
break;
case 'DateTime':
name = 'IsDate'
break;
case 'String':
name = 'IsString'
break;
case 'Boolean':
name = 'IsBoolean'
break;
}
if (name && !dmmfField.isList) {
decorators.push(new DecoratorComponent({ name, importFrom }))
}

if (dmmfField.isRequired) {
name = 'IsDefined'
decorators.push(new DecoratorComponent({ name, importFrom }))
} else {
name = 'IsOptional'
decorators.push(new DecoratorComponent({ name, importFrom }))
}
if (dmmfField.kind === 'enum') {
name = 'IsEnum'
decorators.push(new DecoratorComponent({ name, importFrom, params: [`${String(dmmfField.type)}`] }))
}
return decorators;
}

getClass = (input: ConvertModelInput): ClassComponent => {
/** options */
const options = Object.assign(
Expand Down Expand Up @@ -212,7 +244,11 @@ export class PrismaConvertor {
const relationTypes = uniquify(
model.fields
.filter(
(field) => field.relationName && (this._config.separateRelationFields ? true : model.name !== field.type),
(field) =>
field.relationName &&
(this._config.separateRelationFields
? true
: model.name !== field.type),
)
.map((v) => v.type),
)
Expand Down Expand Up @@ -343,6 +379,11 @@ export class PrismaConvertor {
field.decorators.push(decorator)
}

if (this.config.useValidator) {
const decorators = this.extractValidatorDecoratorsFromField(dmmfField)
field.decorators.push(...decorators)
}

if (this.config.useGraphQL) {
const decorator =
this.extractTypeGraphQLDecoratorFromField(dmmfField)
Expand Down
18 changes: 11 additions & 7 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import { GeneratorOptions } from '@prisma/generator-helper'
import { parseEnvValue } from '@prisma/internals'
import * as path from 'path'
import { PrismaConvertor } from './convertor'
import {
getRelativeTSPath,
parseBoolean,
parseNumber,
prettierFormat,
writeTSFile,
} from './util'
import { getRelativeTSPath, parseBoolean, parseNumber, prettierFormat, rmdir, writeTSFile } from './util'
import { INDEX_TEMPLATE } from './templates/index.template'
import { ImportComponent } from './components/import.component'
import * as prettier from 'prettier'
Expand Down Expand Up @@ -53,6 +47,14 @@ export const PrismaClassGeneratorOptions = {
defaultValue: false,
desc: 'preserve default nullable behavior',
},
useValidator: {
desc: 'use nest js class-validator decorators',
defaultValue: false,
},
rewrite: {
defaultValue: false,
desc: 'rewrite output at generate'
},
} as const

export type PrismaClassGeneratorOptionsKeys =
Expand Down Expand Up @@ -133,6 +135,8 @@ export class PrismaClassGenerator {
const config = this.getConfig()
this.setPrismaClientPath()

await rmdir(output, config.rewrite)

const convertor = PrismaConvertor.getInstance()
convertor.dmmf = dmmf
convertor.config = config
Expand Down
14 changes: 14 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,17 @@ export const writeTSFile = (
export const prettierFormat = (content: string, options: Options = {}) => {
return format(content, { ...options, parser: 'typescript' })
}

export const rmdir = async (
dirPath: string,
rewrite: boolean = false,
) => {
if (fs.existsSync(dirPath) && rewrite) {
const deletePromises = fs.readdirSync(dirPath).map(async (file) => {
const filePath = path.join(dirPath, file)
log(`Remove ${filePath}`)
return fs.unlinkSync(filePath)
})
await Promise.all(deletePromises)
}
}