-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup DuckDB Wasm with Stimulus for client-side queries
- Loading branch information
Showing
9 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
import { startDuckDB, stopDuckDB } from "helpers/duckdb"; | ||
|
||
// Connects to data-controller="duckdb" | ||
export default class extends Controller { | ||
static values = { | ||
databasePath: String, | ||
} | ||
|
||
async connect() { | ||
this.db = await startDuckDB(this.databasePathValue); | ||
window.duckdb = this.db; | ||
this.dispatch("ready"); | ||
} | ||
|
||
async disconnect() { | ||
await stopDuckDB(this.db); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import * as duckdb from "@duckdb/duckdb-wasm"; | ||
|
||
export async function startDuckDB(databasePath) { | ||
if (!databasePath) { | ||
throw new Error("Cannot start DuckDB, invalid database path: '" + databasePath + "'"); | ||
} | ||
|
||
const db = await initialize(); | ||
await openDatabase(db, databasePath); | ||
return db; | ||
} | ||
|
||
export async function stopDuckDB(database) { | ||
if (database) { | ||
await database.close(); | ||
} | ||
} | ||
|
||
async function initialize() { | ||
const bundles = duckdb.getJsDelivrBundles(); | ||
const bundle = await duckdb.selectBundle(bundles); | ||
|
||
const worker_url = URL.createObjectURL( | ||
new Blob([`importScripts("${bundle.mainWorker}");`], { | ||
type: "text/javascript" | ||
}) | ||
); | ||
|
||
const worker = new Worker(worker_url); | ||
const logger = new duckdb.ConsoleLogger(); | ||
const db = new duckdb.AsyncDuckDB(logger, worker); | ||
await db.instantiate(bundle.mainModule, bundle.pthreadWorker); | ||
URL.revokeObjectURL(worker_url); | ||
return db; | ||
} | ||
|
||
async function openDatabase(db, databasePath) { | ||
const response = await fetch(databasePath, { | ||
cache: "default" | ||
}); | ||
|
||
const buffer = await response.arrayBuffer(); | ||
await db.registerFileBuffer("database.duckdb", new Uint8Array(buffer)); | ||
await db.open({ | ||
path: "database.duckdb", | ||
query: { | ||
castTimestampToDate: true | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "helpers/duckdb" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,6 @@ | |
pin "@hotwired/turbo-rails", to: "turbo.min.js" | ||
pin "@hotwired/stimulus", to: "stimulus.min.js" | ||
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js" | ||
pin "@duckdb/duckdb-wasm", to: "https://cdn.jsdelivr.net/npm/@duckdb/[email protected]/+esm" | ||
pin_all_from "app/javascript/controllers", under: "controllers" | ||
pin_all_from "app/javascript/helpers", under: "helpers" |
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.