Skip to content

Commit dbd2f18

Browse files
scope services separated; test added for scope functions
1 parent 74bde9f commit dbd2f18

File tree

7 files changed

+83
-61
lines changed

7 files changed

+83
-61
lines changed

lib/annotations/DefaultScope.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'reflect-metadata';
2-
import {addScopeOptions} from "../services/models";
2+
import {addScopeOptions} from "../services/scopes";
33
import {IScopeFindOptions} from "../interfaces/IScopeFindOptions";
44

55
/**

lib/annotations/Scopes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'reflect-metadata';
2-
import {addScopeOptions} from "../services/models";
2+
import {addScopeOptions} from "../services/scopes";
33
import {IDefineScopeOptions} from "../interfaces/IDefineScopeOptions";
44

55
/**

lib/models/BaseSequelize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Model} from "./Model";
22
import {getModels} from "../services/models";
33
import {getAssociations, processAssociation} from "../services/association";
44
import {ISequelizeConfig} from "../interfaces/ISequelizeConfig";
5-
import {resolveScopes} from "../services/models";
5+
import {resolveScopes} from "../services/scopes";
66
import {ISequelizeValidationOnlyConfig} from "../interfaces/ISequelizeValidationOnlyConfig";
77
import {extend} from "../utils/object";
88
import {ISequelizeAssociation} from "../interfaces/ISequelizeAssociation";

lib/services/models.ts

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@ import {Model} from "../models/Model";
66
import {IPartialDefineAttributeColumnOptions} from "../interfaces/IPartialDefineAttributeColumnOptions";
77
import {inferDataType} from "../utils/data-type";
88
import {deepAssign} from "../utils/object";
9-
import {IScopeOptions} from "../interfaces/IScopeOptions";
10-
import {IFindOptions} from "../interfaces/IFindOptions";
119
import {getAssociationsByRelation} from "./association";
1210
import {uniqueFilter} from "../utils/array";
13-
import {IScopeFindOptions} from "../interfaces/IScopeFindOptions";
1411

1512
const MODEL_NAME_KEY = 'sequelize:modelName';
16-
const SCOPES_KEY = 'sequelize:scopes';
1713
const ATTRIBUTES_KEY = 'sequelize:attributes';
1814
const OPTIONS_KEY = 'sequelize:options';
1915
const DEFAULT_OPTIONS: DefineOptions<any> = {
@@ -210,21 +206,6 @@ export function getModels(arg: Array<typeof Model | string>): Array<typeof Model
210206
return arg as Array<typeof Model>;
211207
}
212208

213-
/**
214-
* Resolves scopes and adds them to the specified models
215-
*/
216-
export function resolveScopes(models: Array<typeof Model>): void {
217-
models.forEach(model => {
218-
const options = getScopeOptions(model.prototype);
219-
220-
if (options) {
221-
Object
222-
.keys(options)
223-
.forEach(key => resolveScope(key, model, options[key]));
224-
}
225-
});
226-
}
227-
228209
/**
229210
* Resolves all model getters of specified options object
230211
* recursively.
@@ -253,26 +234,6 @@ export function resolveModelGetter(options: any): void {
253234
});
254235
}
255236

256-
/**
257-
* Adds scope option meta data for specified prototype
258-
*/
259-
export function addScopeOptions(target: any, options: IScopeOptions): void {
260-
const _options = getScopeOptions(target) || {};
261-
262-
setScopeOptions(target, deepAssign({}, _options, options));
263-
}
264-
265-
/**
266-
* Returns scope option meta data from specified target
267-
*/
268-
export function getScopeOptions(target: any): IScopeOptions | undefined {
269-
const options = Reflect.getMetadata(SCOPES_KEY, target);
270-
271-
if (options) {
272-
return deepAssign({}, options);
273-
}
274-
}
275-
276237
/**
277238
* Pre conform includes, so that "as" value can be inferred from source
278239
*/
@@ -301,15 +262,6 @@ export function inferAlias(options: any, source: any): any {
301262
return options;
302263
}
303264

304-
/**
305-
* Resolves scope
306-
*/
307-
function resolveScope(scopeName: string, model: typeof Model, options: IScopeFindOptions | undefined): void {
308-
resolveModelGetter(options);
309-
options = inferAlias(options, model);
310-
model.addScope(scopeName, options as IFindOptions, {override: true});
311-
}
312-
313265
/**
314266
* Pre conform include, so that alias ("as") value can be inferred from source class
315267
*/
@@ -344,14 +296,6 @@ function inferAliasForInclude(include: any, source: any): any {
344296
return include;
345297
}
346298

347-
/**
348-
* Set scope option meta data for specified prototype
349-
*/
350-
function setScopeOptions(target: any, options: IScopeOptions): void {
351-
352-
Reflect.defineMetadata(SCOPES_KEY, options, target);
353-
}
354-
355299
/**
356300
* Checks if specified filename is importable or not;
357301
* Which means that, it needs to have a specific file extension

lib/services/scopes.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import 'reflect-metadata';
2+
import {Model} from "../models/Model";
3+
import {deepAssign} from "../utils/object";
4+
import {IScopeOptions} from "../interfaces/IScopeOptions";
5+
import {IFindOptions} from "../interfaces/IFindOptions";
6+
import {IScopeFindOptions} from "../interfaces/IScopeFindOptions";
7+
import {inferAlias, resolveModelGetter} from './models';
8+
9+
const SCOPES_KEY = 'sequelize:scopes';
10+
11+
/**
12+
* Resolves scopes and adds them to the specified models
13+
*/
14+
export function resolveScopes(models: Array<typeof Model>): void {
15+
models.forEach(model => {
16+
const options = getScopeOptions(model.prototype);
17+
18+
if (options) {
19+
Object
20+
.keys(options)
21+
.forEach(key => resolveScope(key, model, options[key]));
22+
}
23+
});
24+
}
25+
26+
/**
27+
* Adds scope option meta data for specified prototype
28+
*/
29+
export function addScopeOptions(target: any, options: IScopeOptions): void {
30+
const _options = getScopeOptions(target) || {};
31+
32+
setScopeOptions(target, deepAssign({}, _options, options));
33+
}
34+
35+
/**
36+
* Returns scope option meta data from specified target
37+
*/
38+
export function getScopeOptions(target: any): IScopeOptions | undefined {
39+
const options = Reflect.getMetadata(SCOPES_KEY, target);
40+
41+
if (options) {
42+
return deepAssign({}, options);
43+
}
44+
}
45+
46+
/**
47+
* Resolves scope
48+
*/
49+
function resolveScope(scopeName: string, model: typeof Model, options: IScopeFindOptions | undefined): void {
50+
resolveModelGetter(options);
51+
options = inferAlias(options, model);
52+
model.addScope(scopeName, options as IFindOptions, {override: true});
53+
}
54+
55+
/**
56+
* Set scope option meta data for specified prototype
57+
*/
58+
function setScopeOptions(target: any, options: IScopeOptions): void {
59+
60+
Reflect.defineMetadata(SCOPES_KEY, options, target);
61+
}

test/models/ShoeWithScopes.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export const SHOE_SCOPES = {
1818
},
1919
manufacturerWithScope: {
2020
include: [() => Manufacturer.scope('brandOnly')]
21-
}
21+
},
22+
primaryColor: primaryColor => ({
23+
where: {primaryColor}
24+
}
25+
)
2226
};
2327

2428
@DefaultScope(SHOE_DEFAULT_SCOPE)

test/specs/scopes.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {expect, use} from 'chai';
22
import * as chaiAsPromised from 'chai-as-promised';
33
import {createSequelize} from "../utils/sequelize";
4-
import {getScopeOptions} from "../../lib/services/models";
4+
import {getScopeOptions} from "../../lib/services/scopes";
55
import {ShoeWithScopes, SHOE_DEFAULT_SCOPE, SHOE_SCOPES} from "../models/ShoeWithScopes";
66
import {Manufacturer} from "../models/Manufacturer";
77
import {Person} from "../models/Person";
@@ -198,6 +198,19 @@ describe('scopes', () => {
198198

199199
});
200200

201+
// describe('with scope function', () => {
202+
//
203+
// it('should consider nested scope', () =>
204+
// ShoeWithScopes
205+
// .scope({method: ['primaryColor', 'yellow']})
206+
// .findOne()
207+
// .then(shoe => {
208+
// expect(shoe).to.have.property('primaryColor', 'yellow');
209+
// })
210+
// );
211+
//
212+
// });
213+
201214
});
202215

203216
});

0 commit comments

Comments
 (0)