1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var MetadataArgsStorage_1 = require("../metadata-args/MetadataArgsStorage");
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var EntitySchemaTransformer = (function () {
|
9 | function EntitySchemaTransformer() {
|
10 | }
|
11 |
|
12 |
|
13 |
|
14 | |
15 |
|
16 |
|
17 | EntitySchemaTransformer.prototype.transform = function (schemas) {
|
18 | var metadataArgsStorage = new MetadataArgsStorage_1.MetadataArgsStorage();
|
19 | schemas.forEach(function (entitySchema) {
|
20 | var options = entitySchema.options;
|
21 |
|
22 | var tableMetadata = {
|
23 | target: options.target || options.name,
|
24 | name: options.tableName,
|
25 | database: options.database,
|
26 | schema: options.schema,
|
27 | type: options.type || "regular",
|
28 | orderBy: options.orderBy,
|
29 | synchronize: options.synchronize,
|
30 | expression: options.expression
|
31 | };
|
32 | metadataArgsStorage.tables.push(tableMetadata);
|
33 |
|
34 | Object.keys(options.columns).forEach(function (columnName) {
|
35 | var column = options.columns[columnName];
|
36 | var mode = "regular";
|
37 | if (column.createDate)
|
38 | mode = "createDate";
|
39 | if (column.updateDate)
|
40 | mode = "updateDate";
|
41 | if (column.version)
|
42 | mode = "version";
|
43 | if (column.treeChildrenCount)
|
44 | mode = "treeChildrenCount";
|
45 | if (column.treeLevel)
|
46 | mode = "treeLevel";
|
47 | if (column.objectId)
|
48 | mode = "objectId";
|
49 | var columnAgrs = {
|
50 | target: options.target || options.name,
|
51 | mode: mode,
|
52 | propertyName: columnName,
|
53 | options: {
|
54 | type: column.type,
|
55 | name: column.objectId ? "_id" : column.name,
|
56 | length: column.length,
|
57 | width: column.width,
|
58 | nullable: column.nullable,
|
59 | readonly: column.readonly,
|
60 | update: column.update,
|
61 | select: column.select,
|
62 | insert: column.insert,
|
63 | primary: column.primary,
|
64 | unique: column.unique,
|
65 | comment: column.comment,
|
66 | default: column.default,
|
67 | onUpdate: column.onUpdate,
|
68 | precision: column.precision,
|
69 | scale: column.scale,
|
70 | zerofill: column.zerofill,
|
71 | unsigned: column.unsigned,
|
72 | charset: column.charset,
|
73 | collation: column.collation,
|
74 | enum: column.enum,
|
75 | asExpression: column.asExpression,
|
76 | generatedType: column.generatedType,
|
77 | hstoreType: column.hstoreType,
|
78 | array: column.array,
|
79 | transformer: column.transformer,
|
80 | spatialFeatureType: column.spatialFeatureType,
|
81 | srid: column.srid
|
82 | }
|
83 | };
|
84 | metadataArgsStorage.columns.push(columnAgrs);
|
85 | if (column.generated) {
|
86 | var generationArgs = {
|
87 | target: options.target || options.name,
|
88 | propertyName: columnName,
|
89 | strategy: typeof column.generated === "string" ? column.generated : "increment"
|
90 | };
|
91 | metadataArgsStorage.generations.push(generationArgs);
|
92 | }
|
93 | if (column.unique)
|
94 | metadataArgsStorage.uniques.push({ target: options.target || options.name, columns: [columnName] });
|
95 | });
|
96 |
|
97 | if (options.relations) {
|
98 | Object.keys(options.relations).forEach(function (relationName) {
|
99 | var relationSchema = options.relations[relationName];
|
100 | var relation = {
|
101 | target: options.target || options.name,
|
102 | propertyName: relationName,
|
103 | relationType: relationSchema.type,
|
104 | isLazy: relationSchema.lazy || false,
|
105 | type: relationSchema.target,
|
106 | inverseSideProperty: relationSchema.inverseSide,
|
107 | isTreeParent: relationSchema.treeParent,
|
108 | isTreeChildren: relationSchema.treeChildren,
|
109 | options: {
|
110 | eager: relationSchema.eager || false,
|
111 | cascade: relationSchema.cascade,
|
112 | nullable: relationSchema.nullable,
|
113 | onDelete: relationSchema.onDelete,
|
114 | onUpdate: relationSchema.onUpdate,
|
115 | deferrable: relationSchema.deferrable,
|
116 | primary: relationSchema.primary,
|
117 | persistence: relationSchema.persistence
|
118 | }
|
119 | };
|
120 | metadataArgsStorage.relations.push(relation);
|
121 |
|
122 | if (relationSchema.joinColumn) {
|
123 | if (typeof relationSchema.joinColumn === "boolean") {
|
124 | var joinColumn = {
|
125 | target: options.target || options.name,
|
126 | propertyName: relationName
|
127 | };
|
128 | metadataArgsStorage.joinColumns.push(joinColumn);
|
129 | }
|
130 | else {
|
131 | var joinColumn = {
|
132 | target: options.target || options.name,
|
133 | propertyName: relationName,
|
134 | name: relationSchema.joinColumn.name,
|
135 | referencedColumnName: relationSchema.joinColumn.referencedColumnName
|
136 | };
|
137 | metadataArgsStorage.joinColumns.push(joinColumn);
|
138 | }
|
139 | }
|
140 |
|
141 | if (relationSchema.joinTable) {
|
142 | if (typeof relationSchema.joinTable === "boolean") {
|
143 | var joinTable = {
|
144 | target: options.target || options.name,
|
145 | propertyName: relationName
|
146 | };
|
147 | metadataArgsStorage.joinTables.push(joinTable);
|
148 | }
|
149 | else {
|
150 | var joinTable = {
|
151 | target: options.target || options.name,
|
152 | propertyName: relationName,
|
153 | name: relationSchema.joinTable.name,
|
154 | database: relationSchema.joinTable.database,
|
155 | schema: relationSchema.joinTable.schema,
|
156 | joinColumns: (relationSchema.joinTable.joinColumn ? [relationSchema.joinTable.joinColumn] : relationSchema.joinTable.joinColumns),
|
157 | inverseJoinColumns: (relationSchema.joinTable.inverseJoinColumn ? [relationSchema.joinTable.inverseJoinColumn] : relationSchema.joinTable.inverseJoinColumns),
|
158 | };
|
159 | metadataArgsStorage.joinTables.push(joinTable);
|
160 | }
|
161 | }
|
162 | });
|
163 | }
|
164 |
|
165 | if (options.indices) {
|
166 | options.indices.forEach(function (index) {
|
167 | var indexAgrs = {
|
168 | target: options.target || options.name,
|
169 | name: index.name,
|
170 | unique: index.unique === true ? true : false,
|
171 | spatial: index.spatial === true ? true : false,
|
172 | fulltext: index.fulltext === true ? true : false,
|
173 | synchronize: index.synchronize === false ? false : true,
|
174 | where: index.where,
|
175 | sparse: index.sparse,
|
176 | columns: index.columns
|
177 | };
|
178 | metadataArgsStorage.indices.push(indexAgrs);
|
179 | });
|
180 | }
|
181 |
|
182 | if (options.uniques) {
|
183 | options.uniques.forEach(function (unique) {
|
184 | var uniqueAgrs = {
|
185 | target: options.target || options.name,
|
186 | name: unique.name,
|
187 | columns: unique.columns
|
188 | };
|
189 | metadataArgsStorage.uniques.push(uniqueAgrs);
|
190 | });
|
191 | }
|
192 |
|
193 | if (options.checks) {
|
194 | options.checks.forEach(function (check) {
|
195 | var checkAgrs = {
|
196 | target: options.target || options.name,
|
197 | name: check.name,
|
198 | expression: check.expression
|
199 | };
|
200 | metadataArgsStorage.checks.push(checkAgrs);
|
201 | });
|
202 | }
|
203 |
|
204 | if (options.exclusions) {
|
205 | options.exclusions.forEach(function (exclusion) {
|
206 | var exclusionArgs = {
|
207 | target: options.target || options.name,
|
208 | name: exclusion.name,
|
209 | expression: exclusion.expression
|
210 | };
|
211 | metadataArgsStorage.exclusions.push(exclusionArgs);
|
212 | });
|
213 | }
|
214 | });
|
215 | return metadataArgsStorage;
|
216 | };
|
217 | return EntitySchemaTransformer;
|
218 | }());
|
219 | exports.EntitySchemaTransformer = EntitySchemaTransformer;
|
220 |
|
221 |
|