chDB-bun provides experimental FFI (Foreign Function Interface) bindings for chDB, enabling you to run ClickHouse queries directly in your Bun applications with zero external dependencies.
You’ll need either gcc or clang installed on your system:
2
Install chDB-bun
# Install from the GitHub repositorybun add github:chdb-io/chdb-bun# Or clone and build locallygit clone https://github.com/chdb-io/chdb-bun.gitcd chdb-bunbun installbun run build
For complex operations that require maintaining state across queries:
import { Session } from 'chdb-bun';// Create a session with persistent storageconst sess = new Session('./chdb-bun-tmp');try { // Create a database and table sess.query(` CREATE DATABASE IF NOT EXISTS mydb; CREATE TABLE IF NOT EXISTS mydb.users ( id UInt32, name String, email String ) ENGINE = MergeTree() ORDER BY id `, "CSV"); // Insert data sess.query(` INSERT INTO mydb.users VALUES (1, 'Alice', 'alice@example.com'), (2, 'Bob', 'bob@example.com'), (3, 'Charlie', 'charlie@example.com') `, "CSV"); // Query the data const users = sess.query("SELECT * FROM mydb.users ORDER BY id", "JSON"); console.log("Users:", users); // Create and use custom functions sess.query("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'Hello chDB'", "CSV"); const greeting = sess.query("SELECT hello() as message", "Pretty"); console.log(greeting); // Aggregate queries const stats = sess.query(` SELECT COUNT(*) as total_users, MAX(id) as max_id, MIN(id) as min_id FROM mydb.users `, "JSON"); console.log("Statistics:", stats);} finally { // Always cleanup the session to free resources sess.cleanup(); // This deletes the database files}