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