Query
DuckDB-Wasm provides functions for querying data. Queries are run sequentially.
First, a connection needs to be created by calling connect. Then, queries can be run by calling query or send.
Query Execution
// Create a new connectionconst conn = await db.connect();
// Either materialize the query resultawait conn.query<{ v: arrow.Int }>(` SELECT * FROM generate_series(1, 100) t(v)`);// ..., or fetch the result chunks lazilyfor await (const batch of await conn.send<{ v: arrow.Int }>(` SELECT * FROM generate_series(1, 100) t(v)`)) { // ...}
// Close the connection to release memoryawait conn.close();Prepared Statements
// Create a new connectionconst conn = await db.connect();// Prepare queryconst stmt = await conn.prepare(`SELECT v + ? FROM generate_series(0, 10_000) t(v);`);// ... and run the query with materialized resultsawait stmt.query(234);// ... or result chunksfor await (const batch of await stmt.send(234)) { // ...}// Close the statement to release memoryawait stmt.close();// Closing the connection will release statements as wellawait conn.close();Arrow Table to JSON
// Create a new connectionconst conn = await db.connect();
// Queryconst arrowResult = await conn.query<{ v: arrow.Int }>(` SELECT * FROM generate_series(1, 100) t(v)`);
// Convert arrow table to jsonconst result = arrowResult.toArray().map((row) => row.toJSON());
// Close the connection to release memoryawait conn.close();Export Parquet
// Create a new connectionconst conn = await db.connect();
// Export Parquetconn.send(`COPY (SELECT * FROM tbl) TO 'result-snappy.parquet' (FORMAT parquet);`);const parquet_buffer = await this._db.copyFileToBuffer('result-snappy.parquet');
// Generate a download linkconst link = URL.createObjectURL(new Blob([parquet_buffer]));
// Close the connection to release memoryawait conn.close();