Skip to content

Commit

Permalink
Feature/add preserve default nullable (#50)
Browse files Browse the repository at this point in the history
* feat: add preserveDefaultNullable setting

* docs: document preserveDefaultNullable field
  • Loading branch information
joao-moonward authored Sep 17, 2023
1 parent e26b4e1 commit 4747324
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ export class ProductDto extends IntersectionType(
- set prisma client import path manually, default value is **@prisma/client**
- _useNonNullableAssertions_
- Apply a ! after non-optional class fields to avoid strict mode warnings (Property has no initializer and is not definitely assigned in the constructor.)
- _preserveDefaultNullable_
- Determines how null fields are handled. When set to **false** (default), it turns all null fields to undefined. Otherwise, it follows Prisma generation and adds null to the type.
### **How it works?**
Expand Down
10 changes: 8 additions & 2 deletions src/components/field.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ import { BaseComponent } from './base.component'
export class FieldComponent extends BaseComponent implements Echoable {
name: string
nonNullableAssertion: boolean
preserveDefaultNullable: boolean
nullable: boolean
useUndefinedDefault: boolean
default?: string
type?: string

echo = () => {
let name = this.name
let type = this.type
if (this.nullable === true) {
name += '?'
if (this.preserveDefaultNullable) {
type = this.type + ' | null'
} else {
name += '?'
}
} else if (this.nonNullableAssertion === true) {
name += '!'
}
Expand All @@ -29,7 +35,7 @@ export class FieldComponent extends BaseComponent implements Echoable {

return FIELD_TEMPLATE.replace('#!{NAME}', name)
.replace('#!{NAME}', name)
.replace('#!{TYPE}', this.type)
.replace('#!{TYPE}', type)
.replace('#!{DECORATORS}', this.echoDecorators())
.replace('#!{DEFAULT}', defaultValue)
}
Expand Down
4 changes: 4 additions & 0 deletions src/convertor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ export class PrismaConvertor {
field.nonNullableAssertion = true
}

if(this.config.preserveDefaultNullable) {
field.preserveDefaultNullable = true
}

if (dmmfField.default) {
if (typeof dmmfField.default !== 'object') {
field.default = dmmfField.default?.toString()
Expand Down
4 changes: 4 additions & 0 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export const PrismaClassGeneratorOptions = {
useNonNullableAssertions: {
desc: 'applies non-nullable assertions (!) to class properties',
defaultValue: false
},
preserveDefaultNullable: {
defaultValue: false,
desc: 'preserve default nullable behavior'
}
} as const

Expand Down

0 comments on commit 4747324

Please sign in to comment.