1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var OracleDriver_1 = require("../driver/oracle/OracleDriver");
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var RelationUpdater = (function () {
|
11 |
|
12 |
|
13 |
|
14 | function RelationUpdater(queryBuilder, expressionMap) {
|
15 | this.queryBuilder = queryBuilder;
|
16 | this.expressionMap = expressionMap;
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 | |
22 |
|
23 |
|
24 | RelationUpdater.prototype.update = function (value) {
|
25 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
26 | var relation, updateSet, updateSet_1, ofs, parameters_1, conditions_1, condition, of_1, updateSet, junctionMetadata_1, ofs, values, firstColumnValues, secondColumnValues_1, bulkInserted_1;
|
27 | var _this = this;
|
28 | return tslib_1.__generator(this, function (_a) {
|
29 | switch (_a.label) {
|
30 | case 0:
|
31 | relation = this.expressionMap.relationMetadata;
|
32 | if (!(relation.isManyToOne || relation.isOneToOneOwner)) return [3 , 2];
|
33 | updateSet = relation.joinColumns.reduce(function (updateSet, joinColumn) {
|
34 | var relationValue = value instanceof Object ? joinColumn.referencedColumn.getEntityValue(value) : value;
|
35 | joinColumn.setEntityValue(updateSet, relationValue);
|
36 | return updateSet;
|
37 | }, {});
|
38 | if (!this.expressionMap.of || (this.expressionMap.of instanceof Array && !this.expressionMap.of.length))
|
39 | return [2 ];
|
40 | return [4 , this.queryBuilder
|
41 | .createQueryBuilder()
|
42 | .update(relation.entityMetadata.target)
|
43 | .set(updateSet)
|
44 | .whereInIds(this.expressionMap.of)
|
45 | .execute()];
|
46 | case 1:
|
47 | _a.sent();
|
48 | return [3 , 10];
|
49 | case 2:
|
50 | if (!((relation.isOneToOneNotOwner || relation.isOneToMany) && value === null)) return [3 , 4];
|
51 | updateSet_1 = {};
|
52 | relation.inverseRelation.joinColumns.forEach(function (column) {
|
53 | updateSet_1[column.propertyName] = null;
|
54 | });
|
55 | ofs = this.expressionMap.of instanceof Array ? this.expressionMap.of : [this.expressionMap.of];
|
56 | parameters_1 = {};
|
57 | conditions_1 = [];
|
58 | ofs.forEach(function (of, ofIndex) {
|
59 | relation.inverseRelation.joinColumns.map(function (column, columnIndex) {
|
60 | var parameterName = "joinColumn_" + ofIndex + "_" + columnIndex;
|
61 | parameters_1[parameterName] = of instanceof Object ? column.referencedColumn.getEntityValue(of) : of;
|
62 | conditions_1.push(column.propertyPath + " = :" + parameterName);
|
63 | });
|
64 | });
|
65 | condition = conditions_1.map(function (str) { return "(" + str + ")"; }).join(" OR ");
|
66 | if (!condition)
|
67 | return [2 ];
|
68 | return [4 , this.queryBuilder
|
69 | .createQueryBuilder()
|
70 | .update(relation.inverseEntityMetadata.target)
|
71 | .set(updateSet_1)
|
72 | .where(condition)
|
73 | .setParameters(parameters_1)
|
74 | .execute()];
|
75 | case 3:
|
76 | _a.sent();
|
77 | return [3 , 10];
|
78 | case 4:
|
79 | if (!(relation.isOneToOneNotOwner || relation.isOneToMany)) return [3 , 6];
|
80 | if (this.expressionMap.of instanceof Array)
|
81 | throw new Error("You cannot update relations of multiple entities with the same related object. Provide a single entity into .of method.");
|
82 | of_1 = this.expressionMap.of;
|
83 | updateSet = relation.inverseRelation.joinColumns.reduce(function (updateSet, joinColumn) {
|
84 | var relationValue = of_1 instanceof Object ? joinColumn.referencedColumn.getEntityValue(of_1) : of_1;
|
85 | joinColumn.setEntityValue(updateSet, relationValue);
|
86 | return updateSet;
|
87 | }, {});
|
88 | if (!value || (value instanceof Array && !value.length))
|
89 | return [2 ];
|
90 | return [4 , this.queryBuilder
|
91 | .createQueryBuilder()
|
92 | .update(relation.inverseEntityMetadata.target)
|
93 | .set(updateSet)
|
94 | .whereInIds(value)
|
95 | .execute()];
|
96 | case 5:
|
97 | _a.sent();
|
98 | return [3 , 10];
|
99 | case 6:
|
100 | junctionMetadata_1 = relation.junctionEntityMetadata;
|
101 | ofs = this.expressionMap.of instanceof Array ? this.expressionMap.of : [this.expressionMap.of];
|
102 | values = value instanceof Array ? value : [value];
|
103 | firstColumnValues = relation.isManyToManyOwner ? ofs : values;
|
104 | secondColumnValues_1 = relation.isManyToManyOwner ? values : ofs;
|
105 | bulkInserted_1 = [];
|
106 | firstColumnValues.forEach(function (firstColumnVal) {
|
107 | secondColumnValues_1.forEach(function (secondColumnVal) {
|
108 | var inserted = {};
|
109 | junctionMetadata_1.ownerColumns.forEach(function (column) {
|
110 | inserted[column.databaseName] = firstColumnVal instanceof Object ? column.referencedColumn.getEntityValue(firstColumnVal) : firstColumnVal;
|
111 | });
|
112 | junctionMetadata_1.inverseColumns.forEach(function (column) {
|
113 | inserted[column.databaseName] = secondColumnVal instanceof Object ? column.referencedColumn.getEntityValue(secondColumnVal) : secondColumnVal;
|
114 | });
|
115 | bulkInserted_1.push(inserted);
|
116 | });
|
117 | });
|
118 | if (!bulkInserted_1.length)
|
119 | return [2 ];
|
120 | if (!(this.queryBuilder.connection.driver instanceof OracleDriver_1.OracleDriver)) return [3 , 8];
|
121 | return [4 , Promise.all(bulkInserted_1.map(function (value) {
|
122 | return _this.queryBuilder
|
123 | .createQueryBuilder()
|
124 | .insert()
|
125 | .into(junctionMetadata_1.tableName)
|
126 | .values(value)
|
127 | .execute();
|
128 | }))];
|
129 | case 7:
|
130 | _a.sent();
|
131 | return [3 , 10];
|
132 | case 8: return [4 , this.queryBuilder
|
133 | .createQueryBuilder()
|
134 | .insert()
|
135 | .into(junctionMetadata_1.tableName)
|
136 | .values(bulkInserted_1)
|
137 | .execute()];
|
138 | case 9:
|
139 | _a.sent();
|
140 | _a.label = 10;
|
141 | case 10: return [2 ];
|
142 | }
|
143 | });
|
144 | });
|
145 | };
|
146 | return RelationUpdater;
|
147 | }());
|
148 | exports.RelationUpdater = RelationUpdater;
|
149 |
|
150 |
|