UNPKG

5.97 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * Index metadata contains all information about table's index.
5 */
6var IndexMetadata = /** @class */ (function () {
7 // ---------------------------------------------------------------------
8 // Constructor
9 // ---------------------------------------------------------------------
10 function IndexMetadata(options) {
11 /**
12 * Indicates if this index must be unique.
13 */
14 this.isUnique = false;
15 /**
16 * The SPATIAL modifier indexes the entire column and does not allow indexed columns to contain NULL values.
17 * Works only in MySQL.
18 */
19 this.isSpatial = false;
20 /**
21 * The FULLTEXT modifier indexes the entire column and does not allow prefixing.
22 * Works only in MySQL.
23 */
24 this.isFulltext = false;
25 /**
26 * Indicates if this index must synchronize with database index.
27 */
28 this.synchronize = true;
29 /**
30 * Indexed columns.
31 */
32 this.columns = [];
33 /**
34 * Map of column names with order set.
35 * Used only by MongoDB driver.
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 // Public Build Methods
59 // ---------------------------------------------------------------------
60 /**
61 * Builds some depend index properties.
62 * Must be called after all entity metadata's properties map, columns and relations are built.
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 // if columns already an array of string then simply return it
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 { // todo: indices in embeds are not implemented in this syntax. deprecate this syntax?
83 // if columns is a function that returns array of field names then execute it and get columns names from it
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}());
120exports.IndexMetadata = IndexMetadata;
121
122//# sourceMappingURL=IndexMetadata.js.map