SQLite client for Node.js applications with SQL-based migrations API written in TypeScript
npx @tessl/cli install tessl/npm-sqlite@5.1.00
# SQLite
1
2
SQLite is a TypeScript library that provides a promise-based wrapper around the sqlite3 package with zero dependencies. It adds ES6 promises and a comprehensive migrations API to sqlite3, enabling modern async/await patterns for SQLite database operations in Node.js applications.
3
4
## Package Information
5
6
- **Package Name**: sqlite
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install sqlite sqlite3` (sqlite3 is a required peer dependency)
10
11
## Core Imports
12
13
```typescript
14
import { open, Database, Statement, ISqlite, IMigrate } from "sqlite";
15
import sqlite3 from "sqlite3";
16
```
17
18
For CommonJS:
19
20
```javascript
21
const { open, Database, Statement, ISqlite, IMigrate } = require("sqlite");
22
const sqlite3 = require("sqlite3");
23
```
24
25
For ESM (modern modules):
26
27
```javascript
28
import { open, Database, Statement, ISqlite, IMigrate } from "sqlite";
29
import sqlite3 from "sqlite3";
30
```
31
32
## Basic Usage
33
34
```typescript
35
import { open } from "sqlite";
36
import sqlite3 from "sqlite3";
37
38
// Open database
39
const db = await open({
40
filename: "./database.db",
41
driver: sqlite3.Database
42
});
43
44
// Create and populate table
45
await db.exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)");
46
await db.run("INSERT INTO users (name, email) VALUES (?, ?)", "Alice", "alice@example.com");
47
48
// Query data
49
const user = await db.get("SELECT * FROM users WHERE name = ?", "Alice");
50
const allUsers = await db.all("SELECT * FROM users");
51
52
// Run migrations
53
await db.migrate({
54
migrationsPath: "./migrations"
55
});
56
57
// Close database
58
await db.close();
59
```
60
61
## Architecture
62
63
SQLite is built around several key components:
64
65
- **Database Class**: Main promisified wrapper providing async methods for all database operations
66
- **Statement Class**: Prepared statement wrapper for reusable parameterized queries
67
- **Migration System**: SQL-based migration framework with up/down migration support
68
- **Type System**: Complete TypeScript integration with generic type support for query results
69
- **Driver Abstraction**: Support for multiple sqlite3-compatible drivers including caching
70
71
## Capabilities
72
73
### Database Opening
74
75
Core functionality for opening and configuring SQLite database connections with support for multiple drivers and caching.
76
77
```typescript { .api }
78
function open<
79
Driver extends sqlite3.Database = sqlite3.Database,
80
Stmt extends sqlite3.Statement = sqlite3.Statement
81
>(config: ISqlite.Config): Promise<Database<Driver, Stmt>>;
82
83
interface Config {
84
filename: string;
85
mode?: number;
86
driver: any;
87
}
88
```
89
90
[Database Operations](./database-operations.md)
91
92
### Prepared Statements
93
94
Prepared statement functionality for efficient reusable queries with parameter binding and result processing.
95
96
```typescript { .api }
97
class Statement<S extends sqlite3.Statement = sqlite3.Statement> {
98
bind(...params: any[]): Promise<void>;
99
run(...params: any[]): Promise<ISqlite.RunResult>;
100
get<T>(...params: any[]): Promise<T | undefined>;
101
all<T>(...params: any[]): Promise<T>;
102
reset(): Promise<void>;
103
finalize(): Promise<void>;
104
}
105
```
106
107
[Statement Operations](./statement-operations.md)
108
109
### Database Migrations
110
111
SQL-based migration system supporting up/down migrations, rollback functionality, and migration state management.
112
113
```typescript { .api }
114
interface MigrationParams {
115
force?: boolean;
116
table?: string;
117
migrationsPath?: string;
118
migrations?: readonly MigrationData[];
119
}
120
121
// Available on Database instance
122
migrate(config?: MigrationParams): Promise<void>;
123
```
124
125
[Migrations](./migrations.md)
126
127
## Core Types
128
129
```typescript { .api }
130
interface RunResult<Stmt extends sqlite3.Statement = sqlite3.Statement> {
131
stmt: Statement<Stmt>;
132
lastID?: number;
133
changes?: number;
134
}
135
136
type SqlType = SQLStatement | string;
137
138
interface SQLStatement {
139
sql: string;
140
values?: any[];
141
}
142
```
143
144
[Type Definitions](./types.md)