1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 |
|
4 |
|
5 |
|
6 | var IndexMetadata = (function () {
|
7 |
|
8 |
|
9 |
|
10 | function IndexMetadata(options) {
|
11 | |
12 |
|
13 |
|
14 | this.isUnique = false;
|
15 | |
16 |
|
17 |
|
18 |
|
19 | this.isSpatial = false;
|
20 | |
21 |
|
22 |
|
23 |
|
24 | this.isFulltext = false;
|
25 | |
26 |
|
27 |
|
28 | this.synchronize = true;
|
29 | |
30 |
|
31 |
|
32 | this.columns = [];
|
33 | |
34 |
|
35 |
|
36 |
|
37 | this.columnNamesWithOrderingMap = {};
|
38 | this.entityMetadata = options.entityMetadata;
|
39 | this.embeddedMetadata = options.embeddedMetadata;
|
40 | if (options.columns)
|
41 | this.columns = options.columns;
|
42 | if (options.args) {
|
43 | this.target = options.args.target;
|
44 | if (options.args.synchronize !== null && options.args.synchronize !== undefined)
|
45 | this.synchronize = options.args.synchronize;
|
46 | this.isUnique = !!options.args.unique;
|
47 | this.isSpatial = !!options.args.spatial;
|
48 | this.isFulltext = !!options.args.fulltext;
|
49 | this.where = options.args.where;
|
50 | this.isSparse = options.args.sparse;
|
51 | this.isBackground = options.args.background;
|
52 | this.expireAfterSeconds = options.args.expireAfterSeconds;
|
53 | this.givenName = options.args.name;
|
54 | this.givenColumnNames = options.args.columns;
|
55 | }
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 | IndexMetadata.prototype.build = function (namingStrategy) {
|
65 | var _this = this;
|
66 | if (this.synchronize === false) {
|
67 | this.name = this.givenName;
|
68 | return this;
|
69 | }
|
70 | var map = {};
|
71 |
|
72 | if (this.givenColumnNames) {
|
73 | var columnPropertyPaths = [];
|
74 | if (this.givenColumnNames instanceof Array) {
|
75 | columnPropertyPaths = this.givenColumnNames.map(function (columnName) {
|
76 | if (_this.embeddedMetadata)
|
77 | return _this.embeddedMetadata.propertyPath + "." + columnName;
|
78 | return columnName;
|
79 | });
|
80 | columnPropertyPaths.forEach(function (propertyPath) { return map[propertyPath] = 1; });
|
81 | }
|
82 | else {
|
83 |
|
84 | var columnsFnResult_1 = this.givenColumnNames(this.entityMetadata.propertiesMap);
|
85 | if (columnsFnResult_1 instanceof Array) {
|
86 | columnPropertyPaths = columnsFnResult_1.map(function (i) { return String(i); });
|
87 | columnPropertyPaths.forEach(function (name) { return map[name] = 1; });
|
88 | }
|
89 | else {
|
90 | columnPropertyPaths = Object.keys(columnsFnResult_1).map(function (i) { return String(i); });
|
91 | Object.keys(columnsFnResult_1).forEach(function (columnName) { return map[columnName] = columnsFnResult_1[columnName]; });
|
92 | }
|
93 | }
|
94 | this.columns = columnPropertyPaths.map(function (propertyPath) {
|
95 | var columnWithSameName = _this.entityMetadata.columns.find(function (column) { return column.propertyPath === propertyPath; });
|
96 | if (columnWithSameName) {
|
97 | return [columnWithSameName];
|
98 | }
|
99 | var relationWithSameName = _this.entityMetadata.relations.find(function (relation) { return relation.isWithJoinColumn && relation.propertyName === propertyPath; });
|
100 | if (relationWithSameName) {
|
101 | return relationWithSameName.joinColumns;
|
102 | }
|
103 | var indexName = _this.givenName ? "\"" + _this.givenName + "\" " : "";
|
104 | var entityName = _this.entityMetadata.targetName;
|
105 | throw new Error("Index " + indexName + "contains column that is missing in the entity (" + entityName + "): " + propertyPath);
|
106 | })
|
107 | .reduce(function (a, b) { return a.concat(b); });
|
108 | }
|
109 | this.columnNamesWithOrderingMap = Object.keys(map).reduce(function (updatedMap, key) {
|
110 | var column = _this.entityMetadata.columns.find(function (column) { return column.propertyPath === key; });
|
111 | if (column)
|
112 | updatedMap[column.databasePath] = map[key];
|
113 | return updatedMap;
|
114 | }, {});
|
115 | this.name = this.givenName ? this.givenName : namingStrategy.indexName(this.entityMetadata.tablePath, this.columns.map(function (column) { return column.databaseName; }), this.where);
|
116 | return this;
|
117 | };
|
118 | return IndexMetadata;
|
119 | }());
|
120 | exports.IndexMetadata = IndexMetadata;
|
121 |
|
122 |
|