diff --git a/README.md b/README.md index ad31f62..d3c809b 100755 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ export class ProductDto extends IntersectionType( ``` ```typescript - // product.ts + // company.ts import { Product } from './product' import { ApiProperty } from '@nestjs/swagger' @@ -161,7 +161,7 @@ export class ProductDto extends IntersectionType( ``` ```typescript - // category.ts + // product.ts import { Category } from './category' import { Company } from './company' import { ProductType } from '@prisma/client' diff --git a/prisma/postgresql.prisma b/prisma/postgresql.prisma index 84cf4ce..318cad5 100755 --- a/prisma/postgresql.prisma +++ b/prisma/postgresql.prisma @@ -24,16 +24,23 @@ enum TestEnum { } model Product { - id Int @id - letterName TestEnum - title String @db.VarChar(255) - desc String @default("abc") @db.VarChar(1024) - images Json @db.Json - isShown Boolean? @default(false) - stock Int? @default(0) + id Int @id + title String @db.VarChar(255) + desc String @default("abc") @db.VarChar(1024) + images Json @db.Json + isShown Boolean? @default(false) + stock Int @default(0) + type ProductType + anotherType ProductAnotherType @default(AA) averageRating Float? - createdAt DateTime @default(now()) @db.Timestamp(6) - updatedAt DateTime @updatedAt @db.Timestamp(6) + categoryId Int + companyId Int + category Category @relation(fields: [categoryId], references: [id]) + company Company @relation(fields: [companyId], references: [id]) + releasedAt DateTime @default("2024-01-01T00:00:00Z") @db.Timestamp(6) + createdAt DateTime @default(now()) @db.Timestamp(6) + updatedAt DateTime @updatedAt @db.Timestamp(6) +} category Category @relation(fields: [categoryId], references: [id]) categoryId Int diff --git a/src/components/field.component.ts b/src/components/field.component.ts index f08b71e..4121510 100755 --- a/src/components/field.component.ts +++ b/src/components/field.component.ts @@ -26,16 +26,14 @@ export class FieldComponent extends BaseComponent implements Echoable { let defaultValue = '' - if (this.default) { - if (/* !isNaN(Date.parse(this.default)) */ this.type === 'Date') { + if (this.default !== undefined && this.default !== null) { + if (this.type === 'Date' && !Number.isNaN(Date.parse(this.default))) { defaultValue = `= new Date('${this.default}')` } else { defaultValue = `= ${this.default}` } - } else { - if (this.useUndefinedDefault === true) { - defaultValue = `= undefined` - } + } else if (this.useUndefinedDefault === true) { + defaultValue = `= undefined` } return FIELD_TEMPLATE.replace('#!{NAME}', name) diff --git a/src/convertor.ts b/src/convertor.ts index e952684..092676f 100755 --- a/src/convertor.ts +++ b/src/convertor.ts @@ -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 = @@ -212,7 +206,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), ) @@ -647,13 +645,15 @@ export class PrismaConvertor { field.preserveDefaultNullable = true } - if (dmmfField.default) { + if (dmmfField.default !== undefined && dmmfField.default !== null) { if (typeof dmmfField.default !== 'object') { - field.default = dmmfField.default?.toString() + field.default = dmmfField.default.toString() if (dmmfField.kind === 'enum') { field.default = `${dmmfField.type}.${dmmfField.default}` } else if (dmmfField.type === 'BigInt') { field.default = `BigInt(${field.default})` + } else if (dmmfField.type === 'Int') { + field.default = `${field.default}` } else if (dmmfField.type === 'String') { field.default = `'${field.default}'` } @@ -665,9 +665,20 @@ export class PrismaConvertor { } else { field.default = `[${dmmfField.default.toString()}]` } + } else if (dmmfField.type === 'DateTime') { + const defaultDateStr = JSON.stringify(dmmfField.default); + if (defaultDateStr.includes('now')) { + field.default = 'new Date()'; + } else { + field.default = `new Date('${defaultDateStr}')` + } } } + if (dmmfField.isUpdatedAt) { + field.default = 'new Date()' + } + if (type) { field.type = type } else {