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