@payloadcms/drizzle 3.15.2-canary.ee31a64 → 3.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +1 -1
- package/dist/postgres/createMigration.d.ts +3 -0
- package/dist/postgres/createMigration.d.ts.map +1 -0
- package/dist/postgres/createMigration.js +91 -0
- package/dist/postgres/createMigration.js.map +1 -0
- package/dist/postgres/getMigrationTemplate.d.ts +4 -0
- package/dist/postgres/getMigrationTemplate.d.ts.map +1 -0
- package/dist/postgres/getMigrationTemplate.js +13 -0
- package/dist/postgres/getMigrationTemplate.js.map +1 -0
- package/dist/postgres/schema/build.d.ts +42 -0
- package/dist/postgres/schema/build.d.ts.map +1 -0
- package/dist/postgres/schema/build.js +404 -0
- package/dist/postgres/schema/build.js.map +1 -0
- package/dist/postgres/schema/createIndex.d.ts +11 -0
- package/dist/postgres/schema/createIndex.d.ts.map +1 -0
- package/dist/postgres/schema/createIndex.js +20 -0
- package/dist/postgres/schema/createIndex.js.map +1 -0
- package/dist/postgres/schema/idToUUID.d.ts +3 -0
- package/dist/postgres/schema/idToUUID.d.ts.map +1 -0
- package/dist/postgres/schema/idToUUID.js +11 -0
- package/dist/postgres/schema/idToUUID.js.map +1 -0
- package/dist/postgres/schema/parentIDColumnMap.d.ts +4 -0
- package/dist/postgres/schema/parentIDColumnMap.d.ts.map +1 -0
- package/dist/postgres/schema/parentIDColumnMap.js +9 -0
- package/dist/postgres/schema/parentIDColumnMap.js.map +1 -0
- package/dist/postgres/schema/traverseFields.d.ts +42 -0
- package/dist/postgres/schema/traverseFields.d.ts.map +1 -0
- package/dist/postgres/schema/traverseFields.js +599 -0
- package/dist/postgres/schema/traverseFields.js.map +1 -0
- package/dist/postgres/schema/withDefault.d.ts +4 -0
- package/dist/postgres/schema/withDefault.d.ts.map +1 -0
- package/dist/postgres/schema/withDefault.js +15 -0
- package/dist/postgres/schema/withDefault.js.map +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
import { relations } from 'drizzle-orm';
|
|
2
|
+
import { boolean, foreignKey, index, integer, jsonb, numeric, PgNumericBuilder, PgUUIDBuilder, PgVarcharBuilder, text, timestamp, varchar } from 'drizzle-orm/pg-core';
|
|
3
|
+
import { InvalidConfiguration } from 'payload';
|
|
4
|
+
import { fieldAffectsData, fieldIsVirtual, optionIsObject } from 'payload/shared';
|
|
5
|
+
import toSnakeCase from 'to-snake-case';
|
|
6
|
+
import { createTableName } from '../../createTableName.js';
|
|
7
|
+
import { buildIndexName } from '../../utilities/buildIndexName.js';
|
|
8
|
+
import { hasLocalesTable } from '../../utilities/hasLocalesTable.js';
|
|
9
|
+
import { validateExistingBlockIsIdentical } from '../../utilities/validateExistingBlockIsIdentical.js';
|
|
10
|
+
import { buildTable } from './build.js';
|
|
11
|
+
import { createIndex } from './createIndex.js';
|
|
12
|
+
import { geometryColumn } from './geometryColumn.js';
|
|
13
|
+
import { idToUUID } from './idToUUID.js';
|
|
14
|
+
import { parentIDColumnMap } from './parentIDColumnMap.js';
|
|
15
|
+
import { withDefault } from './withDefault.js';
|
|
16
|
+
export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull, disableRelsTableUnique, disableUnique = false, fieldPrefix, fields, forceLocalized, indexes, localesColumns, localesIndexes, newTableName, parentTableName, relationships, relationsToBuild, rootRelationsToBuild, rootTableIDColType, rootTableName, uniqueRelationships, versions, withinLocalizedArrayOrBlock })=>{
|
|
17
|
+
const throwValidationError = true;
|
|
18
|
+
let hasLocalizedField = false;
|
|
19
|
+
let hasLocalizedRelationshipField = false;
|
|
20
|
+
let hasManyTextField = false;
|
|
21
|
+
let hasLocalizedManyTextField = false;
|
|
22
|
+
let hasManyNumberField = false;
|
|
23
|
+
let hasLocalizedManyNumberField = false;
|
|
24
|
+
let parentIDColType = 'integer';
|
|
25
|
+
if (columns.id instanceof PgUUIDBuilder) {
|
|
26
|
+
parentIDColType = 'uuid';
|
|
27
|
+
}
|
|
28
|
+
if (columns.id instanceof PgNumericBuilder) {
|
|
29
|
+
parentIDColType = 'numeric';
|
|
30
|
+
}
|
|
31
|
+
if (columns.id instanceof PgVarcharBuilder) {
|
|
32
|
+
parentIDColType = 'varchar';
|
|
33
|
+
}
|
|
34
|
+
fields.forEach((field)=>{
|
|
35
|
+
if ('name' in field && field.name === 'id') {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (fieldIsVirtual(field)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
let targetTable = columns;
|
|
42
|
+
let targetIndexes = indexes;
|
|
43
|
+
const columnName = `${columnPrefix || ''}${field.name[0] === '_' ? '_' : ''}${toSnakeCase(field.name)}`;
|
|
44
|
+
const fieldName = `${fieldPrefix?.replace('.', '_') || ''}${field.name}`;
|
|
45
|
+
// If field is localized,
|
|
46
|
+
// add the column to the locale table instead of main table
|
|
47
|
+
if (adapter.payload.config.localization && (field.localized || forceLocalized) && field.type !== 'array' && field.type !== 'blocks' && ('hasMany' in field && field.hasMany !== true || !('hasMany' in field))) {
|
|
48
|
+
hasLocalizedField = true;
|
|
49
|
+
targetTable = localesColumns;
|
|
50
|
+
targetIndexes = localesIndexes;
|
|
51
|
+
}
|
|
52
|
+
if ((field.unique || field.index || [
|
|
53
|
+
'relationship',
|
|
54
|
+
'upload'
|
|
55
|
+
].includes(field.type)) && ![
|
|
56
|
+
'array',
|
|
57
|
+
'blocks',
|
|
58
|
+
'group'
|
|
59
|
+
].includes(field.type) && !('hasMany' in field && field.hasMany === true) && !('relationTo' in field && Array.isArray(field.relationTo))) {
|
|
60
|
+
const unique = disableUnique !== true && field.unique;
|
|
61
|
+
if (unique) {
|
|
62
|
+
const constraintValue = `${fieldPrefix || ''}${field.name}`;
|
|
63
|
+
if (!adapter.fieldConstraints?.[rootTableName]) {
|
|
64
|
+
adapter.fieldConstraints[rootTableName] = {};
|
|
65
|
+
}
|
|
66
|
+
adapter.fieldConstraints[rootTableName][`${columnName}_idx`] = constraintValue;
|
|
67
|
+
}
|
|
68
|
+
const indexName = buildIndexName({
|
|
69
|
+
name: `${newTableName}_${columnName}`,
|
|
70
|
+
adapter
|
|
71
|
+
});
|
|
72
|
+
targetIndexes[indexName] = createIndex({
|
|
73
|
+
name: field.localized ? [
|
|
74
|
+
fieldName,
|
|
75
|
+
'_locale'
|
|
76
|
+
] : fieldName,
|
|
77
|
+
indexName,
|
|
78
|
+
unique
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
switch(field.type){
|
|
82
|
+
case 'array':
|
|
83
|
+
{
|
|
84
|
+
const disableNotNullFromHere = Boolean(field.admin?.condition) || disableNotNull;
|
|
85
|
+
const arrayTableName = createTableName({
|
|
86
|
+
adapter,
|
|
87
|
+
config: field,
|
|
88
|
+
parentTableName: newTableName,
|
|
89
|
+
prefix: `${newTableName}_`,
|
|
90
|
+
throwValidationError,
|
|
91
|
+
versionsCustomName: versions
|
|
92
|
+
});
|
|
93
|
+
const baseColumns = {
|
|
94
|
+
_order: integer('_order').notNull(),
|
|
95
|
+
_parentID: parentIDColumnMap[parentIDColType]('_parent_id').notNull()
|
|
96
|
+
};
|
|
97
|
+
const baseExtraConfig = {
|
|
98
|
+
_orderIdx: (cols)=>index(`${arrayTableName}_order_idx`).on(cols._order),
|
|
99
|
+
_parentIDFk: (cols)=>foreignKey({
|
|
100
|
+
name: `${arrayTableName}_parent_id_fk`,
|
|
101
|
+
columns: [
|
|
102
|
+
cols['_parentID']
|
|
103
|
+
],
|
|
104
|
+
foreignColumns: [
|
|
105
|
+
adapter.tables[parentTableName].id
|
|
106
|
+
]
|
|
107
|
+
}).onDelete('cascade'),
|
|
108
|
+
_parentIDIdx: (cols)=>index(`${arrayTableName}_parent_id_idx`).on(cols._parentID)
|
|
109
|
+
};
|
|
110
|
+
const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock || forceLocalized;
|
|
111
|
+
if (isLocalized) {
|
|
112
|
+
baseColumns._locale = adapter.enums.enum__locales('_locale').notNull();
|
|
113
|
+
baseExtraConfig._localeIdx = (cols)=>index(`${arrayTableName}_locale_idx`).on(cols._locale);
|
|
114
|
+
}
|
|
115
|
+
const { hasLocalizedManyNumberField: subHasLocalizedManyNumberField, hasLocalizedManyTextField: subHasLocalizedManyTextField, hasLocalizedRelationshipField: subHasLocalizedRelationshipField, hasManyNumberField: subHasManyNumberField, hasManyTextField: subHasManyTextField, relationsToBuild: subRelationsToBuild } = buildTable({
|
|
116
|
+
adapter,
|
|
117
|
+
baseColumns,
|
|
118
|
+
baseExtraConfig,
|
|
119
|
+
disableNotNull: disableNotNullFromHere,
|
|
120
|
+
disableRelsTableUnique: true,
|
|
121
|
+
disableUnique,
|
|
122
|
+
fields: disableUnique ? idToUUID(field.flattenedFields) : field.flattenedFields,
|
|
123
|
+
rootRelationships: relationships,
|
|
124
|
+
rootRelationsToBuild,
|
|
125
|
+
rootTableIDColType,
|
|
126
|
+
rootTableName,
|
|
127
|
+
rootUniqueRelationships: uniqueRelationships,
|
|
128
|
+
tableName: arrayTableName,
|
|
129
|
+
versions,
|
|
130
|
+
withinLocalizedArrayOrBlock: isLocalized
|
|
131
|
+
});
|
|
132
|
+
if (subHasLocalizedManyNumberField) {
|
|
133
|
+
hasLocalizedManyNumberField = subHasLocalizedManyNumberField;
|
|
134
|
+
}
|
|
135
|
+
if (subHasLocalizedRelationshipField) {
|
|
136
|
+
hasLocalizedRelationshipField = subHasLocalizedRelationshipField;
|
|
137
|
+
}
|
|
138
|
+
if (subHasLocalizedManyTextField) {
|
|
139
|
+
hasLocalizedManyTextField = subHasLocalizedManyTextField;
|
|
140
|
+
}
|
|
141
|
+
if (subHasManyTextField) {
|
|
142
|
+
if (!hasManyTextField || subHasManyTextField === 'index') {
|
|
143
|
+
hasManyTextField = subHasManyTextField;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (subHasManyNumberField) {
|
|
147
|
+
if (!hasManyNumberField || subHasManyNumberField === 'index') {
|
|
148
|
+
hasManyNumberField = subHasManyNumberField;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
relationsToBuild.set(fieldName, {
|
|
152
|
+
type: 'many',
|
|
153
|
+
// arrays have their own localized table, independent of the base table.
|
|
154
|
+
localized: false,
|
|
155
|
+
target: arrayTableName
|
|
156
|
+
});
|
|
157
|
+
adapter.relations[`relations_${arrayTableName}`] = relations(adapter.tables[arrayTableName], ({ many, one })=>{
|
|
158
|
+
const result = {
|
|
159
|
+
_parentID: one(adapter.tables[parentTableName], {
|
|
160
|
+
fields: [
|
|
161
|
+
adapter.tables[arrayTableName]._parentID
|
|
162
|
+
],
|
|
163
|
+
references: [
|
|
164
|
+
adapter.tables[parentTableName].id
|
|
165
|
+
],
|
|
166
|
+
relationName: fieldName
|
|
167
|
+
})
|
|
168
|
+
};
|
|
169
|
+
if (hasLocalesTable(field.fields)) {
|
|
170
|
+
result._locales = many(adapter.tables[`${arrayTableName}${adapter.localesSuffix}`], {
|
|
171
|
+
relationName: '_locales'
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
subRelationsToBuild.forEach(({ type, localized, target }, key)=>{
|
|
175
|
+
if (type === 'one') {
|
|
176
|
+
const arrayWithLocalized = localized ? `${arrayTableName}${adapter.localesSuffix}` : arrayTableName;
|
|
177
|
+
result[key] = one(adapter.tables[target], {
|
|
178
|
+
fields: [
|
|
179
|
+
adapter.tables[arrayWithLocalized][key]
|
|
180
|
+
],
|
|
181
|
+
references: [
|
|
182
|
+
adapter.tables[target].id
|
|
183
|
+
],
|
|
184
|
+
relationName: key
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
if (type === 'many') {
|
|
188
|
+
result[key] = many(adapter.tables[target], {
|
|
189
|
+
relationName: key
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
return result;
|
|
194
|
+
});
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
case 'blocks':
|
|
198
|
+
{
|
|
199
|
+
const disableNotNullFromHere = Boolean(field.admin?.condition) || disableNotNull;
|
|
200
|
+
field.blocks.forEach((block)=>{
|
|
201
|
+
const blockTableName = createTableName({
|
|
202
|
+
adapter,
|
|
203
|
+
config: block,
|
|
204
|
+
parentTableName: rootTableName,
|
|
205
|
+
prefix: `${rootTableName}_blocks_`,
|
|
206
|
+
throwValidationError,
|
|
207
|
+
versionsCustomName: versions
|
|
208
|
+
});
|
|
209
|
+
if (!adapter.tables[blockTableName]) {
|
|
210
|
+
const baseColumns = {
|
|
211
|
+
_order: integer('_order').notNull(),
|
|
212
|
+
_parentID: parentIDColumnMap[rootTableIDColType]('_parent_id').notNull(),
|
|
213
|
+
_path: text('_path').notNull()
|
|
214
|
+
};
|
|
215
|
+
const baseExtraConfig = {
|
|
216
|
+
_orderIdx: (cols)=>index(`${blockTableName}_order_idx`).on(cols._order),
|
|
217
|
+
_parentIdFk: (cols)=>foreignKey({
|
|
218
|
+
name: `${blockTableName}_parent_id_fk`,
|
|
219
|
+
columns: [
|
|
220
|
+
cols._parentID
|
|
221
|
+
],
|
|
222
|
+
foreignColumns: [
|
|
223
|
+
adapter.tables[rootTableName].id
|
|
224
|
+
]
|
|
225
|
+
}).onDelete('cascade'),
|
|
226
|
+
_parentIDIdx: (cols)=>index(`${blockTableName}_parent_id_idx`).on(cols._parentID),
|
|
227
|
+
_pathIdx: (cols)=>index(`${blockTableName}_path_idx`).on(cols._path)
|
|
228
|
+
};
|
|
229
|
+
const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock || forceLocalized;
|
|
230
|
+
if (isLocalized) {
|
|
231
|
+
baseColumns._locale = adapter.enums.enum__locales('_locale').notNull();
|
|
232
|
+
baseExtraConfig._localeIdx = (cols)=>index(`${blockTableName}_locale_idx`).on(cols._locale);
|
|
233
|
+
}
|
|
234
|
+
const { hasLocalizedManyNumberField: subHasLocalizedManyNumberField, hasLocalizedManyTextField: subHasLocalizedManyTextField, hasLocalizedRelationshipField: subHasLocalizedRelationshipField, hasManyNumberField: subHasManyNumberField, hasManyTextField: subHasManyTextField, relationsToBuild: subRelationsToBuild } = buildTable({
|
|
235
|
+
adapter,
|
|
236
|
+
baseColumns,
|
|
237
|
+
baseExtraConfig,
|
|
238
|
+
disableNotNull: disableNotNullFromHere,
|
|
239
|
+
disableRelsTableUnique: true,
|
|
240
|
+
disableUnique,
|
|
241
|
+
fields: disableUnique ? idToUUID(block.flattenedFields) : block.flattenedFields,
|
|
242
|
+
rootRelationships: relationships,
|
|
243
|
+
rootRelationsToBuild,
|
|
244
|
+
rootTableIDColType,
|
|
245
|
+
rootTableName,
|
|
246
|
+
rootUniqueRelationships: uniqueRelationships,
|
|
247
|
+
tableName: blockTableName,
|
|
248
|
+
versions,
|
|
249
|
+
withinLocalizedArrayOrBlock: isLocalized
|
|
250
|
+
});
|
|
251
|
+
if (subHasLocalizedManyNumberField) {
|
|
252
|
+
hasLocalizedManyNumberField = subHasLocalizedManyNumberField;
|
|
253
|
+
}
|
|
254
|
+
if (subHasLocalizedRelationshipField) {
|
|
255
|
+
hasLocalizedRelationshipField = subHasLocalizedRelationshipField;
|
|
256
|
+
}
|
|
257
|
+
if (subHasLocalizedManyTextField) {
|
|
258
|
+
hasLocalizedManyTextField = subHasLocalizedManyTextField;
|
|
259
|
+
}
|
|
260
|
+
if (subHasManyTextField) {
|
|
261
|
+
if (!hasManyTextField || subHasManyTextField === 'index') {
|
|
262
|
+
hasManyTextField = subHasManyTextField;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (subHasManyNumberField) {
|
|
266
|
+
if (!hasManyNumberField || subHasManyNumberField === 'index') {
|
|
267
|
+
hasManyNumberField = subHasManyNumberField;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
adapter.relations[`relations_${blockTableName}`] = relations(adapter.tables[blockTableName], ({ many, one })=>{
|
|
271
|
+
const result = {
|
|
272
|
+
_parentID: one(adapter.tables[rootTableName], {
|
|
273
|
+
fields: [
|
|
274
|
+
adapter.tables[blockTableName]._parentID
|
|
275
|
+
],
|
|
276
|
+
references: [
|
|
277
|
+
adapter.tables[rootTableName].id
|
|
278
|
+
],
|
|
279
|
+
relationName: `_blocks_${block.slug}`
|
|
280
|
+
})
|
|
281
|
+
};
|
|
282
|
+
if (hasLocalesTable(block.fields)) {
|
|
283
|
+
result._locales = many(adapter.tables[`${blockTableName}${adapter.localesSuffix}`], {
|
|
284
|
+
relationName: '_locales'
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
subRelationsToBuild.forEach(({ type, localized, target }, key)=>{
|
|
288
|
+
if (type === 'one') {
|
|
289
|
+
const blockWithLocalized = localized ? `${blockTableName}${adapter.localesSuffix}` : blockTableName;
|
|
290
|
+
result[key] = one(adapter.tables[target], {
|
|
291
|
+
fields: [
|
|
292
|
+
adapter.tables[blockWithLocalized][key]
|
|
293
|
+
],
|
|
294
|
+
references: [
|
|
295
|
+
adapter.tables[target].id
|
|
296
|
+
],
|
|
297
|
+
relationName: key
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
if (type === 'many') {
|
|
301
|
+
result[key] = many(adapter.tables[target], {
|
|
302
|
+
relationName: key
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
return result;
|
|
307
|
+
});
|
|
308
|
+
} else if (process.env.NODE_ENV !== 'production' && !versions) {
|
|
309
|
+
validateExistingBlockIsIdentical({
|
|
310
|
+
block,
|
|
311
|
+
localized: field.localized,
|
|
312
|
+
rootTableName,
|
|
313
|
+
table: adapter.tables[blockTableName],
|
|
314
|
+
tableLocales: adapter.tables[`${blockTableName}${adapter.localesSuffix}`]
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
// blocks relationships are defined from the collection or globals table down to the block, bypassing any subBlocks
|
|
318
|
+
rootRelationsToBuild.set(`_blocks_${block.slug}`, {
|
|
319
|
+
type: 'many',
|
|
320
|
+
// blocks are not localized on the parent table
|
|
321
|
+
localized: false,
|
|
322
|
+
target: blockTableName
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
case 'checkbox':
|
|
328
|
+
{
|
|
329
|
+
targetTable[fieldName] = withDefault(boolean(columnName), field);
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
case 'code':
|
|
333
|
+
case 'email':
|
|
334
|
+
case 'textarea':
|
|
335
|
+
{
|
|
336
|
+
targetTable[fieldName] = withDefault(varchar(columnName), field);
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
case 'date':
|
|
340
|
+
{
|
|
341
|
+
targetTable[fieldName] = withDefault(timestamp(columnName, {
|
|
342
|
+
mode: 'string',
|
|
343
|
+
precision: 3,
|
|
344
|
+
withTimezone: true
|
|
345
|
+
}), field);
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
case 'group':
|
|
349
|
+
case 'tab':
|
|
350
|
+
{
|
|
351
|
+
const disableNotNullFromHere = Boolean(field.admin?.condition) || disableNotNull;
|
|
352
|
+
const { hasLocalizedField: groupHasLocalizedField, hasLocalizedManyNumberField: groupHasLocalizedManyNumberField, hasLocalizedManyTextField: groupHasLocalizedManyTextField, hasLocalizedRelationshipField: groupHasLocalizedRelationshipField, hasManyNumberField: groupHasManyNumberField, hasManyTextField: groupHasManyTextField } = traverseFields({
|
|
353
|
+
adapter,
|
|
354
|
+
columnPrefix: `${columnName}_`,
|
|
355
|
+
columns,
|
|
356
|
+
disableNotNull: disableNotNullFromHere,
|
|
357
|
+
disableUnique,
|
|
358
|
+
fieldPrefix: `${fieldName}.`,
|
|
359
|
+
fields: field.flattenedFields,
|
|
360
|
+
forceLocalized: field.localized,
|
|
361
|
+
indexes,
|
|
362
|
+
localesColumns,
|
|
363
|
+
localesIndexes,
|
|
364
|
+
newTableName: `${parentTableName}_${columnName}`,
|
|
365
|
+
parentTableName,
|
|
366
|
+
relationships,
|
|
367
|
+
relationsToBuild,
|
|
368
|
+
rootRelationsToBuild,
|
|
369
|
+
rootTableIDColType,
|
|
370
|
+
rootTableName,
|
|
371
|
+
uniqueRelationships,
|
|
372
|
+
versions,
|
|
373
|
+
withinLocalizedArrayOrBlock: withinLocalizedArrayOrBlock || field.localized
|
|
374
|
+
});
|
|
375
|
+
if (groupHasLocalizedField) {
|
|
376
|
+
hasLocalizedField = true;
|
|
377
|
+
}
|
|
378
|
+
if (groupHasLocalizedRelationshipField) {
|
|
379
|
+
hasLocalizedRelationshipField = true;
|
|
380
|
+
}
|
|
381
|
+
if (groupHasManyTextField) {
|
|
382
|
+
hasManyTextField = true;
|
|
383
|
+
}
|
|
384
|
+
if (groupHasLocalizedManyTextField) {
|
|
385
|
+
hasLocalizedManyTextField = true;
|
|
386
|
+
}
|
|
387
|
+
if (groupHasManyNumberField) {
|
|
388
|
+
hasManyNumberField = true;
|
|
389
|
+
}
|
|
390
|
+
if (groupHasLocalizedManyNumberField) {
|
|
391
|
+
hasLocalizedManyNumberField = true;
|
|
392
|
+
}
|
|
393
|
+
break;
|
|
394
|
+
}
|
|
395
|
+
case 'json':
|
|
396
|
+
case 'richText':
|
|
397
|
+
{
|
|
398
|
+
targetTable[fieldName] = withDefault(jsonb(columnName), field);
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
case 'number':
|
|
402
|
+
{
|
|
403
|
+
if (field.hasMany) {
|
|
404
|
+
const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock || forceLocalized;
|
|
405
|
+
if (isLocalized) {
|
|
406
|
+
hasLocalizedManyNumberField = true;
|
|
407
|
+
}
|
|
408
|
+
if (field.index) {
|
|
409
|
+
hasManyNumberField = 'index';
|
|
410
|
+
} else if (!hasManyNumberField) {
|
|
411
|
+
hasManyNumberField = true;
|
|
412
|
+
}
|
|
413
|
+
if (field.unique) {
|
|
414
|
+
throw new InvalidConfiguration('Unique is not supported in Postgres for hasMany number fields.');
|
|
415
|
+
}
|
|
416
|
+
} else {
|
|
417
|
+
targetTable[fieldName] = withDefault(numeric(columnName), field);
|
|
418
|
+
}
|
|
419
|
+
break;
|
|
420
|
+
}
|
|
421
|
+
case 'point':
|
|
422
|
+
{
|
|
423
|
+
targetTable[fieldName] = withDefault(geometryColumn(columnName), field);
|
|
424
|
+
if (!adapter.extensions.postgis) {
|
|
425
|
+
adapter.extensions.postgis = true;
|
|
426
|
+
}
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
case 'radio':
|
|
430
|
+
case 'select':
|
|
431
|
+
{
|
|
432
|
+
const enumName = createTableName({
|
|
433
|
+
adapter,
|
|
434
|
+
config: field,
|
|
435
|
+
parentTableName: newTableName,
|
|
436
|
+
prefix: `enum_${newTableName}_`,
|
|
437
|
+
target: 'enumName',
|
|
438
|
+
throwValidationError
|
|
439
|
+
});
|
|
440
|
+
adapter.enums[enumName] = adapter.pgSchema.enum(enumName, field.options.map((option)=>{
|
|
441
|
+
if (optionIsObject(option)) {
|
|
442
|
+
return option.value;
|
|
443
|
+
}
|
|
444
|
+
return option;
|
|
445
|
+
}));
|
|
446
|
+
if (field.type === 'select' && field.hasMany) {
|
|
447
|
+
const selectTableName = createTableName({
|
|
448
|
+
adapter,
|
|
449
|
+
config: field,
|
|
450
|
+
parentTableName: newTableName,
|
|
451
|
+
prefix: `${newTableName}_`,
|
|
452
|
+
throwValidationError,
|
|
453
|
+
versionsCustomName: versions
|
|
454
|
+
});
|
|
455
|
+
const baseColumns = {
|
|
456
|
+
order: integer('order').notNull(),
|
|
457
|
+
parent: parentIDColumnMap[parentIDColType]('parent_id').notNull(),
|
|
458
|
+
value: adapter.enums[enumName]('value')
|
|
459
|
+
};
|
|
460
|
+
const baseExtraConfig = {
|
|
461
|
+
orderIdx: (cols)=>index(`${selectTableName}_order_idx`).on(cols.order),
|
|
462
|
+
parentFk: (cols)=>foreignKey({
|
|
463
|
+
name: `${selectTableName}_parent_fk`,
|
|
464
|
+
columns: [
|
|
465
|
+
cols.parent
|
|
466
|
+
],
|
|
467
|
+
foreignColumns: [
|
|
468
|
+
adapter.tables[parentTableName].id
|
|
469
|
+
]
|
|
470
|
+
}).onDelete('cascade'),
|
|
471
|
+
parentIdx: (cols)=>index(`${selectTableName}_parent_idx`).on(cols.parent)
|
|
472
|
+
};
|
|
473
|
+
const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock || forceLocalized;
|
|
474
|
+
if (isLocalized) {
|
|
475
|
+
baseColumns.locale = adapter.enums.enum__locales('locale').notNull();
|
|
476
|
+
baseExtraConfig.localeIdx = (cols)=>index(`${selectTableName}_locale_idx`).on(cols.locale);
|
|
477
|
+
}
|
|
478
|
+
if (field.index) {
|
|
479
|
+
baseExtraConfig.value = (cols)=>index(`${selectTableName}_value_idx`).on(cols.value);
|
|
480
|
+
}
|
|
481
|
+
buildTable({
|
|
482
|
+
adapter,
|
|
483
|
+
baseColumns,
|
|
484
|
+
baseExtraConfig,
|
|
485
|
+
disableNotNull,
|
|
486
|
+
disableUnique,
|
|
487
|
+
fields: [],
|
|
488
|
+
rootTableName,
|
|
489
|
+
tableName: selectTableName,
|
|
490
|
+
versions
|
|
491
|
+
});
|
|
492
|
+
relationsToBuild.set(fieldName, {
|
|
493
|
+
type: 'many',
|
|
494
|
+
// selects have their own localized table, independent of the base table.
|
|
495
|
+
localized: false,
|
|
496
|
+
target: selectTableName
|
|
497
|
+
});
|
|
498
|
+
adapter.relations[`relations_${selectTableName}`] = relations(adapter.tables[selectTableName], ({ one })=>({
|
|
499
|
+
parent: one(adapter.tables[parentTableName], {
|
|
500
|
+
fields: [
|
|
501
|
+
adapter.tables[selectTableName].parent
|
|
502
|
+
],
|
|
503
|
+
references: [
|
|
504
|
+
adapter.tables[parentTableName].id
|
|
505
|
+
],
|
|
506
|
+
relationName: fieldName
|
|
507
|
+
})
|
|
508
|
+
}));
|
|
509
|
+
} else {
|
|
510
|
+
targetTable[fieldName] = withDefault(adapter.enums[enumName](columnName), field);
|
|
511
|
+
}
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
514
|
+
case 'relationship':
|
|
515
|
+
case 'upload':
|
|
516
|
+
if (Array.isArray(field.relationTo)) {
|
|
517
|
+
field.relationTo.forEach((relation)=>{
|
|
518
|
+
relationships.add(relation);
|
|
519
|
+
if (field.unique && !disableUnique && !disableRelsTableUnique) {
|
|
520
|
+
uniqueRelationships.add(relation);
|
|
521
|
+
}
|
|
522
|
+
});
|
|
523
|
+
} else if (field.hasMany) {
|
|
524
|
+
relationships.add(field.relationTo);
|
|
525
|
+
if (field.unique && !disableUnique && !disableRelsTableUnique) {
|
|
526
|
+
uniqueRelationships.add(field.relationTo);
|
|
527
|
+
}
|
|
528
|
+
} else {
|
|
529
|
+
// simple relationships get a column on the targetTable with a foreign key to the relationTo table
|
|
530
|
+
const relationshipConfig = adapter.payload.collections[field.relationTo].config;
|
|
531
|
+
const tableName = adapter.tableNameMap.get(toSnakeCase(field.relationTo));
|
|
532
|
+
// get the id type of the related collection
|
|
533
|
+
let colType = adapter.idType === 'uuid' ? 'uuid' : 'integer';
|
|
534
|
+
const relatedCollectionCustomID = relationshipConfig.fields.find((field)=>fieldAffectsData(field) && field.name === 'id');
|
|
535
|
+
if (relatedCollectionCustomID?.type === 'number') {
|
|
536
|
+
colType = 'numeric';
|
|
537
|
+
}
|
|
538
|
+
if (relatedCollectionCustomID?.type === 'text') {
|
|
539
|
+
colType = 'varchar';
|
|
540
|
+
}
|
|
541
|
+
// make the foreign key column for relationship using the correct id column type
|
|
542
|
+
targetTable[fieldName] = parentIDColumnMap[colType](`${columnName}_id`).references(()=>adapter.tables[tableName].id, {
|
|
543
|
+
onDelete: 'set null'
|
|
544
|
+
});
|
|
545
|
+
// add relationship to table
|
|
546
|
+
relationsToBuild.set(fieldName, {
|
|
547
|
+
type: 'one',
|
|
548
|
+
localized: adapter.payload.config.localization && (field.localized || forceLocalized),
|
|
549
|
+
target: tableName
|
|
550
|
+
});
|
|
551
|
+
// add notNull when not required
|
|
552
|
+
if (!disableNotNull && field.required && !field.admin?.condition) {
|
|
553
|
+
targetTable[fieldName].notNull();
|
|
554
|
+
}
|
|
555
|
+
break;
|
|
556
|
+
}
|
|
557
|
+
if (Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock) {
|
|
558
|
+
hasLocalizedRelationshipField = true;
|
|
559
|
+
}
|
|
560
|
+
break;
|
|
561
|
+
case 'text':
|
|
562
|
+
{
|
|
563
|
+
if (field.hasMany) {
|
|
564
|
+
const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock || forceLocalized;
|
|
565
|
+
if (isLocalized) {
|
|
566
|
+
hasLocalizedManyTextField = true;
|
|
567
|
+
}
|
|
568
|
+
if (field.index) {
|
|
569
|
+
hasManyTextField = 'index';
|
|
570
|
+
} else if (!hasManyTextField) {
|
|
571
|
+
hasManyTextField = true;
|
|
572
|
+
}
|
|
573
|
+
if (field.unique) {
|
|
574
|
+
throw new InvalidConfiguration('Unique is not supported in Postgres for hasMany text fields.');
|
|
575
|
+
}
|
|
576
|
+
} else {
|
|
577
|
+
targetTable[fieldName] = withDefault(varchar(columnName), field);
|
|
578
|
+
}
|
|
579
|
+
break;
|
|
580
|
+
}
|
|
581
|
+
default:
|
|
582
|
+
break;
|
|
583
|
+
}
|
|
584
|
+
const condition = field.admin && field.admin.condition;
|
|
585
|
+
if (!disableNotNull && targetTable[fieldName] && 'required' in field && field.required && !condition) {
|
|
586
|
+
targetTable[fieldName].notNull();
|
|
587
|
+
}
|
|
588
|
+
});
|
|
589
|
+
return {
|
|
590
|
+
hasLocalizedField,
|
|
591
|
+
hasLocalizedManyNumberField,
|
|
592
|
+
hasLocalizedManyTextField,
|
|
593
|
+
hasLocalizedRelationshipField,
|
|
594
|
+
hasManyNumberField,
|
|
595
|
+
hasManyTextField
|
|
596
|
+
};
|
|
597
|
+
};
|
|
598
|
+
|
|
599
|
+
//# sourceMappingURL=traverseFields.js.map
|