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

Function handleBatchInsert

apps/sim/app/api/v1/tables/[tableId]/rows/route.ts:52–112  ·  view source on GitHub ↗
(
  requestId: string,
  tableId: string,
  validated: V1BatchInsertTableRowsBody,
  userId: string
)

Source from the content-addressed store, hash-verified

50}
51
52async function handleBatchInsert(
53 requestId: string,
54 tableId: string,
55 validated: V1BatchInsertTableRowsBody,
56 userId: string
57): Promise<NextResponse> {
58 const accessResult = await checkAccess(tableId, userId, 'write')
59 if (!accessResult.ok) return accessError(accessResult, requestId, tableId)
60
61 const { table } = accessResult
62
63 if (validated.workspaceId !== table.workspaceId) {
64 return NextResponse.json({ error: 'Invalid workspace ID' }, { status: 400 })
65 }
66
67 // External callers key row data by column name; storage keys by id.
68 const idByName = buildIdByName(table.schema as TableSchema)
69 const nameById = buildNameById(table.schema as TableSchema)
70 const rows = (validated.rows as RowData[]).map((r) => rowDataNameToId(r, idByName))
71
72 const validation = await validateBatchRows({
73 rows,
74 schema: table.schema as TableSchema,
75 tableId,
76 })
77 if (!validation.valid) return validation.response
78
79 try {
80 const insertedRows = await batchInsertRows(
81 {
82 tableId,
83 rows,
84 workspaceId: validated.workspaceId,
85 userId,
86 },
87 table,
88 requestId
89 )
90
91 return NextResponse.json({
92 success: true,
93 data: {
94 rows: insertedRows.map((r) => ({
95 id: r.id,
96 data: rowDataIdToName(r.data, nameById),
97 position: r.position,
98 createdAt: r.createdAt instanceof Date ? r.createdAt.toISOString() : r.createdAt,
99 updatedAt: r.updatedAt instanceof Date ? r.updatedAt.toISOString() : r.updatedAt,
100 })),
101 insertedCount: insertedRows.length,
102 message: `Successfully inserted ${insertedRows.length} rows`,
103 },
104 })
105 } catch (error) {
106 const response = rowWriteErrorResponse(error)
107 if (response) return response
108
109 logger.error(`[${requestId}] Error batch inserting rows:`, error)

Callers 1

route.tsFile · 0.70

Calls 10

checkAccessFunction · 0.90
accessErrorFunction · 0.90
buildIdByNameFunction · 0.90
buildNameByIdFunction · 0.90
rowDataNameToIdFunction · 0.90
validateBatchRowsFunction · 0.90
batchInsertRowsFunction · 0.90
rowDataIdToNameFunction · 0.90
rowWriteErrorResponseFunction · 0.90
errorMethod · 0.80

Tested by

no test coverage detected