メインコンテンツまでスキップ
メインコンテンツまでスキップ

chDB for Bun

chDB-bunは、chDBのための実験的なFFI(Foreign Function Interface)バインディングを提供し、ClickHouseクエリをBunアプリケーション内で外部依存関係なしに直接実行できます。

インストール

ステップ 1: システム依存関係をインストール

まず、必要なシステム依存関係をインストールします。

libchdbをインストール

curl -sL https://lib.chdb.io | bash

ビルドツールをインストール

システムにgccまたはclangがインストールされている必要があります。

ステップ 2: chDB-bunをインストール


# Install from the GitHub repository
bun add github:chdb-io/chdb-bun


# Or clone and build locally
git clone https://github.com/chdb-io/chdb-bun.git
cd chdb-bun
bun install
bun run build

使用法

chDB-bunは、クエリモードとして一時的クエリ(ワンタイム操作用)と永続セッション(データベースの状態を維持するため)をサポートしています。

一時的クエリ

状態を永続化する必要のない単純な一回限りのクエリの場合:

import { query } from 'chdb-bun';

// Basic query
const result = query("SELECT version()", "CSV");
console.log(result); // "23.10.1.1"

// Query with different output formats
const jsonResult = query("SELECT 1 as id, 'Hello' as message", "JSON");
console.log(jsonResult);

// Query with calculations
const mathResult = query("SELECT 2 + 2 as sum, pi() as pi_value", "Pretty");
console.log(mathResult);

// Query system information
const systemInfo = query("SELECT * FROM system.functions LIMIT 5", "CSV");
console.log(systemInfo);

永続セッション

クエリ間で状態を維持する必要がある複雑な操作の場合:

import { Session } from 'chdb-bun';

// Create a session with persistent storage
const 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', '[email protected]'),
        (2, 'Bob', '[email protected]'),
        (3, 'Charlie', '[email protected]')
    `, "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
}