Skip to content

Commit 04edce6

Browse files
closes #204
1 parent 67d720a commit 04edce6

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

lib/models/Model.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {ICountOptions} from '../interfaces/ICountOptions';
2121
/* tslint:disable:array-type */
2222
/* tslint:disable:max-line-length */
2323
/* tslint:disable:max-classes-per-file */
24+
type ForTypeOf<T> = {[P in keyof T]: T[P]};
25+
type NonAbstractTypeOfModel<T> = (new () => T) & ForTypeOf<typeof Model>;
2426

2527
export declare abstract class Model<T> extends Hooks {
2628

@@ -132,7 +134,7 @@ export declare abstract class Model<T> extends Hooks {
132134
* @return Model A reference to the model, with the scope(s) applied. Calling scope again on the returned
133135
* model will clear the previous scope.
134136
*/
135-
static scope(options?: string | string[] | ScopeOptions | WhereOptions<any>): typeof Model;
137+
static scope<T>(this: NonAbstractTypeOfModel<T>, options?: string | string[] | ScopeOptions | WhereOptions<any>): NonAbstractTypeOfModel<T>;
136138

137139
/**
138140
* Search for multiple instances.
@@ -407,7 +409,7 @@ export declare abstract class Model<T> extends Hooks {
407409
/**
408410
* Unscope the model
409411
*/
410-
static unscoped(): typeof Model;
412+
static unscoped<T>(this: NonAbstractTypeOfModel<T>): NonAbstractTypeOfModel<T>;
411413

412414
/**
413415
* A reference to the sequelize instance

test/specs/scopes.spec.ts

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

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

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

6666
expect(shoe).to.have.property('manufacturer').which.is.not.null;
@@ -79,8 +79,8 @@ describe('scopes', () => {
7979
);
8080

8181
it('should not consider default scope due to unscoped call', () =>
82-
(ShoeWithScopes
83-
.unscoped() as typeof ShoeWithScopes)
82+
ShoeWithScopes
83+
.unscoped()
8484
.findOne()
8585
.then(shoe => {
8686
expect(shoe).to.have.property('secretKey').which.is.a('string');
@@ -91,37 +91,37 @@ describe('scopes', () => {
9191

9292
it('should consider scopes and additional included model (object)', () =>
9393
expect(
94-
(ShoeWithScopes.scope('full') as typeof ShoeWithScopes)
95-
.findOne({
96-
include: [{
97-
model: Person,
98-
}]
99-
})
100-
.then(shoe => {
101-
expect(shoe).to.have.property('manufacturer').which.is.not.null;
102-
expect(shoe).to.have.property('manufacturer').which.have.property('brand', BRAND);
103-
expect(shoe).to.have.property('owner').which.is.not.null;
104-
})
94+
ShoeWithScopes.scope('full')
95+
.findOne({
96+
include: [{
97+
model: Person,
98+
}]
99+
})
100+
.then(shoe => {
101+
expect(shoe).to.have.property('manufacturer').which.is.not.null;
102+
expect(shoe).to.have.property('manufacturer').which.have.property('brand', BRAND);
103+
expect(shoe).to.have.property('owner').which.is.not.null;
104+
})
105105
).not.to.be.rejected
106106
);
107107

108108
it('should consider scopes and additional included model (model)', () =>
109109
expect(
110-
(ShoeWithScopes.scope('full') as typeof ShoeWithScopes)
111-
.findOne({
112-
include: [Person]
113-
})
114-
.then(shoe => {
115-
expect(shoe).to.have.property('manufacturer').which.is.not.null;
116-
expect(shoe).to.have.property('manufacturer').which.have.property('brand', BRAND);
117-
expect(shoe).to.have.property('owner').which.is.not.null;
118-
})
110+
ShoeWithScopes.scope('full')
111+
.findOne({
112+
include: [Person]
113+
})
114+
.then(shoe => {
115+
expect(shoe).to.have.property('manufacturer').which.is.not.null;
116+
expect(shoe).to.have.property('manufacturer').which.have.property('brand', BRAND);
117+
expect(shoe).to.have.property('owner').which.is.not.null;
118+
})
119119
).not.to.be.rejected
120120
);
121121

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

124-
(ShoeWithScopes.unscoped() as typeof ShoeWithScopes)
124+
ShoeWithScopes.unscoped()
125125
.findOne({
126126
include: [{model: Person}]
127127
})
@@ -133,8 +133,8 @@ describe('scopes', () => {
133133

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

136-
(ShoeWithScopes
137-
.unscoped() as typeof ShoeWithScopes)
136+
ShoeWithScopes
137+
.unscoped()
138138
.findOne({
139139
include: [Person]
140140
})
@@ -159,7 +159,7 @@ describe('scopes', () => {
159159
);
160160

161161
it('should consider scope of included model (with own scope)', () =>
162-
(ShoeWithScopes.scope('red') as typeof ShoeWithScopes)
162+
ShoeWithScopes.scope('red')
163163
.findOne({
164164
include: [Manufacturer.scope('brandOnly') as typeof Manufacturer]
165165
})
@@ -177,7 +177,7 @@ describe('scopes', () => {
177177
describe('with nested scope', () => {
178178

179179
it('should consider nested scope', () =>
180-
(ShoeWithScopes.scope('manufacturerWithScope') as typeof ShoeWithScopes)
180+
ShoeWithScopes.scope('manufacturerWithScope')
181181
.findOne()
182182
.then(shoe => {
183183
expect(shoe).to.have.property('manufacturer')
@@ -187,7 +187,7 @@ describe('scopes', () => {
187187
);
188188

189189
it('should not consider nested scope', () =>
190-
(ShoeWithScopes.scope('full') as typeof ShoeWithScopes)
190+
ShoeWithScopes.scope('full')
191191
.findOne()
192192
.then(shoe => {
193193
expect(shoe).to.have.property('manufacturer')
@@ -201,15 +201,15 @@ describe('scopes', () => {
201201
describe('with scope function', () => {
202202

203203
it('should find appropriate shoe due to correctly passed scope function param', () =>
204-
(ShoeWithScopes.scope({method: ['primaryColor', 'red']}) as typeof ShoeWithScopes)
204+
ShoeWithScopes.scope({method: ['primaryColor', 'red']})
205205
.findOne()
206206
.then(shoe => {
207207
expect(shoe).to.have.property('primaryColor', 'red');
208208
})
209209
);
210210

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

0 commit comments

Comments
 (0)