Starlette Support¶
Work with Starlette¶
To use GINO with Starlette, the gino-starlette package should be installed first:
pip install gino-starlette
Note
The gino-starlette package supports only GINO 1.0 or later. Earlier versions of GINO like 0.8.x have built-in Starlette support.
This extension provides a Starlette middleware to setup and cleanup
database according to the configurations that passed in the kwargs
parameter.
The common usage looks like this:
from starlette.applications import Starlette
from gino.ext.starlette import Gino
app = Starlette()
db = Gino(app, **kwargs)
# Or with application factory
app = Starlette()
db = Gino(**kwargs)
db.init_app(app)
Configuration¶
The config includes:
Name |
Description |
Default |
---|---|---|
|
the database driver |
|
|
database server host |
|
|
database server port |
|
|
database server user |
|
|
database server password |
empty |
|
database name |
|
|
a SQLAlchemy database URL to create the engine, its existence will replace all previous connect arguments. |
N/A |
|
the initial number of connections of the db pool. |
N/A |
|
the maximum number of connections in the db pool. |
N/A |
|
enable SQLAlchemy echo mode. |
N/A |
|
SSL context passed to |
|
|
flag to set up lazy connection for requests. |
N/A |
|
other parameters passed to the specified dialects, like |
N/A |
Lazy Connection¶
If use_connection_for_request
is set to be True, then a lazy
connection is available at request['connection']
. By default, a
database connection is borrowed on the first query, shared in the same
execution context, and returned to the pool on response. If you need to
release the connection early in the middle to do some long-running
tasks, you can simply do this:
await request['connection'].release(permanent=False)