Skip to content

@saflib/drizzle


Class: DbManager<S, C> ​

A class which mainly manages "connections" to the sqlite3 database and drizzle ORM. Any package which depends on this will create a single instance given the database schema and config, export the public interface, and be used by queries to access the drizzle ORM. This way the package which depends on @saflib/drizzle has full access to its database, but packages which depend on it only have access to an opaque key which only database queries can use.

Type Parameters ​

Type Parameter
S extends Schema
C extends Config

Constructors ​

Constructor ​

new DbManager<S, C>>(schema, c, rootUrl, options?): DbManager<S, C>>

Parameters ​

ParameterType
schemaS
cC
rootUrlstring
options?DbManagerOptions

Returns ​

DbManager<S, C>

Methods ​

attachConnection() ​

attachConnection(key, sqlitePath, options?): void

Open an on-disk SQLite file and register it under an existing DbKey (after disconnect removed the prior connection). Used for audit seal rotation so the process keeps the same key while replacing the file.

Parameters ​

ParameterType
keysymbol
sqlitePathstring
options?DbOptions

Returns ​

void


backupTo() ​

backupTo(key, destinationPath): Promise<void>>

Online SQLite backup of the database registered under key to destinationPath. Uses better-sqlite3's db.backup() (SQLite Online Backup API), so the source DB can stay open and writable during the copy.

The destination file is overwritten if it exists. Parent directory must exist.

Parameters ​

ParameterType
keysymbol
destinationPathstring

Returns ​

Promise<void>


clearAllTablesForTests() ​

clearAllTablesForTests(key): void

Deletes all application rows from every table in the connected database. Preserves __drizzle_migrations. For query unit tests that reuse one in-memory connection per file (beforeAll connect + beforeEach reset).

Parameters ​

ParameterType
keysymbol

Returns ​

void


connect() ​

connect(options?): symbol

Creates a "connection" to a database.

If onDisk is true, the database will be created on disk, in a "data" folder, with the name of the current environment. If onDisk is a string, the database will be created at the given (absolute) path.

Parameters ​

ParameterType
options?DbOptions

Returns ​

symbol


createBackup() ​

createBackup(key): Promise<undefined | Readable>>

Creates a backup of the database file and returns a readable stream with automatic cleanup. The backup file is created in the same directory as the original database file with a unique temporary name. The stream will automatically clean up the temporary file when it's closed or garbage collected.

Parameters ​

ParameterType
keysymbol

Returns ​

Promise<undefined | Readable>


disconnect() ​

disconnect(key): boolean

Parameters ​

ParameterType
keysymbol

Returns ​

boolean


get() ​

get(key): undefined | DbConnection<S>>

Parameters ​

ParameterType
keysymbol

Returns ​

undefined | DbConnection<S>


getDbPath() ​

getDbPath(key): undefined | string

SQLite path for the key, or :memory:, or undefined if unknown.

Parameters ​

ParameterType
keysymbol

Returns ​

undefined | string


publicInterface() ​

publicInterface(): object

Returns ​

attachConnection() ​

attachConnection: (key, sqlitePath, options?) => void

Open an on-disk SQLite file and register it under an existing DbKey (after disconnect removed the prior connection). Used for audit seal rotation so the process keeps the same key while replacing the file.

Parameters ​
ParameterType
keysymbol
sqlitePathstring
options?DbOptions
Returns ​

void

backupTo() ​

backupTo: (key, destinationPath) => Promise<void>>

Online SQLite backup of the database registered under key to destinationPath. Uses better-sqlite3's db.backup() (SQLite Online Backup API), so the source DB can stay open and writable during the copy.

The destination file is overwritten if it exists. Parent directory must exist.

Parameters ​
ParameterType
keysymbol
destinationPathstring
Returns ​

Promise<void>

clearAllTablesForTests() ​

clearAllTablesForTests: (key) => void

Deletes all application rows from every table in the connected database. Preserves __drizzle_migrations. For query unit tests that reuse one in-memory connection per file (beforeAll connect + beforeEach reset).

Parameters ​
ParameterType
keysymbol
Returns ​

void

connect() ​

connect: (options?) => symbol

Creates a "connection" to a database.

If onDisk is true, the database will be created on disk, in a "data" folder, with the name of the current environment. If onDisk is a string, the database will be created at the given (absolute) path.

Parameters ​
ParameterType
options?DbOptions
Returns ​

symbol

createBackup() ​

createBackup: (key) => Promise<undefined | Readable>>

Creates a backup of the database file and returns a readable stream with automatic cleanup. The backup file is created in the same directory as the original database file with a unique temporary name. The stream will automatically clean up the temporary file when it's closed or garbage collected.

Parameters ​
ParameterType
keysymbol
Returns ​

Promise<undefined | Readable>

disconnect() ​

disconnect: (key) => boolean

Parameters ​
ParameterType
keysymbol
Returns ​

boolean

getDbPath() ​

getDbPath: (key) => undefined | string

SQLite path for the key, or :memory:, or undefined if unknown.

Parameters ​
ParameterType
keysymbol
Returns ​

undefined | string

restore() ​

restore: (key, stream) => Promise<void>>

Parameters ​
ParameterType
keysymbol
streamReadable
Returns ​

Promise<void>

walCheckpointTruncate() ​

walCheckpointTruncate: (key) => void

Flush WAL pages into the main db file and reset the WAL (SQLite backup-safe).

Parameters ​
ParameterType
keysymbol
Returns ​

void


restore() ​

restore(key, stream): Promise<void>>

Parameters ​

ParameterType
keysymbol
streamReadable

Returns ​

Promise<void>


walCheckpointTruncate() ​

walCheckpointTruncate(key): void

Flush WAL pages into the main db file and reset the WAL (SQLite backup-safe).

Parameters ​

ParameterType
keysymbol

Returns ​

void