better-sqlite3-typescript for TypeScript: complete guide with examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course
\n

better-sqlite3-typescript for TypeScript: complete guide with examples

\n\n

In the world of TypeScript development, there is often a need for local data storage without deploying a full-fledged database. SQLite is an ideal solution for such tasks: it is an embedded relational database that works without a separate server. However, the standard better-sqlite3 library is written in JavaScript and does not provide built-in typing. This is where better-sqlite3-typescript comes to the rescue — a wrapper that adds strict TypeScript typing on top of the synchronous better-sqlite3 API.

\n\n

This library allows you to write type-safe code for working with SQLite, getting autocompletion in your IDE, type checking at compile time, and reducing the number of runtime errors. You get all the benefits of synchronous database access (simplicity, predictability, high performance) with the power of the TypeScript type system.

\n\n

In this article, we will thoroughly cover installation, main methods, provide real examples, and give performance tips. The material will be useful for both beginners and experienced developers who want to effectively use SQLite in their TypeScript projects.

\n\n

Installation

\n\n

To install the library, run the following command in your project's terminal:

\n\n
npm install better-sqlite3-typescript better-sqlite3
\n\n

You will also need to install the types for TypeScript:

\n\n
npm install -D @types/better-sqlite3
\n\n

After installation, you can import the library into your project:

\n\n
import Database from 'better-sqlite3-typescript';
\n\n

Quick Start

\n\n

A minimal working example of creating a database, creating a table, and inserting data:

\n\n
import Database from 'better-sqlite3-typescript';\n\n// Create or open a database\nconst db = new Database('my_database.db');\n\n// Create a table\ndb.exec(`\n  CREATE TABLE IF NOT EXISTS users (\n    id INTEGER PRIMARY KEY AUTOINCREMENT,\n    name TEXT NOT NULL,\n    email TEXT UNIQUE NOT NULL,\n    age INTEGER\n  )\n`);\n\n// Insert data\nconst insert = db.prepare('INSERT INTO users (name, email, age) VALUES (?, ?, ?)');\ninsert.run('Alice', 'alice@example.com', 30);\ninsert.run('Bob', 'bob@example.com', 25);\n\n// Read data\nconst select = db.prepare('SELECT * FROM users');\nconst users = select.all();\nconsole.log(users);\n\n// Close the connection\ndb.close();
\n\n

This code will create a my_database.db file in the current directory, create a users table, add two users, and print them to the console.

\n\n

Main Methods and Functions

\n\n

Let's look at the key methods of the library. All examples assume you already have a database instance db.

\n\n

1. new Database(filename, options?)

\n

Signature: new Database(filename: string, options?: DatabaseOptions): Database

\n

Description: Creates or opens an SQLite database. If the file does not exist, it will be created. Options allow you to configure the opening mode (readonly, memory, etc.).

\n

Example:

\n
// Open an in-memory database (not saved to disk)\nconst memDb = new Database(':memory:');\n\n// Open a database in read-only mode\nconst readOnlyDb = new Database('data.db', { readonly: true });
\n\n

2. db.prepare(sql)

\n

Signature: prepare(sql: string): Statement

\n

Description: Compiles an SQL query into a prepared statement. Returns a Statement object that can be executed multiple times with different parameters.

Recommendations