Expand description
Raw-SQL database library for Slabs.
This crate owns the tuned SQLite WAL wrapper (Db), the optional PostgreSQL wrapper (PgDb),
URL engine detection (DbEngine), framework system schemas (SYSTEM_SCHEMA_SQL, POSTGRES_SYSTEM_SCHEMA_SQL),
and FTS5 query escaping (fts5_query). It also re-exports QueryBuilder for dynamic parameterized SQL.
§Owned types and system tables
Calling Db::init_system_tables, Db::migrate, or Db::migrate_dir (and the matching PgDb methods)
creates these 11 framework tables if they do not exist:
slabs_sessionsslabs_magic_linksslabs_access_tokensslabs_jobsslabs_cacheslabs_mail_outboxslabs_blobsslabs_webhooksslabs_webhook_deliveriesslabs_idempotency_keysslabs_audit_log
§Invariants
- Split WAL pools on file-backed
SQLite:Db::readerreturns a read-only pool with up to 16 connections for concurrentSELECTqueries.Db::writerandDb::poolreturn a dedicated single-connection writer pool (max_connections(1)). - Explicit
BEGIN IMMEDIATEtransactions:Db::beginopens aBEGIN IMMEDIATEtransaction on the single writer pool. Use it for multi-step mutations soSQLiteacquires the write lock up front and avoidsSQLITE_BUSY_SNAPSHOTlock-upgrade failures. - In-memory test databases share one connection: when
database_urlis"sqlite::memory:"or contains"mode=memory",Db::connectclones the single writer pool forDb::readerbecauseSQLiteisolates in-memory databases per connection. - Tuned
SQLitepragmas:Db::connectsetsforeign_keys = ON,synchronous = NORMAL,cache_size = -64000,temp_store = MEMORY, andbusy_timeout(10s writer, 5s reader). File-backed databases also enablejournal_mode = WALandmmap_size = 268435456(256 MiB). - Parameterized SQL only: always bind untrusted input with
sqlx::query,sqlx::query_as, orQueryBuilder. Never interpolate user strings into SQL text. - FTS5 search escaping: pass untrusted search strings through
fts5_querybefore binding to an FTS5MATCH ?clause. It strips double quotes and wraps each whitespace token as"token"*joined withAND.
§Usage
use slabs_core::SlabResult;
use slabs_db::{fts5_query, Db};
async fn example() -> SlabResult<()> {
let db = Db::connect("sqlite://app.db?mode=rwc").await?;
db.init_system_tables().await?;
// Concurrent SELECT on the read-only WAL pool.
let (count,): (i64,) = sqlx::query_as("SELECT COUNT(*) FROM slabs_jobs")
.fetch_one(db.reader())
.await?;
// Multi-step write inside an explicit BEGIN IMMEDIATE transaction.
let mut tx = db.begin().await?;
sqlx::query("DELETE FROM slabs_cache WHERE cache_key = ?")
.bind("stale_key")
.execute(&mut *tx)
.await?;
tx.commit().await?;
// Safe FTS5 query construction for user input.
if let Some(match_expr) = fts5_query("deploy \"prod\"") {
assert_eq!(match_expr, "\"deploy\"* AND \"prod\"*");
}
let _ = count;
Ok(())
}Structs§
- Audit
Entry - Persisted audit event row stored in
slabs_audit_log. - Audit
Log - Transactional audit log backed by the
slabs_audit_logsystem table. - Cursor
Page - One page of cursor-paginated results.
- Db
- Raw-SQL
SQLitedatabase wrapper tuned for concurrent WAL workloads. - Pagination
- Query parameters for cursor-based (keyset) pagination.
- PgDb
- Raw-SQL
PostgreSQLdatabase wrapper for multi-node deployments. - Query
Builder - A builder type for constructing queries at runtime.
Enums§
- DbEngine
- Database engine detected from a
DATABASE_URLstring.
Constants§
- POSTGRES_
SYSTEM_ SCHEMA_ SQL - Framework-owned
PostgreSQLDDL for the 11 system tables and their indexes. - SYSTEM_
SCHEMA_ SQL - Framework-owned
SQLiteDDL for the 11 system tables and their indexes.
Functions§
- fts5_
query - Converts untrusted user search text into an FTS5 prefix-AND
MATCHexpression.