diff --git a/adonis-typings/model.ts b/adonis-typings/model.ts index 9cfa61e7..10315744 100644 --- a/adonis-typings/model.ts +++ b/adonis-typings/model.ts @@ -101,8 +101,9 @@ declare module '@ioc:Adonis/Lucid/Model' { type DecoratorFn = (target, property) => void type BaseRelationDecoratorNode = Omit - type ThroughRelationDecoratorNode = Omit + type ThroughRelationDecoratorNode = Omit type ManyToManyRelationDecoratorNode = Omit + type ModelExecuteableQueryBuilder = ModelQueryBuilderContract & ExcutableQueryBuilderContract type ManyToManyExecutableQueryBuilder = ManyToManyQueryBuilderContract & ExcutableQueryBuilderContract @@ -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 /** diff --git a/providers/DatabaseProvider.ts b/providers/DatabaseProvider.ts index 74315109..39269c5c 100644 --- a/providers/DatabaseProvider.ts +++ b/providers/DatabaseProvider.ts @@ -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) { @@ -43,6 +51,8 @@ export default class DatabaseServiceProvider { hasOne, hasMany, belongsTo, + manyToMany, + hasManyThrough, } }) } diff --git a/src/Orm/Decorators/index.ts b/src/Orm/Decorators/index.ts index 1d9b0a8a..a2055c11 100644 --- a/src/Orm/Decorators/index.ts +++ b/src/Orm/Decorators/index.ts @@ -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)) } } diff --git a/test/orm/model-has-many-through.spec.ts b/test/orm/model-has-many-through.spec.ts index 690efe60..8ef01044 100644 --- a/test/orm/model-has-many-through.spec.ts +++ b/test/orm/model-has-many-through.spec.ts @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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()