@@ -8,6 +8,21 @@ import { ValidationError } from 'payload'
8
8
import type { BlockRowToInsert } from '../transform/write/types.js'
9
9
import type { Args } from './types.js'
10
10
11
+ type RelationshipRow = {
12
+ [ key : string ] : number | string | undefined // For relationship ID columns like movies_id, categories_id, etc.
13
+ id ?: number | string
14
+ locale ?: string
15
+ order : number
16
+ parent_id : number | string
17
+ path : string
18
+ }
19
+
20
+ type DatabaseQueryResult =
21
+ | {
22
+ rows ?: RelationshipRow [ ]
23
+ }
24
+ | RelationshipRow [ ]
25
+
11
26
import { buildFindManyArgs } from '../find/buildFindManyArgs.js'
12
27
import { transform } from '../transform/read/index.js'
13
28
import { transformForWrite } from '../transform/write/index.js'
@@ -349,9 +364,10 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
349
364
350
365
// Prepare all relationships for batch insert
351
366
const relationshipsToInsert = rowToInsert . relationshipsToAppend . map ( ( rel , index ) => {
352
- const row : Record < string , any > = {
367
+ const parentId = id || insertedRow . id
368
+ const row : RelationshipRow = {
353
369
order : baseOrder + index ,
354
- parent_id : id || insertedRow . id ,
370
+ parent_id : parentId as number | string ,
355
371
path : rel . path ,
356
372
}
357
373
@@ -373,7 +389,7 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
373
389
// we need to check for existing relationships to prevent duplicates
374
390
const existingCheckConditions : string [ ] = [ ]
375
391
376
- relationshipsToInsert . forEach ( ( row ) => {
392
+ relationshipsToInsert . forEach ( ( row : RelationshipRow ) => {
377
393
let condition = `("parent_id" = ${ row . parent_id } AND "path" = '${ row . path } '`
378
394
379
395
// Add locale condition if present
@@ -396,8 +412,13 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
396
412
397
413
// Single query to check all potential duplicates
398
414
const existingQuery = `SELECT * FROM "${ relationshipsTableName } " WHERE ${ existingCheckConditions . join ( ' OR ' ) } `
399
- const existingRels = await adapter . execute ( { db, raw : existingQuery } )
400
- const existingRows = Array . isArray ( existingRels ) ? existingRels : existingRels . rows || [ ]
415
+ const existingRels = ( await adapter . execute ( {
416
+ db,
417
+ raw : existingQuery ,
418
+ } ) ) as DatabaseQueryResult
419
+ const existingRows : RelationshipRow [ ] = Array . isArray ( existingRels )
420
+ ? existingRels
421
+ : existingRels . rows || [ ]
401
422
402
423
// Filter out relationships that already exist
403
424
const relationshipsToActuallyInsert = relationshipsToInsert . filter ( ( newRow ) => {
@@ -458,7 +479,8 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
458
479
if ( 'itemToRemove' in relToDelete && relToDelete . itemToRemove ) {
459
480
const item = relToDelete . itemToRemove
460
481
461
- let deleteQuery = `DELETE FROM "${ relationshipsTableName } " WHERE "parent_id" = ${ id || insertedRow . id } AND "path" = '${ relToDelete . path } '`
482
+ const parentId = ( id || insertedRow . id ) as number | string
483
+ let deleteQuery = `DELETE FROM "${ relationshipsTableName } " WHERE "parent_id" = ${ parentId } AND "path" = '${ relToDelete . path } '`
462
484
463
485
// Only add locale condition if this relationship table has a locale column
464
486
// (i.e., if the relationship field is localized)
@@ -651,7 +673,7 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
651
673
}
652
674
653
675
if ( Object . keys ( arraysBlocksUUIDMap ) . length > 0 ) {
654
- tableRows . forEach ( ( row : any ) => {
676
+ tableRows . forEach ( ( row : Record < string , number | string > ) => {
655
677
if ( row . parent in arraysBlocksUUIDMap ) {
656
678
row . parent = arraysBlocksUUIDMap [ row . parent ]
657
679
}
0 commit comments