( trx: DbTransaction, data: BatchInsertData, table: TableDefinition, requestId: string )
| 228 | * Callers gate it before opening the tx — see `batchInsertRows` and the import paths. |
| 229 | */ |
| 230 | export async function batchInsertRowsWithTx( |
| 231 | trx: DbTransaction, |
| 232 | data: BatchInsertData, |
| 233 | table: TableDefinition, |
| 234 | requestId: string |
| 235 | ): Promise<TableRow[]> { |
| 236 | for (let i = 0; i < data.rows.length; i++) { |
| 237 | const row = data.rows[i] |
| 238 | |
| 239 | const sizeValidation = validateRowSize(row) |
| 240 | if (!sizeValidation.valid) { |
| 241 | throw new Error(`Row ${i + 1}: ${sizeValidation.errors.join(', ')}`) |
| 242 | } |
| 243 | |
| 244 | const schemaValidation = coerceRowToSchema(row, table.schema) |
| 245 | if (!schemaValidation.valid) { |
| 246 | throw new Error(`Row ${i + 1}: ${schemaValidation.errors.join(', ')}`) |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | const uniqueColumns = getUniqueColumns(table.schema) |
| 251 | if (uniqueColumns.length > 0) { |
| 252 | const uniqueResult = await checkBatchUniqueConstraintsDb( |
| 253 | data.tableId, |
| 254 | data.rows, |
| 255 | table.schema, |
| 256 | trx |
| 257 | ) |
| 258 | if (!uniqueResult.valid) { |
| 259 | const errorMessages = uniqueResult.errors |
| 260 | .map((e) => `Row ${e.row + 1}: ${e.errors.join(', ')}`) |
| 261 | .join('; ') |
| 262 | throw new Error(errorMessages) |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | const now = new Date() |
| 267 | |
| 268 | await setTableTxTimeouts(trx, { statementMs: 60_000 }) |
| 269 | |
| 270 | const buildRow = (rowData: RowData, position: number, orderKey: string) => ({ |
| 271 | id: `row_${generateId().replace(/-/g, '')}`, |
| 272 | tableId: data.tableId, |
| 273 | workspaceId: data.workspaceId, |
| 274 | data: rowData, |
| 275 | position, |
| 276 | orderKey, |
| 277 | createdAt: now, |
| 278 | updatedAt: now, |
| 279 | ...(data.userId ? { createdBy: data.userId } : {}), |
| 280 | }) |
| 281 | |
| 282 | await acquireRowOrderLock(trx, data.tableId) |
| 283 | const fractionalOrdering = await isFeatureEnabled('tables-fractional-ordering') |
| 284 | // Undo restore passes exact saved keys; otherwise derive from positions/append. |
| 285 | const orderKeys = |
| 286 | data.orderKeys && data.orderKeys.length > 0 |
| 287 | ? data.orderKeys |
no test coverage detected