Databases + asyncio.
Project description
Databases
Databases gives you simple asyncio support for a range of databases. Currently PostgreSQL and MySQL are supported.
$ pip install databases
Declare your tables using SQLAlchemy:
import sqlalchemy
metadata = sqlalchemy.MetaData()
notes = sqlalchemy.Table(
"notes",
metadata,
sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),
sqlalchemy.Column("text", sqlalchemy.String(length=100)),
sqlalchemy.Column("completed", sqlalchemy.Boolean),
)
You can now use SQLAlchemy core queries:
from databases import Database
database = Database('postgresql://localhost/example')
# Establish the connection pool
await database.connect()
# Execute
query = notes.insert()
values = {"text": "example1", "completed": True}
await database.execute(query, values)
# Execute many
query = notes.insert()
values = [
{"text": "example2", "completed": False},
{"text": "example3", "completed": True},
]
await database.execute_many(query, values)
# Fetch multiple rows
query = notes.select()
rows = await database.fetch_all()
# Fetch single row
query = notes.select()
row = await database.fetch_one()
Transactions are managed by async context blocks:
async with database.transaction():
...
For a lower-level transaction API:
transaction = await database.transaction()
try:
...
except:
transaction.rollback()
else:
transaction.commit()
For strict test isolation you will always want to rollback the test database to a clean state between each test case:
async with database.transaction(force_rollback=True):
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
databases-0.0.4.tar.gz
(7.4 kB
view details)
File details
Details for the file databases-0.0.4.tar.gz
.
File metadata
- Download URL: databases-0.0.4.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f8502f240859420a96d2b9b3953b1eef440ea08dfe5c61c2e9552c37a989353 |
|
MD5 | 6a9c795fc9b03ee0350e1d87e126f18b |
|
BLAKE2b-256 | e4e5bf645a2d1d0c58eab0a771dd0241f65375daa5b89b8ddc9c8721f2f34914 |