UNPKG

9.13 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var MongoDriver_1 = require("../driver/mongodb/MongoDriver");
5/**
6 * Contains all information about entity's embedded property.
7 */
8var EmbeddedMetadata = /** @class */ (function () {
9 // ---------------------------------------------------------------------
10 // Constructor
11 // ---------------------------------------------------------------------
12 function EmbeddedMetadata(options) {
13 /**
14 * Columns inside this embed.
15 */
16 this.columns = [];
17 /**
18 * Relations inside this embed.
19 */
20 this.relations = [];
21 /**
22 * Entity listeners inside this embed.
23 */
24 this.listeners = [];
25 /**
26 * Indices applied to the embed columns.
27 */
28 this.indices = [];
29 /**
30 * Uniques applied to the embed columns.
31 */
32 this.uniques = [];
33 /**
34 * Relation ids inside this embed.
35 */
36 this.relationIds = [];
37 /**
38 * Relation counts inside this embed.
39 */
40 this.relationCounts = [];
41 /**
42 * Nested embeddable in this embeddable (which has current embedded as parent embedded).
43 */
44 this.embeddeds = [];
45 /**
46 * Indicates if this embedded is in array mode.
47 *
48 * This option works only in mongodb.
49 */
50 this.isArray = false;
51 /**
52 * Returns array of property names of current embed and all its parent embeds.
53 *
54 * example: post[data][information][counters].id where "data", "information" and "counters" are embeds
55 * we need to get value of "id" column from the post real entity object.
56 * this method will return ["data", "information", "counters"]
57 */
58 this.parentPropertyNames = [];
59 /**
60 * Returns array of prefixes of current embed and all its parent embeds.
61 */
62 this.parentPrefixes = [];
63 /**
64 * Returns embed metadatas from all levels of the parent tree.
65 *
66 * example: post[data][information][counters].id where "data", "information" and "counters" are embeds
67 * this method will return [embed metadata of data, embed metadata of information, embed metadata of counters]
68 */
69 this.embeddedMetadataTree = [];
70 /**
71 * Embed metadatas from all levels of the parent tree.
72 *
73 * example: post[data][information][counters].id where "data", "information" and "counters" are embeds
74 * this method will return [embed metadata of data, embed metadata of information, embed metadata of counters]
75 */
76 this.columnsFromTree = [];
77 /**
78 * Relations of this embed and all relations from its child embeds.
79 */
80 this.relationsFromTree = [];
81 /**
82 * Relations of this embed and all relations from its child embeds.
83 */
84 this.listenersFromTree = [];
85 /**
86 * Indices of this embed and all indices from its child embeds.
87 */
88 this.indicesFromTree = [];
89 /**
90 * Uniques of this embed and all uniques from its child embeds.
91 */
92 this.uniquesFromTree = [];
93 /**
94 * Relation ids of this embed and all relation ids from its child embeds.
95 */
96 this.relationIdsFromTree = [];
97 /**
98 * Relation counts of this embed and all relation counts from its child embeds.
99 */
100 this.relationCountsFromTree = [];
101 this.entityMetadata = options.entityMetadata;
102 this.type = options.args.type();
103 this.propertyName = options.args.propertyName;
104 this.customPrefix = options.args.prefix;
105 this.isArray = options.args.isArray;
106 }
107 // ---------------------------------------------------------------------
108 // Public Methods
109 // ---------------------------------------------------------------------
110 /**
111 * Creates a new embedded object.
112 */
113 EmbeddedMetadata.prototype.create = function () {
114 return new this.type;
115 };
116 // ---------------------------------------------------------------------
117 // Builder Methods
118 // ---------------------------------------------------------------------
119 EmbeddedMetadata.prototype.build = function (connection) {
120 this.embeddeds.forEach(function (embedded) { return embedded.build(connection); });
121 this.prefix = this.buildPrefix(connection);
122 this.parentPropertyNames = this.buildParentPropertyNames();
123 this.parentPrefixes = this.buildParentPrefixes();
124 this.propertyPath = this.parentPropertyNames.join(".");
125 this.embeddedMetadataTree = this.buildEmbeddedMetadataTree();
126 this.columnsFromTree = this.buildColumnsFromTree();
127 this.relationsFromTree = this.buildRelationsFromTree();
128 this.listenersFromTree = this.buildListenersFromTree();
129 this.indicesFromTree = this.buildIndicesFromTree();
130 this.uniquesFromTree = this.buildUniquesFromTree();
131 this.relationIdsFromTree = this.buildRelationIdsFromTree();
132 this.relationCountsFromTree = this.buildRelationCountsFromTree();
133 return this;
134 };
135 // ---------------------------------------------------------------------
136 // Protected Methods
137 // ---------------------------------------------------------------------
138 EmbeddedMetadata.prototype.buildPartialPrefix = function () {
139 // if prefix option was not set or explicitly set to true - default prefix
140 if (this.customPrefix === undefined || this.customPrefix === true) {
141 return [this.propertyName];
142 }
143 // if prefix option was set to empty string or explicity set to false - disable prefix
144 if (this.customPrefix === "" || this.customPrefix === false) {
145 return [];
146 }
147 // use custom prefix
148 if (typeof this.customPrefix === "string") {
149 return [this.customPrefix];
150 }
151 throw new Error("Invalid prefix option given for " + this.entityMetadata.targetName + "#" + this.propertyName);
152 };
153 EmbeddedMetadata.prototype.buildPrefix = function (connection) {
154 if (connection.driver instanceof MongoDriver_1.MongoDriver)
155 return this.propertyName;
156 var prefixes = [];
157 if (this.parentEmbeddedMetadata)
158 prefixes.push(this.parentEmbeddedMetadata.buildPrefix(connection));
159 prefixes.push.apply(prefixes, tslib_1.__spread(this.buildPartialPrefix()));
160 return prefixes.join("_"); // todo: use naming strategy instead of "_" !!!
161 };
162 EmbeddedMetadata.prototype.buildParentPropertyNames = function () {
163 return this.parentEmbeddedMetadata ? this.parentEmbeddedMetadata.buildParentPropertyNames().concat(this.propertyName) : [this.propertyName];
164 };
165 EmbeddedMetadata.prototype.buildParentPrefixes = function () {
166 return this.parentEmbeddedMetadata ? this.parentEmbeddedMetadata.buildParentPrefixes().concat(this.buildPartialPrefix()) : this.buildPartialPrefix();
167 };
168 EmbeddedMetadata.prototype.buildEmbeddedMetadataTree = function () {
169 return this.parentEmbeddedMetadata ? this.parentEmbeddedMetadata.buildEmbeddedMetadataTree().concat(this) : [this];
170 };
171 EmbeddedMetadata.prototype.buildColumnsFromTree = function () {
172 return this.embeddeds.reduce(function (columns, embedded) { return columns.concat(embedded.buildColumnsFromTree()); }, this.columns);
173 };
174 EmbeddedMetadata.prototype.buildRelationsFromTree = function () {
175 return this.embeddeds.reduce(function (relations, embedded) { return relations.concat(embedded.buildRelationsFromTree()); }, this.relations);
176 };
177 EmbeddedMetadata.prototype.buildListenersFromTree = function () {
178 return this.embeddeds.reduce(function (relations, embedded) { return relations.concat(embedded.buildListenersFromTree()); }, this.listeners);
179 };
180 EmbeddedMetadata.prototype.buildIndicesFromTree = function () {
181 return this.embeddeds.reduce(function (relations, embedded) { return relations.concat(embedded.buildIndicesFromTree()); }, this.indices);
182 };
183 EmbeddedMetadata.prototype.buildUniquesFromTree = function () {
184 return this.embeddeds.reduce(function (relations, embedded) { return relations.concat(embedded.buildUniquesFromTree()); }, this.uniques);
185 };
186 EmbeddedMetadata.prototype.buildRelationIdsFromTree = function () {
187 return this.embeddeds.reduce(function (relations, embedded) { return relations.concat(embedded.buildRelationIdsFromTree()); }, this.relationIds);
188 };
189 EmbeddedMetadata.prototype.buildRelationCountsFromTree = function () {
190 return this.embeddeds.reduce(function (relations, embedded) { return relations.concat(embedded.buildRelationCountsFromTree()); }, this.relationCounts);
191 };
192 return EmbeddedMetadata;
193}());
194exports.EmbeddedMetadata = EmbeddedMetadata;
195
196//# sourceMappingURL=EmbeddedMetadata.js.map