Skip to content

Commit 505b409

Browse files
SimonSchickeseliger
authored andcommitted
fix(*): use extends for keyof to fix models, add rejectOnEmpty add find options for associations, fix multi scopes (#129)
1 parent 6149b2d commit 505b409

File tree

15 files changed

+58
-59
lines changed

15 files changed

+58
-59
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ install:
99
- typings install
1010

1111
script:
12+
- npm run lint
1213
- npm run build
1314
- npm test
1415

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
21
export * from './lib/sequelize';
2+
export * from './lib/query-interface';
3+
export * from './lib/data-types'

lib/associations/belongs-to-many.d.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,11 @@ export class BelongsToMany extends Association {
8686
* The options for the getAssociations mixin of the belongsToMany association.
8787
* @see BelongsToManyGetAssociationsMixin
8888
*/
89-
export interface BelongsToManyGetAssociationsMixinOptions {
90-
91-
/**
92-
* An optional where clause to limit the associated models.
93-
*/
94-
where?: WhereOptions;
95-
89+
export interface BelongsToManyGetAssociationsMixinOptions extends FindOptions {
9690
/**
9791
* Apply a scope on the related model, or remove its default scope by passing false.
9892
*/
9993
scope?: string | boolean;
100-
101-
/**
102-
* Transaction to run query under
103-
*/
104-
transaction?: Transaction;
10594
}
10695

10796
/**

lib/associations/belongs-to.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
import {Association, SingleAssociationAccessors} from './base';
3-
import {Model, SaveOptions, CreateOptions} from '../model';
2+
import {Association, SingleAssociationAccessors, } from './base';
3+
import {Model, SaveOptions, CreateOptions, FindOptions } from '../model';
44
import {DataType} from '../data-types';
55
import {AssociationOptions} from './base';
66
import {Promise} from '../promise';
@@ -35,11 +35,11 @@ export class BelongsTo extends Association {
3535
* The options for the getAssociation mixin of the belongsTo association.
3636
* @see BelongsToGetAssociationMixin
3737
*/
38-
export interface BelongsToGetAssociationMixinOptions {
38+
export interface BelongsToGetAssociationMixinOptions extends FindOptions {
3939
/**
4040
* Apply a scope on the related model, or remove its default scope by passing false.
4141
*/
42-
scope?: string | boolean;
42+
scope?: string | string[] | boolean;
4343
}
4444

4545
/**

lib/associations/has-many.d.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,11 @@ export class HasMany extends Association {
2626
* The options for the getAssociations mixin of the hasMany association.
2727
* @see HasManyGetAssociationsMixin
2828
*/
29-
export interface HasManyGetAssociationsMixinOptions {
30-
31-
/**
32-
* An optional where clause to limit the associated models.
33-
*/
34-
where?: WhereOptions;
35-
29+
export interface HasManyGetAssociationsMixinOptions extends FindOptions {
3630
/**
3731
* Apply a scope on the related model, or remove its default scope by passing false.
3832
*/
39-
scope?: string | boolean;
40-
41-
/**
42-
* Transaction to run query under
43-
*/
44-
transaction?: Transaction;
33+
scope?: string | string[] | boolean;
4534
}
4635

4736
/**

lib/associations/has-one.d.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import {Association, AssociationOptions, SingleAssociationAccessors} from './base';
3-
import {Model, SaveOptions, CreateOptions} from '../model';
3+
import {Model, SaveOptions, CreateOptions, FindOptions} from '../model';
44
import {DataType} from '../data-types';
55
import {Promise} from '../promise';
66

@@ -20,16 +20,15 @@ export class HasOne extends Association {
2020
constructor(source: typeof Model, target: typeof Model, options: HasOneOptions);
2121
}
2222

23-
2423
/**
2524
* The options for the getAssociation mixin of the hasOne association.
2625
* @see HasOneGetAssociationMixin
2726
*/
28-
export interface HasOneGetAssociationMixinOptions {
27+
export interface HasOneGetAssociationMixinOptions extends FindOptions {
2928
/**
3029
* Apply a scope on the related model, or remove its default scope by passing false.
3130
*/
32-
scope?: string | boolean;
31+
scope?: string | string[] | boolean;
3332
}
3433

3534
/**

lib/model.d.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import {Promise} from './promise';
32
import {Col, Fn, Literal, Where} from './utils';
43
import {SyncOptions} from './sequelize';
@@ -272,7 +271,7 @@ export interface IncludeThroughOptions {
272271
/**
273272
* Options for eager-loading associated models, also allowing for all associations to be loaded at once
274273
*/
275-
export type Includeable = Model | Association | IncludeOptions | { all: true };
274+
export type Includeable = typeof Model | Association | IncludeOptions | { all: true };
276275

277276
/**
278277
* Complex include options
@@ -449,6 +448,10 @@ export interface FindOptions {
449448
*/
450449
subQuery?: boolean;
451450

451+
/**
452+
* Throw if nothing was found.
453+
*/
454+
rejectOnEmpty?: boolean;
452455
}
453456

454457
/**
@@ -1839,15 +1842,19 @@ export abstract class Model {
18391842
* Search for a single instance by its primary key. This applies LIMIT 1, so the listener will
18401843
* always be called with a single instance.
18411844
*/
1842-
static findById<M extends Model>(this: {new (): M} & typeof Model, identifier?: number | string, options?: FindOptions): Promise<M | null>;
1843-
static findByPrimary<M extends Model>(this: {new (): M} & typeof Model, identifier?: number | string, options?: FindOptions): Promise<M | null>;
1845+
static findById<M extends Model>(this: {new (): M} & typeof Model, identifier?: number | string, options?: FindOptions & { rejectOnEmpty?: false }): Promise<M | null>;
1846+
static findById<M extends Model>(this: {new (): M} & typeof Model, identifier: number | string, options: FindOptions & { rejectOnEmpty: true }): Promise<M>;
1847+
static findByPrimary<M extends Model>(this: {new (): M} & typeof Model, identifier?: number | string, options?: FindOptions & { rejectOnEmpty?: false }): Promise<M | null>;
1848+
static findByPrimary<M extends Model>(this: {new (): M} & typeof Model, identifier: number | string, options: FindOptions & { rejectOnEmpty: true }): Promise<M>;
18441849

18451850
/**
18461851
* Search for a single instance. This applies LIMIT 1, so the listener will always be called with a single
18471852
* instance.
18481853
*/
1849-
static findOne<M extends Model>(this: {new (): M} & typeof Model, options?: FindOptions): Promise<M | null>;
1850-
static find<M extends Model>(this: {new (): M} & typeof Model, options?: FindOptions): Promise<M | null>;
1854+
static findOne<M extends Model>(this: {new (): M} & typeof Model, options?: FindOptions & { rejectOnEmpty?: false }): Promise<M | null>;
1855+
static findOne<M extends Model>(this: {new (): M} & typeof Model, options: FindOptions & { rejectOnEmpty: true }): Promise<M>;
1856+
static find<M extends Model>(this: {new (): M} & typeof Model, options?: FindOptions & { rejectOnEmpty?: false }): Promise<M | null>;
1857+
static find<M extends Model>(this: {new (): M} & typeof Model, options: FindOptions & { rejectOnEmpty: true }): Promise<M>;
18511858

18521859
/**
18531860
* Run an aggregation method on the specified field
@@ -1858,7 +1865,7 @@ export abstract class Model {
18581865
* @return Returns the aggregate result cast to `options.dataType`, unless `options.plain` is false, in
18591866
* which case the complete data result is returned.
18601867
*/
1861-
aggregate(field: keyof this, aggregateFunction: string, options?: AggregateOptions): Promise<any>;
1868+
aggregate<K extends keyof this>(field: K, aggregateFunction: string, options?: AggregateOptions): Promise<any>;
18621869
static aggregate<M extends Model>(this: {new (): M} & typeof Model, field: keyof M, aggregateFunction: string, options?: AggregateOptions): Promise<any>;
18631870

18641871
/**
@@ -2517,8 +2524,8 @@ export abstract class Model {
25172524
*
25182525
* If changed is called without an argument and no keys have changed, it will return `false`.
25192526
*/
2520-
changed(key: keyof this): boolean;
2521-
changed(key: keyof this, dirty: boolean): void;
2527+
changed<K extends keyof this>(key: K): boolean;
2528+
changed<K extends keyof this>(key: K, dirty: boolean): void;
25222529
changed(): false | string[];
25232530

25242531
/**
@@ -2592,7 +2599,7 @@ export abstract class Model {
25922599
* If an array is provided, the same is true for each column.
25932600
* If and object is provided, each column is incremented by the value given.
25942601
*/
2595-
increment(fields: keyof this | (keyof this)[] | Partial<this>, options?: IncrementDecrementOptions): Promise<this>;
2602+
increment<K extends keyof this>(fields: K | K[] | Partial<this>, options?: IncrementDecrementOptions): Promise<this>;
25962603

25972604
/**
25982605
* Decrement the value of one or more columns. This is done in the database, which means it does not use
@@ -2614,7 +2621,7 @@ export abstract class Model {
26142621
* If an array is provided, the same is true for each column.
26152622
* If and object is provided, each column is decremented by the value given
26162623
*/
2617-
decrement(fields: keyof this | (keyof this)[] | Partial<this>, options?: IncrementDecrementOptions): Promise<this>;
2624+
decrement<K extends keyof this>(fields: K | K[] | Partial<this>, options?: IncrementDecrementOptions): Promise<this>;
26182625

26192626
/**
26202627
* Check whether all values of this and `other` Instance are the same

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@
99
"@types/validator": "types/validator#83990944fcc53bcb4b05b347c3cdfcb7aa05b3c7"
1010
},
1111
"peerDependencies": {
12-
"typescript": ">=2.0.0 <2.4.0"
12+
"typescript": ">=2.0.0"
1313
},
1414
"devDependencies": {
1515
"tslint": "^5.4.3",
1616
"typedoc": "^0.7.1",
17-
"typescript": "~2.3.4",
17+
"typescript": "~2.5.2",
1818
"typings": "^2.1.1"
1919
},
2020
"scripts": {
2121
"build": "tsc -p . && typings bundle -o dist/index.d.ts",
2222
"test": "tsc -p test",
23-
"typedoc": "typedoc --module es6 --target es6 --includeDeclarations --excludeExternals --readme docs_index.md --name \"TypeScript definitions for Sequelize\" --mode modules --out typedoc lib/sequelize.d.ts lib/model.d.ts lib/transaction.d.ts lib/errors.d.ts lib/data-types.d.ts lib/query-interface.d.ts lib/deferrable.d.ts lib/associations/base.d.ts lib/associations/belongs-to-many.d.ts lib/associations/belongs-to.d.ts lib/associations/has-many.d.ts lib/associations/has-one.d.ts typings/index.d.ts"
23+
"typedoc": "typedoc --module es6 --target es6 --includeDeclarations --excludeExternals --readme docs_index.md --name \"TypeScript definitions for Sequelize\" --mode modules --out typedoc lib/sequelize.d.ts lib/model.d.ts lib/transaction.d.ts lib/errors.d.ts lib/data-types.d.ts lib/query-interface.d.ts lib/deferrable.d.ts lib/associations/base.d.ts lib/associations/belongs-to-many.d.ts lib/associations/belongs-to.d.ts lib/associations/has-many.d.ts lib/associations/has-one.d.ts typings/index.d.ts",
24+
"lint": "tslint -p tsconfig.json && tslint -p test/tsconfig.json"
2425
},
2526
"repository": {
2627
"type": "git",

test/errors.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ async function test() {
1212
}
1313
}
1414
}
15-

test/include.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ MyModel.findAll({
1313
separate: true,
1414
order: [['id', 'DESC']]
1515
}]
16-
})
16+
});
1717

1818
MyModel.findAll({
1919
include: [{ all: true }]
20-
})
20+
});

0 commit comments

Comments
 (0)