Skip to content

Commit

Permalink
feat!: Change relationship API
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Julien-R44 committed Sep 20, 2022
1 parent cd02b06 commit bc99300
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/contracts.ts
Expand Up @@ -94,6 +94,6 @@ export interface RelationshipMeta {
}

export type RelationshipMetaOptions = Optional<
Omit<RelationshipMeta, 'type'>,
Omit<RelationshipMeta, 'type' | 'factory'>,
'foreignKey' | 'localKey'
>
20 changes: 13 additions & 7 deletions packages/core/src/model.ts
Expand Up @@ -33,10 +33,16 @@ export class FactoryModel<Model extends Record<string, any>, 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,
}
Expand All @@ -58,22 +64,22 @@ export class FactoryModel<Model extends Record<string, any>, 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)
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests-helpers/setup.ts
Expand Up @@ -19,8 +19,8 @@ export const UserFactory = defineFactory<any>('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 }) => ({
Expand All @@ -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()
2 changes: 1 addition & 1 deletion tests/has_many.spec.ts
Expand Up @@ -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()
Expand Down

0 comments on commit bc99300

Please sign in to comment.