Skip to main content
Scraps: sqlite
1 items
SQLite – API | Bun Docs
**Summary of SQLite Documentation for Bun** - **Purpose**: SQLite documentation index for Bun, a runtime package manager, bundler, and test runner. - **Key Features**: - Bun natively implements a high-performance SQLite3 driver. - Supports bigint, multi-query statements, and map query results to classes without an ORM. - Faster performance than other SQLite drivers for JavaScript. - Better SQL support, including prepared statements and datatype conversions (BLOB becomes Uint8Array). - Benchmarked on an M1 MacBook Pro, showing superior performance compared to other SQLite libraries (e.g., better-sqlite3 and deno.land/x/sqlite). - **Usage**: - Import the `Database` class from `bun:sqlite`: ```typescript import { Database } from "bun:sqlite"; ``` - Initialize a new database instance: ```typescript const db = new Database(":memory:"); ``` - Execute queries using the `query` method: ```typescript const query = db.query("SELECT 'Hello world' as message;"); console.log(query.get()); // { message: "Hello world" } ``` - Use transactions for grouped operations: ```typescript const insertCat = db.prepare("INSERT INTO cats (name) VALUES ($name)"); const insertCats = db.transaction(cats => { for (const cat of cats) insertCat.run(cat); }); ``` - Load SQLite extensions using `loadExtension` method: ```typescript db.loadExtension("myext"); ``` - Control file behavior using `fileControl` method: ```typescript db.fileControl(constants.SQLITE_FCNTL_PERSIST_WAL, 0); ``` - **Strict Mode**: Enables binding values without prefixes using `strict: true` option: ```typescript const db = new Database(":memory:", { strict: true }); const query = db.query("SELECT $message;"); query.all({ message: "Hello world" }); // strict: true ``` - **Load via ES module import**: Load databases using ES module import syntax: ```typescript import db from "./mydb.sqlite" with { type: "sqlite" }; console.log(db.query("select * from users LIMIT 1").get()); ``` - **WAL (Write-Ahead Logging) Mode**: Enable WAL mode using `PRAGMA journal_mode = WAL;`, and disable persistent WAL on macOS to remove sidecar files after closing the database. - **Statements**: Create statements using `query` or `prepare` methods, and execute them using `all`, `get`, `run`, `values`, or `finalize` methods. - **Binding Values**: Bind values to statements using named or positional parameters (`$param`, `?1`, etc.). - **Datatypes**: Map JavaScript types to SQLite types, supporting bigint, Uint8Array for BLOB, and null for NULL. - **Contributing**: Contribute to Bun's development by following the roadmap, benchmarking, and submitting pull requests.
pinboardbookmarkdocumentationJun 19, 2024
sqlitejavascriptcodejavascript_ecosystem_evolutiondatabase_performance_optimizationruntime_package_manager_advancements
image shared
Conf: tags 84% summary 100%