Skip to main content

database

Multi-driver async SQL database module with support for SQLite, PostgreSQL, MySQL, SQL Server, and Oracle. Provides named connection management, query execution, full schema introspection, security policies, business annotations, read replica routing, and transaction control.

PropertyValue
Module IDdatabase
Version1.0.0
Typedatabase
PlatformsAll
Async Driversaiosqlite, asyncpg, aiomysql, aioodbc, oracledb
Declared Permissionsdatabase:read, database:write, database:admin

Actions (28)

Connection Management

ActionDescriptionKey Parameters
connectOpen named database connectionconnection_id, driver, database, host, port, username, password_env, policy, role, group, persist, options
disconnectClose connection (or all with *)connection_id
list_connectionsList active connections with metadata
pingCheck connection liveness with auto-reconnectconnection_id, reconnect

Query Execution

ActionDescriptionKey Parameters
execute_queryRaw SQL (DDL, DML)connection_id, query, parameters
fetch_resultsSELECT with safety LIMIT injectionconnection_id, query, parameters, limit
explain_queryShow query execution plan (EXPLAIN)connection_id, query, params, analyze
fetch_paginatedCursor-based pagination for large resultsconnection_id, query, params, page_size, cursor
query_historyRecent query history with timing and errorsconnection_id, limit, errors_only

Security:

  • @requires_permission(Permission.DATABASE_WRITE) for execute_query
  • @audit_trail("detailed") for execute_query
  • All queries use parameterized binding — SQL injection is prevented by design

Schema Introspection

ActionDescriptionKey Parameters
list_tablesList all tables with columns and indexesconnection_id
get_table_schemaDetailed column types, nullability, PKsconnection_id, table
table_statsRow count and size statsconnection_id, table
sampleFetch sample rows for data shapeconnection_id, table, limit
introspectFull schema dump (all tables)connection_id
describeFull table context: schema + stats + samples + annotationsconnection_id, table
schema_diffCompare schema against saved baselineconnection_id, save_snapshot

Business Context

ActionDescriptionKey Parameters
annotateAdd/update annotations on tables or columnsconnection_id, table, description, columns
get_annotationsRetrieve annotationsconnection_id, table, column

Security & Audit

ActionDescriptionKey Parameters
set_policyUpdate connection security policyconnection_id, policy
get_audit_logRetrieve audit log entriesconnection_id, limit

Transaction Control

ActionDescriptionKey Parameters
begin_transactionStart transactionconnection_id
commit_transactionCommitconnection_id
rollback_transactionRollbackconnection_id

Watcher Integration

ActionDescriptionKey Parameters
list_itemsList watchable items (tables/views)connection_id
checksumCompute content hashes for change detectionconnection_id, item_ids
extract_for_indexExtract schema for index moduleconnection_id

Read Replica Routing

Connections with the same group and different role values form a replica set:

- action: connect
params:
connection_id: main_db
role: primary
group: main
# ...

- action: connect
params:
connection_id: main_replica
role: replica
group: main
# ...

Read queries on the group are automatically distributed across replicas via round-robin.


Security Policies

Per-connection policies control what queries are allowed:

PresetDescription
readonlyOnly SELECT queries allowed
safe_writeSELECT + INSERT/UPDATE, no DDL
unrestrictedAll queries allowed

Fine-grained control with set_policy:

- action: set_policy
params:
connection_id: main_db
policy:
preset: safe_write
allowed_tables: [users, orders]
blocked_tables: [credentials]
blocked_keywords: [DROP, TRUNCATE, ALTER]
max_rows: 10000

App YAML Configuration

The database module is fully configurable via the Digitorn app YAML system. See docs/app-config.yaml for a complete reference with all options.

Use digitorn app schema database to view the full YAML schema in the terminal.


Implementation Notes

  • Named connection IDs allow multiple concurrent database connections
  • SQLAlchemy async engine with connection pooling (pool_size, max_overflow, pool_recycle)
  • SQLite: WAL mode enabled, foreign key enforcement on
  • Persistent connections survive daemon restarts when persist: true
  • Parameterized queries only — string interpolation is never used
  • Audit log tracks all queries with timestamps, durations, and blocked status
  • Annotations persist across restarts and enrich LLM context