UNPKG

4.65 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * Unique metadata contains all information about table's unique constraints.
5 */
6var UniqueMetadata = /** @class */ (function () {
7 // ---------------------------------------------------------------------
8 // Constructor
9 // ---------------------------------------------------------------------
10 function UniqueMetadata(options) {
11 /**
12 * Unique columns.
13 */
14 this.columns = [];
15 /**
16 * Map of column names with order set.
17 * Used only by MongoDB driver.
18 */
19 this.columnNamesWithOrderingMap = {};
20 this.entityMetadata = options.entityMetadata;
21 this.embeddedMetadata = options.embeddedMetadata;
22 if (options.columns)
23 this.columns = options.columns;
24 if (options.args) {
25 this.target = options.args.target;
26 this.givenName = options.args.name;
27 this.givenColumnNames = options.args.columns;
28 }
29 }
30 // ---------------------------------------------------------------------
31 // Public Build Methods
32 // ---------------------------------------------------------------------
33 /**
34 * Builds some depend unique constraint properties.
35 * Must be called after all entity metadata's properties map, columns and relations are built.
36 */
37 UniqueMetadata.prototype.build = function (namingStrategy) {
38 var _this = this;
39 var map = {};
40 // if columns already an array of string then simply return it
41 if (this.givenColumnNames) {
42 var columnPropertyPaths = [];
43 if (this.givenColumnNames instanceof Array) {
44 columnPropertyPaths = this.givenColumnNames.map(function (columnName) {
45 if (_this.embeddedMetadata)
46 return _this.embeddedMetadata.propertyPath + "." + columnName;
47 return columnName;
48 });
49 columnPropertyPaths.forEach(function (propertyPath) { return map[propertyPath] = 1; });
50 }
51 else {
52 // if columns is a function that returns array of field names then execute it and get columns names from it
53 var columnsFnResult_1 = this.givenColumnNames(this.entityMetadata.propertiesMap);
54 if (columnsFnResult_1 instanceof Array) {
55 columnPropertyPaths = columnsFnResult_1.map(function (i) { return String(i); });
56 columnPropertyPaths.forEach(function (name) { return map[name] = 1; });
57 }
58 else {
59 columnPropertyPaths = Object.keys(columnsFnResult_1).map(function (i) { return String(i); });
60 Object.keys(columnsFnResult_1).forEach(function (columnName) { return map[columnName] = columnsFnResult_1[columnName]; });
61 }
62 }
63 this.columns = columnPropertyPaths.map(function (propertyName) {
64 var columnWithSameName = _this.entityMetadata.columns.find(function (column) { return column.propertyPath === propertyName; });
65 if (columnWithSameName) {
66 return [columnWithSameName];
67 }
68 var relationWithSameName = _this.entityMetadata.relations.find(function (relation) { return relation.isWithJoinColumn && relation.propertyName === propertyName; });
69 if (relationWithSameName) {
70 return relationWithSameName.joinColumns;
71 }
72 var indexName = _this.givenName ? "\"" + _this.givenName + "\" " : "";
73 var entityName = _this.entityMetadata.targetName;
74 throw new Error("Unique constraint " + indexName + "contains column that is missing in the entity (" + entityName + "): " + propertyName);
75 })
76 .reduce(function (a, b) { return a.concat(b); });
77 }
78 this.columnNamesWithOrderingMap = Object.keys(map).reduce(function (updatedMap, key) {
79 var column = _this.entityMetadata.columns.find(function (column) { return column.propertyPath === key; });
80 if (column)
81 updatedMap[column.databasePath] = map[key];
82 return updatedMap;
83 }, {});
84 this.name = this.givenName ? this.givenName : namingStrategy.uniqueConstraintName(this.entityMetadata.tablePath, this.columns.map(function (column) { return column.databaseName; }));
85 return this;
86 };
87 return UniqueMetadata;
88}());
89exports.UniqueMetadata = UniqueMetadata;
90
91//# sourceMappingURL=UniqueMetadata.js.map