@heyhru/server-util-sqlite
v0.1.2
Published
SQLite connection manager built on better-sqlite3
Readme
@heyhru/server-util-sqlite
SQLite connection manager built on better-sqlite3.
Usage
import { createSqlite, getSqlite, closeSqlite } from "@heyhru/server-util-sqlite";
// Initialize once at startup
createSqlite({
path: "./data.db",
pragmas: { journal_mode: "WAL", foreign_keys: "ON" }, // these are defaults
});
// Use anywhere in the app
const db = getSqlite();
const rows = db.prepare("SELECT * FROM users").all();
// Close on shutdown
closeSqlite();API
createSqlite(options: SqliteOptions): Database
Initialize the SQLite connection. Throws if already initialized.
Options:
| Property | Type | Required | Default |
| --------- | ----------------------- | -------- | --------------------------------------------- |
| path | string | Yes | — |
| pragmas | Record<string,string> | No | { journal_mode: "WAL", foreign_keys: "ON" } |
getSqlite(): Database
Get the initialized instance. Throws if not initialized.
closeSqlite(): void
Close the connection and reset the instance. Safe to call when not initialized.
