1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 |
|
4 |
|
5 |
|
6 | var UniqueMetadata = (function () {
|
7 |
|
8 |
|
9 |
|
10 | function UniqueMetadata(options) {
|
11 | |
12 |
|
13 |
|
14 | this.columns = [];
|
15 | |
16 |
|
17 |
|
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 |
|
32 |
|
33 | |
34 |
|
35 |
|
36 |
|
37 | UniqueMetadata.prototype.build = function (namingStrategy) {
|
38 | var _this = this;
|
39 | var map = {};
|
40 |
|
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 |
|
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 | }());
|
89 | exports.UniqueMetadata = UniqueMetadata;
|
90 |
|
91 |
|