Skip to content

Commit

Permalink
refactor: making improvements to has many through decorator function
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jan 12, 2020
1 parent 42b08ec commit 4bb4ff5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
9 changes: 5 additions & 4 deletions adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ declare module '@ioc:Adonis/Lucid/Model' {

type DecoratorFn = (target, property) => void
type BaseRelationDecoratorNode = Omit<BaseRelationNode, 'relatedModel'>
type ThroughRelationDecoratorNode = Omit<ThroughRelationNode, 'relatedModel'>
type ThroughRelationDecoratorNode = Omit<ThroughRelationNode, 'relatedModel' | 'throughModel'>
type ManyToManyRelationDecoratorNode = Omit<ManyToManyRelationNode, 'relatedModel'>

type ModelExecuteableQueryBuilder = ModelQueryBuilderContract<any> & ExcutableQueryBuilderContract<any>
type ManyToManyExecutableQueryBuilder = ManyToManyQueryBuilderContract & ExcutableQueryBuilderContract<any>

Expand Down Expand Up @@ -133,13 +134,13 @@ declare module '@ioc:Adonis/Lucid/Model' {
) => DecoratorFn

export type HasOneThroughFn = (
model: ThroughRelationNode['relatedModel'],
model: [ThroughRelationNode['relatedModel'], ThroughRelationNode['throughModel']],
column?: ThroughRelationDecoratorNode,
) => DecoratorFn

export type HasManyThroughFn = (
model: ThroughRelationNode['relatedModel'],
column: ThroughRelationDecoratorNode,
model: [ThroughRelationNode['relatedModel'], ThroughRelationNode['throughModel']],
column?: ThroughRelationDecoratorNode,
) => DecoratorFn

/**
Expand Down
12 changes: 11 additions & 1 deletion providers/DatabaseProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ import { IocContract } from '@adonisjs/fold'
import { Database } from '../src/Database'
import { Adapter } from '../src/Orm/Adapter'
import { BaseModel } from '../src/Orm/BaseModel'
import { column, computed, hasOne, hasMany, belongsTo } from '../src/Orm/Decorators'
import {
column,
hasOne,
hasMany,
computed,
belongsTo,
manyToMany,
hasManyThrough,
} from '../src/Orm/Decorators'

export default class DatabaseServiceProvider {
constructor (protected $container: IocContract) {
Expand Down Expand Up @@ -43,6 +51,8 @@ export default class DatabaseServiceProvider {
hasOne,
hasMany,
belongsTo,
manyToMany,
hasManyThrough,
}
})
}
Expand Down
8 changes: 4 additions & 4 deletions src/Orm/Decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,21 @@ export const manyToMany: ManyToManyFn = (relatedModel, relation?) => {
/**
* Define hasOneThrough relationship
*/
export const hasOneThrough: HasOneThroughFn = (relatedModel, relation?) => {
export const hasOneThrough: HasOneThroughFn = ([relatedModel, throughModel], relation?) => {
return function decorateAsRelation (target, property: string) {
const Model = target.constructor as ModelConstructorContract
Model.$boot()
Model.$addRelation(property, 'hasOneThrough', Object.assign({ relatedModel }, relation))
Model.$addRelation(property, 'hasOneThrough', Object.assign({ relatedModel, throughModel }, relation))
}
}

/**
* Define hasManyThrough relationship
*/
export const hasManyThrough: HasManyThroughFn = (relatedModel, relation) => {
export const hasManyThrough: HasManyThroughFn = ([relatedModel, throughModel], relation) => {
return function decorateAsRelation (target, property: string) {
const Model = target.constructor as ModelConstructorContract
Model.$boot()
Model.$addRelation(property, 'hasManyThrough', Object.assign({ relatedModel }, relation))
Model.$addRelation(property, 'hasManyThrough', Object.assign({ relatedModel, throughModel }, relation))
}
}
18 changes: 9 additions & 9 deletions test/orm/model-has-many-through.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test.group('Model | Has Many Through', (group) => {
Post.$boot()

class Country extends BaseModel {
@hasManyThrough(() => Post, { throughModel: () => User })
@hasManyThrough([() => Post, () => User])
public posts: Post[]
}
Country.$boot()
Expand Down Expand Up @@ -75,7 +75,7 @@ test.group('Model | Has Many Through', (group) => {
@column({ primary: true })
public id: number

@hasManyThrough(() => Post, { throughModel: () => User })
@hasManyThrough([() => Post, () => User])
public posts: Post[]
}
Country.$boot()
Expand Down Expand Up @@ -107,7 +107,7 @@ test.group('Model | Has Many Through', (group) => {
@column({ primary: true })
public id: number

@hasManyThrough(() => Post, { throughModel: () => User })
@hasManyThrough([() => Post, () => User])
public posts: Post[]
}
Country.$boot()
Expand Down Expand Up @@ -142,7 +142,7 @@ test.group('Model | Has Many Through', (group) => {
@column({ primary: true })
public id: number

@hasManyThrough(() => Post, { throughModel: () => User })
@hasManyThrough([() => Post, () => User])
public posts: Post[]
}
Country.$boot()
Expand Down Expand Up @@ -176,7 +176,7 @@ test.group('Model | Has Many Through', (group) => {
@column({ primary: true })
public id: number

@hasManyThrough(() => Post, { throughModel: () => User })
@hasManyThrough([() => Post, () => User])
public posts: Post[]
}
Country.$boot()
Expand Down Expand Up @@ -224,7 +224,7 @@ test.group('Model | Has Many Through', (group) => {
@column({ primary: true })
public id: number

@hasManyThrough(() => Post, { throughModel: () => User })
@hasManyThrough([() => Post, () => User])
public posts: Post[]
}
Country.$boot()
Expand Down Expand Up @@ -278,7 +278,7 @@ test.group('Model | Has Many Through', (group) => {
@column({ primary: true })
public id: number

@hasManyThrough(() => Post, { throughModel: () => User })
@hasManyThrough([() => Post, () => User])
public posts: Post[]
}
Country.$boot()
Expand Down Expand Up @@ -326,7 +326,7 @@ test.group('Model | Has Many Through', (group) => {
@column({ primary: true })
public id: number

@hasManyThrough(() => Post, { throughModel: () => User })
@hasManyThrough([() => Post, () => User])
public posts: Post[]
}
Country.$boot()
Expand Down Expand Up @@ -383,7 +383,7 @@ test.group('Model | Has Many Through', (group) => {
@column({ primary: true })
public id: number

@hasManyThrough(() => Post, { throughModel: () => User })
@hasManyThrough([() => Post, () => User])
public posts: Post[]
}
Country.$boot()
Expand Down

0 comments on commit 4bb4ff5

Please sign in to comment.