MCPcopy Index your code
hub / github.com/simstudioai/sim / batchInsertRowsWithTx

Function batchInsertRowsWithTx

apps/sim/lib/table/rows/service.ts:230–313  ·  view source on GitHub ↗
(
  trx: DbTransaction,
  data: BatchInsertData,
  table: TableDefinition,
  requestId: string
)

Source from the content-addressed store, hash-verified

228 * Callers gate it before opening the tx — see `batchInsertRows` and the import paths.
229 */
230export 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

Callers 2

importAppendRowsFunction · 0.90
batchInsertRowsFunction · 0.85

Calls 13

validateRowSizeFunction · 0.90
coerceRowToSchemaFunction · 0.90
getUniqueColumnsFunction · 0.90
setTableTxTimeoutsFunction · 0.90
acquireRowOrderLockFunction · 0.90
isFeatureEnabledFunction · 0.90
nextRowPositionFunction · 0.90
reserveBatchPositionsFunction · 0.90
buildRowFunction · 0.85
joinMethod · 0.80

Tested by

no test coverage detected