Skip to content

Commit 5adf10b

Browse files
Merge pull request #48 from RobinBuschmann/refactoring-1
Refactoring part 1
2 parents 06724f0 + f1cb554 commit 5adf10b

23 files changed

+676
-375
lines changed

example/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"module": "commonjs",
4-
"target": "es5",
4+
"target": "es6",
55
"experimentalDecorators": true,
66
"emitDecoratorMetadata": true,
77
"sourceMap": true,

lib/annotations/ForeignKey.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
import {AssociationForeignKeyOptions} from 'sequelize';
2-
import {Model} from "../models/Model";
31
import {addForeignKey} from "../services/association";
2+
import {ModelClassGetter} from "../types/ModelClassGetter";
43

5-
export function ForeignKey(relatedClassGetter: () => typeof Model, options?: AssociationForeignKeyOptions): Function {
4+
export function ForeignKey(relatedClassGetter: ModelClassGetter): Function {
65

76
return (target: any, propertyName: string) => {
87

9-
if (!options) {
10-
options = {name: propertyName};
11-
} else if (!options.name) {
12-
options.name = propertyName;
13-
}
14-
15-
addForeignKey(target, relatedClassGetter, options);
8+
addForeignKey(target, relatedClassGetter, propertyName);
169
};
1710
}
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import {AssociationOptionsBelongsTo} from 'sequelize';
2-
3-
import {Model} from "../../models/Model";
42
import {BELONGS_TO, addAssociation} from "../../services/association";
3+
import {ModelClassGetter} from "../../types/ModelClassGetter";
54

6-
export function BelongsTo(relatedClassGetter: () => typeof Model,
7-
options?: string | AssociationOptionsBelongsTo): Function {
5+
export function BelongsTo(relatedClassGetter: ModelClassGetter,
6+
foreignKey?: string): Function;
7+
export function BelongsTo(relatedClassGetter: ModelClassGetter,
8+
options?: AssociationOptionsBelongsTo): Function;
9+
export function BelongsTo(relatedClassGetter: ModelClassGetter,
10+
optionsOrForeignKey?: string | AssociationOptionsBelongsTo): Function {
811

912
return (target: any, propertyName: string) => {
10-
11-
if (typeof options === 'string') {
12-
options = {foreignKey: {name: options}};
13-
}
14-
1513
addAssociation(
1614
target,
1715
BELONGS_TO,
1816
relatedClassGetter,
1917
propertyName,
20-
options
18+
optionsOrForeignKey
2119
);
2220
};
2321
}
Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
1-
import {AssociationOptionsBelongsToMany} from 'sequelize';
2-
3-
import {Model} from "../../models/Model";
41
import {BELONGS_TO_MANY, addAssociation} from "../../services/association";
2+
import {ModelClassGetter} from "../../types/ModelClassGetter";
3+
import {IAssociationOptionsBelongsToMany} from "../../interfaces/IAssociationOptionsBelongsToMany";
54

6-
export function BelongsToMany(relatedClassGetter: () => typeof Model,
7-
through: (() => typeof Model)|string,
8-
options?: string | AssociationOptionsBelongsToMany,
5+
export function BelongsToMany(relatedClassGetter: ModelClassGetter,
6+
through: (ModelClassGetter) | string,
7+
foreignKey?: string,
8+
otherKey?: string): Function;
9+
export function BelongsToMany(relatedClassGetter: ModelClassGetter,
10+
options: IAssociationOptionsBelongsToMany): Function;
11+
export function BelongsToMany(relatedClassGetter: ModelClassGetter,
12+
throughOrOptions: (ModelClassGetter | string) | IAssociationOptionsBelongsToMany,
13+
foreignKey?: string,
914
otherKey?: string): Function {
15+
const typeOfThroughOrOptions = typeof throughOrOptions;
16+
let through;
17+
let options: Partial<IAssociationOptionsBelongsToMany>;
1018

19+
if (typeOfThroughOrOptions === 'string' || typeOfThroughOrOptions === 'function') {
20+
through = throughOrOptions;
21+
} else {
22+
through = (throughOrOptions as IAssociationOptionsBelongsToMany).through;
23+
options = throughOrOptions;
24+
}
1125
return (target: any, propertyName: string) => {
12-
if (typeof options === 'string') {
13-
// don't worry, through is mainly here to avoid TS error; actual through value is resolved later
14-
options = {through: '', foreignKey: {name: options}};
15-
}
1626
addAssociation(
1727
target,
1828
BELONGS_TO_MANY,
1929
relatedClassGetter,
2030
propertyName,
21-
options,
31+
options || foreignKey,
32+
through,
2233
otherKey,
23-
through
2434
);
2535
};
2636
}
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import {AssociationOptionsHasMany} from 'sequelize';
2-
3-
import {Model} from "../../models/Model";
42
import {HAS_MANY, addAssociation} from "../../services/association";
3+
import {ModelClassGetter} from "../../types/ModelClassGetter";
54

6-
export function HasMany(relatedClassGetter: () => typeof Model,
7-
options?: string | AssociationOptionsHasMany): Function {
8-
5+
export function HasMany(relatedClassGetter: ModelClassGetter,
6+
foreignKey?: string): Function;
7+
export function HasMany(relatedClassGetter: ModelClassGetter,
8+
options?: AssociationOptionsHasMany): Function;
9+
export function HasMany(relatedClassGetter: ModelClassGetter,
10+
optionsOrForeignKey?: string | AssociationOptionsHasMany): Function {
911
return (target: any, propertyName: string) => {
10-
if (typeof options === 'string') {
11-
options = {foreignKey: {name: options}};
12-
}
1312
addAssociation(
1413
target,
1514
HAS_MANY,
1615
relatedClassGetter,
1716
propertyName,
18-
options
17+
optionsOrForeignKey,
1918
);
2019
};
2120
}

lib/annotations/association/HasOne.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import {AssociationOptionsHasOne} from 'sequelize';
2-
3-
import {Model} from "../../models/Model";
42
import {addAssociation, HAS_ONE} from "../../services/association";
3+
import {ModelClassGetter} from "../../types/ModelClassGetter";
54

6-
export function HasOne(relatedClassGetter: () => typeof Model,
7-
options?: string | AssociationOptionsHasOne): Function {
8-
5+
export function HasOne(relatedClassGetter: ModelClassGetter,
6+
foreignKey?: string): Function;
7+
export function HasOne(relatedClassGetter: ModelClassGetter,
8+
options?: AssociationOptionsHasOne): Function;
9+
export function HasOne(relatedClassGetter: ModelClassGetter,
10+
optionsOrForeignKey?: string | AssociationOptionsHasOne): Function {
911
return (target: any, propertyName: string) => {
10-
if (typeof options === 'string') {
11-
options = {foreignKey: {name: options}};
12-
}
1312
addAssociation(
1413
target,
1514
HAS_ONE,
1615
relatedClassGetter,
1716
propertyName,
18-
options
17+
optionsOrForeignKey,
1918
);
2019
};
2120
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {AssociationForeignKeyOptions, AssociationOptionsManyToMany} from "sequelize";
2+
import {ModelClassGetter} from "../types/ModelClassGetter";
3+
4+
export interface IAssociationOptionsBelongsToMany extends AssociationOptionsManyToMany {
5+
through: ModelClassGetter | string;
6+
otherKey?: string | AssociationForeignKeyOptions;
7+
}

lib/interfaces/IScopeFindOptions.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {WhereOptions, LoggingOptions, SearchPathOptions, col, and, or, FindOptionsAttributesArray,
22
literal} from 'sequelize';
3-
import {Model} from "../models/Model";
43
import {IScopeIncludeOptions} from "./IScopeIncludeOptions";
4+
import {ModelClassGetter} from "../types/ModelClassGetter";
55

66
/* tslint:disable:array-type */
77
/* tslint:disable:max-line-length */
@@ -33,16 +33,16 @@ export interface IScopeFindOptions extends LoggingOptions, SearchPathOptions {
3333
* If your association are set up with an `as` (eg. `X.hasMany(Y, { as: 'Z }`, you need to specify Z in
3434
* the as attribute when eager loading Y).
3535
*/
36-
include?: Array<(() => typeof Model) | IScopeIncludeOptions>;
36+
include?: Array<ModelClassGetter | IScopeIncludeOptions>;
3737

3838
/**
3939
* Specifies an ordering. If a string is provided, it will be escaped. Using an array, you can provide
4040
* several columns / functions to order by. Each element can be further wrapped in a two-element array. The
4141
* first element is the column / function to order by, the second is the direction. For example:
4242
* `order: [['name', 'DESC']]`. In this way the column will be escaped, but the direction will not.
4343
*/
44-
order?: string | col | literal | Array<string | number | (() => typeof Model) | { model: (() => typeof Model), as?: string }> |
45-
Array<string | col | literal | Array<string | number | (() => typeof Model) | { model: (() => typeof Model), as?: string }>>;
44+
order?: string | col | literal | Array<string | number | ModelClassGetter | { model: ModelClassGetter, as?: string }> |
45+
Array<string | col | literal | Array<string | number | ModelClassGetter | { model: ModelClassGetter, as?: string }>>;
4646

4747
/**
4848
* Limit the results
@@ -59,7 +59,7 @@ export interface IScopeFindOptions extends LoggingOptions, SearchPathOptions {
5959
* Postgres also supports transaction.LOCK.KEY_SHARE, transaction.LOCK.NO_KEY_UPDATE and specific model
6060
* locks with joins. See [transaction.LOCK for an example](transaction#lock)
6161
*/
62-
lock?: string | { level: string, of: (() => typeof Model) };
62+
lock?: string | { level: string, of: ModelClassGetter };
6363

6464
/**
6565
* Return raw result. See sequelize.query for more information.
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import {Model} from "../models/Model";
2-
1+
import {ModelClassGetter} from "../types/ModelClassGetter";
32
/**
43
* Association Object for Include Options
54
*/
65
export interface IScopeIncludeAssociation {
7-
source: (() => typeof Model);
8-
target: (() => typeof Model);
6+
source: ModelClassGetter;
7+
target: ModelClassGetter;
98
identifier: string;
109
}

lib/interfaces/IScopeIncludeOptions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {WhereOptions, IncludeThroughOptions} from 'sequelize';
2-
import {Model} from "../models/Model";
32
import {IScopeIncludeAssociation} from "./IScopeIncludeAssociation";
3+
import {ModelClassGetter} from "../types/ModelClassGetter";
44

55
/**
66
* Complex include options
@@ -10,7 +10,7 @@ export interface IScopeIncludeOptions {
1010
/**
1111
* The model you want to eagerly load
1212
*/
13-
model?: (() => typeof Model);
13+
model?: ModelClassGetter;
1414

1515
/**
1616
* The alias of the relation, in case the model you want to eagerly load is aliassed. For `hasOne` /
@@ -48,6 +48,6 @@ export interface IScopeIncludeOptions {
4848
/**
4949
* Load further nested related models
5050
*/
51-
include?: Array<(() => typeof Model) | IScopeIncludeOptions>;
51+
include?: Array<ModelClassGetter | IScopeIncludeOptions>;
5252

5353
}

0 commit comments

Comments
 (0)