Using SQLite with Bun: A Complete Guide
Bun ships with a native SQLite driver built directly into the runtime. No npm install, no native compilation, no configuration. Just import { Database } from "bun:sqlite" and you're working with SQLite. It's remarkably fast, often faster than better-sqlite3, and the API is clean.
Getting Started
That's it. No dependencies, no setup.
Basic Operations
Creating Tables
Inserting Data
Querying Data
Prepared Statements
For repeated queries, prepare once and reuse:
Prepared statements are significantly faster for repeated operations.
Transactions
Wrap multiple operations in a transaction:
Transactions also dramatically improve performance for bulk inserts, hundreds of times faster than individual inserts.
TypeScript Support
Bun's SQLite has excellent TypeScript support:
Working with JSON
SQLite has JSON functions, and Bun handles them well:
Performance Tips
Use WAL Mode
Write-Ahead Logging improves concurrent read/write performance:
This should be one of the first things you do after opening the database.
Use Prepared Statements
The difference is significant:
Batch with Transactions
Individual inserts commit separately. Transactions batch commits:
The batched version can be 100x faster.
Common Patterns
Repository Pattern
Migrations
When to Use Bun SQLite
Great for:
- Single-server applications
- CLI tools and scripts
- Development and prototyping
- Embedded databases in desktop apps
- Testing (fast in-memory databases)
- Read-heavy workloads
Consider alternatives when:
- You need multi-server access (use PostgreSQL/MySQL)
- You need high write concurrency (SQLite has a single writer)
- Your data exceeds what fits comfortably on one disk
- You need advanced features like full-text search at scale
Comparison to other SQLite Libraries
Bun's SQLite is comparable to better-sqlite3 but with some advantages:
- No native compilation: Works immediately, no node-gyp issues
- Bundled: No dependency to install
- Fast: Performance is on par with or better than better-sqlite3
- TypeScript-first: Generic type parameters built in
The API is synchronous (like better-sqlite3), which is actually ideal for SQLite since it's file-based and synchronous anyway.
Conclusion
Bun's SQLite integration is one of the best features of the runtime. Zero setup, excellent performance, clean API, full TypeScript support. For many applications, especially tools, prototypes, and single-server deployments. It's all you need.
The combination of Bun's speed and SQLite's simplicity is compelling. If you're building something that doesn't need the complexity of a client-server database, give it a try.