From bc993005d6e89b35e469f50833d89ed6c7fe44f1 Mon Sep 17 00:00:00 2001 From: Julien Date: Tue, 20 Sep 2022 23:47:59 +0200 Subject: [PATCH] feat!: Change relationship API Relationship factory callback is now accepted as a second argument. The Relationship meta { foreignKey, localKey } is optional and accepted as a third argument. This makes the API less verbeous --- packages/core/src/contracts.ts | 2 +- packages/core/src/model.ts | 20 +++++++++++++------- tests-helpers/setup.ts | 8 ++++---- tests/has_many.spec.ts | 2 +- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/core/src/contracts.ts b/packages/core/src/contracts.ts index cde2212..2a97be8 100644 --- a/packages/core/src/contracts.ts +++ b/packages/core/src/contracts.ts @@ -94,6 +94,6 @@ export interface RelationshipMeta { } export type RelationshipMetaOptions = Optional< - Omit, + Omit, 'foreignKey' | 'localKey' > diff --git a/packages/core/src/model.ts b/packages/core/src/model.ts index 976a159..f588808 100644 --- a/packages/core/src/model.ts +++ b/packages/core/src/model.ts @@ -33,10 +33,16 @@ export class FactoryModel, States extends stri this.callback = callback } - private addRelation(name: string, type: RelationType, meta: RelationshipMetaOptions) { + private addRelation( + name: string, + factory: RelationshipMeta['factory'], + type: RelationType, + meta?: RelationshipMetaOptions + ) { this.relations[name] = { foreignKey: `${this.tableName}_id`, localKey: 'id', + factory, ...meta, type, } @@ -58,22 +64,22 @@ export class FactoryModel, States extends stri /** * Add hasOne relationship */ - public hasOne(name: string, meta: RelationshipMetaOptions) { - return this.addRelation(name, RelationType.HasOne, meta) + public hasOne(name: string, cb: RelationshipMeta['factory'], meta?: RelationshipMetaOptions) { + return this.addRelation(name, cb, RelationType.HasOne, meta) } /** * Add hasMany relationship */ - public hasMany(name: string, meta: RelationshipMetaOptions) { - return this.addRelation(name, RelationType.HasMany, meta) + public hasMany(name: string, cb: RelationshipMeta['factory'], meta?: RelationshipMetaOptions) { + return this.addRelation(name, cb, RelationType.HasMany, meta) } /** * Add belongsTo relationship */ - public belongsTo(name: string, meta: RelationshipMetaOptions) { - return this.addRelation(name, RelationType.BelongsTo, meta) + public belongsTo(name: string, cb: RelationshipMeta['factory'], meta?: RelationshipMetaOptions) { + return this.addRelation(name, cb, RelationType.BelongsTo, meta) } /** diff --git a/tests-helpers/setup.ts b/tests-helpers/setup.ts index 7b7681c..70cb784 100644 --- a/tests-helpers/setup.ts +++ b/tests-helpers/setup.ts @@ -19,8 +19,8 @@ export const UserFactory = defineFactory('user', ({ faker }) => ({ })) .state('easyPassword', () => ({ password: 'easy' })) .state('easyEmail', () => ({ email: 'easy@easy.com' })) - .hasOne('profile', { foreignKey: 'user_id', localKey: 'id', factory: () => ProfileFactory }) - .hasMany('posts', { foreignKey: 'user_id', localKey: 'id', factory: () => PostFactory }) + .hasOne('profile', () => ProfileFactory, { foreignKey: 'user_id', localKey: 'id' }) + .hasMany('posts', () => PostFactory, { foreignKey: 'user_id', localKey: 'id' }) .build() export const AdminFactory = defineFactory('admin', ({ faker }) => ({ @@ -30,6 +30,6 @@ export const AdminFactory = defineFactory('admin', ({ faker }) => ({ export const AccountFactory = defineFactory('account', ({ faker }) => ({ name: faker.commerce.productName(), })) - .belongsTo('user', { foreignKey: 'user_id', localKey: 'id', factory: () => UserFactory }) - .belongsTo('admin', { foreignKey: 'admin_id', localKey: 'id', factory: () => AdminFactory }) + .belongsTo('user', () => UserFactory, { foreignKey: 'user_id', localKey: 'id' }) + .belongsTo('admin', () => AdminFactory, { foreignKey: 'admin_id', localKey: 'id' }) .build() diff --git a/tests/has_many.spec.ts b/tests/has_many.spec.ts index 28b9caa..f8632d3 100644 --- a/tests/has_many.spec.ts +++ b/tests/has_many.spec.ts @@ -89,7 +89,7 @@ test.group('HasMany', (group) => { email: faker.internet.email(), password: faker.random.alphaNumeric(6), })) - .hasMany('post', { factory: () => postFactory }) + .hasMany('post', () => postFactory) .build() const user = await userFactory.with('post', 5).create()