返回介绍

Starlette Support

发布于 2024-06-23 19:15:23 字数 3658 浏览 0 评论 0 收藏 0

Work with Starlette

To use GINO with Starlette, the gino-starlette package should be installed first:

pip install gino-starlette

注解

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

driver

the database driver

asyncpg

host

database server host

localhost

port

database server port

5432

user

database server user

postgres

password

database server password

empty

database

database name

postgres

dsn

a SQLAlchemy database URL to create the engine, its existence will replace all previous connect arguments.

N/A

pool_min_size

the initial number of connections of the db pool.

N/A

pool_max_size

the maximum number of connections in the db pool.

N/A

echo

enable SQLAlchemy echo mode.

N/A

ssl

SSL context passed to asyncpg.connect

None

use_connection_for_request

flag to set up lazy connection for requests.

N/A

kwargs

other 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文