better-sqlite3
Synchronous SQLite bindings for Node.js with prepared statements, transactions and high performance.
Official Documentation ↗v13.0.3. Check the official docs for the latest API surface.Installation
npm install better-sqlite3pnpm add better-sqlite3yarn add better-sqlite3Methods
No methods match this filter.
Overview
better-sqlite3 exposes SQLite through synchronous, prepared-statement APIs. Because SQLite is embedded, synchronicity removes callback overhead and keeps the code linear, at the cost of blocking the event loop during queries.
Core Methods
Open a Database, prepare statements, then run, get or all to execute them. db.transaction wraps a batch in a real transaction, and pragmas tune behavior such as journal mode and foreign keys.
Usage Notes
Prepare statements once and reuse them, wrap multi-row writes in a transaction, and enable WAL for read-heavy workloads. Always bind parameters instead of string concatenation.
Common Pitfalls
Synchronous calls block the event loop, so avoid unbounded scans in a web request path. One connection per thread is required. String-built SQL invites injection and defeats statement caching.
FAQ
- Why is better-sqlite3 synchronous?
- SQLite is an embedded database, so a synchronous API avoids the overhead of async plumbing and is often faster for local reads and writes. Avoid long-running statements because they block the event loop.
- How do I enable WAL mode?
- Execute PRAGMA journal_mode = WAL once after opening the database. WAL improves concurrent read performance and reduces writer blocking for many workloads.
- Is it safe to use across worker threads?
- Each thread needs its own database connection. Sharing one connection across threads is unsafe; open a connection per thread and rely on WAL for concurrency.
Report a problem with this API docOpens a pre-filled issue on GitHub.