• Stars
    star
    125
  • Rank 286,335 (Top 6 %)
  • Language
    Python
  • License
    MIT License
  • Created over 7 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

SQLAlchemy support for Tornado

tornado-sqlalchemy

https://travis-ci.org/siddhantgoel/tornado-sqlalchemy.svg?branch=stable https://readthedocs.org/projects/tornado-sqlalchemy/badge/?version=latest

Python helpers for using SQLAlchemy with Tornado.

Installation

$ pip install tornado-sqlalchemy

In case you prefer installing from the Github repository, please note that master is the development branch so stable is what you should be installing from.

Usage

from tornado.gen import coroutine
from tornado.web import Application, RequestHandler
from tornado_sqlalchemy import as_future, SessionMixin, SQLAlchemy

class NativeCoroutinesRequestHandler(SessionMixin, RequestHandler):
    async def get(self):
        with self.make_session() as session:
            count = await as_future(session.query(User).count)

        self.write('{} users so far!'.format(count))

class GenCoroutinesRequestHandler(SessionMixin, RequestHandler):
    @coroutine
    def get(self):
        with self.make_session() as session:
            count = yield as_future(session.query(User).count)

        self.write('{} users so far!'.format(count))

class SynchronousRequestHandler(SessionMixin, RequestHandler):
    def get(self):
        with self.make_session() as session:
            count = session.query(User).count()

        self.write('{} users so far!'.format(count))

handlers = (
   (r'/native-coroutines', NativeCoroutinesRequestHandler),
   (r'/gen-coroutines', GenCoroutinesRequestHandler),
   (r'/sync', SynchronousRequestHandler),
)

app = Application(
   handlers,
   db=SQLAlchemy('postgres://user:password@host/database')
)

Documentation

Documentation is available at Read The Docs.

Development

Please make sure you have Python 3.5+ and Poetry installed.

Since we run tests against multiple databases (currently MySQL, PostgreSQL, and SQLite), we use docker-compose to make our lives easier.

  1. Git clone the repository - git clone https://github.com/siddhantgoel/tornado-sqlalchemy
  2. Install the packages required for development - poetry install
  3. Ensure that the MySQL and PostgreSQL services (containers) are up - docker-compose up -d
  4. That should basically be it. You should now be able to run the test suite - poetry run py.test tests/.