Skip to content

Commit a2715c4

Browse files
Merge pull request #191 from schmod/fix-build
fix build errors
2 parents 707d356 + 0463a38 commit a2715c4

File tree

8 files changed

+38
-49
lines changed

8 files changed

+38
-49
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,14 @@ export class ShoeWithScopes extends Model<ShoeWithScopes> {
553553
}
554554
```
555555

556+
Note that `Model.scope()` and `Model.unscoped()` are **not** typesafe. Their return values must be explicily cast back to the correct Model type:
557+
558+
```typescript
559+
(ShoeWithScopes.scope('full') as typeof ShoeWithScopes)
560+
.findAll(args)
561+
.then(shoes => '...');
562+
```
563+
556564
## Hooks
557565
Hooks can be attached to your models. All Model-level hooks are supported. See [the related unit tests](test/models/Hook.ts) for a summary.
558566

lib/models/Model.d.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export declare abstract class Model<T> extends Hooks {
130130
* @return Model A reference to the model, with the scope(s) applied. Calling scope again on the returned
131131
* model will clear the previous scope.
132132
*/
133-
static scope<T extends Model<T>>(this: (new () => T), options?: string | string[] | ScopeOptions | WhereOptions<any>): typeof T;
133+
static scope(options?: string | string[] | ScopeOptions | WhereOptions<any>): typeof Model;
134134

135135
/**
136136
* Search for multiple instances.
@@ -291,33 +291,28 @@ export declare abstract class Model<T> extends Hooks {
291291
*/
292292
static build<T extends Model<T>>(this: (new () => T), record?: any, options?: IBuildOptions): T;
293293
static build<T extends Model<T>, A>(this: (new () => T), record?: A, options?: IBuildOptions): T;
294-
static build<A>(this: (new () => T), record?: A, options?: IBuildOptions): T;
295294

296295
/**
297296
* Undocumented bulkBuild
298297
*/
299298
static bulkBuild<T extends Model<T>>(this: (new () => T), records: any[], options?: IBuildOptions): T[];
300299
static bulkBuild<T extends Model<T>, A>(this: (new () => T), records: A[], options?: IBuildOptions): T[];
301-
static bulkBuild<A>(this: (new () => T), records: A[], options?: IBuildOptions): T[];
302300

303301
/**
304302
* Builds a new model instance and calls save on it.
305303
*/
306304
static create<T extends Model<T>>(this: (new () => T), values?: any, options?: ICreateOptions): Promise<T>;
307305
static create<T extends Model<T>, A>(this: (new () => T), values?: A, options?: ICreateOptions): Promise<T>;
308-
static create<A>(this: (new () => T), values?: A, options?: ICreateOptions): Promise<T>;
309306

310307
/**
311308
* Find a row that matches the query, or build (but don't save) the row if none is found.
312309
* The successfull result of the promise will be (instance, initialized) - Make sure to use .spread()
313310
*/
314311
static findOrInitialize<T extends Model<T>>(this: (new () => T), options: IFindOrInitializeOptions<any>): Promise<[T, boolean]>;
315312
static findOrInitialize<T extends Model<T>, A>(this: (new () => T), options: IFindOrInitializeOptions<A>): Promise<[T, boolean]>;
316-
static findOrInitialize<A>(this: (new () => T), options: IFindOrInitializeOptions<A>): Promise<[T, boolean]>;
317313

318314
static findOrBuild<T extends Model<T>>(this: (new () => T), options: IFindOrInitializeOptions<any>): Promise<[T, boolean]>;
319315
static findOrBuild<T extends Model<T>, A>(this: (new () => T), options: IFindOrInitializeOptions<A>): Promise<[T, boolean]>;
320-
static findOrBuild<A>(this: (new () => T), options: IFindOrInitializeOptions<A>): Promise<[T, boolean]>;
321316

322317
/**
323318
* Find a row that matches the query, or build and save the row if none is found
@@ -332,15 +327,13 @@ export declare abstract class Model<T> extends Hooks {
332327
*/
333328
static findOrCreate<T extends Model<T>>(this: (new () => T), options: IFindOrInitializeOptions<any>): Promise<[T, boolean]>;
334329
static findOrCreate<T extends Model<T>, A>(this: (new () => T), options: IFindOrInitializeOptions<A>): Promise<[T, boolean]>;
335-
static findOrCreate<A>(this: (new () => T), options: IFindOrInitializeOptions<A>): Promise<[T, boolean]>;
336330

337331
/**
338332
* A more performant findOrCreate that will not work under a transaction (at least not in postgres)
339333
* Will execute a find call, if empty then attempt to create, if unique constraint then attempt to find again
340334
*/
341335
static findCreateFind<T extends Model<T>>(this: (new () => T), options: IFindCreateFindOptions<any>): Promise<[T, boolean]>;
342336
static findCreateFind<T extends Model<T>, A>(this: (new () => T), options: IFindCreateFindOptions<A>): Promise<[T, boolean]>;
343-
static findCreateFind<A>(this: (new () => T), options: IFindCreateFindOptions<A>): Promise<[T, boolean]>;
344337

345338
/**
346339
* Insert or update a single row. An update will be executed if a row which matches the supplied values on
@@ -378,7 +371,6 @@ export declare abstract class Model<T> extends Hooks {
378371
*/
379372
static bulkCreate<T extends Model<T>>(this: (new () => T), records: any[], options?: BulkCreateOptions): Promise<T[]>;
380373
static bulkCreate<T extends Model<T>, A>(this: (new () => T), records: A[], options?: BulkCreateOptions): Promise<T[]>;
381-
static bulkCreate<A>(this: (new () => T), records: A[], options?: BulkCreateOptions): Promise<T[]>;
382374

383375
/**
384376
* Truncate all instances of the model. This is a convenient method for Model.destroy({ truncate: true }).
@@ -404,8 +396,6 @@ export declare abstract class Model<T> extends Hooks {
404396
*/
405397
static update<T extends Model<T>>(this: (new () => T), values: any, options: UpdateOptions): Promise<[number, Array<T>]>;
406398
static update<T extends Model<T>, A>(this: (new () => T), values: A, options: UpdateOptions): Promise<[number, Array<T>]>;
407-
static update<A>(this: (new () => T), values: A, options: UpdateOptions): Promise<[number, Array<T>]>;
408-
409399
/**
410400
* Run a describe query on the table. The result will be return to the listener as a hash of attributes and
411401
* their types.
@@ -415,7 +405,7 @@ export declare abstract class Model<T> extends Hooks {
415405
/**
416406
* Unscope the model
417407
*/
418-
static unscoped<T extends Model<T>>(this: (new () => T)): typeof T;
408+
static unscoped(): typeof Model;
419409

420410
/**
421411
* A reference to the sequelize instance

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"sqlite3": "3.1.8",
7777
"ts-node": "3.0.4",
7878
"tslint": "4.3.1",
79-
"typescript": "2.5.3",
79+
"typescript": "2.6.1",
8080
"uuid-validate": "0.0.2"
8181
},
8282
"engines": {

test/specs/instance.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ describe('instance', () => {
816816

817817
it('should update the associations after one element deleted', () =>
818818
Team
819-
.create<{name: string; players: Array<{name: string}>}>({
819+
.create({
820820
name: 'the team',
821821
players: [{
822822
name: 'the player1'

test/specs/scopes.spec.ts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,27 @@ describe('scopes', () => {
6060

6161
it('should consider other scopes', () =>
6262

63-
ShoeWithScopes.scope('full').findOne()
63+
(ShoeWithScopes.scope('full') as typeof ShoeWithScopes).findOne()
6464
.then(shoe => {
6565

6666
expect(shoe).to.have.property('manufacturer').which.is.not.null;
6767
expect(shoe).to.have.property('manufacturer').which.have.property('brand', BRAND);
6868
})
69-
.then(() => ShoeWithScopes.scope('yellow').findAll())
69+
.then(() => (ShoeWithScopes.scope('yellow') as typeof ShoeWithScopes).findAll())
7070
.then(yellowShoes => {
7171

7272
expect(yellowShoes).to.be.empty;
7373
})
74-
.then(() => ShoeWithScopes.scope('noImg').findAll())
74+
.then(() => (ShoeWithScopes.scope('noImg') as typeof ShoeWithScopes).findAll())
7575
.then(noImgShoes => {
7676

7777
expect(noImgShoes).to.be.not.empty;
7878
})
7979
);
8080

8181
it('should not consider default scope due to unscoped call', () =>
82-
83-
ShoeWithScopes
84-
.unscoped()
82+
(ShoeWithScopes
83+
.unscoped() as typeof ShoeWithScopes)
8584
.findOne()
8685
.then(shoe => {
8786
expect(shoe).to.have.property('secretKey').which.is.a('string');
@@ -91,8 +90,8 @@ describe('scopes', () => {
9190
describe('with include options', () => {
9291

9392
it('should consider scopes and additional included model (object)', () =>
94-
expect(ShoeWithScopes
95-
.scope('full')
93+
expect(
94+
(ShoeWithScopes.scope('full') as typeof ShoeWithScopes)
9695
.findOne({
9796
include: [{
9897
model: Person,
@@ -107,8 +106,8 @@ describe('scopes', () => {
107106
);
108107

109108
it('should consider scopes and additional included model (model)', () =>
110-
expect(ShoeWithScopes
111-
.scope('full')
109+
expect(
110+
(ShoeWithScopes.scope('full') as typeof ShoeWithScopes)
112111
.findOne({
113112
include: [Person]
114113
})
@@ -122,8 +121,7 @@ describe('scopes', () => {
122121

123122
it('should not consider default scope due to unscoped call, but additonal includes (object)', () =>
124123

125-
ShoeWithScopes
126-
.unscoped()
124+
(ShoeWithScopes.unscoped() as typeof ShoeWithScopes)
127125
.findOne({
128126
include: [{model: Person}]
129127
})
@@ -135,8 +133,8 @@ describe('scopes', () => {
135133

136134
it('should not consider default scope due to unscoped call, but additonal includes (model)', () =>
137135

138-
ShoeWithScopes
139-
.unscoped()
136+
(ShoeWithScopes
137+
.unscoped() as typeof ShoeWithScopes)
140138
.findOne({
141139
include: [Person]
142140
})
@@ -161,10 +159,9 @@ describe('scopes', () => {
161159
);
162160

163161
it('should consider scope of included model (with own scope)', () =>
164-
ShoeWithScopes
165-
.scope('red')
162+
(ShoeWithScopes.scope('red') as typeof ShoeWithScopes)
166163
.findOne({
167-
include: [Manufacturer.scope('brandOnly')]
164+
include: [Manufacturer.scope('brandOnly') as typeof Manufacturer]
168165
})
169166
.then(shoe => {
170167
expect(shoe).to.have.property('manufacturer')
@@ -180,8 +177,7 @@ describe('scopes', () => {
180177
describe('with nested scope', () => {
181178

182179
it('should consider nested scope', () =>
183-
ShoeWithScopes
184-
.scope('manufacturerWithScope')
180+
(ShoeWithScopes.scope('manufacturerWithScope') as typeof ShoeWithScopes)
185181
.findOne()
186182
.then(shoe => {
187183
expect(shoe).to.have.property('manufacturer')
@@ -191,8 +187,7 @@ describe('scopes', () => {
191187
);
192188

193189
it('should not consider nested scope', () =>
194-
ShoeWithScopes
195-
.scope('full')
190+
(ShoeWithScopes.scope('full') as typeof ShoeWithScopes)
196191
.findOne()
197192
.then(shoe => {
198193
expect(shoe).to.have.property('manufacturer')
@@ -206,17 +201,15 @@ describe('scopes', () => {
206201
describe('with scope function', () => {
207202

208203
it('should find appropriate shoe due to correctly passed scope function param', () =>
209-
ShoeWithScopes
210-
.scope({method: ['primaryColor', 'red']})
204+
(ShoeWithScopes.scope({method: ['primaryColor', 'red']}) as typeof ShoeWithScopes)
211205
.findOne()
212206
.then(shoe => {
213207
expect(shoe).to.have.property('primaryColor', 'red');
214208
})
215209
);
216210

217211
it('should find appropriate shoe due to correctly passed scope function param including associated model', () =>
218-
ShoeWithScopes
219-
.scope({method: ['primaryColorWithManufacturer', 'red']})
212+
(ShoeWithScopes.scope({method: ['primaryColorWithManufacturer', 'red']}) as typeof ShoeWithScopes)
220213
.findOne()
221214
.then(shoe => {
222215
expect(shoe).to.have.property('primaryColor', 'red');

test/tsconfig.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
"emitDecoratorMetadata": true,
77
"sourceMap": true,
88
"strictNullChecks": false,
9-
"noUnusedLocals": true,
9+
"noUnusedLocals": false,
1010
"pretty": true,
11-
"skipLibCheck": true,
12-
"lib": ["es2015"]
11+
"lib": ["es2017"]
1312
},
1413
"include": [
1514
"./**/*"
@@ -19,4 +18,4 @@
1918
"../lib",
2019
"../index.ts"
2120
]
22-
}
21+
}

tsconfig.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"strictNullChecks": true,
1010
"noUnusedLocals": true,
1111
"pretty": true,
12-
"skipLibCheck": true,
13-
"lib": ["es2015"]
12+
"lib": ["es2017"]
1413
},
1514
"include": [
1615
"lib/**/*"
@@ -24,4 +23,4 @@
2423
"lib/models/v4/Model.ts",
2524
"test"
2625
]
27-
}
26+
}

0 commit comments

Comments
 (0)