Skip to content

Commit

Permalink
feat: added ability to instantiate a DTO instance without initial data
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgobich committed Jun 16, 2024
1 parent dde8b77 commit c05e38a
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

> Easily make and generate DTOs from Lucid Models

[![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url]

Converting Lucid Models to DTO files can be a tedious task.
Expand Down Expand Up @@ -268,7 +267,8 @@ export default class AccountDto {
declare isBudgetable: boolean
declare balanceDisplay: string

constructor(account: Account) {
constructor(account?: Account) {
if (!account) return
this.id = account.id
this.userId = account.userId
this.accountTypeId = account.accountTypeId
Expand Down
3 changes: 2 additions & 1 deletion stubs/make/dto/main.stub
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {{ model.name }} from '#models/{{ string.snakeCase(model.name) }}'
export default class {{ dto.className }} {{{ '{' }}}{{ #each dto.properties as property }}
{{{ property.declaration }}}{{ /each }}

constructor({{ model.variable }}: {{ model.name }}) {{{ '{' }}}{{ #each dto.properties as property }}
constructor({{ model.variable }}?: {{ model.name }}) {
if (!{{ model.variable }}) return{{ #each dto.properties as property }}
this.{{ property.name }} = {{{ property.valueSetter }}}{{ /each }}
}

Expand Down
3 changes: 2 additions & 1 deletion test-files/expectations/account.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export default class AccountDto {
declare isBudgetable: boolean
declare balanceDisplay: string

constructor(account: Account) {
constructor(account?: Account) {
if (!account) return
this.id = account.id
this.userId = account.userId
this.accountTypeId = account.accountTypeId
Expand Down
3 changes: 2 additions & 1 deletion test-files/expectations/some_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export default class SomeTestDto {
declare id: number
declare createdAt: string

constructor(test: Test) {
constructor(test?: Test) {
if (!test) return
this.id = test.id
this.createdAt = test.createdAt.toISO()!
}
Expand Down
3 changes: 2 additions & 1 deletion test-files/expectations/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export default class TestDto {
declare id: number
declare createdAt: string

constructor(test: Test) {
constructor(test?: Test) {
if (!test) return
this.id = test.id
this.createdAt = test.createdAt.toISO()!
}
Expand Down
3 changes: 2 additions & 1 deletion test-files/expectations/user.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export default class UserDto {
declare stockPurchases: StockPurchaseDto[]
declare stocks: StockDto[]

constructor(user: User) {
constructor(user?: User) {
if (!user) return
this.id = user.id
this.fullName = user.fullName
this.email = user.email
Expand Down

0 comments on commit c05e38a

Please sign in to comment.