https://github.com/siddhantgoel/tornado-sqlalchemy
SQLAlchemy support for Tornado
https://github.com/siddhantgoel/tornado-sqlalchemy
asynchronous asyncio database orm python sqlalchemy tornado web
Last synced: 7 months ago
JSON representation
SQLAlchemy support for Tornado
- Host: GitHub
- URL: https://github.com/siddhantgoel/tornado-sqlalchemy
- Owner: siddhantgoel
- License: mit
- Created: 2017-06-30T16:27:38.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2024-06-06T23:29:19.000Z (over 1 year ago)
- Last Synced: 2025-03-29T15:04:15.968Z (7 months ago)
- Topics: asynchronous, asyncio, database, orm, python, sqlalchemy, tornado, web
- Language: Python
- Homepage: https://tornado-sqlalchemy.readthedocs.io/en/latest/
- Size: 277 KB
- Stars: 123
- Watchers: 6
- Forks: 20
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.txt
Awesome Lists containing this project
README
# tornado-sqlalchemy
[](https://travis-ci.org/siddhantgoel/tornado-sqlalchemy)
[](https://pypi.python.org/pypi/tornado-sqlalchemy)
[](https://tornado-sqlalchemy.readthedocs.io/en/latest/)
[](https://pypi.python.org/pypi/tornado-sqlalchemy)
Python helpers for using [SQLAlchemy] with [Tornado].
## Installation
```sh
$ pip install tornado-sqlalchemy
```
In case you prefer installing from the Github repository, please note that `main` is the
development branch so `stable` is what you should be installing from.
## Usage
```python
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.8+ 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/`.
[docker-compose]: https://docs.docker.com/compose/
[Poetry]: https://poetry.eustace.io/
[Read The Docs]: https://tornado-sqlalchemy.readthedocs.io/en/stable/
[SQLAlchemy]: http://www.sqlalchemy.org/
[tornado]: https://www.tornadoweb.org/en/stable/