Skip to content

@saflib/drizzle


index ​

Classes ​

ClassDescription
DbManagerA 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.
HandledDatabaseErrorA subclass of Error which is used to indicate that an error was caught and handled by the database library. Database packages should subclass this error, and these are not necessarily considered bugs if they occur.
UnhandledDatabaseErrorA subclass of Error which is used to indicate that an error was not caught and handled by the database library. The cause of the error is not propagated, since consumers of the database libary should not have access to underlying SQL issues.

Interfaces ​

InterfaceDescription
AddressA shared address type that can be used across the platform.
DbManagerOptionsOptions for constructing a DbManager.
DbOptionsWhen a "connection" is created, these parameters are provided.
FileMetadataFieldsTypeScript interface for file metadata fields. Use this in your Entity interface when your table includes fileMetadataColumns. Add created_at / updated_at on the entity itself (not via this interface).

Type Aliases ​

Type AliasDescription
CreateOnDiskDbKeyAccessorOptions-
DbConnectionThe result of calling drizzle, typed to the schema you connected to.
DbKeyA symbol returned when connecting to the database. This key should be provided to any queries used by the consumer of the package. This is the only way a database consumer may interact with the database.
DbTransactionConvenience type; the tx object passed to the drizzle transaction callback, with a generic parameter for the schema.
EqualTo be used with "Expect" to check explicit table interfaces match Drizzle's inferred interfaces.
ExpectTo be used with "Equal" to check explicit table interfaces match Drizzle's inferred interfaces.
FkCascadeViolation-
SchemaCurrently this package expects the schema to be an object where some values are the result of sqliteTable calls. Organize your schema in this fashion when creating your DbManager and such.
TransactionCallbackConvenience type; the first parameter of the transaction method, with a generic parameter for the schema.

Variables ​

VariableDescription
addressSchemaDrizzle schema for Address type. Use this in your table definitions for address fields.
fileMetadataColumnsCommon file metadata columns for tables that reference files stored in Azure Blob Storage. Use these columns when your table stores metadata about a file (blob_name, file_original_name, mimetype, size). Timestamps (created_at / updated_at) are not included — declare them on each table so non-file tables and file tables share the same timestamp pattern.

Functions ​

FunctionDescription
assertNoFkCascadesThrow if any migration SQL or schema TS in packageRoot declares FK cascades. Defaults to process.cwd() so package tests can call it with no args.
createOnDiskDbKeyAccessorLazy singleton for package-owned SQLite files (cron-db, jobs-db, audit-db, …).
findFkCascadeViolationsFind FK cascade declarations in a drizzle package.
generateShortIdShort random ID (12 base64url chars by default). Uses crypto.getRandomValues; URL-safe. Default byteLength 9 → 72 bits of entropy. Optional byteLength override for callers that need a different width.
packageSqlitePathAbsolute SQLite path for a package-owned on-disk DB file.
queryWrapperAll queries should use this wrapper. It will catch and obfuscate unhandled errors, and rethrow handled errors, though really handled errors should be returned, not thrown.