1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var MongoDriver_1 = require("../driver/mongodb/MongoDriver");
|
5 |
|
6 |
|
7 |
|
8 | var EmbeddedMetadata = (function () {
|
9 |
|
10 |
|
11 |
|
12 | function EmbeddedMetadata(options) {
|
13 | |
14 |
|
15 |
|
16 | this.columns = [];
|
17 | |
18 |
|
19 |
|
20 | this.relations = [];
|
21 | |
22 |
|
23 |
|
24 | this.listeners = [];
|
25 | |
26 |
|
27 |
|
28 | this.indices = [];
|
29 | |
30 |
|
31 |
|
32 | this.uniques = [];
|
33 | |
34 |
|
35 |
|
36 | this.relationIds = [];
|
37 | |
38 |
|
39 |
|
40 | this.relationCounts = [];
|
41 | |
42 |
|
43 |
|
44 | this.embeddeds = [];
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 | this.isArray = false;
|
51 | |
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | this.parentPropertyNames = [];
|
59 | |
60 |
|
61 |
|
62 | this.parentPrefixes = [];
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | this.embeddedMetadataTree = [];
|
70 | |
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | this.columnsFromTree = [];
|
77 | |
78 |
|
79 |
|
80 | this.relationsFromTree = [];
|
81 | |
82 |
|
83 |
|
84 | this.listenersFromTree = [];
|
85 | |
86 |
|
87 |
|
88 | this.indicesFromTree = [];
|
89 | |
90 |
|
91 |
|
92 | this.uniquesFromTree = [];
|
93 | |
94 |
|
95 |
|
96 | this.relationIdsFromTree = [];
|
97 | |
98 |
|
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 |
|
109 |
|
110 | |
111 |
|
112 |
|
113 | EmbeddedMetadata.prototype.create = function () {
|
114 | return new this.type;
|
115 | };
|
116 |
|
117 |
|
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 |
|
137 |
|
138 | EmbeddedMetadata.prototype.buildPartialPrefix = function () {
|
139 |
|
140 | if (this.customPrefix === undefined || this.customPrefix === true) {
|
141 | return [this.propertyName];
|
142 | }
|
143 |
|
144 | if (this.customPrefix === "" || this.customPrefix === false) {
|
145 | return [];
|
146 | }
|
147 |
|
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("_");
|
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 | }());
|
194 | exports.EmbeddedMetadata = EmbeddedMetadata;
|
195 |
|
196 |
|