@payloadcms/db-sqlite 3.0.0-canary.9de3ffd → 3.0.0-canary.a14e066
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/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +10 -3
- package/dist/connect.js.map +1 -1
- package/dist/countDistinct.d.ts.map +1 -1
- package/dist/countDistinct.js +3 -3
- package/dist/countDistinct.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -3
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +24 -4
- package/dist/init.js.map +1 -1
- package/dist/schema/build.d.ts +15 -3
- package/dist/schema/build.d.ts.map +1 -1
- package/dist/schema/build.js +41 -10
- package/dist/schema/build.js.map +1 -1
- package/dist/schema/createIndex.d.ts.map +1 -1
- package/dist/schema/createIndex.js +3 -1
- package/dist/schema/createIndex.js.map +1 -1
- package/dist/schema/traverseFields.d.ts +11 -3
- package/dist/schema/traverseFields.d.ts.map +1 -1
- package/dist/schema/traverseFields.js +213 -67
- package/dist/schema/traverseFields.js.map +1 -1
- package/dist/schema/withDefault.d.ts.map +1 -1
- package/dist/schema/withDefault.js +3 -1
- package/dist/schema/withDefault.js.map +1 -1
- package/dist/types.d.ts +27 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +8 -6
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { createTableName, hasLocalesTable, validateExistingBlockIsIdentical } from '@payloadcms/drizzle';
|
|
2
2
|
import { relations } from 'drizzle-orm';
|
|
3
|
-
import {
|
|
3
|
+
import { foreignKey, index, integer, numeric, SQLiteIntegerBuilder, SQLiteNumericBuilder, SQLiteTextBuilder, text } from 'drizzle-orm/sqlite-core';
|
|
4
4
|
import { InvalidConfiguration } from 'payload';
|
|
5
|
-
import { fieldAffectsData, optionIsObject } from 'payload/shared';
|
|
5
|
+
import { fieldAffectsData, fieldIsVirtual, optionIsObject } from 'payload/shared';
|
|
6
6
|
import toSnakeCase from 'to-snake-case';
|
|
7
7
|
import { buildTable } from './build.js';
|
|
8
8
|
import { createIndex } from './createIndex.js';
|
|
9
9
|
import { getIDColumn } from './getIDColumn.js';
|
|
10
10
|
import { idToUUID } from './idToUUID.js';
|
|
11
11
|
import { withDefault } from './withDefault.js';
|
|
12
|
-
export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull, disableUnique = false, fieldPrefix, fields, forceLocalized, indexes, locales, localesColumns, localesIndexes, newTableName, parentTableName,
|
|
12
|
+
export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull, disableRelsTableUnique, disableUnique = false, fieldPrefix, fields, forceLocalized, indexes, joins, locales, localesColumns, localesIndexes, newTableName, parentTableName, relationships, relationsToBuild, rootRelationsToBuild, rootTableIDColType, rootTableName, uniqueRelationships, versions, withinLocalizedArrayOrBlock })=>{
|
|
13
13
|
let hasLocalizedField = false;
|
|
14
14
|
let hasLocalizedRelationshipField = false;
|
|
15
15
|
let hasManyTextField = false;
|
|
@@ -17,11 +17,22 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
17
17
|
let hasManyNumberField = false;
|
|
18
18
|
let hasLocalizedManyNumberField = false;
|
|
19
19
|
let parentIDColType = 'integer';
|
|
20
|
-
if (columns.id instanceof SQLiteIntegerBuilder)
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
if (columns.id instanceof SQLiteIntegerBuilder) {
|
|
21
|
+
parentIDColType = 'integer';
|
|
22
|
+
}
|
|
23
|
+
if (columns.id instanceof SQLiteNumericBuilder) {
|
|
24
|
+
parentIDColType = 'numeric';
|
|
25
|
+
}
|
|
26
|
+
if (columns.id instanceof SQLiteTextBuilder) {
|
|
27
|
+
parentIDColType = 'text';
|
|
28
|
+
}
|
|
23
29
|
fields.forEach((field)=>{
|
|
24
|
-
if ('name' in field && field.name === 'id')
|
|
30
|
+
if ('name' in field && field.name === 'id') {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (fieldIsVirtual(field)) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
25
36
|
let columnName;
|
|
26
37
|
let fieldName;
|
|
27
38
|
let targetTable = columns;
|
|
@@ -36,14 +47,15 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
36
47
|
targetTable = localesColumns;
|
|
37
48
|
targetIndexes = localesIndexes;
|
|
38
49
|
}
|
|
39
|
-
if ((field.unique || field.index
|
|
50
|
+
if ((field.unique || field.index || [
|
|
51
|
+
'relationship',
|
|
52
|
+
'upload'
|
|
53
|
+
].includes(field.type)) && ![
|
|
40
54
|
'array',
|
|
41
55
|
'blocks',
|
|
42
56
|
'group',
|
|
43
|
-
'point'
|
|
44
|
-
|
|
45
|
-
'upload'
|
|
46
|
-
].includes(field.type) && !('hasMany' in field && field.hasMany === true)) {
|
|
57
|
+
'point'
|
|
58
|
+
].includes(field.type) && !('hasMany' in field && field.hasMany === true) && !('relationTo' in field && Array.isArray(field.relationTo))) {
|
|
47
59
|
const unique = disableUnique !== true && field.unique;
|
|
48
60
|
if (unique) {
|
|
49
61
|
const constraintValue = `${fieldPrefix || ''}${field.name}`;
|
|
@@ -53,7 +65,10 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
53
65
|
adapter.fieldConstraints[rootTableName][`${columnName}_idx`] = constraintValue;
|
|
54
66
|
}
|
|
55
67
|
targetIndexes[`${newTableName}_${field.name}Idx`] = createIndex({
|
|
56
|
-
name:
|
|
68
|
+
name: field.localized ? [
|
|
69
|
+
fieldName,
|
|
70
|
+
'_locale'
|
|
71
|
+
] : fieldName,
|
|
57
72
|
columnName,
|
|
58
73
|
tableName: newTableName,
|
|
59
74
|
unique
|
|
@@ -64,7 +79,8 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
64
79
|
case 'text':
|
|
65
80
|
{
|
|
66
81
|
if (field.hasMany) {
|
|
67
|
-
|
|
82
|
+
const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock || forceLocalized;
|
|
83
|
+
if (isLocalized) {
|
|
68
84
|
hasLocalizedManyTextField = true;
|
|
69
85
|
}
|
|
70
86
|
if (field.index) {
|
|
@@ -90,7 +106,8 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
90
106
|
case 'number':
|
|
91
107
|
{
|
|
92
108
|
if (field.hasMany) {
|
|
93
|
-
|
|
109
|
+
const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock || forceLocalized;
|
|
110
|
+
if (isLocalized) {
|
|
94
111
|
hasLocalizedManyNumberField = true;
|
|
95
112
|
}
|
|
96
113
|
if (field.index) {
|
|
@@ -165,7 +182,8 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
165
182
|
}).onDelete('cascade'),
|
|
166
183
|
parentIdx: (cols)=>index(`${selectTableName}_parent_idx`).on(cols.parent)
|
|
167
184
|
};
|
|
168
|
-
|
|
185
|
+
const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock || forceLocalized;
|
|
186
|
+
if (isLocalized) {
|
|
169
187
|
baseColumns.locale = text('locale', {
|
|
170
188
|
enum: locales
|
|
171
189
|
}).notNull();
|
|
@@ -203,7 +221,7 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
203
221
|
})
|
|
204
222
|
}));
|
|
205
223
|
} else {
|
|
206
|
-
targetTable[fieldName] = withDefault(text(
|
|
224
|
+
targetTable[fieldName] = withDefault(text(columnName, {
|
|
207
225
|
enum: options
|
|
208
226
|
}), field);
|
|
209
227
|
}
|
|
@@ -248,31 +266,48 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
248
266
|
}).onDelete('cascade'),
|
|
249
267
|
_parentIDIdx: (cols)=>index(`${arrayTableName}_parent_id_idx`).on(cols._parentID)
|
|
250
268
|
};
|
|
251
|
-
|
|
269
|
+
const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock || forceLocalized;
|
|
270
|
+
if (isLocalized) {
|
|
252
271
|
baseColumns._locale = text('_locale', {
|
|
253
272
|
enum: locales
|
|
254
273
|
}).notNull();
|
|
255
274
|
baseExtraConfig._localeIdx = (cols)=>index(`${arrayTableName}_locale_idx`).on(cols._locale);
|
|
256
275
|
}
|
|
257
|
-
const { hasManyNumberField: subHasManyNumberField, hasManyTextField: subHasManyTextField, relationsToBuild: subRelationsToBuild } = buildTable({
|
|
276
|
+
const { hasLocalizedManyNumberField: subHasLocalizedManyNumberField, hasLocalizedManyTextField: subHasLocalizedManyTextField, hasLocalizedRelationshipField: subHasLocalizedRelationshipField, hasManyNumberField: subHasManyNumberField, hasManyTextField: subHasManyTextField, relationsToBuild: subRelationsToBuild } = buildTable({
|
|
258
277
|
adapter,
|
|
259
278
|
baseColumns,
|
|
260
279
|
baseExtraConfig,
|
|
261
280
|
disableNotNull: disableNotNullFromHere,
|
|
281
|
+
disableRelsTableUnique: true,
|
|
262
282
|
disableUnique,
|
|
263
283
|
fields: disableUnique ? idToUUID(field.fields) : field.fields,
|
|
264
|
-
rootRelationsToBuild,
|
|
265
284
|
rootRelationships: relationships,
|
|
285
|
+
rootRelationsToBuild,
|
|
266
286
|
rootTableIDColType,
|
|
267
287
|
rootTableName,
|
|
288
|
+
rootUniqueRelationships: uniqueRelationships,
|
|
268
289
|
tableName: arrayTableName,
|
|
269
|
-
versions
|
|
290
|
+
versions,
|
|
291
|
+
withinLocalizedArrayOrBlock: isLocalized
|
|
270
292
|
});
|
|
293
|
+
if (subHasLocalizedManyNumberField) {
|
|
294
|
+
hasLocalizedManyNumberField = subHasLocalizedManyNumberField;
|
|
295
|
+
}
|
|
296
|
+
if (subHasLocalizedRelationshipField) {
|
|
297
|
+
hasLocalizedRelationshipField = subHasLocalizedRelationshipField;
|
|
298
|
+
}
|
|
299
|
+
if (subHasLocalizedManyTextField) {
|
|
300
|
+
hasLocalizedManyTextField = subHasLocalizedManyTextField;
|
|
301
|
+
}
|
|
271
302
|
if (subHasManyTextField) {
|
|
272
|
-
if (!hasManyTextField || subHasManyTextField === 'index')
|
|
303
|
+
if (!hasManyTextField || subHasManyTextField === 'index') {
|
|
304
|
+
hasManyTextField = subHasManyTextField;
|
|
305
|
+
}
|
|
273
306
|
}
|
|
274
307
|
if (subHasManyNumberField) {
|
|
275
|
-
if (!hasManyNumberField || subHasManyNumberField === 'index')
|
|
308
|
+
if (!hasManyNumberField || subHasManyNumberField === 'index') {
|
|
309
|
+
hasManyNumberField = subHasManyNumberField;
|
|
310
|
+
}
|
|
276
311
|
}
|
|
277
312
|
relationsToBuild.set(fieldName, {
|
|
278
313
|
type: 'many',
|
|
@@ -344,7 +379,6 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
344
379
|
};
|
|
345
380
|
const baseExtraConfig = {
|
|
346
381
|
_orderIdx: (cols)=>index(`${blockTableName}_order_idx`).on(cols._order),
|
|
347
|
-
_parentIDIdx: (cols)=>index(`${blockTableName}_parent_id_idx`).on(cols._parentID),
|
|
348
382
|
_parentIdFk: (cols)=>foreignKey({
|
|
349
383
|
name: `${blockTableName}_parent_id_fk`,
|
|
350
384
|
columns: [
|
|
@@ -354,33 +388,51 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
354
388
|
adapter.tables[rootTableName].id
|
|
355
389
|
]
|
|
356
390
|
}).onDelete('cascade'),
|
|
391
|
+
_parentIDIdx: (cols)=>index(`${blockTableName}_parent_id_idx`).on(cols._parentID),
|
|
357
392
|
_pathIdx: (cols)=>index(`${blockTableName}_path_idx`).on(cols._path)
|
|
358
393
|
};
|
|
359
|
-
|
|
394
|
+
const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock || forceLocalized;
|
|
395
|
+
if (isLocalized) {
|
|
360
396
|
baseColumns._locale = text('_locale', {
|
|
361
397
|
enum: locales
|
|
362
398
|
}).notNull();
|
|
363
399
|
baseExtraConfig._localeIdx = (cols)=>index(`${blockTableName}_locale_idx`).on(cols._locale);
|
|
364
400
|
}
|
|
365
|
-
const { hasManyNumberField: subHasManyNumberField, hasManyTextField: subHasManyTextField, relationsToBuild: subRelationsToBuild } = buildTable({
|
|
401
|
+
const { hasLocalizedManyNumberField: subHasLocalizedManyNumberField, hasLocalizedManyTextField: subHasLocalizedManyTextField, hasLocalizedRelationshipField: subHasLocalizedRelationshipField, hasManyNumberField: subHasManyNumberField, hasManyTextField: subHasManyTextField, relationsToBuild: subRelationsToBuild } = buildTable({
|
|
366
402
|
adapter,
|
|
367
403
|
baseColumns,
|
|
368
404
|
baseExtraConfig,
|
|
369
405
|
disableNotNull: disableNotNullFromHere,
|
|
406
|
+
disableRelsTableUnique: true,
|
|
370
407
|
disableUnique,
|
|
371
408
|
fields: disableUnique ? idToUUID(block.fields) : block.fields,
|
|
372
|
-
rootRelationsToBuild,
|
|
373
409
|
rootRelationships: relationships,
|
|
410
|
+
rootRelationsToBuild,
|
|
374
411
|
rootTableIDColType,
|
|
375
412
|
rootTableName,
|
|
413
|
+
rootUniqueRelationships: uniqueRelationships,
|
|
376
414
|
tableName: blockTableName,
|
|
377
|
-
versions
|
|
415
|
+
versions,
|
|
416
|
+
withinLocalizedArrayOrBlock: isLocalized
|
|
378
417
|
});
|
|
418
|
+
if (subHasLocalizedManyNumberField) {
|
|
419
|
+
hasLocalizedManyNumberField = subHasLocalizedManyNumberField;
|
|
420
|
+
}
|
|
421
|
+
if (subHasLocalizedRelationshipField) {
|
|
422
|
+
hasLocalizedRelationshipField = subHasLocalizedRelationshipField;
|
|
423
|
+
}
|
|
424
|
+
if (subHasLocalizedManyTextField) {
|
|
425
|
+
hasLocalizedManyTextField = subHasLocalizedManyTextField;
|
|
426
|
+
}
|
|
379
427
|
if (subHasManyTextField) {
|
|
380
|
-
if (!hasManyTextField || subHasManyTextField === 'index')
|
|
428
|
+
if (!hasManyTextField || subHasManyTextField === 'index') {
|
|
429
|
+
hasManyTextField = subHasManyTextField;
|
|
430
|
+
}
|
|
381
431
|
}
|
|
382
432
|
if (subHasManyNumberField) {
|
|
383
|
-
if (!hasManyNumberField || subHasManyNumberField === 'index')
|
|
433
|
+
if (!hasManyNumberField || subHasManyNumberField === 'index') {
|
|
434
|
+
hasManyNumberField = subHasManyNumberField;
|
|
435
|
+
}
|
|
384
436
|
}
|
|
385
437
|
adapter.relations[`relations_${blockTableName}`] = relations(adapter.tables[blockTableName], ({ many, one })=>{
|
|
386
438
|
const result = {
|
|
@@ -453,24 +505,39 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
453
505
|
fields: field.fields,
|
|
454
506
|
forceLocalized,
|
|
455
507
|
indexes,
|
|
508
|
+
joins,
|
|
456
509
|
locales,
|
|
457
510
|
localesColumns,
|
|
458
511
|
localesIndexes,
|
|
459
512
|
newTableName,
|
|
460
513
|
parentTableName,
|
|
461
|
-
relationsToBuild,
|
|
462
514
|
relationships,
|
|
515
|
+
relationsToBuild,
|
|
463
516
|
rootRelationsToBuild,
|
|
464
517
|
rootTableIDColType,
|
|
465
518
|
rootTableName,
|
|
466
|
-
|
|
519
|
+
uniqueRelationships,
|
|
520
|
+
versions,
|
|
521
|
+
withinLocalizedArrayOrBlock
|
|
467
522
|
});
|
|
468
|
-
if (groupHasLocalizedField)
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
if (
|
|
472
|
-
|
|
473
|
-
|
|
523
|
+
if (groupHasLocalizedField) {
|
|
524
|
+
hasLocalizedField = true;
|
|
525
|
+
}
|
|
526
|
+
if (groupHasLocalizedRelationshipField) {
|
|
527
|
+
hasLocalizedRelationshipField = true;
|
|
528
|
+
}
|
|
529
|
+
if (groupHasManyTextField) {
|
|
530
|
+
hasManyTextField = true;
|
|
531
|
+
}
|
|
532
|
+
if (groupHasLocalizedManyTextField) {
|
|
533
|
+
hasLocalizedManyTextField = true;
|
|
534
|
+
}
|
|
535
|
+
if (groupHasManyNumberField) {
|
|
536
|
+
hasManyNumberField = true;
|
|
537
|
+
}
|
|
538
|
+
if (groupHasLocalizedManyNumberField) {
|
|
539
|
+
hasLocalizedManyNumberField = true;
|
|
540
|
+
}
|
|
474
541
|
break;
|
|
475
542
|
}
|
|
476
543
|
const disableNotNullFromHere = Boolean(field.admin?.condition) || disableNotNull;
|
|
@@ -484,24 +551,39 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
484
551
|
fields: field.fields,
|
|
485
552
|
forceLocalized: field.localized,
|
|
486
553
|
indexes,
|
|
554
|
+
joins,
|
|
487
555
|
locales,
|
|
488
556
|
localesColumns,
|
|
489
557
|
localesIndexes,
|
|
490
558
|
newTableName: `${parentTableName}_${columnName}`,
|
|
491
559
|
parentTableName,
|
|
492
|
-
relationsToBuild,
|
|
493
560
|
relationships,
|
|
561
|
+
relationsToBuild,
|
|
494
562
|
rootRelationsToBuild,
|
|
495
563
|
rootTableIDColType,
|
|
496
564
|
rootTableName,
|
|
497
|
-
|
|
565
|
+
uniqueRelationships,
|
|
566
|
+
versions,
|
|
567
|
+
withinLocalizedArrayOrBlock: withinLocalizedArrayOrBlock || field.localized
|
|
498
568
|
});
|
|
499
|
-
if (groupHasLocalizedField)
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
if (
|
|
503
|
-
|
|
504
|
-
|
|
569
|
+
if (groupHasLocalizedField) {
|
|
570
|
+
hasLocalizedField = true;
|
|
571
|
+
}
|
|
572
|
+
if (groupHasLocalizedRelationshipField) {
|
|
573
|
+
hasLocalizedRelationshipField = true;
|
|
574
|
+
}
|
|
575
|
+
if (groupHasManyTextField) {
|
|
576
|
+
hasManyTextField = true;
|
|
577
|
+
}
|
|
578
|
+
if (groupHasLocalizedManyTextField) {
|
|
579
|
+
hasLocalizedManyTextField = true;
|
|
580
|
+
}
|
|
581
|
+
if (groupHasManyNumberField) {
|
|
582
|
+
hasManyNumberField = true;
|
|
583
|
+
}
|
|
584
|
+
if (groupHasLocalizedManyNumberField) {
|
|
585
|
+
hasLocalizedManyNumberField = true;
|
|
586
|
+
}
|
|
505
587
|
break;
|
|
506
588
|
}
|
|
507
589
|
case 'tabs':
|
|
@@ -520,24 +602,39 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
520
602
|
})),
|
|
521
603
|
forceLocalized,
|
|
522
604
|
indexes,
|
|
605
|
+
joins,
|
|
523
606
|
locales,
|
|
524
607
|
localesColumns,
|
|
525
608
|
localesIndexes,
|
|
526
609
|
newTableName,
|
|
527
610
|
parentTableName,
|
|
528
|
-
relationsToBuild,
|
|
529
611
|
relationships,
|
|
612
|
+
relationsToBuild,
|
|
530
613
|
rootRelationsToBuild,
|
|
531
614
|
rootTableIDColType,
|
|
532
615
|
rootTableName,
|
|
533
|
-
|
|
616
|
+
uniqueRelationships,
|
|
617
|
+
versions,
|
|
618
|
+
withinLocalizedArrayOrBlock
|
|
534
619
|
});
|
|
535
|
-
if (tabHasLocalizedField)
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
if (
|
|
539
|
-
|
|
540
|
-
|
|
620
|
+
if (tabHasLocalizedField) {
|
|
621
|
+
hasLocalizedField = true;
|
|
622
|
+
}
|
|
623
|
+
if (tabHasLocalizedRelationshipField) {
|
|
624
|
+
hasLocalizedRelationshipField = true;
|
|
625
|
+
}
|
|
626
|
+
if (tabHasManyTextField) {
|
|
627
|
+
hasManyTextField = true;
|
|
628
|
+
}
|
|
629
|
+
if (tabHasLocalizedManyTextField) {
|
|
630
|
+
hasLocalizedManyTextField = true;
|
|
631
|
+
}
|
|
632
|
+
if (tabHasManyNumberField) {
|
|
633
|
+
hasManyNumberField = true;
|
|
634
|
+
}
|
|
635
|
+
if (tabHasLocalizedManyNumberField) {
|
|
636
|
+
hasLocalizedManyNumberField = true;
|
|
637
|
+
}
|
|
541
638
|
break;
|
|
542
639
|
}
|
|
543
640
|
case 'row':
|
|
@@ -554,32 +651,55 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
554
651
|
fields: field.fields,
|
|
555
652
|
forceLocalized,
|
|
556
653
|
indexes,
|
|
654
|
+
joins,
|
|
557
655
|
locales,
|
|
558
656
|
localesColumns,
|
|
559
657
|
localesIndexes,
|
|
560
658
|
newTableName,
|
|
561
659
|
parentTableName,
|
|
562
|
-
relationsToBuild,
|
|
563
660
|
relationships,
|
|
661
|
+
relationsToBuild,
|
|
564
662
|
rootRelationsToBuild,
|
|
565
663
|
rootTableIDColType,
|
|
566
664
|
rootTableName,
|
|
567
|
-
|
|
665
|
+
uniqueRelationships,
|
|
666
|
+
versions,
|
|
667
|
+
withinLocalizedArrayOrBlock
|
|
568
668
|
});
|
|
569
|
-
if (rowHasLocalizedField)
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
if (
|
|
573
|
-
|
|
574
|
-
|
|
669
|
+
if (rowHasLocalizedField) {
|
|
670
|
+
hasLocalizedField = true;
|
|
671
|
+
}
|
|
672
|
+
if (rowHasLocalizedRelationshipField) {
|
|
673
|
+
hasLocalizedRelationshipField = true;
|
|
674
|
+
}
|
|
675
|
+
if (rowHasManyTextField) {
|
|
676
|
+
hasManyTextField = true;
|
|
677
|
+
}
|
|
678
|
+
if (rowHasLocalizedManyTextField) {
|
|
679
|
+
hasLocalizedManyTextField = true;
|
|
680
|
+
}
|
|
681
|
+
if (rowHasManyNumberField) {
|
|
682
|
+
hasManyNumberField = true;
|
|
683
|
+
}
|
|
684
|
+
if (rowHasLocalizedManyNumberField) {
|
|
685
|
+
hasLocalizedManyNumberField = true;
|
|
686
|
+
}
|
|
575
687
|
break;
|
|
576
688
|
}
|
|
577
689
|
case 'relationship':
|
|
578
690
|
case 'upload':
|
|
579
691
|
if (Array.isArray(field.relationTo)) {
|
|
580
|
-
field.relationTo.forEach((relation)=>
|
|
692
|
+
field.relationTo.forEach((relation)=>{
|
|
693
|
+
relationships.add(relation);
|
|
694
|
+
if (field.unique && !disableUnique && !disableRelsTableUnique) {
|
|
695
|
+
uniqueRelationships.add(relation);
|
|
696
|
+
}
|
|
697
|
+
});
|
|
581
698
|
} else if (field.hasMany) {
|
|
582
699
|
relationships.add(field.relationTo);
|
|
700
|
+
if (field.unique && !disableUnique && !disableRelsTableUnique) {
|
|
701
|
+
uniqueRelationships.add(field.relationTo);
|
|
702
|
+
}
|
|
583
703
|
} else {
|
|
584
704
|
// simple relationships get a column on the targetTable with a foreign key to the relationTo table
|
|
585
705
|
const relationshipConfig = adapter.payload.collections[field.relationTo].config;
|
|
@@ -587,8 +707,12 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
587
707
|
// get the id type of the related collection
|
|
588
708
|
let colType = 'integer';
|
|
589
709
|
const relatedCollectionCustomID = relationshipConfig.fields.find((field)=>fieldAffectsData(field) && field.name === 'id');
|
|
590
|
-
if (relatedCollectionCustomID?.type === 'number')
|
|
591
|
-
|
|
710
|
+
if (relatedCollectionCustomID?.type === 'number') {
|
|
711
|
+
colType = 'numeric';
|
|
712
|
+
}
|
|
713
|
+
if (relatedCollectionCustomID?.type === 'text') {
|
|
714
|
+
colType = 'text';
|
|
715
|
+
}
|
|
592
716
|
// make the foreign key column for relationship using the correct id column type
|
|
593
717
|
targetTable[fieldName] = getIDColumn({
|
|
594
718
|
name: `${columnName}_id`,
|
|
@@ -600,7 +724,7 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
600
724
|
// add relationship to table
|
|
601
725
|
relationsToBuild.set(fieldName, {
|
|
602
726
|
type: 'one',
|
|
603
|
-
localized: adapter.payload.config.localization && field.localized,
|
|
727
|
+
localized: adapter.payload.config.localization && (field.localized || forceLocalized),
|
|
604
728
|
target: tableName
|
|
605
729
|
});
|
|
606
730
|
// add notNull when not required
|
|
@@ -609,10 +733,32 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
609
733
|
}
|
|
610
734
|
break;
|
|
611
735
|
}
|
|
612
|
-
if (adapter.payload.config.localization
|
|
736
|
+
if (Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock) {
|
|
613
737
|
hasLocalizedRelationshipField = true;
|
|
614
738
|
}
|
|
615
739
|
break;
|
|
740
|
+
case 'join':
|
|
741
|
+
{
|
|
742
|
+
// fieldName could be 'posts' or 'group_posts'
|
|
743
|
+
// using `on` as the key for the relation
|
|
744
|
+
const localized = adapter.payload.config.localization && field.localized;
|
|
745
|
+
const fieldSchemaPath = `${fieldPrefix || ''}${field.name}`;
|
|
746
|
+
let target;
|
|
747
|
+
const joinConfig = joins[field.collection].find(({ schemaPath })=>fieldSchemaPath === schemaPath);
|
|
748
|
+
if (joinConfig.targetField.hasMany) {
|
|
749
|
+
target = `${adapter.tableNameMap.get(toSnakeCase(field.collection))}${adapter.relationshipsSuffix}`;
|
|
750
|
+
} else {
|
|
751
|
+
target = `${adapter.tableNameMap.get(toSnakeCase(field.collection))}${localized ? adapter.localesSuffix : ''}`;
|
|
752
|
+
}
|
|
753
|
+
relationsToBuild.set(fieldName, {
|
|
754
|
+
type: 'many',
|
|
755
|
+
// joins are not localized on the parent table
|
|
756
|
+
localized: false,
|
|
757
|
+
relationName: field.on.replaceAll('.', '_'),
|
|
758
|
+
target
|
|
759
|
+
});
|
|
760
|
+
break;
|
|
761
|
+
}
|
|
616
762
|
default:
|
|
617
763
|
break;
|
|
618
764
|
}
|