UNPKG

2.72 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * Contains all information about entity's relation count.
5 */
6var RelationIdMetadata = /** @class */ (function () {
7 // ---------------------------------------------------------------------
8 // Constructor
9 // ---------------------------------------------------------------------
10 function RelationIdMetadata(options) {
11 this.entityMetadata = options.entityMetadata;
12 this.target = options.args.target;
13 this.propertyName = options.args.propertyName;
14 this.relationNameOrFactory = options.args.relation;
15 this.alias = options.args.alias;
16 this.queryBuilderFactory = options.args.queryBuilderFactory;
17 }
18 // ---------------------------------------------------------------------
19 // Public Methods
20 // ---------------------------------------------------------------------
21 /**
22 * Sets relation id value from the given entity.
23 *
24 * todo: make it to work in embeds as well.
25 */
26 RelationIdMetadata.prototype.setValue = function (entity) {
27 var _this = this;
28 var inverseEntity = this.relation.getEntityValue(entity);
29 if (inverseEntity instanceof Array) {
30 entity[this.propertyName] = inverseEntity.map(function (item) {
31 return _this.relation.inverseEntityMetadata.getEntityIdMixedMap(item);
32 }).filter(function (item) { return item !== null && item !== undefined; });
33 }
34 else {
35 var value = this.relation.inverseEntityMetadata.getEntityIdMixedMap(inverseEntity);
36 if (value !== undefined)
37 entity[this.propertyName] = value;
38 }
39 };
40 // ---------------------------------------------------------------------
41 // Public Builder Methods
42 // ---------------------------------------------------------------------
43 /**
44 * Builds some depend relation id properties.
45 * This builder method should be used only after entity metadata, its properties map and all relations are build.
46 */
47 RelationIdMetadata.prototype.build = function () {
48 var propertyPath = this.relationNameOrFactory instanceof Function ? this.relationNameOrFactory(this.entityMetadata.propertiesMap) : this.relationNameOrFactory;
49 var relation = this.entityMetadata.findRelationWithPropertyPath(propertyPath);
50 if (!relation)
51 throw new Error("Cannot find relation " + propertyPath + ". Wrong relation specified for @RelationId decorator.");
52 this.relation = relation;
53 };
54 return RelationIdMetadata;
55}());
56exports.RelationIdMetadata = RelationIdMetadata;
57
58//# sourceMappingURL=RelationIdMetadata.js.map