Skip to content

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.

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"]
ComponentResponsibility
BootstrapDatabaseReads environment values and validates typed settings.
ConnectionManager / IConnectionManagerResolves and reuses named connections.
DB / IQueryBuilderSelects tables, executes SQL and controls transactions without models.
RawQueryBuilderBuilds query plans over tables.
Model / BuilderAdd attributes, persistence and relationships.
SQLCompilerTranslates plans and definitions to the selected dialect.
Connection / IConnectionAcquires resources, executes statements, buffers results and manages transactions.
Schema, migrator and seedersDefine structures, apply changes and populate data.

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 JSONResponse
from 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.

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.

  1. Configuration is loaded and validated without connecting to a server.
  2. The manager creates and caches Connection when its name is first resolved.
  3. The engine is created when I/O is needed; constructing the object does not check connectivity or credentials.
  4. Without an explicit transaction, each operation acquires a physical connection, starts a temporary transaction, buffers its results and commits or rolls back before release.
  5. Inside an explicit transaction, operations from its owning task reuse the same physical connection.
  6. 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.

Database command reference: syntax, arguments and options for application migrations, internal storage, inspection and wiping.

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.