Skip to main content

Performant async database support for Python.

Project description

Morcilla

Test Suite Package version

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+


Installation

$ 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.


Quickstart

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.

Why hard fork?

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.

Why "morcilla"?

Morcilla means blood sausage in Spanish. So the name reflects the bloody mess gory nature of the code.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

morcilla-0.5.37.tar.gz (15.6 kB view hashes)

Uploaded Source

Built Distribution

morcilla-0.5.37-py3-none-any.whl (15.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page