UNPKG

9.84 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var QueryBuilder_1 = require("./QueryBuilder");
5var RelationUpdater_1 = require("./RelationUpdater");
6var RelationRemover_1 = require("./RelationRemover");
7/**
8 * Allows to work with entity relations and perform specific operations with those relations.
9 *
10 * todo: add transactions everywhere
11 */
12var RelationQueryBuilder = /** @class */ (function (_super) {
13 tslib_1.__extends(RelationQueryBuilder, _super);
14 function RelationQueryBuilder() {
15 return _super !== null && _super.apply(this, arguments) || this;
16 }
17 // -------------------------------------------------------------------------
18 // Public Implemented Methods
19 // -------------------------------------------------------------------------
20 /**
21 * Gets generated sql query without parameters being replaced.
22 */
23 RelationQueryBuilder.prototype.getQuery = function () {
24 return "";
25 };
26 // -------------------------------------------------------------------------
27 // Public Methods
28 // -------------------------------------------------------------------------
29 /**
30 * Sets entity (target) which relations will be updated.
31 */
32 RelationQueryBuilder.prototype.of = function (entity) {
33 this.expressionMap.of = entity;
34 return this;
35 };
36 /**
37 * Sets entity relation's value.
38 * Value can be entity, entity id or entity id map (if entity has composite ids).
39 * Works only for many-to-one and one-to-one relations.
40 * For many-to-many and one-to-many relations use #add and #remove methods instead.
41 */
42 RelationQueryBuilder.prototype.set = function (value) {
43 return tslib_1.__awaiter(this, void 0, void 0, function () {
44 var relation, updater;
45 return tslib_1.__generator(this, function (_a) {
46 relation = this.expressionMap.relationMetadata;
47 if (!this.expressionMap.of) // todo: move this check before relation query builder creation?
48 throw new Error("Entity whose relation needs to be set is not set. Use .of method to define whose relation you want to set.");
49 if (relation.isManyToMany || relation.isOneToMany)
50 throw new Error("Set operation is only supported for many-to-one and one-to-one relations. " +
51 ("However given \"" + relation.propertyPath + "\" has " + relation.relationType + " relation. ") +
52 "Use .add() method instead.");
53 // if there are multiple join columns then user must send id map as "value" argument. check if he really did it
54 if (relation.joinColumns &&
55 relation.joinColumns.length > 1 &&
56 (!(value instanceof Object) || Object.keys(value).length < relation.joinColumns.length))
57 throw new Error("Value to be set into the relation must be a map of relation ids, for example: .set({ firstName: \"...\", lastName: \"...\" })");
58 updater = new RelationUpdater_1.RelationUpdater(this, this.expressionMap);
59 return [2 /*return*/, updater.update(value)];
60 });
61 });
62 };
63 /**
64 * Adds (binds) given value to entity relation.
65 * Value can be entity, entity id or entity id map (if entity has composite ids).
66 * Value also can be array of entities, array of entity ids or array of entity id maps (if entity has composite ids).
67 * Works only for many-to-many and one-to-many relations.
68 * For many-to-one and one-to-one use #set method instead.
69 */
70 RelationQueryBuilder.prototype.add = function (value) {
71 return tslib_1.__awaiter(this, void 0, void 0, function () {
72 var relation, updater;
73 return tslib_1.__generator(this, function (_a) {
74 if (value instanceof Array && value.length === 0)
75 return [2 /*return*/];
76 relation = this.expressionMap.relationMetadata;
77 if (!this.expressionMap.of) // todo: move this check before relation query builder creation?
78 throw new Error("Entity whose relation needs to be set is not set. Use .of method to define whose relation you want to set.");
79 if (relation.isManyToOne || relation.isOneToOne)
80 throw new Error("Add operation is only supported for many-to-many and one-to-many relations. " +
81 ("However given \"" + relation.propertyPath + "\" has " + relation.relationType + " relation. ") +
82 "Use .set() method instead.");
83 // if there are multiple join columns then user must send id map as "value" argument. check if he really did it
84 if (relation.joinColumns &&
85 relation.joinColumns.length > 1 &&
86 (!(value instanceof Object) || Object.keys(value).length < relation.joinColumns.length))
87 throw new Error("Value to be set into the relation must be a map of relation ids, for example: .set({ firstName: \"...\", lastName: \"...\" })");
88 updater = new RelationUpdater_1.RelationUpdater(this, this.expressionMap);
89 return [2 /*return*/, updater.update(value)];
90 });
91 });
92 };
93 /**
94 * Removes (unbinds) given value from entity relation.
95 * Value can be entity, entity id or entity id map (if entity has composite ids).
96 * Value also can be array of entities, array of entity ids or array of entity id maps (if entity has composite ids).
97 * Works only for many-to-many and one-to-many relations.
98 * For many-to-one and one-to-one use #set method instead.
99 */
100 RelationQueryBuilder.prototype.remove = function (value) {
101 return tslib_1.__awaiter(this, void 0, void 0, function () {
102 var relation, remover;
103 return tslib_1.__generator(this, function (_a) {
104 if (value instanceof Array && value.length === 0)
105 return [2 /*return*/];
106 relation = this.expressionMap.relationMetadata;
107 if (!this.expressionMap.of) // todo: move this check before relation query builder creation?
108 throw new Error("Entity whose relation needs to be set is not set. Use .of method to define whose relation you want to set.");
109 if (relation.isManyToOne || relation.isOneToOne)
110 throw new Error("Add operation is only supported for many-to-many and one-to-many relations. " +
111 ("However given \"" + relation.propertyPath + "\" has " + relation.relationType + " relation. ") +
112 "Use .set(null) method instead.");
113 remover = new RelationRemover_1.RelationRemover(this, this.expressionMap);
114 return [2 /*return*/, remover.remove(value)];
115 });
116 });
117 };
118 /**
119 * Adds (binds) and removes (unbinds) given values to/from entity relation.
120 * Value can be entity, entity id or entity id map (if entity has composite ids).
121 * Value also can be array of entities, array of entity ids or array of entity id maps (if entity has composite ids).
122 * Works only for many-to-many and one-to-many relations.
123 * For many-to-one and one-to-one use #set method instead.
124 */
125 RelationQueryBuilder.prototype.addAndRemove = function (added, removed) {
126 return tslib_1.__awaiter(this, void 0, void 0, function () {
127 return tslib_1.__generator(this, function (_a) {
128 switch (_a.label) {
129 case 0: return [4 /*yield*/, this.remove(removed)];
130 case 1:
131 _a.sent();
132 return [4 /*yield*/, this.add(added)];
133 case 2:
134 _a.sent();
135 return [2 /*return*/];
136 }
137 });
138 });
139 };
140 /**
141 * Gets entity's relation id.
142 async getId(): Promise<any> {
143
144 }*/
145 /**
146 * Gets entity's relation ids.
147 async getIds(): Promise<any[]> {
148 return [];
149 }*/
150 /**
151 * Loads a single entity (relational) from the relation.
152 * You can also provide id of relational entity to filter by.
153 */
154 RelationQueryBuilder.prototype.loadOne = function () {
155 return tslib_1.__awaiter(this, void 0, void 0, function () {
156 return tslib_1.__generator(this, function (_a) {
157 return [2 /*return*/, this.loadMany().then(function (results) { return results[0]; })];
158 });
159 });
160 };
161 /**
162 * Loads many entities (relational) from the relation.
163 * You can also provide ids of relational entities to filter by.
164 */
165 RelationQueryBuilder.prototype.loadMany = function () {
166 return tslib_1.__awaiter(this, void 0, void 0, function () {
167 var of, metadata;
168 return tslib_1.__generator(this, function (_a) {
169 of = this.expressionMap.of;
170 if (!(of instanceof Object)) {
171 metadata = this.expressionMap.mainAlias.metadata;
172 if (metadata.hasMultiplePrimaryKeys)
173 throw new Error("Cannot load entity because only one primary key was specified, however entity contains multiple primary keys");
174 of = metadata.primaryColumns[0].createValueMap(of);
175 }
176 return [2 /*return*/, this.connection.relationLoader.load(this.expressionMap.relationMetadata, of)];
177 });
178 });
179 };
180 return RelationQueryBuilder;
181}(QueryBuilder_1.QueryBuilder));
182exports.RelationQueryBuilder = RelationQueryBuilder;
183
184//# sourceMappingURL=RelationQueryBuilder.js.map