Morcilla gives you simple and high-performant asyncio support for a range of databases. The project is a hard fork of encode/databases.
It allows you to make queries using the powerful SQLAlchemy Core expression language, and provides support for PostgreSQL and SQLite.
Morcilla is suitable for integrating against any async Web framework.
Requirements: Python 3.8+
$ pip install morcilla
You can install the required database drivers with:
$ pip install morcilla[postgresql]
$ pip install morcilla[sqlite]
Default driver support is provided using one of asyncpg or aiosqlite.
Note that if you are using any synchronous SQLAlchemy functions such as engine.create_all()
or alembic migrations then you still have to install a synchronous DB driver: psycopg2 for PostgreSQL.
For this example we'll create a very simple SQLite database to run some queries against.
$ pip install morcilla[sqlite]
$ pip install ipython
We can now run a simple example from the console.
Note that we want to use ipython
here, because it supports using await
expressions directly from the console.
# Create a database instance, and connect to it.
from morcilla import Database
database = Database('sqlite:///example.db')
await database.connect()
# Create a table.
query = """CREATE TABLE HighScores (id INTEGER PRIMARY KEY, name VARCHAR(100), score INTEGER)"""
await database.execute(query=query)
# Insert some data.
query = "INSERT INTO HighScores(name, score) VALUES (:name, :score)"
values = [
{"name": "Daisy", "score": 92},
{"name": "Neil", "score": 87},
{"name": "Carol", "score": 43},
]
await database.execute_many(query=query, values=values)
#Â Run a database query.
query = "SELECT * FROM HighScores"
rows = await database.fetch_all(query=query)
print('High Scores:', rows)
Check out the documentation on making database queries for examples of how to start using morcilla together with SQLAlchemy core expressions.
Morcilla satisfies one particular requirement that Athenian has: to provide the best performance
at any cost, while sacrificing as little developer experience as possible. Hence it rejects
the uniform Record interface that encode/databases
provides in favor of native backend objects.
Thus there is no guarantee that the same code will work equally successful for all the supported
DB backends. Besides, we have optimized Morcilla for asyncpg, so e.g. asyncpg performs JSON serialization and
deserialization instead of sqlalchemy for performance boost. The character of the changes is
very much breaking the existing code, and they should not be submitted upstream.
Finally, we are going to add new backends such as Clickhouse in the future.
Morcilla means blood sausage in Spanish. So the name reflects the bloody mess gory nature of the code.