Asynchronous, non-blocking SQLite3 bindings for Node.js
npx @tessl/cli install tessl/npm-sqlite3@5.1.0SQLite3 provides asynchronous, non-blocking SQLite3 bindings for Node.js. It offers a comprehensive interface for SQLite database operations including query execution, parameter binding, full Buffer/Blob support, extensive debugging capabilities, query serialization API, and extension support.
npm install sqlite3const sqlite3 = require('sqlite3').verbose();For ES modules:
import sqlite3 from 'sqlite3';
const { Database, Statement, Backup } = sqlite3;const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database(':memory:');
db.serialize(() => {
db.run("CREATE TABLE lorem (info TEXT)");
const stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (let i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
db.each("SELECT rowid AS id, info FROM lorem", (err, row) => {
console.log(row.id + ": " + row.info);
});
});
db.close();SQLite3 is built around several key components:
sqlite3.cachedCore database functionality for connecting, querying, and managing SQLite databases. Supports both callback-based and event-driven patterns.
class Database extends EventEmitter {
constructor(filename: string, mode?: number, callback?: (err: Error | null) => void);
run(sql: string, params?: any, callback?: (this: RunResult, err: Error | null) => void): this;
get<T>(sql: string, params?: any, callback?: (this: Statement, err: Error | null, row: T) => void): this;
all<T>(sql: string, params?: any, callback?: (this: Statement, err: Error | null, rows: T[]) => void): this;
each<T>(sql: string, params?: any, callback?: (this: Statement, err: Error | null, row: T) => void, complete?: (err: Error | null, count: number) => void): this;
// Enhanced event management with automatic SQLite configuration
addListener(event: string, listener: (...args: any[]) => void): this;
removeListener(event: string, listener: (...args: any[]) => void): this;
removeAllListeners(event?: string): this;
}Statement preparation and execution with parameter binding. Provides efficient query execution for repeated operations.
class Statement extends EventEmitter {
bind(params?: any, callback?: (err: Error | null) => void): this;
run(params?: any, callback?: (this: RunResult, err: Error | null) => void): this;
get<T>(params?: any, callback?: (this: RunResult, err: Error | null, row?: T) => void): this;
all<T>(params?: any, callback?: (this: RunResult, err: Error | null, rows: T[]) => void): this;
finalize(callback?: (err: Error) => void): Database;
}Database backup and restoration functionality with step-by-step control and progress monitoring.
class Backup extends EventEmitter {
step(pages: number, callback?: (err: Error | null) => void): this;
finish(callback?: (err: Error | null) => void): this;
readonly remaining: number;
readonly pageCount: number;
readonly completed: boolean;
readonly failed: boolean;
readonly idle: boolean;
}SQLite constants for database modes, error codes, and configuration limits.
// Database mode flags
const OPEN_READONLY: number;
const OPEN_READWRITE: number;
const OPEN_CREATE: number;
// Error codes
const OK: number;
const ERROR: number;
const BUSY: number;
// ... and many more
// Version information
const VERSION: string;
const VERSION_NUMBER: number;/**
* Enables verbose mode with enhanced stack trace support
* @returns The sqlite3 module with verbose mode enabled
*/
function verbose(): sqlite3;const cached: {
/**
* Returns cached database instances based on filename
* @param filename - Database file path
* @param mode - Optional database mode
* @param callback - Optional callback function
* @returns Database instance (cached or new)
*/
Database(filename: string, mode?: number, callback?: (this: Database, err: Error | null) => void): Database;
};interface RunResult extends Statement {
/** ID of the last inserted row */
lastID: number;
/** Number of rows changed by the last statement */
changes: number;
}
interface sqlite3 {
Database: typeof Database;
Statement: typeof Statement;
Backup: typeof Backup;
cached: typeof cached;
verbose(): this;
// All constants (mode flags, error codes, limits)
OPEN_READONLY: number;
OPEN_READWRITE: number;
// ... (all other constants)
}