Skip to content

Commit

Permalink
feat: add some overload on with method
Browse files Browse the repository at this point in the history
We can now skip the `count` parameter to be less verbose
  • Loading branch information
Julien-R44 committed Sep 22, 2022
1 parent fd1fcd5 commit 8f9c32a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/core/src/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,18 @@ export class Builder<
/**
* Apply a relationship
*/
public with(name: Relationships, count = 1, callback?: WithCallback) {
this.relationshipBuilder.apply(name as string, count, callback)
public with(name: Relationships, callback?: WithCallback): this
public with(name: Relationships, count: number, callback?: WithCallback): this
public with(
name: Relationships,
countOrCallback?: number | WithCallback,
callback?: WithCallback
) {
if (typeof countOrCallback === 'function') {
this.relationshipBuilder.apply(name as string, 1, countOrCallback)
} else {
this.relationshipBuilder.apply(name as string, countOrCallback || 1, callback)
}
return this
}

Expand Down
11 changes: 11 additions & 0 deletions tests/has_one.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ test.group('HasOne', (group) => {
await database.assertHas('profile', { user_id: users[1].id }, 1)
})

test('with overloading', async ({ database }) => {
const user = await UserFactory.with('profile', (profile) =>
profile.merge({ email: 'hey@ok.com' })
).create()

await database.assertCount('user', 1)
await database.assertCount('profile', 1)

await database.assertHas('profile', { user_id: user.id, email: 'hey@ok.com' }, 1)
})

test('returns relationship', async ({ expect }) => {
const user = await UserFactory.with('profile').create()

Expand Down

0 comments on commit 8f9c32a

Please sign in to comment.