@payloadcms/db-sqlite 3.0.0-canary.013af1b → 3.0.0-canary.0374de4

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.
@@ -1,6 +1,6 @@
1
1
  import { createTableName, hasLocalesTable, validateExistingBlockIsIdentical } from '@payloadcms/drizzle';
2
2
  import { relations } from 'drizzle-orm';
3
- import { SQLiteIntegerBuilder, SQLiteNumericBuilder, SQLiteTextBuilder, foreignKey, index, integer, numeric, text } from 'drizzle-orm/sqlite-core';
3
+ import { foreignKey, index, integer, numeric, SQLiteIntegerBuilder, SQLiteNumericBuilder, SQLiteTextBuilder, text } from 'drizzle-orm/sqlite-core';
4
4
  import { InvalidConfiguration } from 'payload';
5
5
  import { fieldAffectsData, optionIsObject } from 'payload/shared';
6
6
  import toSnakeCase from 'to-snake-case';
@@ -9,7 +9,7 @@ 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, relationsToBuild, relationships, rootRelationsToBuild, rootTableIDColType, rootTableName, versions })=>{
12
+ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull, disableUnique = false, fieldPrefix, fields, forceLocalized, indexes, locales, localesColumns, localesIndexes, newTableName, parentTableName, relationships, relationsToBuild, rootRelationsToBuild, rootTableIDColType, rootTableName, versions, withinLocalizedArrayOrBlock })=>{
13
13
  let hasLocalizedField = false;
14
14
  let hasLocalizedRelationshipField = false;
15
15
  let hasManyTextField = false;
@@ -17,11 +17,19 @@ 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) parentIDColType = 'integer';
21
- if (columns.id instanceof SQLiteNumericBuilder) parentIDColType = 'numeric';
22
- if (columns.id instanceof SQLiteTextBuilder) parentIDColType = 'text';
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') return;
30
+ if ('name' in field && field.name === 'id') {
31
+ return;
32
+ }
25
33
  let columnName;
26
34
  let fieldName;
27
35
  let targetTable = columns;
@@ -64,7 +72,8 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
64
72
  case 'text':
65
73
  {
66
74
  if (field.hasMany) {
67
- if (field.localized) {
75
+ const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock;
76
+ if (isLocalized) {
68
77
  hasLocalizedManyTextField = true;
69
78
  }
70
79
  if (field.index) {
@@ -90,7 +99,8 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
90
99
  case 'number':
91
100
  {
92
101
  if (field.hasMany) {
93
- if (field.localized) {
102
+ const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock;
103
+ if (isLocalized) {
94
104
  hasLocalizedManyNumberField = true;
95
105
  }
96
106
  if (field.index) {
@@ -165,7 +175,8 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
165
175
  }).onDelete('cascade'),
166
176
  parentIdx: (cols)=>index(`${selectTableName}_parent_idx`).on(cols.parent)
167
177
  };
168
- if (field.localized) {
178
+ const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock;
179
+ if (isLocalized) {
169
180
  baseColumns.locale = text('locale', {
170
181
  enum: locales
171
182
  }).notNull();
@@ -248,31 +259,46 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
248
259
  }).onDelete('cascade'),
249
260
  _parentIDIdx: (cols)=>index(`${arrayTableName}_parent_id_idx`).on(cols._parentID)
250
261
  };
251
- if (field.localized && adapter.payload.config.localization) {
262
+ const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock;
263
+ if (isLocalized) {
252
264
  baseColumns._locale = text('_locale', {
253
265
  enum: locales
254
266
  }).notNull();
255
267
  baseExtraConfig._localeIdx = (cols)=>index(`${arrayTableName}_locale_idx`).on(cols._locale);
256
268
  }
257
- const { hasManyNumberField: subHasManyNumberField, hasManyTextField: subHasManyTextField, relationsToBuild: subRelationsToBuild } = buildTable({
269
+ const { hasLocalizedManyNumberField: subHasLocalizedManyNumberField, hasLocalizedManyTextField: subHasLocalizedManyTextField, hasLocalizedRelationshipField: subHasLocalizedRelationshipField, hasManyNumberField: subHasManyNumberField, hasManyTextField: subHasManyTextField, relationsToBuild: subRelationsToBuild } = buildTable({
258
270
  adapter,
259
271
  baseColumns,
260
272
  baseExtraConfig,
261
273
  disableNotNull: disableNotNullFromHere,
262
274
  disableUnique,
263
275
  fields: disableUnique ? idToUUID(field.fields) : field.fields,
264
- rootRelationsToBuild,
265
276
  rootRelationships: relationships,
277
+ rootRelationsToBuild,
266
278
  rootTableIDColType,
267
279
  rootTableName,
268
280
  tableName: arrayTableName,
269
- versions
281
+ versions,
282
+ withinLocalizedArrayOrBlock: isLocalized
270
283
  });
284
+ if (subHasLocalizedManyNumberField) {
285
+ hasLocalizedManyNumberField = subHasLocalizedManyNumberField;
286
+ }
287
+ if (subHasLocalizedRelationshipField) {
288
+ hasLocalizedRelationshipField = subHasLocalizedRelationshipField;
289
+ }
290
+ if (subHasLocalizedManyTextField) {
291
+ hasLocalizedManyTextField = subHasLocalizedManyTextField;
292
+ }
271
293
  if (subHasManyTextField) {
272
- if (!hasManyTextField || subHasManyTextField === 'index') hasManyTextField = subHasManyTextField;
294
+ if (!hasManyTextField || subHasManyTextField === 'index') {
295
+ hasManyTextField = subHasManyTextField;
296
+ }
273
297
  }
274
298
  if (subHasManyNumberField) {
275
- if (!hasManyNumberField || subHasManyNumberField === 'index') hasManyNumberField = subHasManyNumberField;
299
+ if (!hasManyNumberField || subHasManyNumberField === 'index') {
300
+ hasManyNumberField = subHasManyNumberField;
301
+ }
276
302
  }
277
303
  relationsToBuild.set(fieldName, {
278
304
  type: 'many',
@@ -344,7 +370,6 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
344
370
  };
345
371
  const baseExtraConfig = {
346
372
  _orderIdx: (cols)=>index(`${blockTableName}_order_idx`).on(cols._order),
347
- _parentIDIdx: (cols)=>index(`${blockTableName}_parent_id_idx`).on(cols._parentID),
348
373
  _parentIdFk: (cols)=>foreignKey({
349
374
  name: `${blockTableName}_parent_id_fk`,
350
375
  columns: [
@@ -354,33 +379,49 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
354
379
  adapter.tables[rootTableName].id
355
380
  ]
356
381
  }).onDelete('cascade'),
382
+ _parentIDIdx: (cols)=>index(`${blockTableName}_parent_id_idx`).on(cols._parentID),
357
383
  _pathIdx: (cols)=>index(`${blockTableName}_path_idx`).on(cols._path)
358
384
  };
359
- if (field.localized && adapter.payload.config.localization) {
385
+ const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock;
386
+ if (isLocalized) {
360
387
  baseColumns._locale = text('_locale', {
361
388
  enum: locales
362
389
  }).notNull();
363
390
  baseExtraConfig._localeIdx = (cols)=>index(`${blockTableName}_locale_idx`).on(cols._locale);
364
391
  }
365
- const { hasManyNumberField: subHasManyNumberField, hasManyTextField: subHasManyTextField, relationsToBuild: subRelationsToBuild } = buildTable({
392
+ const { hasLocalizedManyNumberField: subHasLocalizedManyNumberField, hasLocalizedManyTextField: subHasLocalizedManyTextField, hasLocalizedRelationshipField: subHasLocalizedRelationshipField, hasManyNumberField: subHasManyNumberField, hasManyTextField: subHasManyTextField, relationsToBuild: subRelationsToBuild } = buildTable({
366
393
  adapter,
367
394
  baseColumns,
368
395
  baseExtraConfig,
369
396
  disableNotNull: disableNotNullFromHere,
370
397
  disableUnique,
371
398
  fields: disableUnique ? idToUUID(block.fields) : block.fields,
372
- rootRelationsToBuild,
373
399
  rootRelationships: relationships,
400
+ rootRelationsToBuild,
374
401
  rootTableIDColType,
375
402
  rootTableName,
376
403
  tableName: blockTableName,
377
- versions
404
+ versions,
405
+ withinLocalizedArrayOrBlock: isLocalized
378
406
  });
407
+ if (subHasLocalizedManyNumberField) {
408
+ hasLocalizedManyNumberField = subHasLocalizedManyNumberField;
409
+ }
410
+ if (subHasLocalizedRelationshipField) {
411
+ hasLocalizedRelationshipField = subHasLocalizedRelationshipField;
412
+ }
413
+ if (subHasLocalizedManyTextField) {
414
+ hasLocalizedManyTextField = subHasLocalizedManyTextField;
415
+ }
379
416
  if (subHasManyTextField) {
380
- if (!hasManyTextField || subHasManyTextField === 'index') hasManyTextField = subHasManyTextField;
417
+ if (!hasManyTextField || subHasManyTextField === 'index') {
418
+ hasManyTextField = subHasManyTextField;
419
+ }
381
420
  }
382
421
  if (subHasManyNumberField) {
383
- if (!hasManyNumberField || subHasManyNumberField === 'index') hasManyNumberField = subHasManyNumberField;
422
+ if (!hasManyNumberField || subHasManyNumberField === 'index') {
423
+ hasManyNumberField = subHasManyNumberField;
424
+ }
384
425
  }
385
426
  adapter.relations[`relations_${blockTableName}`] = relations(adapter.tables[blockTableName], ({ many, one })=>{
386
427
  const result = {
@@ -458,19 +499,32 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
458
499
  localesIndexes,
459
500
  newTableName,
460
501
  parentTableName,
461
- relationsToBuild,
462
502
  relationships,
503
+ relationsToBuild,
463
504
  rootRelationsToBuild,
464
505
  rootTableIDColType,
465
506
  rootTableName,
466
- versions
507
+ versions,
508
+ withinLocalizedArrayOrBlock
467
509
  });
468
- if (groupHasLocalizedField) hasLocalizedField = true;
469
- if (groupHasLocalizedRelationshipField) hasLocalizedRelationshipField = true;
470
- if (groupHasManyTextField) hasManyTextField = true;
471
- if (groupHasLocalizedManyTextField) hasLocalizedManyTextField = true;
472
- if (groupHasManyNumberField) hasManyNumberField = true;
473
- if (groupHasLocalizedManyNumberField) hasLocalizedManyNumberField = true;
510
+ if (groupHasLocalizedField) {
511
+ hasLocalizedField = true;
512
+ }
513
+ if (groupHasLocalizedRelationshipField) {
514
+ hasLocalizedRelationshipField = true;
515
+ }
516
+ if (groupHasManyTextField) {
517
+ hasManyTextField = true;
518
+ }
519
+ if (groupHasLocalizedManyTextField) {
520
+ hasLocalizedManyTextField = true;
521
+ }
522
+ if (groupHasManyNumberField) {
523
+ hasManyNumberField = true;
524
+ }
525
+ if (groupHasLocalizedManyNumberField) {
526
+ hasLocalizedManyNumberField = true;
527
+ }
474
528
  break;
475
529
  }
476
530
  const disableNotNullFromHere = Boolean(field.admin?.condition) || disableNotNull;
@@ -489,19 +543,32 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
489
543
  localesIndexes,
490
544
  newTableName: `${parentTableName}_${columnName}`,
491
545
  parentTableName,
492
- relationsToBuild,
493
546
  relationships,
547
+ relationsToBuild,
494
548
  rootRelationsToBuild,
495
549
  rootTableIDColType,
496
550
  rootTableName,
497
- versions
551
+ versions,
552
+ withinLocalizedArrayOrBlock
498
553
  });
499
- if (groupHasLocalizedField) hasLocalizedField = true;
500
- if (groupHasLocalizedRelationshipField) hasLocalizedRelationshipField = true;
501
- if (groupHasManyTextField) hasManyTextField = true;
502
- if (groupHasLocalizedManyTextField) hasLocalizedManyTextField = true;
503
- if (groupHasManyNumberField) hasManyNumberField = true;
504
- if (groupHasLocalizedManyNumberField) hasLocalizedManyNumberField = true;
554
+ if (groupHasLocalizedField) {
555
+ hasLocalizedField = true;
556
+ }
557
+ if (groupHasLocalizedRelationshipField) {
558
+ hasLocalizedRelationshipField = true;
559
+ }
560
+ if (groupHasManyTextField) {
561
+ hasManyTextField = true;
562
+ }
563
+ if (groupHasLocalizedManyTextField) {
564
+ hasLocalizedManyTextField = true;
565
+ }
566
+ if (groupHasManyNumberField) {
567
+ hasManyNumberField = true;
568
+ }
569
+ if (groupHasLocalizedManyNumberField) {
570
+ hasLocalizedManyNumberField = true;
571
+ }
505
572
  break;
506
573
  }
507
574
  case 'tabs':
@@ -525,19 +592,32 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
525
592
  localesIndexes,
526
593
  newTableName,
527
594
  parentTableName,
528
- relationsToBuild,
529
595
  relationships,
596
+ relationsToBuild,
530
597
  rootRelationsToBuild,
531
598
  rootTableIDColType,
532
599
  rootTableName,
533
- versions
600
+ versions,
601
+ withinLocalizedArrayOrBlock
534
602
  });
535
- if (tabHasLocalizedField) hasLocalizedField = true;
536
- if (tabHasLocalizedRelationshipField) hasLocalizedRelationshipField = true;
537
- if (tabHasManyTextField) hasManyTextField = true;
538
- if (tabHasLocalizedManyTextField) hasLocalizedManyTextField = true;
539
- if (tabHasManyNumberField) hasManyNumberField = true;
540
- if (tabHasLocalizedManyNumberField) hasLocalizedManyNumberField = true;
603
+ if (tabHasLocalizedField) {
604
+ hasLocalizedField = true;
605
+ }
606
+ if (tabHasLocalizedRelationshipField) {
607
+ hasLocalizedRelationshipField = true;
608
+ }
609
+ if (tabHasManyTextField) {
610
+ hasManyTextField = true;
611
+ }
612
+ if (tabHasLocalizedManyTextField) {
613
+ hasLocalizedManyTextField = true;
614
+ }
615
+ if (tabHasManyNumberField) {
616
+ hasManyNumberField = true;
617
+ }
618
+ if (tabHasLocalizedManyNumberField) {
619
+ hasLocalizedManyNumberField = true;
620
+ }
541
621
  break;
542
622
  }
543
623
  case 'row':
@@ -559,19 +639,32 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
559
639
  localesIndexes,
560
640
  newTableName,
561
641
  parentTableName,
562
- relationsToBuild,
563
642
  relationships,
643
+ relationsToBuild,
564
644
  rootRelationsToBuild,
565
645
  rootTableIDColType,
566
646
  rootTableName,
567
- versions
647
+ versions,
648
+ withinLocalizedArrayOrBlock
568
649
  });
569
- if (rowHasLocalizedField) hasLocalizedField = true;
570
- if (rowHasLocalizedRelationshipField) hasLocalizedRelationshipField = true;
571
- if (rowHasManyTextField) hasManyTextField = true;
572
- if (rowHasLocalizedManyTextField) hasLocalizedManyTextField = true;
573
- if (rowHasManyNumberField) hasManyNumberField = true;
574
- if (rowHasLocalizedManyNumberField) hasLocalizedManyNumberField = true;
650
+ if (rowHasLocalizedField) {
651
+ hasLocalizedField = true;
652
+ }
653
+ if (rowHasLocalizedRelationshipField) {
654
+ hasLocalizedRelationshipField = true;
655
+ }
656
+ if (rowHasManyTextField) {
657
+ hasManyTextField = true;
658
+ }
659
+ if (rowHasLocalizedManyTextField) {
660
+ hasLocalizedManyTextField = true;
661
+ }
662
+ if (rowHasManyNumberField) {
663
+ hasManyNumberField = true;
664
+ }
665
+ if (rowHasLocalizedManyNumberField) {
666
+ hasLocalizedManyNumberField = true;
667
+ }
575
668
  break;
576
669
  }
577
670
  case 'relationship':
@@ -587,8 +680,12 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
587
680
  // get the id type of the related collection
588
681
  let colType = 'integer';
589
682
  const relatedCollectionCustomID = relationshipConfig.fields.find((field)=>fieldAffectsData(field) && field.name === 'id');
590
- if (relatedCollectionCustomID?.type === 'number') colType = 'numeric';
591
- if (relatedCollectionCustomID?.type === 'text') colType = 'text';
683
+ if (relatedCollectionCustomID?.type === 'number') {
684
+ colType = 'numeric';
685
+ }
686
+ if (relatedCollectionCustomID?.type === 'text') {
687
+ colType = 'text';
688
+ }
592
689
  // make the foreign key column for relationship using the correct id column type
593
690
  targetTable[fieldName] = getIDColumn({
594
691
  name: `${columnName}_id`,
@@ -609,10 +706,25 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
609
706
  }
610
707
  break;
611
708
  }
612
- if (adapter.payload.config.localization && field.localized) {
709
+ if (Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock) {
613
710
  hasLocalizedRelationshipField = true;
614
711
  }
615
712
  break;
713
+ case 'join':
714
+ {
715
+ // fieldName could be 'posts' or 'group_posts'
716
+ // using on as the key for the relation
717
+ const localized = adapter.payload.config.localization && field.localized;
718
+ const target = `${adapter.tableNameMap.get(toSnakeCase(field.collection))}${localized ? adapter.localesSuffix : ''}`;
719
+ relationsToBuild.set(fieldName, {
720
+ type: 'many',
721
+ // joins are not localized on the parent table
722
+ localized: false,
723
+ relationName: toSnakeCase(field.on),
724
+ target
725
+ });
726
+ break;
727
+ }
616
728
  default:
617
729
  break;
618
730
  }