Python/SQLAlchemy async implementation of Prisma Web3 database models
Project description
Prisma Web3 Python
Async PostgreSQL ORM layer for HawkFi and downstream bots, built on SQLAlchemy 2.x async. Models mirror the Prisma schema (managed in the JS project) and expose repositories plus 2.x-style session helpers.
Contents
- Tables & Repositories
- Session & Config
- Install
- Usage
- Testing
- CI / GitHub Actions
- Cleanup
- Best Practices
Tables & Repositories
| Model (table) | Repository | Purpose / notable fields |
|---|---|---|
Token ("Token") |
TokenRepository |
Chain-normalized token catalog; search/fuzzy/alias; batch upsert (chain, token_address, symbol, aliases, platforms). |
Signal ("Signal") |
SignalRepository |
Token alerts; source, signal_type, occurrence_count, last_occurrence. |
PreSignal ("PreSignal") |
PreSignalRepository |
Early signals; heat scores (channel_calls, multi_signals, kol_discussions). |
CryptoNews ("CryptoNews") |
CryptoNewsRepository |
News ingestion with unique source_link; JSONB fields matched_currencies, entity_list. |
AIAnalysisResult ("AIAnalysisResult") |
AIAnalysisRepository |
Unified analysis (Twitter/News/etc.); JSONB heavy (tokens, key_points, final_signal, trading_recommendation, event_struct); JSONB search/stats helpers. |
EventImpacts ("EventImpacts") |
EventImpactRepository |
Post-event pricing snapshots (price_t0/30m/4h/24h, deltas, meta). |
EventLabels ("EventLabels") |
EventLabelsRepository |
Label catalog for events. |
Utilities: ChainConfig (chain normalization), datetime helpers, TokenImporter.
Session & Config
- URL:
DATABASE_URL(orTEST_DATABASE_URLin tests).postgres:///postgresql://auto-normalized topostgresql+asyncpg://. - Pools:
DB_POOL_SIZE,DB_POOL_OVERFLOW,DB_POOL_TIMEOUT. - Engine/Session helpers (SQLAlchemy 2.x async):
from prisma_web3_py import session_scope, configure_engine, dispose_engine
# Optionally override URL (e.g., tests)
configure_engine("postgresql+asyncpg://user:pass@localhost:5432/prisma_web3")
async def main():
async with session_scope() as session: # commits on success, rollback on error
...
async with session_scope(readonly=True) as session: # read-only, no commit
...
Install
# editable dev install
pip install -e python[dev]
# or published package
pip install prisma-web3-py
Usage
from prisma_web3_py import session_scope, TokenRepository
repo = TokenRepository()
async def create_token():
async with session_scope() as session:
await repo.upsert_token(session, {"chain": "eth", "token_address": "0x...", "symbol": "UNI"})
async def search():
async with session_scope(readonly=True) as session:
tokens = await repo.search_by_symbol(session, "UNI")
Testing
- Unit-only (no DB required):
cd python
pytest -q tests/test_ai_analysis_utils.py tests/test_config_and_datetime.py tests/test_base_repository_unit.py
- Full + integration (requires Postgres):
export DATABASE_URL=postgresql+asyncpg://user:pass@localhost:5432/prisma_web3_test
export RUN_DB_TESTS=1
cd python
pytest # integration tests will run
- Schema setup in CI: call
python - <<'PY'to runinit_db()(fallback create_all) if Prisma migrations aren’t applied yet.
CI / GitHub Actions
prisma-web3/.github/workflows/publish.yml runs unit + integration tests before publish. Key envs:
DATABASE_URL– set via repo/org secrets for self-hosted DB or use Postgres service.RUN_DB_TESTS=1– enables integration suite.
Postgres service example (already in workflow):
DATABASE_URL=postgresql+asyncpg://prisma:prisma@localhost:5432/prisma_web3_test
Integration step creates schema via init_db() prior to pytest.
Cleanup
python/scripts/cleanup_test_data.py deletes test tokens/signals/pre-signals using session_scope.
Best Practices
- Use
session_scope/get_db(alias) for unit-of-work; avoid manual commits in callers. - Keep repositories thin; heavy normalization belongs in services. AIAnalysis repo currently mixes normalization with persistence—plan to extract to service layer.
- JSONB/pg_trgm features are Postgres-specific; document for consumers.
- Prefer ORM
select().scalars(),insert().on_conflict_do_update(); avoid relying on rowcount to detect changes. - Production deployments should run Prisma migrations;
init_db()is a fallback for tests/local only.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file prisma_web3_py-1.0.10.tar.gz.
File metadata
- Download URL: prisma_web3_py-1.0.10.tar.gz
- Upload date:
- Size: 476.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eea1f8fcdbbeb33faa7e692ed0592e43f9e4b0223b271389b9cfd89af3b50a4b
|
|
| MD5 |
729499f4c88b1b3bb7611544b47eab8c
|
|
| BLAKE2b-256 |
8d9bd7e8b54da26bf93dcf6055cc3ced303d5ef5c8679362481fc864a7fa9472
|
File details
Details for the file prisma_web3_py-1.0.10-py3-none-any.whl.
File metadata
- Download URL: prisma_web3_py-1.0.10-py3-none-any.whl
- Upload date:
- Size: 70.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ef86dc75f1de4a78a21d0c57f2acb671c3ad908de14985df016cedfc85b7d82
|
|
| MD5 |
97a3b6096deba13b9093c34656259922
|
|
| BLAKE2b-256 |
a9acaab13b26a72c4a494489b6dfce5de70d48ad6655ccb7d5d77b06e4b0cfa9
|