Skip to content

Instantly share code, notes, and snippets.

@ilyabo
Created March 10, 2024 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilyabo/4c5a6421c55fb9fdca09c04081b21d75 to your computer and use it in GitHub Desktop.
Save ilyabo/4c5a6421c55fb9fdca09c04081b21d75 to your computer and use it in GitHub Desktop.
duckdb-wasm conversion errors not thrown
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
const getDb = async () => {
const duckdb = window.duckdbduckdbWasm;
// @ts-ignore
if (window._db) return window._db;
const JSDELIVR_BUNDLES = duckdb.getJsDelivrBundles();
// Select a bundle based on browser checks
const bundle = await duckdb.selectBundle(JSDELIVR_BUNDLES);
const worker_url = URL.createObjectURL(
new Blob([`importScripts("${bundle.mainWorker}");`], {
type: 'text/javascript',
})
);
// Instantiate the asynchronus version of DuckDB-wasm
const worker = new Worker(worker_url);
// const logger = null //new duckdb.ConsoleLogger();
const logger = new duckdb.ConsoleLogger();
const db = new duckdb.AsyncDuckDB(logger, worker);
await db.instantiate(bundle.mainModule, bundle.pthreadWorker);
URL.revokeObjectURL(worker_url);
window._db = db;
return db;
};
</script>
<script type="module">
import * as duckdbduckdbWasm from 'https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@1.28.1-dev106.0/+esm';
window.duckdbduckdbWasm = duckdbduckdbWasm;
getDb().then(async (db) => {
// Create a new connection
const conn = await db.connect();
// Prepare query
const query = `select '24:01:01'::time`;
const stmt = await conn.prepare(query);
// ... and run the query with materialized results
document.write(
'<pre>' +
'Query: ' +
query +
'\n\nResult: ' +
JSON.stringify((await stmt.query()).toArray()) +
'<pre>'
);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment