import { fetch } from "https://esm.town/v/std/fetch";
export let duckdbExample = (async () => {
async function createWorker(url: string) {
const workerScript = await fetch(url);
const workerURL = URL.createObjectURL(await workerScript.blob());
return new Worker(workerURL, { type: "module" });
}
const duckdb = await import(
"https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@1.28.1-dev234.0/+esm"
);
const bundles = duckdb.getJsDelivrBundles();
const bundle = await duckdb.selectBundle(bundles);
const logger = new duckdb.ConsoleLogger();
const worker = await createWorker(bundle.mainWorker);
const db = new duckdb.AsyncDuckDB(logger, worker);
await db.instantiate(bundle.mainModule);
const c = await db.connect();
const version = await db.getVersion();
console.log(version);
const res = await c.query(`SELECT * FROM generate_series(1, 100) t(v)`);
c.close();
db.terminate();
return res.toArray().map((r) => {
const row = r.toJSON();
return JSON.parse(JSON.stringify(row, (k, v) => {
return typeof v === "bigint" ? +v.toString() : v;
}));
});
})();