CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-sqlite3

Asynchronous, non-blocking SQLite3 bindings for Node.js

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

SQLite3

SQLite3 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.

Package Information

  • Package Name: sqlite3
  • Package Type: npm
  • Language: JavaScript (with TypeScript definitions)
  • Installation: npm install sqlite3

Core Imports

const sqlite3 = require('sqlite3').verbose();

For ES modules:

import sqlite3 from 'sqlite3';
const { Database, Statement, Backup } = sqlite3;

Basic Usage

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();

Architecture

SQLite3 is built around several key components:

  • Database Class: Main interface for database connections and operations
  • Statement Class: Prepared statement interface for efficient query execution
  • Backup Class: Database backup and restoration functionality
  • Event System: EventEmitter-based events for trace, profile, change, and error handling
  • Native Bindings: C++ implementation using Node-API for cross-platform compatibility
  • Connection Caching: Built-in database connection caching via sqlite3.cached

Capabilities

Database Operations

Core 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;
}

Database Operations

Prepared Statements

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;
}

Prepared Statements

Database Backup

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;
}

Database Backup

Constants and Configuration

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;

Constants and Configuration

Utility Functions

Verbose Mode

/**
 * Enables verbose mode with enhanced stack trace support
 * @returns The sqlite3 module with verbose mode enabled
 */
function verbose(): sqlite3;

Database Caching

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;
};

Types

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)
}

docs

backup.md

constants.md

database.md

index.md

statement.md

tile.json