1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var EntityNotFoundError_1 = require("../error/EntityNotFoundError");
|
5 | var QueryRunnerProviderAlreadyReleasedError_1 = require("../error/QueryRunnerProviderAlreadyReleasedError");
|
6 | var NoNeedToReleaseEntityManagerError_1 = require("../error/NoNeedToReleaseEntityManagerError");
|
7 | var TreeRepository_1 = require("../repository/TreeRepository");
|
8 | var Repository_1 = require("../repository/Repository");
|
9 | var FindOptionsUtils_1 = require("../find-options/FindOptionsUtils");
|
10 | var PlainObjectToNewEntityTransformer_1 = require("../query-builder/transformer/PlainObjectToNewEntityTransformer");
|
11 | var PlainObjectToDatabaseEntityTransformer_1 = require("../query-builder/transformer/PlainObjectToDatabaseEntityTransformer");
|
12 | var CustomRepositoryNotFoundError_1 = require("../error/CustomRepositoryNotFoundError");
|
13 | var index_1 = require("../index");
|
14 | var AbstractRepository_1 = require("../repository/AbstractRepository");
|
15 | var CustomRepositoryCannotInheritRepositoryError_1 = require("../error/CustomRepositoryCannotInheritRepositoryError");
|
16 | var MongoDriver_1 = require("../driver/mongodb/MongoDriver");
|
17 | var RepositoryNotFoundError_1 = require("../error/RepositoryNotFoundError");
|
18 | var RepositoryNotTreeError_1 = require("../error/RepositoryNotTreeError");
|
19 | var RepositoryFactory_1 = require("../repository/RepositoryFactory");
|
20 | var TreeRepositoryNotSupportedError_1 = require("../error/TreeRepositoryNotSupportedError");
|
21 | var EntityPersistExecutor_1 = require("../persistence/EntityPersistExecutor");
|
22 | var OracleDriver_1 = require("../driver/oracle/OracleDriver");
|
23 | var ObjectUtils_1 = require("../util/ObjectUtils");
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | var EntityManager = (function () {
|
29 |
|
30 |
|
31 |
|
32 | function EntityManager(connection, queryRunner) {
|
33 |
|
34 |
|
35 |
|
36 | |
37 |
|
38 |
|
39 | this.repositories = [];
|
40 | |
41 |
|
42 |
|
43 | this.plainObjectToEntityTransformer = new PlainObjectToNewEntityTransformer_1.PlainObjectToNewEntityTransformer();
|
44 | this.connection = connection;
|
45 | if (queryRunner) {
|
46 | this.queryRunner = queryRunner;
|
47 |
|
48 | ObjectUtils_1.ObjectUtils.assign(this.queryRunner, { manager: this });
|
49 | }
|
50 | }
|
51 | |
52 |
|
53 |
|
54 |
|
55 | EntityManager.prototype.transaction = function (isolationOrRunInTransaction, runInTransactionParam) {
|
56 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
57 | var isolation, runInTransaction, queryRunner, result, err_1, rollbackError_1;
|
58 | return tslib_1.__generator(this, function (_a) {
|
59 | switch (_a.label) {
|
60 | case 0:
|
61 | isolation = typeof isolationOrRunInTransaction === "string" ? isolationOrRunInTransaction : undefined;
|
62 | runInTransaction = typeof isolationOrRunInTransaction === "function" ? isolationOrRunInTransaction : runInTransactionParam;
|
63 | if (!runInTransaction) {
|
64 | throw new Error("Transaction method requires callback in second paramter if isolation level is supplied.");
|
65 | }
|
66 | if (this.connection.driver instanceof MongoDriver_1.MongoDriver)
|
67 | throw new Error("Transactions aren't supported by MongoDB.");
|
68 | if (this.queryRunner && this.queryRunner.isReleased)
|
69 | throw new QueryRunnerProviderAlreadyReleasedError_1.QueryRunnerProviderAlreadyReleasedError();
|
70 | if (this.queryRunner && this.queryRunner.isTransactionActive)
|
71 | throw new Error("Cannot start transaction because its already started");
|
72 | queryRunner = this.queryRunner || this.connection.createQueryRunner("master");
|
73 | _a.label = 1;
|
74 | case 1:
|
75 | _a.trys.push([1, 8, 13, 16]);
|
76 | if (!isolation) return [3 , 3];
|
77 | return [4 , queryRunner.startTransaction(isolation)];
|
78 | case 2:
|
79 | _a.sent();
|
80 | return [3 , 5];
|
81 | case 3: return [4 , queryRunner.startTransaction()];
|
82 | case 4:
|
83 | _a.sent();
|
84 | _a.label = 5;
|
85 | case 5: return [4 , runInTransaction(queryRunner.manager)];
|
86 | case 6:
|
87 | result = _a.sent();
|
88 | return [4 , queryRunner.commitTransaction()];
|
89 | case 7:
|
90 | _a.sent();
|
91 | return [2 , result];
|
92 | case 8:
|
93 | err_1 = _a.sent();
|
94 | _a.label = 9;
|
95 | case 9:
|
96 | _a.trys.push([9, 11, , 12]);
|
97 | return [4 , queryRunner.rollbackTransaction()];
|
98 | case 10:
|
99 | _a.sent();
|
100 | return [3 , 12];
|
101 | case 11:
|
102 | rollbackError_1 = _a.sent();
|
103 | return [3 , 12];
|
104 | case 12: throw err_1;
|
105 | case 13:
|
106 | if (!!this.queryRunner) return [3 , 15];
|
107 | return [4 , queryRunner.release()];
|
108 | case 14:
|
109 | _a.sent();
|
110 | _a.label = 15;
|
111 | case 15: return [7 ];
|
112 | case 16: return [2 ];
|
113 | }
|
114 | });
|
115 | });
|
116 | };
|
117 | |
118 |
|
119 |
|
120 | EntityManager.prototype.query = function (query, parameters) {
|
121 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
122 | return tslib_1.__generator(this, function (_a) {
|
123 | return [2 , this.connection.query(query, parameters, this.queryRunner)];
|
124 | });
|
125 | });
|
126 | };
|
127 | |
128 |
|
129 |
|
130 | EntityManager.prototype.createQueryBuilder = function (entityClass, alias, queryRunner) {
|
131 | if (alias) {
|
132 | return this.connection.createQueryBuilder(entityClass, alias, queryRunner || this.queryRunner);
|
133 | }
|
134 | else {
|
135 | return this.connection.createQueryBuilder(entityClass || queryRunner || this.queryRunner);
|
136 | }
|
137 | };
|
138 | |
139 |
|
140 |
|
141 | EntityManager.prototype.hasId = function (targetOrEntity, maybeEntity) {
|
142 | var target = arguments.length === 2 ? targetOrEntity : targetOrEntity.constructor;
|
143 | var entity = arguments.length === 2 ? maybeEntity : targetOrEntity;
|
144 | var metadata = this.connection.getMetadata(target);
|
145 | return metadata.hasId(entity);
|
146 | };
|
147 | |
148 |
|
149 |
|
150 | EntityManager.prototype.getId = function (targetOrEntity, maybeEntity) {
|
151 | var target = arguments.length === 2 ? targetOrEntity : targetOrEntity.constructor;
|
152 | var entity = arguments.length === 2 ? maybeEntity : targetOrEntity;
|
153 | var metadata = this.connection.getMetadata(target);
|
154 | return metadata.getEntityIdMixedMap(entity);
|
155 | };
|
156 | |
157 |
|
158 |
|
159 |
|
160 | EntityManager.prototype.create = function (entityClass, plainObjectOrObjects) {
|
161 | var _this = this;
|
162 | var metadata = this.connection.getMetadata(entityClass);
|
163 | if (!plainObjectOrObjects)
|
164 | return metadata.create(this.queryRunner);
|
165 | if (plainObjectOrObjects instanceof Array)
|
166 | return plainObjectOrObjects.map(function (plainEntityLike) { return _this.create(entityClass, plainEntityLike); });
|
167 | var mergeIntoEntity = metadata.create(this.queryRunner);
|
168 | this.plainObjectToEntityTransformer.transform(mergeIntoEntity, plainObjectOrObjects, metadata, true);
|
169 | return mergeIntoEntity;
|
170 | };
|
171 | |
172 |
|
173 |
|
174 | EntityManager.prototype.merge = function (entityClass, mergeIntoEntity) {
|
175 | var _this = this;
|
176 | var entityLikes = [];
|
177 | for (var _i = 2; _i < arguments.length; _i++) {
|
178 | entityLikes[_i - 2] = arguments[_i];
|
179 | }
|
180 | var metadata = this.connection.getMetadata(entityClass);
|
181 | entityLikes.forEach(function (object) { return _this.plainObjectToEntityTransformer.transform(mergeIntoEntity, object, metadata); });
|
182 | return mergeIntoEntity;
|
183 | };
|
184 | |
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | EntityManager.prototype.preload = function (entityClass, entityLike) {
|
191 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
192 | var metadata, plainObjectToDatabaseEntityTransformer, transformedEntity;
|
193 | return tslib_1.__generator(this, function (_a) {
|
194 | switch (_a.label) {
|
195 | case 0:
|
196 | metadata = this.connection.getMetadata(entityClass);
|
197 | plainObjectToDatabaseEntityTransformer = new PlainObjectToDatabaseEntityTransformer_1.PlainObjectToDatabaseEntityTransformer(this.connection.manager);
|
198 | return [4 , plainObjectToDatabaseEntityTransformer.transform(entityLike, metadata)];
|
199 | case 1:
|
200 | transformedEntity = _a.sent();
|
201 | if (transformedEntity)
|
202 | return [2 , this.merge(entityClass, transformedEntity, entityLike)];
|
203 | return [2 , undefined];
|
204 | }
|
205 | });
|
206 | });
|
207 | };
|
208 | |
209 |
|
210 |
|
211 | EntityManager.prototype.save = function (targetOrEntity, maybeEntityOrOptions, maybeOptions) {
|
212 |
|
213 | var target = (arguments.length > 1 && (targetOrEntity instanceof Function || targetOrEntity instanceof index_1.EntitySchema || typeof targetOrEntity === "string")) ? targetOrEntity : undefined;
|
214 | var entity = target ? maybeEntityOrOptions : targetOrEntity;
|
215 | var options = target ? maybeOptions : maybeEntityOrOptions;
|
216 | if (target instanceof index_1.EntitySchema)
|
217 | target = target.options.name;
|
218 |
|
219 | if (entity instanceof Array && entity.length === 0)
|
220 | return Promise.resolve(entity);
|
221 |
|
222 | return new EntityPersistExecutor_1.EntityPersistExecutor(this.connection, this.queryRunner, "save", target, entity, options)
|
223 | .execute()
|
224 | .then(function () { return entity; });
|
225 | };
|
226 | |
227 |
|
228 |
|
229 | EntityManager.prototype.remove = function (targetOrEntity, maybeEntityOrOptions, maybeOptions) {
|
230 |
|
231 | var target = (arguments.length > 1 && (targetOrEntity instanceof Function || typeof targetOrEntity === "string")) ? targetOrEntity : undefined;
|
232 | var entity = target ? maybeEntityOrOptions : targetOrEntity;
|
233 | var options = target ? maybeOptions : maybeEntityOrOptions;
|
234 |
|
235 | if (entity instanceof Array && entity.length === 0)
|
236 | return Promise.resolve(entity);
|
237 |
|
238 | return new EntityPersistExecutor_1.EntityPersistExecutor(this.connection, this.queryRunner, "remove", target, entity, options)
|
239 | .execute()
|
240 | .then(function () { return entity; });
|
241 | };
|
242 | |
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 | EntityManager.prototype.insert = function (target, entity) {
|
250 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
251 | var results;
|
252 | var _this = this;
|
253 | return tslib_1.__generator(this, function (_a) {
|
254 | switch (_a.label) {
|
255 | case 0:
|
256 | if (!(this.connection.driver instanceof OracleDriver_1.OracleDriver && entity instanceof Array)) return [3 , 2];
|
257 | return [4 , Promise.all(entity.map(function (entity) { return _this.insert(target, entity); }))];
|
258 | case 1:
|
259 | results = _a.sent();
|
260 | return [2 , results.reduce(function (mergedResult, result) { return Object.assign(mergedResult, result); }, {})];
|
261 | case 2: return [2 , this.createQueryBuilder()
|
262 | .insert()
|
263 | .into(target)
|
264 | .values(entity)
|
265 | .execute()];
|
266 | }
|
267 | });
|
268 | });
|
269 | };
|
270 | |
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 | EntityManager.prototype.update = function (target, criteria, partialEntity) {
|
278 |
|
279 | if (criteria === undefined ||
|
280 | criteria === null ||
|
281 | criteria === "" ||
|
282 | (criteria instanceof Array && criteria.length === 0)) {
|
283 | return Promise.reject(new Error("Empty criteria(s) are not allowed for the update method."));
|
284 | }
|
285 | if (typeof criteria === "string" ||
|
286 | typeof criteria === "number" ||
|
287 | criteria instanceof Date ||
|
288 | criteria instanceof Array) {
|
289 | return this.createQueryBuilder()
|
290 | .update(target)
|
291 | .set(partialEntity)
|
292 | .whereInIds(criteria)
|
293 | .execute();
|
294 | }
|
295 | else {
|
296 | return this.createQueryBuilder()
|
297 | .update(target)
|
298 | .set(partialEntity)
|
299 | .where(criteria)
|
300 | .execute();
|
301 | }
|
302 | };
|
303 | |
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 | EntityManager.prototype.delete = function (targetOrEntity, criteria) {
|
311 |
|
312 | if (criteria === undefined ||
|
313 | criteria === null ||
|
314 | criteria === "" ||
|
315 | (criteria instanceof Array && criteria.length === 0)) {
|
316 | return Promise.reject(new Error("Empty criteria(s) are not allowed for the delete method."));
|
317 | }
|
318 | if (typeof criteria === "string" ||
|
319 | typeof criteria === "number" ||
|
320 | criteria instanceof Date ||
|
321 | criteria instanceof Array) {
|
322 | return this.createQueryBuilder()
|
323 | .delete()
|
324 | .from(targetOrEntity)
|
325 | .whereInIds(criteria)
|
326 | .execute();
|
327 | }
|
328 | else {
|
329 | return this.createQueryBuilder()
|
330 | .delete()
|
331 | .from(targetOrEntity)
|
332 | .where(criteria)
|
333 | .execute();
|
334 | }
|
335 | };
|
336 | |
337 |
|
338 |
|
339 |
|
340 | EntityManager.prototype.count = function (entityClass, optionsOrConditions) {
|
341 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
342 | var metadata, qb;
|
343 | return tslib_1.__generator(this, function (_a) {
|
344 | metadata = this.connection.getMetadata(entityClass);
|
345 | qb = this.createQueryBuilder(entityClass, FindOptionsUtils_1.FindOptionsUtils.extractFindManyOptionsAlias(optionsOrConditions) || metadata.name);
|
346 | return [2 , FindOptionsUtils_1.FindOptionsUtils.applyFindManyOptionsOrConditionsToQueryBuilder(qb, optionsOrConditions).getCount()];
|
347 | });
|
348 | });
|
349 | };
|
350 | |
351 |
|
352 |
|
353 | EntityManager.prototype.find = function (entityClass, optionsOrConditions) {
|
354 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
355 | var metadata, qb;
|
356 | return tslib_1.__generator(this, function (_a) {
|
357 | metadata = this.connection.getMetadata(entityClass);
|
358 | qb = this.createQueryBuilder(entityClass, FindOptionsUtils_1.FindOptionsUtils.extractFindManyOptionsAlias(optionsOrConditions) || metadata.name);
|
359 | if (!FindOptionsUtils_1.FindOptionsUtils.isFindManyOptions(optionsOrConditions) || optionsOrConditions.loadEagerRelations !== false)
|
360 | FindOptionsUtils_1.FindOptionsUtils.joinEagerRelations(qb, qb.alias, metadata);
|
361 | return [2 , FindOptionsUtils_1.FindOptionsUtils.applyFindManyOptionsOrConditionsToQueryBuilder(qb, optionsOrConditions).getMany()];
|
362 | });
|
363 | });
|
364 | };
|
365 | |
366 |
|
367 |
|
368 |
|
369 |
|
370 | EntityManager.prototype.findAndCount = function (entityClass, optionsOrConditions) {
|
371 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
372 | var metadata, qb;
|
373 | return tslib_1.__generator(this, function (_a) {
|
374 | metadata = this.connection.getMetadata(entityClass);
|
375 | qb = this.createQueryBuilder(entityClass, FindOptionsUtils_1.FindOptionsUtils.extractFindManyOptionsAlias(optionsOrConditions) || metadata.name);
|
376 | if (!FindOptionsUtils_1.FindOptionsUtils.isFindManyOptions(optionsOrConditions) || optionsOrConditions.loadEagerRelations !== false)
|
377 | FindOptionsUtils_1.FindOptionsUtils.joinEagerRelations(qb, qb.alias, metadata);
|
378 | return [2 , FindOptionsUtils_1.FindOptionsUtils.applyFindManyOptionsOrConditionsToQueryBuilder(qb, optionsOrConditions).getManyAndCount()];
|
379 | });
|
380 | });
|
381 | };
|
382 | |
383 |
|
384 |
|
385 |
|
386 | EntityManager.prototype.findByIds = function (entityClass, ids, optionsOrConditions) {
|
387 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
388 | var metadata, qb;
|
389 | return tslib_1.__generator(this, function (_a) {
|
390 |
|
391 | if (!ids.length)
|
392 | return [2 , Promise.resolve([])];
|
393 | metadata = this.connection.getMetadata(entityClass);
|
394 | qb = this.createQueryBuilder(entityClass, FindOptionsUtils_1.FindOptionsUtils.extractFindManyOptionsAlias(optionsOrConditions) || metadata.name);
|
395 | FindOptionsUtils_1.FindOptionsUtils.applyFindManyOptionsOrConditionsToQueryBuilder(qb, optionsOrConditions);
|
396 | if (!FindOptionsUtils_1.FindOptionsUtils.isFindManyOptions(optionsOrConditions) || optionsOrConditions.loadEagerRelations !== false)
|
397 | FindOptionsUtils_1.FindOptionsUtils.joinEagerRelations(qb, qb.alias, metadata);
|
398 | return [2 , qb.andWhereInIds(ids).getMany()];
|
399 | });
|
400 | });
|
401 | };
|
402 | |
403 |
|
404 |
|
405 | EntityManager.prototype.findOne = function (entityClass, idOrOptionsOrConditions, maybeOptions) {
|
406 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
407 | var findOptions, options, metadata, alias, qb;
|
408 | return tslib_1.__generator(this, function (_a) {
|
409 | findOptions = undefined;
|
410 | if (FindOptionsUtils_1.FindOptionsUtils.isFindOneOptions(idOrOptionsOrConditions)) {
|
411 | findOptions = idOrOptionsOrConditions;
|
412 | }
|
413 | else if (maybeOptions && FindOptionsUtils_1.FindOptionsUtils.isFindOneOptions(maybeOptions)) {
|
414 | findOptions = maybeOptions;
|
415 | }
|
416 | options = undefined;
|
417 | if (idOrOptionsOrConditions instanceof Object && !FindOptionsUtils_1.FindOptionsUtils.isFindOneOptions(idOrOptionsOrConditions))
|
418 | options = idOrOptionsOrConditions;
|
419 | metadata = this.connection.getMetadata(entityClass);
|
420 | alias = metadata.name;
|
421 | if (findOptions && findOptions.join) {
|
422 | alias = findOptions.join.alias;
|
423 | }
|
424 | else if (maybeOptions && FindOptionsUtils_1.FindOptionsUtils.isFindOneOptions(maybeOptions) && maybeOptions.join) {
|
425 | alias = maybeOptions.join.alias;
|
426 | }
|
427 | qb = this.createQueryBuilder(entityClass, alias);
|
428 | if (!findOptions || findOptions.loadEagerRelations !== false)
|
429 | FindOptionsUtils_1.FindOptionsUtils.joinEagerRelations(qb, qb.alias, qb.expressionMap.mainAlias.metadata);
|
430 | findOptions = tslib_1.__assign({}, (findOptions || {}), { take: 1 });
|
431 | FindOptionsUtils_1.FindOptionsUtils.applyOptionsToQueryBuilder(qb, findOptions);
|
432 | if (options) {
|
433 | qb.where(options);
|
434 | }
|
435 | else if (typeof idOrOptionsOrConditions === "string" || typeof idOrOptionsOrConditions === "number" || idOrOptionsOrConditions instanceof Date) {
|
436 | qb.andWhereInIds(metadata.ensureEntityIdMap(idOrOptionsOrConditions));
|
437 | }
|
438 | return [2 , qb.getOne()];
|
439 | });
|
440 | });
|
441 | };
|
442 | |
443 |
|
444 |
|
445 | EntityManager.prototype.findOneOrFail = function (entityClass, idOrOptionsOrConditions, maybeOptions) {
|
446 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
447 | return tslib_1.__generator(this, function (_a) {
|
448 | return [2 , this.findOne(entityClass, idOrOptionsOrConditions, maybeOptions).then(function (value) {
|
449 | if (value === undefined) {
|
450 | return Promise.reject(new EntityNotFoundError_1.EntityNotFoundError(entityClass, idOrOptionsOrConditions));
|
451 | }
|
452 | return Promise.resolve(value);
|
453 | })];
|
454 | });
|
455 | });
|
456 | };
|
457 | |
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 | EntityManager.prototype.clear = function (entityClass) {
|
464 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
465 | var metadata, queryRunner;
|
466 | return tslib_1.__generator(this, function (_a) {
|
467 | switch (_a.label) {
|
468 | case 0:
|
469 | metadata = this.connection.getMetadata(entityClass);
|
470 | queryRunner = this.queryRunner || this.connection.createQueryRunner("master");
|
471 | _a.label = 1;
|
472 | case 1:
|
473 | _a.trys.push([1, , 3, 6]);
|
474 | return [4 , queryRunner.clearTable(metadata.tablePath)];
|
475 | case 2: return [2 , _a.sent()];
|
476 | case 3:
|
477 | if (!!this.queryRunner) return [3 , 5];
|
478 | return [4 , queryRunner.release()];
|
479 | case 4:
|
480 | _a.sent();
|
481 | _a.label = 5;
|
482 | case 5: return [7 ];
|
483 | case 6: return [2 ];
|
484 | }
|
485 | });
|
486 | });
|
487 | };
|
488 | |
489 |
|
490 |
|
491 | EntityManager.prototype.increment = function (entityClass, conditions, propertyPath, value) {
|
492 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
493 | var metadata, column, values;
|
494 | var _this = this;
|
495 | return tslib_1.__generator(this, function (_a) {
|
496 | metadata = this.connection.getMetadata(entityClass);
|
497 | column = metadata.findColumnWithPropertyPath(propertyPath);
|
498 | if (!column)
|
499 | throw new Error("Column " + propertyPath + " was not found in " + metadata.targetName + " entity.");
|
500 | if (isNaN(Number(value)))
|
501 | throw new Error("Value \"" + value + "\" is not a number.");
|
502 | values = propertyPath
|
503 | .split(".")
|
504 | .reduceRight(function (value, key) {
|
505 | var _a;
|
506 | return (_a = {}, _a[key] = value, _a);
|
507 | }, function () { return _this.connection.driver.escape(column.databaseName) + " + " + value; });
|
508 | return [2 , this
|
509 | .createQueryBuilder(entityClass, "entity")
|
510 | .update(entityClass)
|
511 | .set(values)
|
512 | .where(conditions)
|
513 | .execute()];
|
514 | });
|
515 | });
|
516 | };
|
517 | |
518 |
|
519 |
|
520 | EntityManager.prototype.decrement = function (entityClass, conditions, propertyPath, value) {
|
521 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
522 | var metadata, column, values;
|
523 | var _this = this;
|
524 | return tslib_1.__generator(this, function (_a) {
|
525 | metadata = this.connection.getMetadata(entityClass);
|
526 | column = metadata.findColumnWithPropertyPath(propertyPath);
|
527 | if (!column)
|
528 | throw new Error("Column " + propertyPath + " was not found in " + metadata.targetName + " entity.");
|
529 | if (isNaN(Number(value)))
|
530 | throw new Error("Value \"" + value + "\" is not a number.");
|
531 | values = propertyPath
|
532 | .split(".")
|
533 | .reduceRight(function (value, key) {
|
534 | var _a;
|
535 | return (_a = {}, _a[key] = value, _a);
|
536 | }, function () { return _this.connection.driver.escape(column.databaseName) + " - " + value; });
|
537 | return [2 , this
|
538 | .createQueryBuilder(entityClass, "entity")
|
539 | .update(entityClass)
|
540 | .set(values)
|
541 | .where(conditions)
|
542 | .execute()];
|
543 | });
|
544 | });
|
545 | };
|
546 | |
547 |
|
548 |
|
549 |
|
550 |
|
551 |
|
552 | EntityManager.prototype.getRepository = function (target) {
|
553 |
|
554 | if (!this.connection.hasMetadata(target))
|
555 | throw new RepositoryNotFoundError_1.RepositoryNotFoundError(this.connection.name, target);
|
556 |
|
557 | var metadata = this.connection.getMetadata(target);
|
558 | var repository = this.repositories.find(function (repository) { return repository.metadata === metadata; });
|
559 | if (repository)
|
560 | return repository;
|
561 |
|
562 | var newRepository = new RepositoryFactory_1.RepositoryFactory().create(this, metadata, this.queryRunner);
|
563 | this.repositories.push(newRepository);
|
564 | return newRepository;
|
565 | };
|
566 | |
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 | EntityManager.prototype.getTreeRepository = function (target) {
|
573 |
|
574 | if (this.connection.driver.treeSupport === false)
|
575 | throw new TreeRepositoryNotSupportedError_1.TreeRepositoryNotSupportedError(this.connection.driver);
|
576 |
|
577 | var repository = this.getRepository(target);
|
578 | if (!(repository instanceof TreeRepository_1.TreeRepository))
|
579 | throw new RepositoryNotTreeError_1.RepositoryNotTreeError(target);
|
580 | return repository;
|
581 | };
|
582 | |
583 |
|
584 |
|
585 | EntityManager.prototype.getMongoRepository = function (target) {
|
586 | return this.connection.getMongoRepository(target);
|
587 | };
|
588 | |
589 |
|
590 |
|
591 | EntityManager.prototype.getCustomRepository = function (customRepository) {
|
592 | var entityRepositoryMetadataArgs = index_1.getMetadataArgsStorage().entityRepositories.find(function (repository) {
|
593 | return repository.target === (customRepository instanceof Function ? customRepository : customRepository.constructor);
|
594 | });
|
595 | if (!entityRepositoryMetadataArgs)
|
596 | throw new CustomRepositoryNotFoundError_1.CustomRepositoryNotFoundError(customRepository);
|
597 | var entityMetadata = entityRepositoryMetadataArgs.entity ? this.connection.getMetadata(entityRepositoryMetadataArgs.entity) : undefined;
|
598 | var entityRepositoryInstance = new entityRepositoryMetadataArgs.target(this, entityMetadata);
|
599 |
|
600 |
|
601 | if (entityRepositoryInstance instanceof AbstractRepository_1.AbstractRepository) {
|
602 | if (!entityRepositoryInstance["manager"])
|
603 | entityRepositoryInstance["manager"] = this;
|
604 | }
|
605 | if (entityRepositoryInstance instanceof Repository_1.Repository) {
|
606 | if (!entityMetadata)
|
607 | throw new CustomRepositoryCannotInheritRepositoryError_1.CustomRepositoryCannotInheritRepositoryError(customRepository);
|
608 | entityRepositoryInstance["manager"] = this;
|
609 | entityRepositoryInstance["metadata"] = entityMetadata;
|
610 | }
|
611 | return entityRepositoryInstance;
|
612 | };
|
613 | |
614 |
|
615 |
|
616 |
|
617 |
|
618 | EntityManager.prototype.release = function () {
|
619 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
620 | return tslib_1.__generator(this, function (_a) {
|
621 | if (!this.queryRunner)
|
622 | throw new NoNeedToReleaseEntityManagerError_1.NoNeedToReleaseEntityManagerError();
|
623 | return [2 , this.queryRunner.release()];
|
624 | });
|
625 | });
|
626 | };
|
627 | return EntityManager;
|
628 | }());
|
629 | exports.EntityManager = EntityManager;
|
630 |
|
631 |
|