Skip to content

Commit 10da10b

Browse files
committed
chore: improve TypeScript types in Drizzle upsertRow
- Replace 'unknown' and 'any' types with specific RelationshipRow interface - Add proper type for DatabaseQueryResult - Ensure type safety for relationship database operations - Fix template literal type issues with proper type assertions
1 parent 44f9309 commit 10da10b

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

packages/drizzle/src/upsertRow/index.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ import { ValidationError } from 'payload'
88
import type { BlockRowToInsert } from '../transform/write/types.js'
99
import type { Args } from './types.js'
1010

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+
1126
import { buildFindManyArgs } from '../find/buildFindManyArgs.js'
1227
import { transform } from '../transform/read/index.js'
1328
import { transformForWrite } from '../transform/write/index.js'
@@ -349,9 +364,10 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
349364

350365
// Prepare all relationships for batch insert
351366
const relationshipsToInsert = rowToInsert.relationshipsToAppend.map((rel, index) => {
352-
const row: Record<string, any> = {
367+
const parentId = id || insertedRow.id
368+
const row: RelationshipRow = {
353369
order: baseOrder + index,
354-
parent_id: id || insertedRow.id,
370+
parent_id: parentId as number | string,
355371
path: rel.path,
356372
}
357373

@@ -373,7 +389,7 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
373389
// we need to check for existing relationships to prevent duplicates
374390
const existingCheckConditions: string[] = []
375391

376-
relationshipsToInsert.forEach((row) => {
392+
relationshipsToInsert.forEach((row: RelationshipRow) => {
377393
let condition = `("parent_id" = ${row.parent_id} AND "path" = '${row.path}'`
378394

379395
// Add locale condition if present
@@ -396,8 +412,13 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
396412

397413
// Single query to check all potential duplicates
398414
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 || []
401422

402423
// Filter out relationships that already exist
403424
const relationshipsToActuallyInsert = relationshipsToInsert.filter((newRow) => {
@@ -458,7 +479,8 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
458479
if ('itemToRemove' in relToDelete && relToDelete.itemToRemove) {
459480
const item = relToDelete.itemToRemove
460481

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}'`
462484

463485
// Only add locale condition if this relationship table has a locale column
464486
// (i.e., if the relationship field is localized)
@@ -651,7 +673,7 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
651673
}
652674

653675
if (Object.keys(arraysBlocksUUIDMap).length > 0) {
654-
tableRows.forEach((row: any) => {
676+
tableRows.forEach((row: Record<string, number | string>) => {
655677
if (row.parent in arraysBlocksUUIDMap) {
656678
row.parent = arraysBlocksUUIDMap[row.parent]
657679
}

0 commit comments

Comments
 (0)