|
1 | | -import {BELONGS_TO_MANY, addAssociation} from "../../services/association"; |
| 1 | +import {addAssociation} from "../../services/association"; |
2 | 2 | import {ModelClassGetter} from "../../types/ModelClassGetter"; |
3 | 3 | import {IAssociationOptionsBelongsToMany} from "../../interfaces/IAssociationOptionsBelongsToMany"; |
| 4 | +import {BelongsToManyAssociation} from '../../models/association/BelongsToManyAssociation'; |
4 | 5 |
|
5 | | -export function BelongsToMany(relatedClassGetter: ModelClassGetter, |
6 | | - through: (ModelClassGetter) | string, |
| 6 | +export function BelongsToMany(associatedClassGetter: ModelClassGetter, |
| 7 | + through: ModelClassGetter | string, |
7 | 8 | foreignKey?: string, |
8 | 9 | otherKey?: string): Function; |
9 | | -export function BelongsToMany(relatedClassGetter: ModelClassGetter, |
| 10 | +export function BelongsToMany(associatedClassGetter: ModelClassGetter, |
10 | 11 | options: IAssociationOptionsBelongsToMany): Function; |
11 | | -export function BelongsToMany(relatedClassGetter: ModelClassGetter, |
12 | | - throughOrOptions: (ModelClassGetter | string) | IAssociationOptionsBelongsToMany, |
| 12 | +export function BelongsToMany(associatedClassGetter: ModelClassGetter, |
| 13 | + throughOrOptions: ModelClassGetter | string | IAssociationOptionsBelongsToMany, |
13 | 14 | foreignKey?: string, |
14 | 15 | otherKey?: string): Function { |
15 | | - const typeOfThroughOrOptions = typeof throughOrOptions; |
16 | | - let through; |
17 | | - let options: Partial<IAssociationOptionsBelongsToMany>; |
18 | 16 |
|
19 | | - if (typeOfThroughOrOptions === 'string' || typeOfThroughOrOptions === 'function') { |
20 | | - through = throughOrOptions; |
21 | | - } else { |
22 | | - through = (throughOrOptions as IAssociationOptionsBelongsToMany).through; |
23 | | - options = throughOrOptions as IAssociationOptionsBelongsToMany; |
24 | | - } |
25 | 17 | return (target: any, propertyName: string) => { |
26 | | - addAssociation( |
27 | | - target, |
28 | | - BELONGS_TO_MANY, |
29 | | - relatedClassGetter, |
30 | | - propertyName, |
31 | | - options || foreignKey, |
32 | | - through, |
33 | | - otherKey, |
| 18 | + let options: Partial<IAssociationOptionsBelongsToMany> = {foreignKey, otherKey}; |
| 19 | + |
| 20 | + if (typeof throughOrOptions === 'string' || |
| 21 | + typeof throughOrOptions === 'function') { |
| 22 | + options.through = throughOrOptions; |
| 23 | + } else { |
| 24 | + options = {...throughOrOptions}; |
| 25 | + } |
| 26 | + |
| 27 | + if (!options.as) options.as = propertyName; |
| 28 | + |
| 29 | + addAssociation(target, new BelongsToManyAssociation( |
| 30 | + associatedClassGetter, |
| 31 | + options as IAssociationOptionsBelongsToMany, |
| 32 | + ) |
34 | 33 | ); |
35 | 34 | }; |
36 | 35 | } |
0 commit comments