UNPKG

9.42 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var QueryBuilderUtils_1 = require("./QueryBuilderUtils");
5var ObjectUtils_1 = require("../util/ObjectUtils");
6/**
7 * Stores all join attributes which will be used to build a JOIN query.
8 */
9var JoinAttribute = /** @class */ (function () {
10 // -------------------------------------------------------------------------
11 // Constructor
12 // -------------------------------------------------------------------------
13 function JoinAttribute(connection, queryExpressionMap, joinAttribute) {
14 this.connection = connection;
15 this.queryExpressionMap = queryExpressionMap;
16 this.isSelectedEvalueated = false;
17 this.relationEvalueated = false;
18 ObjectUtils_1.ObjectUtils.assign(this, joinAttribute || {});
19 }
20 Object.defineProperty(JoinAttribute.prototype, "isMany", {
21 // -------------------------------------------------------------------------
22 // Public Methods
23 // -------------------------------------------------------------------------
24 get: function () {
25 if (this.isMappingMany !== undefined)
26 return this.isMappingMany;
27 if (this.relation)
28 return this.relation.isManyToMany || this.relation.isOneToMany;
29 return false;
30 },
31 enumerable: true,
32 configurable: true
33 });
34 Object.defineProperty(JoinAttribute.prototype, "isSelected", {
35 /**
36 * Indicates if this join is selected.
37 */
38 get: function () {
39 var _this = this;
40 if (!this.isSelectedEvalueated) {
41 var getValue = function () {
42 var e_1, _a;
43 var _loop_1 = function (select) {
44 if (select.selection === _this.alias.name)
45 return { value: true };
46 if (_this.metadata && !!_this.metadata.columns.find(function (column) { return select.selection === _this.alias.name + "." + column.propertyPath; }))
47 return { value: true };
48 };
49 try {
50 for (var _b = tslib_1.__values(_this.queryExpressionMap.selects), _c = _b.next(); !_c.done; _c = _b.next()) {
51 var select = _c.value;
52 var state_1 = _loop_1(select);
53 if (typeof state_1 === "object")
54 return state_1.value;
55 }
56 }
57 catch (e_1_1) { e_1 = { error: e_1_1 }; }
58 finally {
59 try {
60 if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
61 }
62 finally { if (e_1) throw e_1.error; }
63 }
64 return false;
65 };
66 this.isSelectedCache = getValue();
67 this.isSelectedEvalueated = true;
68 }
69 return this.isSelectedCache;
70 },
71 enumerable: true,
72 configurable: true
73 });
74 Object.defineProperty(JoinAttribute.prototype, "tablePath", {
75 /**
76 * Name of the table which we should join.
77 */
78 get: function () {
79 return this.metadata ? this.metadata.tablePath : this.entityOrProperty;
80 },
81 enumerable: true,
82 configurable: true
83 });
84 Object.defineProperty(JoinAttribute.prototype, "parentAlias", {
85 /**
86 * Alias of the parent of this join.
87 * For example, if we join ("post.category", "categoryAlias") then "post" is a parent alias.
88 * This value is extracted from entityOrProperty value.
89 * This is available when join was made using "post.category" syntax.
90 */
91 get: function () {
92 if (!QueryBuilderUtils_1.QueryBuilderUtils.isAliasProperty(this.entityOrProperty))
93 return undefined;
94 return this.entityOrProperty.substr(0, this.entityOrProperty.indexOf("."));
95 },
96 enumerable: true,
97 configurable: true
98 });
99 Object.defineProperty(JoinAttribute.prototype, "relationPropertyPath", {
100 /**
101 * Relation property name of the parent.
102 * This is used to understand what is joined.
103 * For example, if we join ("post.category", "categoryAlias") then "category" is a relation property.
104 * This value is extracted from entityOrProperty value.
105 * This is available when join was made using "post.category" syntax.
106 */
107 get: function () {
108 if (!QueryBuilderUtils_1.QueryBuilderUtils.isAliasProperty(this.entityOrProperty))
109 return undefined;
110 return this.entityOrProperty.substr(this.entityOrProperty.indexOf(".") + 1);
111 },
112 enumerable: true,
113 configurable: true
114 });
115 Object.defineProperty(JoinAttribute.prototype, "relation", {
116 /**
117 * Relation of the parent.
118 * This is used to understand what is joined.
119 * This is available when join was made using "post.category" syntax.
120 * Relation can be undefined if entityOrProperty is regular entity or custom table.
121 */
122 get: function () {
123 var _this = this;
124 if (!this.relationEvalueated) {
125 var getValue = function () {
126 if (!QueryBuilderUtils_1.QueryBuilderUtils.isAliasProperty(_this.entityOrProperty))
127 return undefined;
128 var relationOwnerSelection = _this.queryExpressionMap.findAliasByName(_this.parentAlias);
129 var relation = relationOwnerSelection.metadata.findRelationWithPropertyPath(_this.relationPropertyPath);
130 if (relation) {
131 return relation;
132 }
133 if (relationOwnerSelection.metadata.parentEntityMetadata) {
134 relation = relationOwnerSelection.metadata.parentEntityMetadata.findRelationWithPropertyPath(_this.relationPropertyPath);
135 if (relation) {
136 return relation;
137 }
138 }
139 throw new Error("Relation with property path " + _this.relationPropertyPath + " in entity was not found.");
140 };
141 this.relationCache = getValue.bind(this)();
142 this.relationEvalueated = true;
143 }
144 return this.relationCache;
145 },
146 enumerable: true,
147 configurable: true
148 });
149 Object.defineProperty(JoinAttribute.prototype, "metadata", {
150 /**
151 * Metadata of the joined entity.
152 * If table without entity was joined, then it will return undefined.
153 */
154 get: function () {
155 // entityOrProperty is relation, e.g. "post.category"
156 if (this.relation)
157 return this.relation.inverseEntityMetadata;
158 // entityOrProperty is Entity class
159 if (this.connection.hasMetadata(this.entityOrProperty))
160 return this.connection.getMetadata(this.entityOrProperty);
161 return undefined;
162 /*if (typeof this.entityOrProperty === "string") { // entityOrProperty is a custom table
163
164 // first try to find entity with such name, this is needed when entity does not have a target class,
165 // and its target is a string name (scenario when plain old javascript is used or entity schema is loaded from files)
166 const metadata = this.connection.entityMetadatas.find(metadata => metadata.name === this.entityOrProperty);
167 if (metadata)
168 return metadata;
169
170 // check if we have entity with such table name, and use its metadata if found
171 return this.connection.entityMetadatas.find(metadata => metadata.tableName === this.entityOrProperty);
172 }*/
173 },
174 enumerable: true,
175 configurable: true
176 });
177 Object.defineProperty(JoinAttribute.prototype, "junctionAlias", {
178 /**
179 * Generates alias of junction table, whose ids we get.
180 */
181 get: function () {
182 if (!this.relation)
183 throw new Error("Cannot get junction table for join without relation.");
184 return this.relation.isOwning ? this.parentAlias + "_" + this.alias.name : this.alias.name + "_" + this.parentAlias;
185 },
186 enumerable: true,
187 configurable: true
188 });
189 Object.defineProperty(JoinAttribute.prototype, "mapToPropertyParentAlias", {
190 get: function () {
191 if (!this.mapToProperty)
192 return undefined;
193 return this.mapToProperty.split(".")[0];
194 },
195 enumerable: true,
196 configurable: true
197 });
198 Object.defineProperty(JoinAttribute.prototype, "mapToPropertyPropertyName", {
199 get: function () {
200 if (!this.mapToProperty)
201 return undefined;
202 return this.mapToProperty.split(".")[1];
203 },
204 enumerable: true,
205 configurable: true
206 });
207 return JoinAttribute;
208}());
209exports.JoinAttribute = JoinAttribute;
210
211//# sourceMappingURL=JoinAttribute.js.map