better-sqlite3
better-sqlite3 is the fastest and simplest SQLite3 library for Node.js, providing a synchronous API that is significantly faster than asynchronous alternatives …
Installation
npm install better-sqlite3
yarn add better-sqlite3
pnpm add better-sqlite3
Import
import Database from 'better-sqlite3';
Quick Example
import Database from 'better-sqlite3';
const db = new Database('mydb.sqlite');
db.exec(`CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
)`);
const insert = db.prepare('INSERT INTO users (name) VALUES (?)');
insert.run('Alice');
const user = db.prepare('SELECT * FROM users WHERE name = ?').get('Alice');About better-sqlite3
better-sqlite3 is the fastest and simplest SQLite3 library for Node.js, providing a synchronous API that is significantly faster than asynchronous alternatives for typical database workloads. Unlike other SQLite bindings that wrap every operation in asynchronous callbacks or promises, better-sqlite3 leverages the fact that SQLite operations on a local database file complete in microseconds, making the overhead of async wrapping counterproductive. The synchronous API is simpler to use and easier to reason about: const row = db.prepare('SELECT * FROM users WHERE id = ?').get(id). The library supports transactions (which are automatically rolled back on error), user-defined functions (scalar, aggregate, and table-valued), virtual tables, WAL mode for concurrent reading, and backup operations. better-sqlite3 uses N-API for native bindings, ensuring stability across Node.js versions. The library pre-compiles statements for repeated execution, supports parameter binding for SQL injection prevention, and provides iterator-based result streaming for large queries. better-sqlite3 is the SQLite driver used by Drizzle ORM and is popular for local databases, embedded applications, testing, and serverless functions where a file-based database eliminates the need for external database services.
Quick Facts
| Package | better-sqlite3 |
| Category | Database |
| Weekly Downloads | 1.5M+ |
| License | MIT |
| Install | npm install better-sqlite3 |
Related Packages
pg (node-postgres) is the standard PostgreSQL client for Node.js, providing low-level access to Post…
mysql2 is a high-performance MySQL client for Node.js that provides prepared statements, non-UTF-8 e…
Drizzle ORM is a TypeScript ORM that provides a SQL-like query builder with full type safety, zero o…
Knex.js is a batteries-included SQL query builder for Node.js that supports PostgreSQL, MySQL, Maria…
Prisma is a next-generation Node.js and TypeScript ORM that provides a declarative data modeling lan…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.