-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from RobinBuschmann/issue-#122
Issue #122
- Loading branch information
Showing
38 changed files
with
884 additions
and
456 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,22 @@ | ||
import {AssociationOptionsBelongsTo} from 'sequelize'; | ||
import {BELONGS_TO, addAssociation} from "../../services/association"; | ||
import {addAssociation, getPreparedAssociationOptions} from "../../services/association"; | ||
import {ModelClassGetter} from "../../types/ModelClassGetter"; | ||
import {BelongsToAssociation} from '../../models/association/BelongsToAssociation'; | ||
|
||
export function BelongsTo(relatedClassGetter: ModelClassGetter, | ||
export function BelongsTo(associatedClassGetter: ModelClassGetter, | ||
foreignKey?: string): Function; | ||
export function BelongsTo(relatedClassGetter: ModelClassGetter, | ||
export function BelongsTo(associatedClassGetter: ModelClassGetter, | ||
options?: AssociationOptionsBelongsTo): Function; | ||
export function BelongsTo(relatedClassGetter: ModelClassGetter, | ||
export function BelongsTo(associatedClassGetter: ModelClassGetter, | ||
optionsOrForeignKey?: string | AssociationOptionsBelongsTo): Function { | ||
|
||
return (target: any, propertyName: string) => { | ||
addAssociation( | ||
target, | ||
BELONGS_TO, | ||
relatedClassGetter, | ||
propertyName, | ||
optionsOrForeignKey | ||
const options: AssociationOptionsBelongsTo = getPreparedAssociationOptions(optionsOrForeignKey); | ||
if (!options.as) options.as = propertyName; | ||
addAssociation(target, new BelongsToAssociation( | ||
associatedClassGetter, | ||
options, | ||
) | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,35 @@ | ||
import {BELONGS_TO_MANY, addAssociation} from "../../services/association"; | ||
import {addAssociation} from "../../services/association"; | ||
import {ModelClassGetter} from "../../types/ModelClassGetter"; | ||
import {IAssociationOptionsBelongsToMany} from "../../interfaces/IAssociationOptionsBelongsToMany"; | ||
import {BelongsToManyAssociation} from '../../models/association/BelongsToManyAssociation'; | ||
|
||
export function BelongsToMany(relatedClassGetter: ModelClassGetter, | ||
through: (ModelClassGetter) | string, | ||
export function BelongsToMany(associatedClassGetter: ModelClassGetter, | ||
through: ModelClassGetter | string, | ||
foreignKey?: string, | ||
otherKey?: string): Function; | ||
export function BelongsToMany(relatedClassGetter: ModelClassGetter, | ||
export function BelongsToMany(associatedClassGetter: ModelClassGetter, | ||
options: IAssociationOptionsBelongsToMany): Function; | ||
export function BelongsToMany(relatedClassGetter: ModelClassGetter, | ||
throughOrOptions: (ModelClassGetter | string) | IAssociationOptionsBelongsToMany, | ||
export function BelongsToMany(associatedClassGetter: ModelClassGetter, | ||
throughOrOptions: ModelClassGetter | string | IAssociationOptionsBelongsToMany, | ||
foreignKey?: string, | ||
otherKey?: string): Function { | ||
const typeOfThroughOrOptions = typeof throughOrOptions; | ||
let through; | ||
let options: Partial<IAssociationOptionsBelongsToMany>; | ||
|
||
if (typeOfThroughOrOptions === 'string' || typeOfThroughOrOptions === 'function') { | ||
through = throughOrOptions; | ||
} else { | ||
through = (throughOrOptions as IAssociationOptionsBelongsToMany).through; | ||
options = throughOrOptions as IAssociationOptionsBelongsToMany; | ||
} | ||
return (target: any, propertyName: string) => { | ||
addAssociation( | ||
target, | ||
BELONGS_TO_MANY, | ||
relatedClassGetter, | ||
propertyName, | ||
options || foreignKey, | ||
through, | ||
otherKey, | ||
let options: Partial<IAssociationOptionsBelongsToMany> = {foreignKey, otherKey}; | ||
|
||
if (typeof throughOrOptions === 'string' || | ||
typeof throughOrOptions === 'function') { | ||
options.through = throughOrOptions; | ||
} else { | ||
options = {...throughOrOptions}; | ||
} | ||
|
||
if (!options.as) options.as = propertyName; | ||
|
||
addAssociation(target, new BelongsToManyAssociation( | ||
associatedClassGetter, | ||
options as IAssociationOptionsBelongsToMany, | ||
) | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
import {AssociationOptionsHasMany} from 'sequelize'; | ||
import {HAS_MANY, addAssociation} from "../../services/association"; | ||
import {addAssociation, getPreparedAssociationOptions} from "../../services/association"; | ||
import {Association} from "../../enums/Association"; | ||
import {ModelClassGetter} from "../../types/ModelClassGetter"; | ||
import {HasAssociation} from '../../models/association/HasAssociation'; | ||
|
||
export function HasMany(relatedClassGetter: ModelClassGetter, | ||
export function HasMany(associatedClassGetter: ModelClassGetter, | ||
foreignKey?: string): Function; | ||
export function HasMany(relatedClassGetter: ModelClassGetter, | ||
export function HasMany(associatedClassGetter: ModelClassGetter, | ||
options?: AssociationOptionsHasMany): Function; | ||
export function HasMany(relatedClassGetter: ModelClassGetter, | ||
export function HasMany(associatedClassGetter: ModelClassGetter, | ||
optionsOrForeignKey?: string | AssociationOptionsHasMany): Function { | ||
|
||
return (target: any, propertyName: string) => { | ||
addAssociation( | ||
target, | ||
HAS_MANY, | ||
relatedClassGetter, | ||
propertyName, | ||
optionsOrForeignKey, | ||
const options: AssociationOptionsHasMany = getPreparedAssociationOptions(optionsOrForeignKey); | ||
if (!options.as) options.as = propertyName; | ||
addAssociation(target, new HasAssociation( | ||
associatedClassGetter, | ||
options, | ||
Association.HasMany, | ||
) | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
import {AssociationOptionsHasOne} from 'sequelize'; | ||
import {addAssociation, HAS_ONE} from "../../services/association"; | ||
import {Association} from "../../enums/Association"; | ||
import {ModelClassGetter} from "../../types/ModelClassGetter"; | ||
import {addAssociation, getPreparedAssociationOptions} from '../../services/association'; | ||
import {HasAssociation} from '../../models/association/HasAssociation'; | ||
|
||
export function HasOne(relatedClassGetter: ModelClassGetter, | ||
export function HasOne(associatedClassGetter: ModelClassGetter, | ||
foreignKey?: string): Function; | ||
export function HasOne(relatedClassGetter: ModelClassGetter, | ||
export function HasOne(associatedClassGetter: ModelClassGetter, | ||
options?: AssociationOptionsHasOne): Function; | ||
export function HasOne(relatedClassGetter: ModelClassGetter, | ||
export function HasOne(associatedClassGetter: ModelClassGetter, | ||
optionsOrForeignKey?: string | AssociationOptionsHasOne): Function { | ||
|
||
return (target: any, propertyName: string) => { | ||
addAssociation( | ||
target, | ||
HAS_ONE, | ||
relatedClassGetter, | ||
propertyName, | ||
optionsOrForeignKey, | ||
const options: AssociationOptionsHasOne = getPreparedAssociationOptions(optionsOrForeignKey); | ||
if (!options.as) options.as = propertyName; | ||
addAssociation(target, new HasAssociation( | ||
associatedClassGetter, | ||
options, | ||
Association.HasOne, | ||
) | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export enum Association { | ||
BelongsToMany = 'belongsToMany', | ||
BelongsTo = 'belongsTo', | ||
HasMany = 'hasMany', | ||
HasOne = 'hasOne', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { | ||
AssociationOptionsBelongsTo, AssociationOptionsHasMany, | ||
AssociationOptionsHasOne, AssociationOptionsManyToMany | ||
} from 'sequelize'; | ||
import {IPreparedAssociationOptionsBelongsToMany} from './IPreparedAssociationOptionsBelongsToMany'; | ||
|
||
export type AssociationOptions = | ||
AssociationOptionsBelongsTo | | ||
IPreparedAssociationOptionsBelongsToMany | | ||
AssociationOptionsHasMany | | ||
AssociationOptionsHasOne | | ||
AssociationOptionsManyToMany; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import {AssociationForeignKeyOptions, AssociationOptionsManyToMany} from "sequelize"; | ||
import {ModelClassGetter} from "../types/ModelClassGetter"; | ||
import {IThroughOptions} from './IThroughOptions'; | ||
|
||
export interface IAssociationOptionsBelongsToMany extends AssociationOptionsManyToMany { | ||
through: ModelClassGetter | string; | ||
through: ModelClassGetter | string | IThroughOptions; | ||
otherKey?: string | AssociationForeignKeyOptions; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {AssociationForeignKeyOptions, AssociationOptionsManyToMany} from "sequelize"; | ||
import {IPreparedThroughOptions} from './IPreparedThroughOptions'; | ||
|
||
export interface IPreparedAssociationOptionsBelongsToMany extends AssociationOptionsManyToMany { | ||
through: IPreparedThroughOptions; | ||
otherKey?: string | AssociationForeignKeyOptions; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {AssociationScope} from 'sequelize'; | ||
import {Model} from '../models/Model'; | ||
|
||
/** | ||
* Used for a association table in n:m associations. | ||
* | ||
* @see AssociationOptionsBelongsToMany | ||
*/ | ||
export interface IPreparedThroughOptions { | ||
|
||
/** | ||
* The model used to join both sides of the N:M association. | ||
*/ | ||
model: typeof Model; | ||
|
||
/** | ||
* A key/value set that will be used for association create and find defaults on the through model. | ||
* (Remember to add the attributes to the through model) | ||
*/ | ||
scope?: AssociationScope; | ||
|
||
/** | ||
* If true a unique key will be generated from the foreign keys used (might want to turn this off and create | ||
* specific unique keys when using scopes) | ||
* | ||
* Defaults to true | ||
*/ | ||
unique?: boolean; | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {AssociationScope} from 'sequelize'; | ||
import {ModelClassGetter} from '../types/ModelClassGetter'; | ||
|
||
/** | ||
* Used for a association table in n:m associations. | ||
* | ||
* @see AssociationOptionsBelongsToMany | ||
*/ | ||
export interface IThroughOptions { | ||
|
||
/** | ||
* The model used to join both sides of the N:M association. | ||
*/ | ||
model: ModelClassGetter | string; | ||
|
||
/** | ||
* A key/value set that will be used for association create and find defaults on the through model. | ||
* (Remember to add the attributes to the through model) | ||
*/ | ||
scope?: AssociationScope; | ||
|
||
/** | ||
* If true a unique key will be generated from the foreign keys used (might want to turn this off and create | ||
* specific unique keys when using scopes) | ||
* | ||
* Defaults to true | ||
*/ | ||
unique?: boolean; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.