Skip to content
Merged
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class ProductDto extends IntersectionType(
```

```typescript
// product.ts
// company.ts
import { Product } from './product'
import { ApiProperty } from '@nestjs/swagger'

Expand Down Expand Up @@ -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'
Expand Down
25 changes: 16 additions & 9 deletions prisma/postgresql.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions src/components/field.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 21 additions & 10 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 @@ -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),
)
Expand Down Expand Up @@ -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}'`
}
Expand All @@ -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 {
Expand Down