Databasic
A small async database interface built on SQLAlchemy Core and psycopg.
SQLAlchemy Core handles query construction and compilation. Databasic handles connection and transaction lifetimes and executes the resulting queries directly through psycopg.
Usage
import sqlalchemy as sa
from databasic import Databasic
users = sa.Table(
"users",
sa.MetaData(),
sa.Column("id", sa.Integer, primary_key=True),
sa.Column("name", sa.Text, nullable=False),
)
db = Databasic("postgresql://user:password@localhost/database")
async with db:
async with db.session() as session:
rows = await session.fetch_all(
users.select().where(users.c.name == "Alice")
)
Databasic can also be connected and disconnected explicitly. Both forms have
the same semantics; explicit lifecycle management is useful when the lifetime is
controlled by an application or framework.
db = Databasic("postgresql://user:password@localhost/database")
await db.connect()
try:
async with db.session() as session:
rows = await session.fetch_all(users.select())
finally:
await db.disconnect()
A session is a transaction. It commits when the context exits successfully and rolls back when it exits with an exception.
API
Databasic
db = Databasic(conninfo, force_rollback=False)
connect()
Open the connection pool.
await db.connect()
disconnect()
Close the connection pool.
await db.disconnect()
session()
Create a transactional session.
async with db.session() as session:
...
Successful exit commits the transaction. An exception rolls it back.
Session
execute()
Execute a statement without returning rows.
await session.execute(
users.insert().values(name="Alice")
)
execute_many()
Execute the same statement multiple times with different parameters.
query = users.insert().values(
name=sa.bindparam("name"),
)
await session.execute_many(
query,
[
{"name": "Alice"},
{"name": "Bob"},
],
)
The statement must define its values using explicit bind parameters. Values are supplied exclusively through the parameter mappings.
fetch_one()
Execute a statement and return one row, or None if there is no row.
user = await session.fetch_one(
users.select().where(users.c.id == 1)
)
A row is simply a dict[str, Any]:
{"id": 1, "name": "Alice"}
There is no result or row wrapper.
fetch_all()
Execute a statement and return all rows as a list of dictionaries.
users = await session.fetch_all(
users.select()
)
The result has the shape:
[
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"},
]
transaction()
Create a nested transaction within the current session.
async with db.session() as session:
await session.execute(
users.insert().values(name="Alice")
)
async with session.transaction():
await session.execute(
users.insert().values(name="Bob")
)
Nested transactions use database savepoints. An exception rolls back the nested transaction without rolling back earlier work in the session.
Testing
force_rollback=True runs all sessions within an outer transaction that is
rolled back when Databasic is disconnected.
async with Databasic(
"postgresql://user:password@localhost/test_database",
force_rollback=True,
) as db:
...
This allows integration tests to use normal sessions and transactions without leaving persistent database state.
Metadata
Release files for databasic 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| databasic-0.1.0.tar.gz | 3.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| databasic-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.9 kB
Release files / databasic-0.1.0.tar.gz
| Download URL | databasic-0.1.0.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
984d3eb32d62c99b59dac8649ce8c69ef6968c4264a0af80c6d915f6d73c8ae6
|
|
BLAKE2b-256 checksum How to use checksums |
c16c4b0e53beed74f48dc192cd8bbfcb0cd20a26c56adaa0154c79be59b669bd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.22 {"installer":{"name":"uv","version":"0.12.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / databasic-0.1.0-py3-none-any.whl
| Download URL | databasic-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b5810a30831dfda6647d6b7f1e7712e99d722a72474625a58dd04e17895ba775
|
|
BLAKE2b-256 checksum How to use checksums |
cd0413f9a31d80e269eea4dea85800fd1aad456fd588092e0dd8b5cfe54837d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.22 {"installer":{"name":"uv","version":"0.12.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|