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

Function materialize

apps/sim/lib/table/snapshot-cache.ts:92–129  ·  view source on GitHub ↗

* Streams the table CSV (keyset-paginated, like the export worker) into storage under `key`, * aborting if it crosses SNAPSHOT_MAX_BYTES. Returns the stored byte size. Bytes match the * canonical export format (id-keyed reads, display-name headers).

(table: TableDefinition, key: string)

Source from the content-addressed store, hash-verified

90 * canonical export format (id-keyed reads, display-name headers).
91 */
92async function materialize(table: TableDefinition, key: string): Promise<number> {
93 const columns = table.schema.columns
94 const handle = await createMultipartUpload({
95 key,
96 context: SNAPSHOT_STORAGE_CONTEXT,
97 contentType: SNAPSHOT_CONTENT_TYPE,
98 })
99
100 try {
101 let bytes = 0
102 const header = `${toCsvRow(columns.map((c) => neutralizeCsvFormula(c.name)))}\n`
103 bytes += Buffer.byteLength(header)
104 await handle.write(header)
105
106 let after: { orderKey: string; id: string } | null = null
107 while (true) {
108 const page = await selectExportRowPage(table, after, SNAPSHOT_BATCH_SIZE)
109 if (page.length === 0) break
110
111 const chunk = page
112 .map((row) => `${toCsvRow(columns.map((c) => formatCsvValue(row.data[getColumnId(c)])))}\n`)
113 .join('')
114 bytes += Buffer.byteLength(chunk)
115 if (bytes > SNAPSHOT_MAX_BYTES) throw new TableSnapshotTooLargeError(table.id)
116 await handle.write(chunk)
117
118 const last = page[page.length - 1]
119 after = { orderKey: last.orderKey, id: last.id }
120 if (page.length < SNAPSHOT_BATCH_SIZE) break
121 }
122
123 const { size } = await handle.complete()
124 return size
125 } catch (err) {
126 await handle.abort().catch(() => {})
127 throw err
128 }
129}
130
131/** Best-effort removal of the immediately-prior version (the common single-mutation case). */
132async function deletePreviousVersion(

Callers 1

getOrCreateTableSnapshotFunction · 0.85

Calls 10

createMultipartUploadFunction · 0.90
toCsvRowFunction · 0.90
neutralizeCsvFormulaFunction · 0.90
selectExportRowPageFunction · 0.90
formatCsvValueFunction · 0.90
getColumnIdFunction · 0.90
joinMethod · 0.80
writeMethod · 0.65
completeMethod · 0.65
abortMethod · 0.65

Tested by

no test coverage detected