UNPKG

5.04 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.INFER_ALIAS_MAP = exports.Model = void 0;
4const sequelize_1 = require("sequelize");
5const string_1 = require("../../shared/string");
6const alias_inference_service_1 = require("../../associations/alias-inference/alias-inference-service");
7const model_not_initialized_error_1 = require("../shared/model-not-initialized-error");
8const object_1 = require("../../shared/object");
9class Model extends sequelize_1.Model {
10 constructor(values, options) {
11 if (!new.target.isInitialized) {
12 throw new model_not_initialized_error_1.ModelNotInitializedError(new.target, `${new.target.name} cannot be instantiated.`);
13 }
14 super(values, (0, alias_inference_service_1.inferAlias)(options, new.target));
15 }
16 static initialize(attributes, options) {
17 this.isInitialized = true;
18 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
19 // @ts-ignore
20 return super.init(attributes, options);
21 }
22 /**
23 * Adds relation between specified instances and source instance
24 */
25 $add(propertyKey, instances, options) {
26 return this['add' + (0, string_1.capitalize)(propertyKey)](instances, options);
27 }
28 /**
29 * Sets relation between specified instances and source instance
30 * (replaces old relations)
31 */
32 $set(propertyKey, instances, options) {
33 return this['set' + (0, string_1.capitalize)(propertyKey)](instances, options);
34 }
35 /**
36 * Returns related instance (specified by propertyKey) of source instance
37 */
38 $get(propertyKey, options) {
39 return this['get' + (0, string_1.capitalize)(propertyKey)](options);
40 }
41 /**
42 * Counts related instances (specified by propertyKey) of source instance
43 */
44 $count(propertyKey, options) {
45 return this['count' + (0, string_1.capitalize)(propertyKey)](options);
46 }
47 /**
48 * Creates instances and relate them to source instance
49 */
50 $create(propertyKey, values, options) {
51 return this['create' + (0, string_1.capitalize)(propertyKey)](values, options);
52 }
53 /**
54 * Checks if specified instances is related to source instance
55 */
56 $has(propertyKey, instances, options) {
57 return this['has' + (0, string_1.capitalize)(propertyKey)](instances, options);
58 }
59 /**
60 * Removes specified instances from source instance
61 */
62 $remove(propertyKey, instances, options) {
63 return this['remove' + (0, string_1.capitalize)(propertyKey)](instances, options);
64 }
65 reload(options) {
66 return super.reload((0, alias_inference_service_1.inferAlias)(options, this));
67 }
68}
69exports.Model = Model;
70Model.isInitialized = false;
71/**
72 * Indicates which static methods of Model has to be proxied,
73 * to prepare include option to automatically resolve alias;
74 * The index represents the index of the options of the
75 * corresponding method parameter
76 */
77exports.INFER_ALIAS_MAP = {
78 bulkBuild: 1,
79 build: 1,
80 create: 1,
81 aggregate: 2,
82 all: 0,
83 find: 0,
84 findAll: 0,
85 findAndCount: 0,
86 findAndCountAll: 0,
87 findById: 1,
88 findByPrimary: 1,
89 findCreateFind: 0,
90 findOne: 0,
91 findOrBuild: 0,
92 findOrCreate: 0,
93 findOrInitialize: 0,
94 reload: 0,
95};
96const staticModelFunctionProperties = (0, object_1.getAllPropertyNames)(sequelize_1.Model).filter((key) => !isForbiddenMember(key) && isFunctionMember(key, sequelize_1.Model) && !isPrivateMember(key));
97function isFunctionMember(propertyKey, target) {
98 return typeof target[propertyKey] === 'function';
99}
100function isForbiddenMember(propertyKey) {
101 const FORBIDDEN_KEYS = [
102 'name',
103 'constructor',
104 'length',
105 'prototype',
106 'caller',
107 'arguments',
108 'apply',
109 'queryInterface',
110 'queryGenerator',
111 'init',
112 'replaceHookAliases',
113 'refreshAttributes',
114 'inspect',
115 ];
116 return FORBIDDEN_KEYS.indexOf(propertyKey) !== -1;
117}
118function isPrivateMember(propertyKey) {
119 return propertyKey.charAt(0) === '_';
120}
121function addThrowNotInitializedProxy() {
122 staticModelFunctionProperties.forEach((key) => {
123 const superFn = Model[key];
124 Model[key] = function (...args) {
125 if (!this.isInitialized) {
126 throw new model_not_initialized_error_1.ModelNotInitializedError(this, `Member "${key}" cannot be called.`);
127 }
128 return superFn.call(this, ...args);
129 };
130 });
131}
132function addInferAliasOverrides() {
133 Object.keys(exports.INFER_ALIAS_MAP).forEach((key) => {
134 const optionIndex = exports.INFER_ALIAS_MAP[key];
135 const superFn = Model[key];
136 Model[key] = function (...args) {
137 args[optionIndex] = (0, alias_inference_service_1.inferAlias)(args[optionIndex], this);
138 return superFn.call(this, ...args);
139 };
140 });
141}
142addThrowNotInitializedProxy();
143addInferAliasOverrides();
144//# sourceMappingURL=model.js.map
\No newline at end of file