@@ -24,6 +24,7 @@ import { isRelationFieldMetadataType } from 'src/engine/utils/is-relation-field-
24
24
import { isFunctionDefaultValue } from 'src/engine/metadata-modules/field-metadata/utils/is-function-default-value.util' ;
25
25
import { FieldMetadataDefaultValueFunctionNames } from 'src/engine/metadata-modules/field-metadata/dtos/default-value.input' ;
26
26
import { compositeTypeDefintions } from 'src/engine/metadata-modules/field-metadata/composite-types' ;
27
+ import { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util' ;
27
28
28
29
@Injectable ( )
29
30
export class DatabaseStructureService {
@@ -74,9 +75,13 @@ export class DatabaseStructureService {
74
75
c.table_schema AS "tableSchema",
75
76
c.table_name AS "tableName",
76
77
c.column_name AS "columnName",
77
- CASE
78
- WHEN (c.data_type = 'USER-DEFINED') THEN c.udt_name
79
- ELSE data_type
78
+ CASE
79
+ WHEN c.data_type = 'ARRAY' THEN
80
+ (SELECT typname FROM pg_type WHERE oid = t.typelem)
81
+ WHEN c.data_type = 'USER-DEFINED' THEN
82
+ c.udt_name
83
+ ELSE
84
+ c.data_type
80
85
END AS "dataType",
81
86
c.is_nullable AS "isNullable",
82
87
c.column_default AS "columnDefault",
@@ -92,10 +97,15 @@ export class DatabaseStructureService {
92
97
WHEN uc.column_name IS NOT NULL THEN 'TRUE'
93
98
ELSE 'FALSE'
94
99
END AS "isUnique",
100
+ CASE
101
+ WHEN c.data_type = 'ARRAY' THEN 'TRUE'
102
+ ELSE 'FALSE'
103
+ END AS "isArray",
95
104
rc.update_rule AS "onUpdateAction",
96
105
rc.delete_rule AS "onDeleteAction"
97
106
FROM
98
107
information_schema.columns AS c
108
+ LEFT JOIN pg_type t ON t.typname = c.udt_name
99
109
LEFT JOIN
100
110
information_schema.constraint_column_usage AS ccu
101
111
ON c.column_name = ccu.column_name
@@ -132,10 +142,12 @@ export class DatabaseStructureService {
132
142
133
143
return results . map ( ( item ) => ( {
134
144
...item ,
145
+ dataType : item . isArray === 'TRUE' ? `${ item . dataType } []` : item . dataType ,
135
146
isNullable : item . isNullable === 'YES' ,
136
147
isPrimaryKey : item . isPrimaryKey === 'TRUE' ,
137
148
isForeignKey : item . isForeignKey === 'TRUE' ,
138
149
isUnique : item . isUnique === 'TRUE' ,
150
+ isArray : item . isArray === 'TRUE' ,
139
151
} ) ) ;
140
152
}
141
153
@@ -160,14 +172,18 @@ export class DatabaseStructureService {
160
172
getPostgresDataTypes ( fieldMetadata : FieldMetadataEntity ) : string [ ] {
161
173
const mainDataSource = this . typeORMService . getMainDataSource ( ) ;
162
174
163
- const normalizer = ( type : FieldMetadataType , columnName : string ) => {
175
+ const normalizer = (
176
+ type : FieldMetadataType ,
177
+ isArray : boolean | undefined ,
178
+ columnName : string ,
179
+ ) => {
164
180
const typeORMType = fieldMetadataTypeToColumnType ( type ) ;
165
181
166
182
// Compute enum name to compare data type properly
167
183
if ( typeORMType === 'enum' ) {
168
- const objectName = fieldMetadata . object ?. nameSingular ;
184
+ const objectName = computeObjectTargetTable ( fieldMetadata . object ) ;
169
185
170
- return `${ objectName } _${ columnName } _enum` ;
186
+ return `${ objectName } _${ columnName } _enum${ isArray ? '[]' : '' } ` ;
171
187
}
172
188
173
189
return mainDataSource . driver . normalizeType ( {
@@ -185,11 +201,21 @@ export class DatabaseStructureService {
185
201
}
186
202
187
203
return compositeType . properties . map ( ( compositeProperty ) =>
188
- normalizer ( compositeProperty . type , compositeProperty . name ) ,
204
+ normalizer (
205
+ compositeProperty . type ,
206
+ compositeProperty . isArray ,
207
+ compositeProperty . name ,
208
+ ) ,
189
209
) ;
190
210
}
191
211
192
- return [ normalizer ( fieldMetadata . type , fieldMetadata . name ) ] ;
212
+ return [
213
+ normalizer (
214
+ fieldMetadata . type ,
215
+ fieldMetadata . type === FieldMetadataType . MULTI_SELECT ,
216
+ fieldMetadata . name ,
217
+ ) ,
218
+ ] ;
193
219
}
194
220
195
221
getFieldMetadataTypeFromPostgresDataType (
0 commit comments