Database
Database manages named SQL connections and provides query, transaction, schema, migration and seeder infrastructure. You can execute SQL, use the query builder or work with ORM models. All three alternatives share the manager and transactions.
Await I/O operations with await. Selecting a connection, building a query and defining a table are synchronous operations; terminal operations such as get() or insert() execute SQL.
Architecture
Section titled “Architecture”flowchart TD
A["config/database.py + .env"] --> B["ConnectionManager"]
C["DB / IQueryBuilder"] --> D["RawQueryBuilder"]
E["Model / Builder"] --> F["Query plans"]
D --> F
C -->|"SQL + bindings"| G["Connection"]
F --> H["SQLCompiler"]
H --> G
B --> G
G --> I{"Driver"}
I --> J["Async engine"]
I --> K["Redshift: Core + reserved worker"]
J --> L["Database server / SQLite"]
K --> L
L --> M["Dictionaries, affected rows, InsertResult or models"]| Component | Responsibility |
|---|---|
BootstrapDatabase | Reads environment values and validates typed settings. |
ConnectionManager / IConnectionManager | Resolves and reuses named connections. |
DB / IQueryBuilder | Selects tables, executes SQL and controls transactions without models. |
RawQueryBuilder | Builds query plans over tables. |
Model / Builder | Add attributes, persistence and relationships. |
SQLCompiler | Translates plans and definitions to the selected dialect. |
Connection / IConnection | Acquires resources, executes statements, buffers results and manages transactions. |
Schema, migrator and seeders | Define structures, apply changes and populate data. |
First query from a controller
Section titled “First query from a controller”Configure a connection and create tables through migrations. This example assumes a notes table with id and body columns. Register the controller through HTTP routes:
from orionis.http import JSONResponsefrom orionis.support.facades import DB
class NoteController: async def index(self) -> JSONResponse: notes = await ( DB.table("notes") .select("id", "body") .orderBy("id") .limit(20) .get() ) return JSONResponse({"notes": notes.all()})Import the facade from orionis.support.facades. Application boot registers its services. Async services, commands, jobs and MCP components use the same API; IQueryBuilder or IConnectionManager can also be injected.
Tables inside a schema
Section titled “Tables inside a schema”Use DB.table("schema.table"), for example DB.table("sales.products"). To also select a registered connection, write DB.connection("reporting").table("sales.products"). The schema belongs to the selected database; it is not the connection name.
The query builder explains filtering, ordering and retrieving results, with alias and qualified name examples. For models, declare the schema in a TableDefinition.
Lifecycle
Section titled “Lifecycle”- Configuration is loaded and validated without connecting to a server.
- The manager creates and caches
Connectionwhen its name is first resolved. - The engine is created when I/O is needed; constructing the object does not check connectivity or credentials.
- Without an explicit transaction, each operation acquires a physical connection, starts a temporary transaction, buffers its results and commits or rolls back before release.
- Inside an explicit transaction, operations from its owning task reuse the same physical connection.
disconnect()disposes the engine and idle connections; later use can recreate it. Do not call it per request or while other tasks use the shared service.
Guides and reference
Section titled “Guides and reference”Database command reference: syntax, arguments and options for application migrations, internal storage, inspection and wiping.
- Drivers and dependencies: backends and
reactor packages. - Configuration: fields and environment variables.
- Connection management: names, injection and registration.
- DB facade: its eleven entry-point methods.
- SQL and results: bindings, return values and errors.
- Query builder: model-less queries.
- Transactions: contexts, savepoints and concurrency.
- Schemas, migrations and seeders.
- Dialects and production.
- Connection and compilation API.
Use DB without model instances; its builder returns collections of dictionaries and does not dispatch model events. Use the ORM for entity state, casts, scopes, events, relationships and factories.