-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
79 lines (64 loc) · 2.32 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!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>
<button id="load">load</button>
<button id="count">count</button>
<script type="module">
import * as duckdb from "https://cdn.jsdelivr.net/npm/@duckdb/[email protected]/+esm";
const getDb = async () => {
if (window._db) {
return window._db;
}
// Select a bundle based on browser checks
const jsdelivr_bundles = duckdb.getJsDelivrBundles();
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;
};
getDb();
window.duckdb = {
load: async () => {
const db = await getDb();
const conn = await db.connect();
const stmt = await conn.query(
`create table athletes as from "https://idl.uw.edu/mosaic/data/athletes.parquet"`
);
console.log('load');
await conn.close();
},
query: async () => {
const db = await getDb();
const conn = await db.connect();
const result = (await conn.query(`select count(*) as nb from athletes`)).toArray().map((row) => row.toJSON());
await conn.close();
return result;
}
};
</script>
<script>
document.addEventListener('DOMContentLoaded', function() {
const loadButton = document.getElementById('load');
loadButton.addEventListener('click', window.duckdb.load);
const countButton = document.getElementById('count');
countButton.addEventListener('click', window.duckdb.count);
});
</script>
</body>
</html>