1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getScopeOptions = exports.addScopeOptions = exports.resolvesDeprecatedScopes = exports.setScopeOptionsGetters = exports.getScopeOptionsGetters = exports.addScopeOptionsGetter = exports.resolveScope = exports.resolveScopes = void 0;
|
4 | const object_1 = require("../shared/object");
|
5 | const model_service_1 = require("../model/shared/model-service");
|
6 | const alias_inference_service_1 = require("../associations/alias-inference/alias-inference-service");
|
7 | const SCOPES_KEY = 'sequelize:scopes';
|
8 | const SCOPES_OPTIONS_KEY = 'sequelize:scopes-options';
|
9 |
|
10 |
|
11 |
|
12 | function resolveScopes(models) {
|
13 | models.forEach((model) => {
|
14 | (0, exports.resolvesDeprecatedScopes)(model);
|
15 | const { getDefaultScope, getScopes } = (0, exports.getScopeOptionsGetters)(model.prototype);
|
16 | let options = {};
|
17 | if (getDefaultScope) {
|
18 | options = Object.assign(Object.assign({}, options), { defaultScope: getDefaultScope() });
|
19 | }
|
20 | if (getScopes) {
|
21 | options = Object.assign(Object.assign({}, options), getScopes());
|
22 | }
|
23 | Object.keys(options).forEach((key) => (0, exports.resolveScope)(key, model, options[key]));
|
24 | });
|
25 | }
|
26 | exports.resolveScopes = resolveScopes;
|
27 | const resolveScope = (scopeName, model, options) => {
|
28 | if (typeof options === 'function') {
|
29 | const fn = options;
|
30 | options = (...args) => (0, alias_inference_service_1.inferAlias)(fn(...args), model);
|
31 | }
|
32 | else {
|
33 | options = (0, alias_inference_service_1.inferAlias)(options, model);
|
34 | }
|
35 | model.addScope(scopeName, options, { override: true });
|
36 | };
|
37 | exports.resolveScope = resolveScope;
|
38 | const addScopeOptionsGetter = (target, options) => {
|
39 | const currentOptions = (0, exports.getScopeOptionsGetters)(target) || {};
|
40 | (0, exports.setScopeOptionsGetters)(target, Object.assign(Object.assign({}, currentOptions), options));
|
41 | };
|
42 | exports.addScopeOptionsGetter = addScopeOptionsGetter;
|
43 | const getScopeOptionsGetters = (target) => {
|
44 | const options = Reflect.getMetadata(SCOPES_OPTIONS_KEY, target);
|
45 | if (options) {
|
46 | return Object.assign({}, options);
|
47 | }
|
48 | return {};
|
49 | };
|
50 | exports.getScopeOptionsGetters = getScopeOptionsGetters;
|
51 | const setScopeOptionsGetters = (target, options) => {
|
52 | Reflect.defineMetadata(SCOPES_OPTIONS_KEY, options, target);
|
53 | };
|
54 | exports.setScopeOptionsGetters = setScopeOptionsGetters;
|
55 |
|
56 |
|
57 |
|
58 | const resolvesDeprecatedScopes = (model) => {
|
59 | const options = getScopeOptions(model.prototype) || {};
|
60 | Object.keys(options).forEach((key) => resolveDeprecatedScope(key, model, options[key]));
|
61 | };
|
62 | exports.resolvesDeprecatedScopes = resolvesDeprecatedScopes;
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | function addScopeOptions(target, options) {
|
68 | const _options = getScopeOptions(target) || {};
|
69 | setScopeOptions(target, (0, object_1.deepAssign)({}, _options, options));
|
70 | }
|
71 | exports.addScopeOptions = addScopeOptions;
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | function getScopeOptions(target) {
|
77 | const options = Reflect.getMetadata(SCOPES_KEY, target);
|
78 | if (options) {
|
79 | return (0, object_1.deepAssign)({}, options);
|
80 | }
|
81 | }
|
82 | exports.getScopeOptions = getScopeOptions;
|
83 |
|
84 |
|
85 |
|
86 | function resolveDeprecatedScope(scopeName, model, options) {
|
87 | if (typeof options === 'function') {
|
88 | const fn = options;
|
89 | options = (...args) => (0, alias_inference_service_1.inferAlias)(fn(...args), model);
|
90 | }
|
91 | else {
|
92 | options = (0, alias_inference_service_1.inferAlias)((0, model_service_1.resolveModelGetter)(options), model);
|
93 | }
|
94 | model.addScope(scopeName, options, { override: true });
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | function setScopeOptions(target, options) {
|
101 | Reflect.defineMetadata(SCOPES_KEY, options, target);
|
102 | }
|
103 |
|
\ | No newline at end of file |