Tortoise-ORM Quart integration¶
We have a lightweight integration util tortoise.contrib.quart which has a single function register_tortoise which sets up Tortoise-ORM on startup and cleans up on teardown.
Note that the modules path can not be __main__ as that changes depending on the launch point. One wants to be able to launch a Quart service from the ASGI runner directly, so all paths need to be explicit.
See the Quart Example
Usage¶
QUART_APP=main quart
...
Commands:
generate-schemas Populate DB with Tortoise-ORM schemas.
run Start and run a development server.
shell Open a shell within the app context.
# To generate schemas
QUART_APP=main quart generate-schemas
# To run
QUART_APP=main quart run
Reference¶
-
tortoise.contrib.quart.register_tortoise(app, config=
None, config_file=None, db_url=None, modules=None, generate_schemas=False)[source]¶ Registers
before_servingandafter_servinghooks to set-up and tear-down Tortoise-ORM inside a Quart service. It also registers a CLI commandgenerate_schemasthat will generate the schemas.You can configure using only one of
config,config_fileand(db_url, modules).- Parameters:¶
- app¶
Quart app.
- config=
None¶ Dict containing config:
Example
{ 'connections': { # Dict format for connection 'default': { 'engine': 'tortoise.backends.asyncpg', 'credentials': { 'host': 'localhost', 'port': '5432', 'user': 'tortoise', 'password': 'qwerty123', 'database': 'test', } }, # Using a DB_URL string 'default': 'postgres://postgres:qwerty123@localhost:5432/events' }, 'apps': { 'models': { 'models': ['__main__'], # If no default_connection specified, defaults to 'default' 'default_connection': 'default', } } }- config_file=
None¶ Path to .json or .yml (if PyYAML installed) file containing config with same format as above.
- db_url=
None¶ Use a DB_URL string. See DB_URL
- modules=
None¶ Dictionary of
key: [list_of_modules] that defined “apps” and modules that should be discovered for models.- generate_schemas=
False¶ True to generate schema immediately. Only useful for dev environments or SQLite
:memory:databases
- Raises:¶
ConfigurationError – For any configuration error