UNPKG

4.04 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getScopeOptions = exports.addScopeOptions = exports.resolvesDeprecatedScopes = exports.setScopeOptionsGetters = exports.getScopeOptionsGetters = exports.addScopeOptionsGetter = exports.resolveScope = exports.resolveScopes = void 0;
4const object_1 = require("../shared/object");
5const model_service_1 = require("../model/shared/model-service");
6const alias_inference_service_1 = require("../associations/alias-inference/alias-inference-service");
7const SCOPES_KEY = 'sequelize:scopes';
8const SCOPES_OPTIONS_KEY = 'sequelize:scopes-options';
9/**
10 * Resolves scopes and adds them to the specified models
11 */
12function 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}
26exports.resolveScopes = resolveScopes;
27const 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};
37exports.resolveScope = resolveScope;
38const addScopeOptionsGetter = (target, options) => {
39 const currentOptions = (0, exports.getScopeOptionsGetters)(target) || {};
40 (0, exports.setScopeOptionsGetters)(target, Object.assign(Object.assign({}, currentOptions), options));
41};
42exports.addScopeOptionsGetter = addScopeOptionsGetter;
43const getScopeOptionsGetters = (target) => {
44 const options = Reflect.getMetadata(SCOPES_OPTIONS_KEY, target);
45 if (options) {
46 return Object.assign({}, options);
47 }
48 return {};
49};
50exports.getScopeOptionsGetters = getScopeOptionsGetters;
51const setScopeOptionsGetters = (target, options) => {
52 Reflect.defineMetadata(SCOPES_OPTIONS_KEY, options, target);
53};
54exports.setScopeOptionsGetters = setScopeOptionsGetters;
55/**
56 * @deprecated
57 */
58const resolvesDeprecatedScopes = (model) => {
59 const options = getScopeOptions(model.prototype) || {};
60 Object.keys(options).forEach((key) => resolveDeprecatedScope(key, model, options[key]));
61};
62exports.resolvesDeprecatedScopes = resolvesDeprecatedScopes;
63/**
64 * Adds scope option meta data for specified prototype
65 * @deprecated
66 */
67function addScopeOptions(target, options) {
68 const _options = getScopeOptions(target) || {};
69 setScopeOptions(target, (0, object_1.deepAssign)({}, _options, options));
70}
71exports.addScopeOptions = addScopeOptions;
72/**
73 * Returns scope option meta data from specified target
74 * @deprecated
75 */
76function getScopeOptions(target) {
77 const options = Reflect.getMetadata(SCOPES_KEY, target);
78 if (options) {
79 return (0, object_1.deepAssign)({}, options);
80 }
81}
82exports.getScopeOptions = getScopeOptions;
83/**
84 * @deprecated
85 */
86function 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 * Set scope option meta data for specified prototype
98 * @deprecated
99 */
100function setScopeOptions(target, options) {
101 Reflect.defineMetadata(SCOPES_KEY, options, target);
102}
103//# sourceMappingURL=scope-service.js.map
\No newline at end of file