MCPcopy Create free account
hub / github.com/observablehq/framework / insertFile

Function insertFile

src/client/stdlib/duckdb.js:238–293  ·  view source on GitHub ↗
(database, name, file, options)

Source from the content-addressed store, hash-verified

236}
237
238async function insertFile(database, name, file, options) {
239 const url = await file.url();
240 if (url.startsWith("blob:")) {
241 const buffer = await file.arrayBuffer();
242 await database.registerFileBuffer(file.name, new Uint8Array(buffer));
243 } else {
244 await database.registerFileURL(file.name, new URL(url, location).href, 4); // duckdb.DuckDBDataProtocol.HTTP
245 }
246 const connection = await database.connect();
247 try {
248 switch (file.mimeType) {
249 case "text/csv":
250 case "text/tab-separated-values": {
251 return await connection
252 .insertCSVFromPath(file.name, {
253 name,
254 schema: "main",
255 ...options
256 })
257 .catch(async (error) => {
258 // If initial attempt to insert CSV resulted in a conversion
259 // error, try again, this time treating all columns as strings.
260 if (error.toString().includes("Could not convert")) {
261 return await insertUntypedCSV(connection, file, name);
262 }
263 throw error;
264 });
265 }
266 case "application/json":
267 return await connection.insertJSONFromPath(file.name, {
268 name,
269 schema: "main",
270 ...options
271 });
272 default:
273 if (/\.arrow$/i.test(file.name)) {
274 const buffer = new Uint8Array(await file.arrayBuffer());
275 return await connection.insertArrowFromIPCStream(buffer, {
276 name,
277 schema: "main",
278 ...options
279 });
280 }
281 if (/\.parquet$/i.test(file.name)) {
282 const table = file.size < 50e6 ? "TABLE" : "VIEW"; // for small files, materialize the table
283 return await connection.query(`CREATE ${table} '${name}' AS SELECT * FROM parquet_scan('${file.name}')`);
284 }
285 if (/\.(db|ddb|duckdb)$/i.test(file.name)) {
286 return await connection.query(`ATTACH '${file.name}' AS ${name} (READ_ONLY)`);
287 }
288 throw new Error(`unknown file type: ${file.mimeType}`);
289 }
290 } finally {
291 await connection.close();
292 }
293}
294
295async function insertUntypedCSV(connection, file, name) {

Callers 1

insertSourceFunction · 0.85

Calls 6

insertUntypedCSVFunction · 0.85
urlMethod · 0.45
arrayBufferMethod · 0.45
toStringMethod · 0.45
queryMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected