Home

Awesome

gino-starlette

Codacy Badge

Introduction

An extension for GINO to support starlette server.

Usage

The common usage looks like this:

from starlette.applications import Starlette
from gino.ext.starlette import Gino

app = Starlette()
db = Gino(app, **kwargs)

Configuration

GINO adds a middleware to the Starlette app to setup and cleanup database according to the configurations that passed in the kwargs parameter.

The config includes:

NameDescriptionDefault
driverthe database driverasyncpg
hostdatabase server hostlocalhost
portdatabase server port5432
userdatabase server userpostgres
passworddatabase server passwordempty
databasedatabase namepostgres
dsna SQLAlchemy database URL to create the engine, its existence will replace all previous connect arguments.N/A
retry_timesthe retry times when database failed to connect20
retry_intervalthe interval in seconds between each time of retry5
pool_min_sizethe initial number of connections of the db pool.N/A
pool_max_sizethe maximum number of connections in the db pool.N/A
echoenable SQLAlchemy echo mode.N/A
sslSSL context passed to asyncpg.connectNone
use_connection_for_requestflag to set up lazy connection for requests.N/A
retry_limitthe number of retries to connect to the database on start up.1
retry_intervalseconds to wait between retries.1
kwargsother parameters passed to the specified dialects, like asyncpg. Unrecognized parameters will cause exceptions.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)