auto_rest.models¶
The models
module facilitates communication with relational databases
via dynamically generated object relational mappers (ORMs). Building
on the popular SQLAlchemy package, it natively supports multiple
Database Management Systems (DBMS) without requiring custom configuration
or setup.
Example: Mapping Database Metadata
Utility functions are provided for connecting to the database and mapping the underlying schema.
connection_args = dict(...)
db_url = create_db_url(**connection_args)
db_conn = create_db_engine(db_url)
db_meta = create_db_metadata(db_conn)
Support for asynchronous operations is automatically determined based on the chosen database. If the driver supports asynchronous operations, the connection and session handling are configured accordingly.
Developer Note
When working with database objects, the returned object type may vary
depending on whether the underlying driver is synchronous or asynchronous.
Of particular note are database engines (Engine
/ AsyncEngine
) and
sessions (Session
/ AsyncSession
).
create_db_engine(url, **kwargs)
¶
Initialize a new database engine.
Instantiates and returns an Engine
or AsyncEngine
instance depending
on whether the database URL uses a driver with support for async operations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
URL
|
A fully qualified database URL. |
required |
**kwargs
|
dict[str:any]
|
Keyword arguments passed to |
{}
|
Returns:
Type | Description |
---|---|
DBEngine
|
A SQLAlchemy |
Source code in auto_rest/models.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
create_db_metadata(engine)
¶
Create and reflect metadata for the database connection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine
|
DBEngine
|
The database engine to use for reflection. |
required |
Returns:
Type | Description |
---|---|
MetaData
|
A MetaData object reflecting the database schema. |
Source code in auto_rest/models.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
create_db_url(driver, database, host=None, port=None, username=None, password=None)
¶
Create a database URL from the provided parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
driver
|
str
|
The SQLAlchemy-compatible database driver. |
required |
database
|
str
|
The database name or file path (for SQLite). |
required |
host
|
str | None
|
The database server hostname or IP address. |
None
|
port
|
int | None
|
The database server port number. |
None
|
username
|
str | None
|
The username for authentication. |
None
|
password
|
str | None
|
The password for the database user. |
None
|
Returns:
Type | Description |
---|---|
URL
|
A fully qualified database URL. |
Source code in auto_rest/models.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
create_session_iterator(engine)
¶
Create a generator for database sessions.
Returns a synchronous or asynchronous function depending on whether
the database engine supports async operations. The type of session
returned also depends on the underlying database engine, and will
either be a Session
or AsyncSession
instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine
|
DBEngine
|
Database engine to use when generating new sessions. |
required |
Returns:
Type | Description |
---|---|
Callable[[], DBSession]
|
A function that yields a single new database session. |
Source code in auto_rest/models.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
parse_db_settings(path)
¶
Parse engine configuration settings from a given file path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Path | None
|
Path to the configuration file. |
required |
Returns:
Type | Description |
---|---|
dict[str, any]
|
Engine configuration settings. |
Source code in auto_rest/models.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|