UNPKG

@megaorm/sqlite

Version:

This package provides a simple, high-level, unified API for interacting with SQLite databases.

92 lines (91 loc) 3.31 kB
import { MegaDriver } from '@megaorm/driver'; import { MegaConnection } from '@megaorm/driver'; /** * SQLite driver responsible for creating SQLite connections. * @implements `MegaDriver` interface. * @example * * // Create a new SQLite driver using a file-based database * const driver = new SQLite('./database.sqlite'); * * // Create a new SQLite driver using an in-memory database * const driver = new SQLite(':memory:'); * * // Create connection * const connection = await driver.create(); * * // Execute your queries * const result = await connection.query(sql, values); * console.log(result); * * // Begin a transaction * await connection.beginTransaction(); * * // Commit transaction * await connection.commit(); * * // Rollback transaction * await connection.rollback(); * * @note * SQLite supports two types of databases: * 1. **File-based database**: The database is stored in a file on disk (e.g., `./database.sqlite`). Data is persisted. * 2. **In-memory database**: The database is stored entirely in memory and is not persisted to disk. When the connection is closed or the application stops, all data is lost. */ export declare class SQLite implements MegaDriver { /** * Unique identifier for the driver instance. */ id: Symbol; /** * The SQLite database file path. */ private path; /** * Constructs a SQLite driver with the given options. * @param path SQLite database file path like `./database.sqlite`, or `:memory:` for an in-memory database. * @example * * // Create a new SQLite driver with a file-based database * const driver = new SQLite('./database.sqlite'); * * // Create a new SQLite driver with an in-memory database * const driver = new SQLite(':memory:'); * * @note * - **File-based databases** are persistent. You can use them for long-term storage * - **In-memory databases** are non-persistent. They are faster because they don't involve file I/O, but all data is lost when the connection is closed or the application stops. Useful for testing or temporary data storage. */ constructor(path: string); /** * Creates a new SQLite connection. * @returns A `Promise` that resolves with a new SQLite connection. * @throws `CreateConnectionError` If connection creation fails. * @example * * // Create a new SQLite driver * const driver = new SQLite(path); * * // Create connection * const connection = await driver.create(); * * // Execute your queries * const result = await connection.query(sql, values); * console.log(result); * * // Begin a transaction * await connection.beginTransaction(); * * // Commit transaction * await connection.commit(); * * // Rollback transaction * await connection.rollback(); * * @note * - When using `:memory:` as the path, SQLite creates an in-memory database that is non-persistent. * - For a file-based database, the path should point to a valid file location like `./database.sqlite` * - An in-memory database can be ideal for tests and temporary storage because you lose all data once the application ends. */ create(): Promise<MegaConnection>; }