From 8f9c32a747a152deecdf2e1ea225ed107f82815d Mon Sep 17 00:00:00 2001 From: Julien Date: Fri, 23 Sep 2022 00:50:09 +0200 Subject: [PATCH] feat: add some overload on `with` method We can now skip the `count` parameter to be less verbose --- packages/core/src/builder/builder.ts | 14 ++++++++++++-- tests/has_one.spec.ts | 11 +++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/core/src/builder/builder.ts b/packages/core/src/builder/builder.ts index 0a7777b..c7f53a2 100644 --- a/packages/core/src/builder/builder.ts +++ b/packages/core/src/builder/builder.ts @@ -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 } diff --git a/tests/has_one.spec.ts b/tests/has_one.spec.ts index 29c8fac..2443207 100644 --- a/tests/has_one.spec.ts +++ b/tests/has_one.spec.ts @@ -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()