1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 |
|
4 |
|
5 |
|
6 | var Broadcaster = (function () {
|
7 |
|
8 |
|
9 |
|
10 | function Broadcaster(queryRunner) {
|
11 | this.queryRunner = queryRunner;
|
12 | }
|
13 |
|
14 |
|
15 |
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | Broadcaster.prototype.broadcastBeforeInsertEvent = function (result, metadata, entity) {
|
25 | var _this = this;
|
26 | if (entity && metadata.beforeInsertListeners.length) {
|
27 | metadata.beforeInsertListeners.forEach(function (listener) {
|
28 | if (listener.isAllowed(entity)) {
|
29 | var executionResult = listener.execute(entity);
|
30 | if (executionResult instanceof Promise)
|
31 | result.promises.push(executionResult);
|
32 | result.count++;
|
33 | }
|
34 | });
|
35 | }
|
36 | if (this.queryRunner.connection.subscribers.length) {
|
37 | this.queryRunner.connection.subscribers.forEach(function (subscriber) {
|
38 | if (_this.isAllowedSubscriber(subscriber, metadata.target) && subscriber.beforeInsert) {
|
39 | var executionResult = subscriber.beforeInsert({
|
40 | connection: _this.queryRunner.connection,
|
41 | queryRunner: _this.queryRunner,
|
42 | manager: _this.queryRunner.manager,
|
43 | entity: entity,
|
44 | metadata: metadata
|
45 | });
|
46 | if (executionResult instanceof Promise)
|
47 | result.promises.push(executionResult);
|
48 | result.count++;
|
49 | }
|
50 | });
|
51 | }
|
52 | };
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | Broadcaster.prototype.broadcastBeforeUpdateEvent = function (result, metadata, entity, databaseEntity, updatedColumns, updatedRelations) {
|
62 | var _this = this;
|
63 | if (entity && metadata.beforeUpdateListeners.length) {
|
64 | metadata.beforeUpdateListeners.forEach(function (listener) {
|
65 | if (listener.isAllowed(entity)) {
|
66 | var executionResult = listener.execute(entity);
|
67 | if (executionResult instanceof Promise)
|
68 | result.promises.push(executionResult);
|
69 | result.count++;
|
70 | }
|
71 | });
|
72 | }
|
73 | if (this.queryRunner.connection.subscribers.length) {
|
74 | this.queryRunner.connection.subscribers.forEach(function (subscriber) {
|
75 | if (_this.isAllowedSubscriber(subscriber, metadata.target) && subscriber.beforeUpdate) {
|
76 | var executionResult = subscriber.beforeUpdate({
|
77 | connection: _this.queryRunner.connection,
|
78 | queryRunner: _this.queryRunner,
|
79 | manager: _this.queryRunner.manager,
|
80 | entity: entity,
|
81 | metadata: metadata,
|
82 | databaseEntity: databaseEntity,
|
83 | updatedColumns: updatedColumns || [],
|
84 | updatedRelations: updatedRelations || []
|
85 | });
|
86 | if (executionResult instanceof Promise)
|
87 | result.promises.push(executionResult);
|
88 | result.count++;
|
89 | }
|
90 | });
|
91 | }
|
92 | };
|
93 | |
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | Broadcaster.prototype.broadcastBeforeRemoveEvent = function (result, metadata, entity, databaseEntity) {
|
102 | var _this = this;
|
103 | if (entity && metadata.beforeRemoveListeners.length) {
|
104 | metadata.beforeRemoveListeners.forEach(function (listener) {
|
105 | if (listener.isAllowed(entity)) {
|
106 | var executionResult = listener.execute(entity);
|
107 | if (executionResult instanceof Promise)
|
108 | result.promises.push(executionResult);
|
109 | result.count++;
|
110 | }
|
111 | });
|
112 | }
|
113 | if (this.queryRunner.connection.subscribers.length) {
|
114 | this.queryRunner.connection.subscribers.forEach(function (subscriber) {
|
115 | if (_this.isAllowedSubscriber(subscriber, metadata.target) && subscriber.beforeRemove) {
|
116 | var executionResult = subscriber.beforeRemove({
|
117 | connection: _this.queryRunner.connection,
|
118 | queryRunner: _this.queryRunner,
|
119 | manager: _this.queryRunner.manager,
|
120 | entity: entity,
|
121 | metadata: metadata,
|
122 | databaseEntity: databaseEntity,
|
123 | entityId: metadata.getEntityIdMixedMap(databaseEntity)
|
124 | });
|
125 | if (executionResult instanceof Promise)
|
126 | result.promises.push(executionResult);
|
127 | result.count++;
|
128 | }
|
129 | });
|
130 | }
|
131 | };
|
132 | |
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | Broadcaster.prototype.broadcastAfterInsertEvent = function (result, metadata, entity) {
|
141 | var _this = this;
|
142 | if (entity && metadata.afterInsertListeners.length) {
|
143 | metadata.afterInsertListeners.forEach(function (listener) {
|
144 | if (listener.isAllowed(entity)) {
|
145 | var executionResult = listener.execute(entity);
|
146 | if (executionResult instanceof Promise)
|
147 | result.promises.push(executionResult);
|
148 | result.count++;
|
149 | }
|
150 | });
|
151 | }
|
152 | if (this.queryRunner.connection.subscribers.length) {
|
153 | this.queryRunner.connection.subscribers.forEach(function (subscriber) {
|
154 | if (_this.isAllowedSubscriber(subscriber, metadata.target) && subscriber.afterInsert) {
|
155 | var executionResult = subscriber.afterInsert({
|
156 | connection: _this.queryRunner.connection,
|
157 | queryRunner: _this.queryRunner,
|
158 | manager: _this.queryRunner.manager,
|
159 | entity: entity,
|
160 | metadata: metadata
|
161 | });
|
162 | if (executionResult instanceof Promise)
|
163 | result.promises.push(executionResult);
|
164 | result.count++;
|
165 | }
|
166 | });
|
167 | }
|
168 | };
|
169 | |
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | Broadcaster.prototype.broadcastAfterUpdateEvent = function (result, metadata, entity, databaseEntity, updatedColumns, updatedRelations) {
|
178 | var _this = this;
|
179 | if (entity && metadata.afterUpdateListeners.length) {
|
180 | metadata.afterUpdateListeners.forEach(function (listener) {
|
181 | if (listener.isAllowed(entity)) {
|
182 | var executionResult = listener.execute(entity);
|
183 | if (executionResult instanceof Promise)
|
184 | result.promises.push(executionResult);
|
185 | result.count++;
|
186 | }
|
187 | });
|
188 | }
|
189 | if (this.queryRunner.connection.subscribers.length) {
|
190 | this.queryRunner.connection.subscribers.forEach(function (subscriber) {
|
191 | if (_this.isAllowedSubscriber(subscriber, metadata.target) && subscriber.afterUpdate) {
|
192 | var executionResult = subscriber.afterUpdate({
|
193 | connection: _this.queryRunner.connection,
|
194 | queryRunner: _this.queryRunner,
|
195 | manager: _this.queryRunner.manager,
|
196 | entity: entity,
|
197 | metadata: metadata,
|
198 | databaseEntity: databaseEntity,
|
199 | updatedColumns: updatedColumns || [],
|
200 | updatedRelations: updatedRelations || []
|
201 | });
|
202 | if (executionResult instanceof Promise)
|
203 | result.promises.push(executionResult);
|
204 | result.count++;
|
205 | }
|
206 | });
|
207 | }
|
208 | };
|
209 | |
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | Broadcaster.prototype.broadcastAfterRemoveEvent = function (result, metadata, entity, databaseEntity) {
|
218 | var _this = this;
|
219 | if (entity && metadata.afterRemoveListeners.length) {
|
220 | metadata.afterRemoveListeners.forEach(function (listener) {
|
221 | if (listener.isAllowed(entity)) {
|
222 | var executionResult = listener.execute(entity);
|
223 | if (executionResult instanceof Promise)
|
224 | result.promises.push(executionResult);
|
225 | result.count++;
|
226 | }
|
227 | });
|
228 | }
|
229 | if (this.queryRunner.connection.subscribers.length) {
|
230 | this.queryRunner.connection.subscribers.forEach(function (subscriber) {
|
231 | if (_this.isAllowedSubscriber(subscriber, metadata.target) && subscriber.afterRemove) {
|
232 | var executionResult = subscriber.afterRemove({
|
233 | connection: _this.queryRunner.connection,
|
234 | queryRunner: _this.queryRunner,
|
235 | manager: _this.queryRunner.manager,
|
236 | entity: entity,
|
237 | metadata: metadata,
|
238 | databaseEntity: databaseEntity,
|
239 | entityId: metadata.getEntityIdMixedMap(databaseEntity)
|
240 | });
|
241 | if (executionResult instanceof Promise)
|
242 | result.promises.push(executionResult);
|
243 | result.count++;
|
244 | }
|
245 | });
|
246 | }
|
247 | };
|
248 | |
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 | Broadcaster.prototype.broadcastLoadEventsForAll = function (result, metadata, entities) {
|
257 | var _this = this;
|
258 | entities.forEach(function (entity) {
|
259 | if (entity instanceof Promise)
|
260 | return;
|
261 |
|
262 | if (metadata.relations.length) {
|
263 | metadata.relations.forEach(function (relation) {
|
264 |
|
265 | if (relation.isLazy && !entity.hasOwnProperty(relation.propertyName))
|
266 | return;
|
267 | var value = relation.getEntityValue(entity);
|
268 | if (value instanceof Object)
|
269 | _this.broadcastLoadEventsForAll(result, relation.inverseEntityMetadata, value instanceof Array ? value : [value]);
|
270 | });
|
271 | }
|
272 | if (metadata.afterLoadListeners.length) {
|
273 | metadata.afterLoadListeners.forEach(function (listener) {
|
274 | if (listener.isAllowed(entity)) {
|
275 | var executionResult = listener.execute(entity);
|
276 | if (executionResult instanceof Promise)
|
277 | result.promises.push(executionResult);
|
278 | result.count++;
|
279 | }
|
280 | });
|
281 | }
|
282 | if (_this.queryRunner.connection.subscribers.length) {
|
283 | _this.queryRunner.connection.subscribers.forEach(function (subscriber) {
|
284 | if (_this.isAllowedSubscriber(subscriber, metadata.target) && subscriber.afterLoad) {
|
285 | var executionResult = subscriber.afterLoad(entity, {
|
286 | connection: _this.queryRunner.connection,
|
287 | queryRunner: _this.queryRunner,
|
288 | manager: _this.queryRunner.manager,
|
289 | entity: entity,
|
290 | metadata: metadata
|
291 | });
|
292 | if (executionResult instanceof Promise)
|
293 | result.promises.push(executionResult);
|
294 | result.count++;
|
295 | }
|
296 | });
|
297 | }
|
298 | });
|
299 | };
|
300 |
|
301 |
|
302 |
|
303 | |
304 |
|
305 |
|
306 |
|
307 | Broadcaster.prototype.isAllowedSubscriber = function (subscriber, target) {
|
308 | return !subscriber.listenTo ||
|
309 | !subscriber.listenTo() ||
|
310 | subscriber.listenTo() === Object ||
|
311 | subscriber.listenTo() === target ||
|
312 | subscriber.listenTo().isPrototypeOf(target);
|
313 | };
|
314 | return Broadcaster;
|
315 | }());
|
316 | exports.Broadcaster = Broadcaster;
|
317 |
|
318 |
|