gino.ext package

Submodules

gino.ext.aiohttp module

class gino.ext.aiohttp.AiohttpModelMixin[源代码]

基类:object

coroutine get_or_404(*args, **kwargs)[源代码]
class gino.ext.aiohttp.AiohttpStrategy[源代码]

基类:gino.strategies.GinoStrategy

engine_cls

GinoEngine 的别名

name = 'aiohttp'
class gino.ext.aiohttp.Gino(bind=None, model_classes=None, query_ext=True, schema_ext=True, ext=True, **kwargs)[源代码]

基类:gino.api.Gino

Support aiohttp.web server.

The common usage looks like this:

from aiohttp import web
from gino.ext.aiohttp import Gino

db = Gino()
app = web.Application(middlewares=[db])
db.init_app(app)

By init_app() GINO subscribes to a few signals on aiohttp, so that GINO could use database configuration to initialize the bound engine.

The configuration can be passed in the config parameter of init_app, or if that is not set, in app['config']['gino'], both of which should be a dictionary.

The config includes:

  • driver - the database driver, default is asyncpg.
  • host - database server host, default is localhost.
  • port - database server port, default is 5432.
  • user - database server user, default is postgres.
  • password - database server password, default is empty.
  • database - database name, default is postgres.
  • dsn - a SQLAlchemy database URL to create the engine, its existence will replace all previous connect arguments.
  • pool_min_size - the initial number of connections of the db pool.
  • pool_max_size - the maximum number of connections in the db pool.
  • echo - enable SQLAlchemy echo mode.
  • ssl - SSL context passed to asyncpg.connect, default is None.
  • kwargs - other parameters passed to the specified dialects, like asyncpg. Unrecognized parameters will cause exceptions.

If the db is set as an aiohttp middleware, 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)
参数:
  • bind -- A GinoEngine instance to bind. Also accepts string or URL, which will be passed to create_engine() when this Gino instance is awaited. Default is None.
  • model_classes -- A tuple of base class and mixin classes to create the Model class. Default is (CRUDModel, ).
  • query_ext -- Boolean value to control the installation of the gino extension on Executable for implicit execution. Default is to install (True).
  • schema_ext -- Boolean value to control the installation of the gino extension on SchemaItem for implicit execution. Default is to install (True).
  • ext -- Boolean value to control the installation of the two gino extensions. False for no extension at all, while it depends on the two individual switches when this is set to True (default).
  • kwargs -- Other arguments accepted by MetaData.
coroutine first_or_404(*args, **kwargs)[源代码]
init_app(app, config=None)[源代码]
model_base_classes = (<class 'gino.crud.CRUDModel'>, <class 'gino.ext.aiohttp.AiohttpModelMixin'>)
query_executor

GinoExecutor 的别名

coroutine set_bind(bind, loop=None, **kwargs)[源代码]

Bind self to the given GinoEngine and return it.

If the given bind is a string or URL, all arguments will be sent to create_engine() to create a new engine, and return it.

返回:GinoEngine
class gino.ext.aiohttp.GinoConnection(dialect, sa_conn, stack=None)[源代码]

基类:gino.engine.GinoConnection

coroutine first_or_404(*args, **kwargs)[源代码]
class gino.ext.aiohttp.GinoEngine(dialect, pool, loop, logging_name=None, echo=None, execution_options=None)[源代码]

基类:gino.engine.GinoEngine

connection_cls

GinoConnection 的别名

coroutine first_or_404(*args, **kwargs)[源代码]
class gino.ext.aiohttp.GinoExecutor(query)[源代码]

基类:gino.api.GinoExecutor

coroutine first_or_404(*args, **kwargs)[源代码]

gino.ext.quart module

gino.ext.sanic module

class gino.ext.sanic.Gino(app=None, *args, **kwargs)[源代码]

基类:gino.api.Gino

Support Sanic web server.

By init_app() GINO registers a few hooks on Sanic, so that GINO could use database configuration in Sanic config to initialize the bound engine.

A lazy connection context is enabled by default for every request. You can change this default behavior by setting DB_USE_CONNECTION_FOR_REQUEST config value to False. 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)
coroutine first_or_404(*args, **kwargs)[源代码]
init_app(app)[源代码]
model_base_classes = (<class 'gino.crud.CRUDModel'>, <class 'gino.ext.sanic.SanicModelMixin'>)
query_executor

GinoExecutor 的别名

coroutine set_bind(bind, loop=None, **kwargs)[源代码]

Bind self to the given GinoEngine and return it.

If the given bind is a string or URL, all arguments will be sent to create_engine() to create a new engine, and return it.

返回:GinoEngine
class gino.ext.sanic.GinoConnection(dialect, sa_conn, stack=None)[源代码]

基类:gino.engine.GinoConnection

coroutine first_or_404(*args, **kwargs)[源代码]
class gino.ext.sanic.GinoEngine(dialect, pool, loop, logging_name=None, echo=None, execution_options=None)[源代码]

基类:gino.engine.GinoEngine

connection_cls

GinoConnection 的别名

coroutine first_or_404(*args, **kwargs)[源代码]
class gino.ext.sanic.GinoExecutor(query)[源代码]

基类:gino.api.GinoExecutor

coroutine first_or_404(*args, **kwargs)[源代码]
class gino.ext.sanic.SanicModelMixin[源代码]

基类:object

coroutine get_or_404(*args, **kwargs)[源代码]
class gino.ext.sanic.SanicStrategy[源代码]

基类:gino.strategies.GinoStrategy

engine_cls

GinoEngine 的别名

name = 'sanic'

gino.ext.tornado module

GINO provides a convenient plugin for integrating with Tornado webserver.

Provide tornado-specific methods on models

GINO can provide a web framework-aware .get_or_404() method which work similar to .get() but raises an appropriate error whenever requested object not found. In case of tornado, an appropriate error is tornado.web.HTTPError(404).

To have it working, simply use gino.ext.tornado.Gino as your database metadata.

Integrate GINO with application

GINO provides two ways to initialize the db instances for tornado:

1) Initialize by db.init_app(app), and get the Gino instance in the RequestHandler subclasses by self.application.db.

2) For subclassed tornado Application, use gino.ext.tornado.DBMixin and run init_db().

Request Handler Mixin

A mixin to provide a convenience property access to db using self.db instead of self.application.db.

An example application

A hello world application that uses tornado and GINO may look like this:

import ssl

import tornado.web
import tornado.ioloop
import tornado.options
import tornado.escape

from gino.ext.tornado import Gino, RequestHandlerMixin

# Define your database metadata
# -----------------------------

db = Gino()


# Define tables as you would normally do
# --------------------------------------

class User(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer(), primary_key=True, autoincrement=True)
    nickname = db.Column(db.Unicode(), nullable=False)


# Now just use your tables
# ------------------------

class AllUsers(tornado.web.RequestHandler, RequestHandlerMixin):
    async def get(self):
        users = await User.query.gino.all()

        for user in users:
            url = self.application.reverse_url('user', user.id)
            nickname = tornado.escape.xhtml_escape(user.nickname)
            self.write(f'<a href="{url}">{nickname}</a><br/>')


class GetUser(tornado.web.RequestHandler, RequestHandlerMixin):
    async def get(self, uid):
        async with self.db.acquire() as conn:
            async with conn.transaction():
                user: User = await User.get_or_404(int(uid))
                self.write(f'Hi, {user.nickname}!')


if __name__ == '__main__':
    app = tornado.web.Application([
        tornado.web.URLSpec(r'/', AllUsers, name='index'),
        tornado.web.URLSpec(r'/users/(?P<uid>[0-9]+)', GetUser,
                            name='user')
    ], debug=True)
    tornado.ioloop.IOLoop.current().run_sync(
        lambda: db.init_app(app, ssl=True))
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

API reference

class gino.ext.tornado.DBMixin[源代码]

基类:object

A mixin for tornado.web.Application to initialize and have convenience methods for database accesses.

db = None
coroutine init_db(db: gino.ext.tornado.Gino, **kwargs)[源代码]
class gino.ext.tornado.Gino(bind=None, model_classes=None, query_ext=True, schema_ext=True, ext=True, **kwargs)[源代码]

基类:gino.api.Gino

Base class for GINO database.

Using this class as a metadata for your database adds an additional get_or_404() method to all of your table classes.

参数:
  • bind -- A GinoEngine instance to bind. Also accepts string or URL, which will be passed to create_engine() when this Gino instance is awaited. Default is None.
  • model_classes -- A tuple of base class and mixin classes to create the Model class. Default is (CRUDModel, ).
  • query_ext -- Boolean value to control the installation of the gino extension on Executable for implicit execution. Default is to install (True).
  • schema_ext -- Boolean value to control the installation of the gino extension on SchemaItem for implicit execution. Default is to install (True).
  • ext -- Boolean value to control the installation of the two gino extensions. False for no extension at all, while it depends on the two individual switches when this is set to True (default).
  • kwargs -- Other arguments accepted by MetaData.
coroutine first_or_404(*args, **kwargs)[源代码]
coroutine init_app(app, *, loop=None, dsn='', driver='asyncpg', host='localhost', port=5432, user='postgres', password='', database='postgres', echo=False, pool_min_size=5, pool_max_size=10, ssl=None, **kwargs)[源代码]

Initialize database

参数:
  • app -- tornado.web.Application
  • loop -- User-defined event loop. If not defined, tornado default

loop will be used. :param driver: the database driver, default is asyncpg. :param host: database server host, default is localhost. :param port: database server port, default is 5432. :param user: database server user, default is postgres. :param password: database server password, default is empty. :param database: database name, default is postgres. :param dsn: a SQLAlchemy database URL to create the engine, its existence will replace all previous connect arguments. :param echo: enable SQLAlchemy echo mode. :param pool_min_size: the initial number of connections of the db pool, default is 5. :param pool_max_size: the maximum number of connections in the db pool, default is 10. :param ssl: SSL context passed to asyncpg.connect, default is None. This can be True or an instance of ssl.SSLContext. :param kwargs: other parameters passed to the specified dialects, like asyncpg. Unrecognized parameters will cause exceptions.

model_base_classes = (<class 'gino.crud.CRUDModel'>, <class 'gino.ext.tornado.TornadoModelMixin'>)
query_executor

GinoExecutor 的别名

coroutine set_bind(bind, loop=None, **kwargs)[源代码]

Bind self to the given GinoEngine and return it.

If the given bind is a string or URL, all arguments will be sent to create_engine() to create a new engine, and return it.

返回:GinoEngine
class gino.ext.tornado.GinoConnection(dialect, sa_conn, stack=None)[源代码]

基类:gino.engine.GinoConnection

coroutine first_or_404(*args, **kwargs)[源代码]
class gino.ext.tornado.GinoEngine(dialect, pool, loop, logging_name=None, echo=None, execution_options=None)[源代码]

基类:gino.engine.GinoEngine

connection_cls

GinoConnection 的别名

coroutine first_or_404(*args, **kwargs)[源代码]
class gino.ext.tornado.GinoExecutor(query)[源代码]

基类:gino.api.GinoExecutor

coroutine first_or_404(*args, **kwargs)[源代码]
class gino.ext.tornado.RequestHandlerMixin[源代码]

基类:object

A mixin to provide convenience methods to access GINO object

db
class gino.ext.tornado.TornadoModelMixin[源代码]

基类:object

coroutine get_or_404(*args, **kwargs)[源代码]
class gino.ext.tornado.TornadoStrategy[源代码]

基类:gino.strategies.GinoStrategy

engine_cls

GinoEngine 的别名

name = 'tornado'

Module contents