1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.INFER_ALIAS_MAP = exports.Model = void 0;
|
4 | const sequelize_1 = require("sequelize");
|
5 | const string_1 = require("../../shared/string");
|
6 | const alias_inference_service_1 = require("../../associations/alias-inference/alias-inference-service");
|
7 | const model_not_initialized_error_1 = require("../shared/model-not-initialized-error");
|
8 | const object_1 = require("../../shared/object");
|
9 | class 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 |
|
19 |
|
20 | return super.init(attributes, options);
|
21 | }
|
22 | |
23 |
|
24 |
|
25 | $add(propertyKey, instances, options) {
|
26 | return this['add' + (0, string_1.capitalize)(propertyKey)](instances, options);
|
27 | }
|
28 | |
29 |
|
30 |
|
31 |
|
32 | $set(propertyKey, instances, options) {
|
33 | return this['set' + (0, string_1.capitalize)(propertyKey)](instances, options);
|
34 | }
|
35 | |
36 |
|
37 |
|
38 | $get(propertyKey, options) {
|
39 | return this['get' + (0, string_1.capitalize)(propertyKey)](options);
|
40 | }
|
41 | |
42 |
|
43 |
|
44 | $count(propertyKey, options) {
|
45 | return this['count' + (0, string_1.capitalize)(propertyKey)](options);
|
46 | }
|
47 | |
48 |
|
49 |
|
50 | $create(propertyKey, values, options) {
|
51 | return this['create' + (0, string_1.capitalize)(propertyKey)](values, options);
|
52 | }
|
53 | |
54 |
|
55 |
|
56 | $has(propertyKey, instances, options) {
|
57 | return this['has' + (0, string_1.capitalize)(propertyKey)](instances, options);
|
58 | }
|
59 | |
60 |
|
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 | }
|
69 | exports.Model = Model;
|
70 | Model.isInitialized = false;
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | exports.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 | };
|
96 | const staticModelFunctionProperties = (0, object_1.getAllPropertyNames)(sequelize_1.Model).filter((key) => !isForbiddenMember(key) && isFunctionMember(key, sequelize_1.Model) && !isPrivateMember(key));
|
97 | function isFunctionMember(propertyKey, target) {
|
98 | return typeof target[propertyKey] === 'function';
|
99 | }
|
100 | function 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 | }
|
118 | function isPrivateMember(propertyKey) {
|
119 | return propertyKey.charAt(0) === '_';
|
120 | }
|
121 | function 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 | }
|
132 | function 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 | }
|
142 | addThrowNotInitializedProxy();
|
143 | addInferAliasOverrides();
|
144 |
|
\ | No newline at end of file |