import { query } from 'chdb-bun';// 기본 쿼리const result = query("SELECT version()", "CSV");console.log(result); // "23.10.1.1"// 다양한 출력 형식으로 쿼리const jsonResult = query("SELECT 1 as id, 'Hello' as message", "JSON");console.log(jsonResult);// 계산이 포함된 쿼리const mathResult = query("SELECT 2 + 2 as sum, pi() as pi_value", "Pretty");console.log(mathResult);// 시스템 정보 쿼리const systemInfo = query("SELECT * FROM system.functions LIMIT 5", "CSV");console.log(systemInfo);
import { Session } from 'chdb-bun';// 영구 저장소로 세션 생성const sess = new Session('./chdb-bun-tmp');try { // 데이터베이스 및 테이블 생성 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"); // 데이터 삽입 sess.query(` INSERT INTO mydb.users VALUES (1, 'Alice', 'alice@example.com'), (2, 'Bob', 'bob@example.com'), (3, 'Charlie', 'charlie@example.com') `, "CSV"); // 데이터 쿼리 const users = sess.query("SELECT * FROM mydb.users ORDER BY id", "JSON"); console.log("Users:", users); // 사용자 정의 함수 생성 및 사용 sess.query("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'Hello chDB'", "CSV"); const greeting = sess.query("SELECT hello() as message", "Pretty"); console.log(greeting); // 집계 쿼리 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 { // 리소스 해제를 위해 반드시 세션을 정리 sess.cleanup(); // 데이터베이스 파일을 삭제합니다}