Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/drizzle/src/upsertRow/handleUpsertError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,24 @@ export const handleUpsertError = ({
}
}

// Compose path with optional provided block path
let path = fieldName
if (
typeof caughtError === 'object' &&
'_blockPath' in caughtError &&
typeof caughtError._blockPath === 'string'
) {
path = `${caughtError._blockPath}.${path}`
}

throw new ValidationError(
{
id,
collection: collectionSlug,
errors: [
{
message: req?.t ? req.t('error:valueMustBeUnique') : 'Value must be unique',
path: fieldName,
path,
},
],
global: globalSlug,
Expand Down
47 changes: 35 additions & 12 deletions packages/drizzle/src/upsertRow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,11 +625,23 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
const arraysBlocksUUIDMap: Record<string, number | string> = {}

for (const [tableName, blockRows] of Object.entries(blocksToInsert)) {
insertedBlockRows[tableName] = await adapter.insert({
db,
tableName,
values: blockRows.map(({ row }) => row),
})
// To resolve issue path on e.g. unique constraint error, we can't use
// batch insert here. Instead, original blockPath is added to errors.
insertedBlockRows[tableName] = await Promise.all(
blockRows.map(async (blockRow) => {
try {
const [insertedRow] = await adapter.insert({
db,
tableName,
values: [blockRow.row],
})
return insertedRow
} catch (error) {
error._blockPath = blockRow.row._path
throw error
}
}),
)

insertedBlockRows[tableName].forEach((row, i) => {
blockRows[i].row = row
Expand Down Expand Up @@ -666,13 +678,24 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
})
}

await insertArrays({
adapter,
arrays: blockRows.map(({ arrays }) => arrays),
db,
parentRows: insertedBlockRows[tableName],
uuidMap: arraysBlocksUUIDMap,
})
// To resolve issue path on e.g. unique constraint error, we can't use
// batch insert here. Instead, original blockPath is added to errors.
await Promise.all(
blockRows.map(async (blockRow) => {
try {
await insertArrays({
adapter,
arrays: [blockRow.arrays],
db,
parentRows: insertedBlockRows[tableName],
uuidMap: arraysBlocksUUIDMap,
})
} catch (error) {
error._blockPath = blockRow.row._path
throw error
}
}),
)
}

// //////////////////////////////////
Expand Down