(sampleRows: Record<string, unknown>[])
| 125 | |
| 126 | /** Infer the schema from the buffered sample and create the (empty) table. */ |
| 127 | const buildTable = async (sampleRows: Record<string, unknown>[]): Promise<ImportState> => { |
| 128 | const inferred = inferSchemaFromCsv(Object.keys(sampleRows[0]), sampleRows) |
| 129 | const schema: TableSchema = { columns: inferred.columns.map(normalizeColumn) } |
| 130 | const planLimits = await getWorkspaceTableLimits(workspaceId) |
| 131 | const tableName = sanitizeName(file.filename.replace(/\.[^.]+$/, ''), 'imported_table').slice( |
| 132 | 0, |
| 133 | TABLE_LIMITS.MAX_TABLE_NAME_LENGTH |
| 134 | ) |
| 135 | const table = await createTable( |
| 136 | { |
| 137 | name: tableName, |
| 138 | description: `Imported from ${file.filename}`, |
| 139 | schema, |
| 140 | workspaceId, |
| 141 | userId, |
| 142 | maxTables: planLimits.maxTables, |
| 143 | }, |
| 144 | requestId |
| 145 | ) |
| 146 | // Coerce against the *created* schema so rows key by the ids `createTable` |
| 147 | // assigned (the local `schema` is the id-less inferred one). |
| 148 | return { table, schema: table.schema, headerToColumn: inferred.headerToColumn } |
| 149 | } |
| 150 | |
| 151 | let state: ImportState | null = null |
| 152 | let inserted = 0 |
no test coverage detected