holdenmatt / duckdb-wasm-kit

Hooks and utilities to make it easier to use duckdb-wasm in React apps.
https://www.npmjs.com/package/duckdb-wasm-kit
MIT License
101 stars 4 forks source link

Allow importing DuckDB databases #19

Open tobilg opened 2 weeks ago

tobilg commented 2 weeks ago

It'd be great to be able to import (attach) DuckDB database files as well, similar to CSV, Parquet and Arrow files.

The code I'm currently using is basically this:

const loadDatabase = async (db: AsyncDuckDB, acceptedFile: File) => {
    try {
      const fileName = acceptedFile.name.replace(".duckdb", "");
      // Connect to the database
      const cid = await db.connectInternal();
      // Register the file
      await db.registerFileBuffer(fileName, new Uint8Array(await acceptedFile.arrayBuffer()));
      // Attach the database
      await db.runQuery(cid, `ATTACH '${fileName}' (READ_ONLY)`);
      // Disconnect from the database
      await db.disconnect(cid);
    } catch (err: any) {
      console.log(err);
    }
  }