pico-sqlalchemy 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pico_sqlalchemy-0.1.0/.coveragerc +17 -0
- pico_sqlalchemy-0.1.0/.github/workflows/ci.yml +106 -0
- pico_sqlalchemy-0.1.0/.github/workflows/publish-to-pypi.yml +36 -0
- pico_sqlalchemy-0.1.0/CHANGELOG.md +30 -0
- pico_sqlalchemy-0.1.0/LICENSE +21 -0
- pico_sqlalchemy-0.1.0/MANIFEST.in +24 -0
- pico_sqlalchemy-0.1.0/PKG-INFO +357 -0
- pico_sqlalchemy-0.1.0/README.md +301 -0
- pico_sqlalchemy-0.1.0/docs/architecture.md +236 -0
- pico_sqlalchemy-0.1.0/docs/overview.md +197 -0
- pico_sqlalchemy-0.1.0/pyproject.toml +71 -0
- pico_sqlalchemy-0.1.0/setup.cfg +4 -0
- pico_sqlalchemy-0.1.0/src/pico_sqlalchemy/__init__.py +20 -0
- pico_sqlalchemy-0.1.0/src/pico_sqlalchemy/_version.py +1 -0
- pico_sqlalchemy-0.1.0/src/pico_sqlalchemy/base.py +10 -0
- pico_sqlalchemy-0.1.0/src/pico_sqlalchemy/config.py +23 -0
- pico_sqlalchemy-0.1.0/src/pico_sqlalchemy/decorators.py +89 -0
- pico_sqlalchemy-0.1.0/src/pico_sqlalchemy/factory.py +46 -0
- pico_sqlalchemy-0.1.0/src/pico_sqlalchemy/interceptor.py +39 -0
- pico_sqlalchemy-0.1.0/src/pico_sqlalchemy/session.py +205 -0
- pico_sqlalchemy-0.1.0/src/pico_sqlalchemy.egg-info/PKG-INFO +357 -0
- pico_sqlalchemy-0.1.0/src/pico_sqlalchemy.egg-info/SOURCES.txt +26 -0
- pico_sqlalchemy-0.1.0/src/pico_sqlalchemy.egg-info/dependency_links.txt +1 -0
- pico_sqlalchemy-0.1.0/src/pico_sqlalchemy.egg-info/requires.txt +10 -0
- pico_sqlalchemy-0.1.0/src/pico_sqlalchemy.egg-info/top_level.txt +1 -0
- pico_sqlalchemy-0.1.0/tests/test_ioc_integration.py +208 -0
- pico_sqlalchemy-0.1.0/tests/test_transaction_manager.py +47 -0
- pico_sqlalchemy-0.1.0/tox.ini +30 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[run]
|
|
2
|
+
branch = True
|
|
3
|
+
source =
|
|
4
|
+
pico_fastapi
|
|
5
|
+
omit =
|
|
6
|
+
*/_version.py
|
|
7
|
+
|
|
8
|
+
[paths]
|
|
9
|
+
pico_fastapi =
|
|
10
|
+
src/pico_fastapi
|
|
11
|
+
.tox/*/lib/*/site-packages/pico_fastapi
|
|
12
|
+
.tox/*/site-packages/pico_fastapi
|
|
13
|
+
*/site-packages/pico_fastapi
|
|
14
|
+
|
|
15
|
+
[report]
|
|
16
|
+
show_missing = True
|
|
17
|
+
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
name: CI & Coverage
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ main ]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [ main ]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
tests:
|
|
11
|
+
name: Tests (Python ${{ matrix.python-version }})
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
strategy:
|
|
14
|
+
fail-fast: false
|
|
15
|
+
matrix:
|
|
16
|
+
python-version: [ "3.10", "3.11", "3.12", "3.13", "3.14" ]
|
|
17
|
+
|
|
18
|
+
steps:
|
|
19
|
+
- name: Checkout
|
|
20
|
+
uses: actions/checkout@v4
|
|
21
|
+
with:
|
|
22
|
+
fetch-depth: 0
|
|
23
|
+
|
|
24
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
25
|
+
uses: actions/setup-python@v5
|
|
26
|
+
with:
|
|
27
|
+
python-version: ${{ matrix.python-version }}
|
|
28
|
+
cache: pip
|
|
29
|
+
|
|
30
|
+
- name: Install tox
|
|
31
|
+
run: |
|
|
32
|
+
python -m pip install --upgrade pip
|
|
33
|
+
pip install tox
|
|
34
|
+
|
|
35
|
+
- name: Install build dependencies
|
|
36
|
+
run: |
|
|
37
|
+
sudo apt-get update
|
|
38
|
+
sudo apt-get install -y build-essential
|
|
39
|
+
|
|
40
|
+
- name: Run tests with coverage (writes to repo root)
|
|
41
|
+
shell: bash
|
|
42
|
+
run: |
|
|
43
|
+
set -euxo pipefail
|
|
44
|
+
tox -e cov
|
|
45
|
+
# Rename to per-version file for artifact + merge job
|
|
46
|
+
mv .coverage .coverage.${{ matrix.python-version }}
|
|
47
|
+
ls -la .coverage.${{ matrix.python-version }}
|
|
48
|
+
|
|
49
|
+
- name: Upload .coverage artifact
|
|
50
|
+
uses: actions/upload-artifact@v4
|
|
51
|
+
with:
|
|
52
|
+
name: coverage-${{ matrix.python-version }}
|
|
53
|
+
path: .coverage.${{ matrix.python-version }}
|
|
54
|
+
include-hidden-files: true
|
|
55
|
+
if-no-files-found: error
|
|
56
|
+
|
|
57
|
+
coverage-merge:
|
|
58
|
+
name: Merge coverage
|
|
59
|
+
runs-on: ubuntu-latest
|
|
60
|
+
needs: tests
|
|
61
|
+
steps:
|
|
62
|
+
- name: Checkout (for repo context)
|
|
63
|
+
uses: actions/checkout@v4
|
|
64
|
+
|
|
65
|
+
- name: Download all coverage artifacts
|
|
66
|
+
uses: actions/download-artifact@v4
|
|
67
|
+
with:
|
|
68
|
+
path: coverage-reports
|
|
69
|
+
|
|
70
|
+
- name: Combine coverage and generate reports
|
|
71
|
+
shell: bash
|
|
72
|
+
run: |
|
|
73
|
+
set -euxo pipefail
|
|
74
|
+
python -m pip install coverage
|
|
75
|
+
files=$(find coverage-reports -type f -name ".coverage.*" -print)
|
|
76
|
+
if [ -z "$files" ]; then
|
|
77
|
+
echo "No coverage data files found"; exit 1
|
|
78
|
+
fi
|
|
79
|
+
coverage combine $files
|
|
80
|
+
coverage report -m --rcfile=.coveragerc
|
|
81
|
+
coverage xml -o coverage.xml --rcfile=.coveragerc
|
|
82
|
+
coverage html -d htmlcov --rcfile=.coveragerc
|
|
83
|
+
|
|
84
|
+
- name: Upload combined coverage XML
|
|
85
|
+
uses: actions/upload-artifact@v4
|
|
86
|
+
with:
|
|
87
|
+
name: coverage-xml
|
|
88
|
+
path: coverage.xml
|
|
89
|
+
if-no-files-found: error
|
|
90
|
+
|
|
91
|
+
- name: Upload HTML coverage report
|
|
92
|
+
uses: actions/upload-artifact@v4
|
|
93
|
+
with:
|
|
94
|
+
name: coverage-html
|
|
95
|
+
path: htmlcov
|
|
96
|
+
if-no-files-found: error
|
|
97
|
+
|
|
98
|
+
- name: Upload to Codecov
|
|
99
|
+
if: always() && !cancelled()
|
|
100
|
+
uses: codecov/codecov-action@v5
|
|
101
|
+
continue-on-error: true
|
|
102
|
+
with:
|
|
103
|
+
files: coverage.xml
|
|
104
|
+
fail_ci_if_error: false
|
|
105
|
+
token: ${{ secrets.CODECOV_TOKEN }}
|
|
106
|
+
slug: dperezcabrera/pico-sqlalchemy
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
name: Publish Python Package to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
deploy:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
permissions:
|
|
11
|
+
id-token: write
|
|
12
|
+
contents: read
|
|
13
|
+
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
with:
|
|
17
|
+
fetch-depth: 0
|
|
18
|
+
|
|
19
|
+
- name: Set up Python
|
|
20
|
+
uses: actions/setup-python@v5
|
|
21
|
+
with:
|
|
22
|
+
python-version: '3.x'
|
|
23
|
+
|
|
24
|
+
- name: Install build deps
|
|
25
|
+
run: python -m pip install --upgrade pip build
|
|
26
|
+
|
|
27
|
+
- name: Build package
|
|
28
|
+
run: python -m build
|
|
29
|
+
|
|
30
|
+
- name: Publish to PyPI
|
|
31
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
32
|
+
with:
|
|
33
|
+
skip-existing: true
|
|
34
|
+
verify-metadata: true
|
|
35
|
+
attestations: false
|
|
36
|
+
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.html).
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## [Unreleased]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
* Initial public release of `pico-sqlalchemy`.
|
|
15
|
+
* **Async-Native Core:** Built entirely on SQLAlchemy's async ORM (`AsyncSession`, `create_async_engine`).
|
|
16
|
+
* **`@transactional`** decorator providing Spring-style, async-native transactional method boundaries with propagation modes:
|
|
17
|
+
`REQUIRED`, `REQUIRES_NEW`, `SUPPORTS`, `MANDATORY`, `NOT_SUPPORTED`, and `NEVER`.
|
|
18
|
+
* **`SessionManager`** singleton responsible for:
|
|
19
|
+
* creating the SQLAlchemy `AsyncEngine`
|
|
20
|
+
* managing `AsyncSession` instances
|
|
21
|
+
* implementing async transaction semantics
|
|
22
|
+
* `await commit()` / `await rollback()` behavior
|
|
23
|
+
* **`get_session()`** helper for retrieving the currently active `AsyncSession` inside transactional methods.
|
|
24
|
+
* **`TransactionalInterceptor`** implementing AOP-based async transaction handling for methods decorated with `@transactional`.
|
|
25
|
+
* **`DatabaseSettings`** dataclass for type-safe, IOC-managed configuration of SQLAlchemy (URL, pool options, echo).
|
|
26
|
+
* **`DatabaseConfigurer`** protocol for extensible, ordered database initialization hooks (e.g., migrations, DDL, seeding).
|
|
27
|
+
* **`SqlAlchemyFactory`** to register and wire the `SessionManager` and configuration into the IoC container.
|
|
28
|
+
* **`AppBase`**, `Mapped`, and `mapped_column` declarative base components registered for SQLAlchemy models.
|
|
29
|
+
* Async-native in-memory SQLite support (`aiosqlite`) out of the box (useful for testing).
|
|
30
|
+
* Test suite validating async `SessionManager` commit/rollback behavior and transactional propagation.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 David Perez
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Incluir ficheros importantes
|
|
2
|
+
include README.md
|
|
3
|
+
include LICENSE
|
|
4
|
+
|
|
5
|
+
# Incluir todo el cรณdigo fuente
|
|
6
|
+
recursive-include src *.py
|
|
7
|
+
|
|
8
|
+
# Incluir datos de tests si quieres que otros puedan correrlos desde el sdist
|
|
9
|
+
# (opcional โ puedes quitarlo si no quieres incluir tests en PyPI)
|
|
10
|
+
recursive-include tests *.py
|
|
11
|
+
|
|
12
|
+
# Excluir cosas innecesarias
|
|
13
|
+
exclude .gitignore
|
|
14
|
+
exclude Dockerfile*
|
|
15
|
+
exclude docker-compose*.yml
|
|
16
|
+
exclude Makefile
|
|
17
|
+
|
|
18
|
+
# Excluir carpetas de build, virtualenvs, caches, etc.
|
|
19
|
+
prune build
|
|
20
|
+
prune dist
|
|
21
|
+
prune .tox
|
|
22
|
+
prune .pytest_cache
|
|
23
|
+
prune __pycache__
|
|
24
|
+
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pico-sqlalchemy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Pico-ioc integration for SQLAlchemy. Adds Spring-style transactional support, configuration, and helpers.
|
|
5
|
+
Author-email: David Perez Cabrera <dperezcabrera@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 David Perez
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/dperezcabrera/pico-sqlalchemy
|
|
29
|
+
Project-URL: Repository, https://github.com/dperezcabrera/pico-sqlalchemy
|
|
30
|
+
Project-URL: Issue Tracker, https://github.com/dperezcabrera/pico-sqlalchemy/issues
|
|
31
|
+
Keywords: ioc,di,dependency injection,sqlalchemy,transaction,orm,inversion of control,asyncio
|
|
32
|
+
Classifier: Development Status :: 4 - Beta
|
|
33
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
34
|
+
Classifier: Topic :: Database
|
|
35
|
+
Classifier: Programming Language :: Python :: 3
|
|
36
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
42
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
43
|
+
Classifier: Operating System :: OS Independent
|
|
44
|
+
Requires-Python: >=3.10
|
|
45
|
+
Description-Content-Type: text/markdown
|
|
46
|
+
License-File: LICENSE
|
|
47
|
+
Requires-Dist: pico-ioc>=2.0
|
|
48
|
+
Requires-Dist: sqlalchemy>=2.0
|
|
49
|
+
Provides-Extra: async
|
|
50
|
+
Requires-Dist: asyncpg>=0.29.0; extra == "async"
|
|
51
|
+
Provides-Extra: test
|
|
52
|
+
Requires-Dist: pytest>=8; extra == "test"
|
|
53
|
+
Requires-Dist: pytest-asyncio>=0.23.5; extra == "test"
|
|
54
|
+
Requires-Dist: pytest-cov>=5; extra == "test"
|
|
55
|
+
Dynamic: license-file
|
|
56
|
+
|
|
57
|
+
# ๐ฆ pico-sqlalchemy
|
|
58
|
+
|
|
59
|
+
[](https://pypi.org/project/pico-sqlalchemy/)
|
|
60
|
+
[](https://deepwiki.com/dperezcabrera/pico-sqlalchemy)
|
|
61
|
+
[](https://opensource.org/licenses/MIT)
|
|
62
|
+

|
|
63
|
+
[](https://codecov.io/gh/dperezcabrera/pico-sqlalchemy)
|
|
64
|
+
[](https://sonarcloud.io/summary/new_code?id=dperezcabrera_pico-sqlalchemy)
|
|
65
|
+
[](https://sonarcloud.io/summary/new_code?id=dperezcabrera_pico-sqlalchemy)
|
|
66
|
+
[](https://sonarcloud.io/summary/new_code?id=dperezcabrera_pico-sqlalchemy)
|
|
67
|
+
|
|
68
|
+
# Pico-SQLAlchemy
|
|
69
|
+
|
|
70
|
+
**Pico-SQLAlchemy** integrates **[Pico-IoC](https://github.com/dperezcabrera/pico-ioc)** with **SQLAlchemy**, providing real inversion of control for your persistence layer, with declarative repositories, transactional boundaries, and clean architectural isolation.
|
|
71
|
+
|
|
72
|
+
It brings constructor-based dependency injection, transparent transaction management, and a repository pattern inspired by the elegance of Spring Data โ but using pure Python, Pico-IoC, and SQLAlchemyโs ORM.
|
|
73
|
+
|
|
74
|
+
> ๐ Requires Python 3.10+
|
|
75
|
+
> ๐ **Async-Native:** Built entirely on SQLAlchemy's async ORM (`AsyncSession`, `create_async_engine`).
|
|
76
|
+
> ๐งฉ Works with SQLAlchemy 2.0+ ORM
|
|
77
|
+
> ๐ Automatic async transaction management
|
|
78
|
+
> ๐งช Fully testable without a running DB
|
|
79
|
+
|
|
80
|
+
With Pico-SQLAlchemy you get the expressive power of SQLAlchemy with proper IoC, clean layering, and annotation-driven transactions.
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## ๐ฏ Why pico-sqlalchemy
|
|
85
|
+
|
|
86
|
+
SQLAlchemy is powerful, but most applications end up with raw session handling, manual transaction scopes, or ad-hoc repository patterns.
|
|
87
|
+
|
|
88
|
+
Pico-SQLAlchemy provides:
|
|
89
|
+
|
|
90
|
+
* Constructor-injected repositories and services
|
|
91
|
+
* Declarative `@transactional` boundaries
|
|
92
|
+
* `REQUIRES_NEW`, `READ_ONLY`, `MANDATORY`, and all familiar propagation modes
|
|
93
|
+
* `SessionManager` that centralizes engine/session lifecycle
|
|
94
|
+
* Clean decoupling from frameworks (FastAPI, Flask, CLI, workers)
|
|
95
|
+
|
|
96
|
+
| Concern | SQLAlchemy Default | pico-sqlalchemy |
|
|
97
|
+
| :--- | :--- | :--- |
|
|
98
|
+
| Managing sessions | Manual `AsyncSession()` | Automatic |
|
|
99
|
+
| Transactions | Explicit `await commit()` / `await rollback()` | Declarative `@transactional` |
|
|
100
|
+
| Repository pattern | DIY, inconsistent | First-class `@repository` |
|
|
101
|
+
| Dependency injection | None | IoC-driven constructor injection |
|
|
102
|
+
| Testability | Manual setup | Container-managed + overrides |
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## ๐งฑ Core Features
|
|
107
|
+
|
|
108
|
+
* Repository classes with `@repository`
|
|
109
|
+
* Declarative transactions via `@transactional`
|
|
110
|
+
* Full propagation semantics (`REQUIRED`, `REQUIRES_NEW`, `MANDATORY`, etc.)
|
|
111
|
+
* Automatic `AsyncSession` lifecycle
|
|
112
|
+
* Centralized `AsyncEngine` + session factory via `SessionManager`
|
|
113
|
+
* Transaction-aware `get_session()` for repository methods
|
|
114
|
+
* Plug-and-play integration with any Pico-IoC app (FastAPI, CLI tools, workers, event handlers)
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## ๐ฆ Installation
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
pip install pico-sqlalchemy
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Also install `pico-ioc`, `sqlalchemy`, and an **async driver**:
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
pip install pico-ioc sqlalchemy
|
|
128
|
+
pip install aiosqlite # For SQLite
|
|
129
|
+
# pip install asyncpg # For PostgreSQL
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
-----
|
|
133
|
+
|
|
134
|
+
## ๐ Quick Example
|
|
135
|
+
|
|
136
|
+
### Define your model:
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
from sqlalchemy import Integer, String
|
|
140
|
+
from pico_sqlalchemy import AppBase, Mapped, mapped_column
|
|
141
|
+
|
|
142
|
+
class User(AppBase):
|
|
143
|
+
__tablename__ = "users"
|
|
144
|
+
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
145
|
+
username: Mapped[str] = mapped_column(String(50))
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Define a repository:
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
from sqlalchemy.future import select
|
|
152
|
+
from pico_sqlalchemy import repository, transactional, get_session, SessionManager
|
|
153
|
+
|
|
154
|
+
@repository
|
|
155
|
+
class UserRepository:
|
|
156
|
+
def __init__(self, manager: SessionManager):
|
|
157
|
+
self.manager = manager
|
|
158
|
+
|
|
159
|
+
@transactional
|
|
160
|
+
async def save(self, user: User) -> User:
|
|
161
|
+
session = get_session(self.manager)
|
|
162
|
+
session.add(user)
|
|
163
|
+
return user
|
|
164
|
+
|
|
165
|
+
@transactional(read_only=True)
|
|
166
|
+
async def find_all(self) -> list[User]:
|
|
167
|
+
session = get_session(self.manager)
|
|
168
|
+
stmt = select(User).order_by(User.username)
|
|
169
|
+
result = await session.scalars(stmt)
|
|
170
|
+
return list(result.all())
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Define a service:
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
from pico_ioc import component
|
|
177
|
+
|
|
178
|
+
@component
|
|
179
|
+
class UserService:
|
|
180
|
+
def __init__(self, repo: UserRepository):
|
|
181
|
+
self.repo = repo
|
|
182
|
+
|
|
183
|
+
@transactional
|
|
184
|
+
async def create(self, name: str) -> User:
|
|
185
|
+
user = User(username=name)
|
|
186
|
+
user = await self.repo.save(user)
|
|
187
|
+
|
|
188
|
+
session = get_session(self.repo.manager)
|
|
189
|
+
await session.flush()
|
|
190
|
+
await session.refresh(user)
|
|
191
|
+
return user
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Initialize Pico-IoC and run:
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
import asyncio
|
|
198
|
+
from pico_ioc import init, configuration, DictSource
|
|
199
|
+
|
|
200
|
+
config = configuration(DictSource({
|
|
201
|
+
"database": {
|
|
202
|
+
"url": "sqlite+aiosqlite:///:memory:", # Async URL
|
|
203
|
+
"echo": False
|
|
204
|
+
}
|
|
205
|
+
}))
|
|
206
|
+
|
|
207
|
+
container = init(
|
|
208
|
+
modules=["services", "repositories", "pico_sqlalchemy"],
|
|
209
|
+
config=config,
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
async def main():
|
|
213
|
+
# Use await container.aget() for async resolution
|
|
214
|
+
service = await container.aget(UserService)
|
|
215
|
+
|
|
216
|
+
# Await the async service method
|
|
217
|
+
user = await service.create("alice")
|
|
218
|
+
print(f"Created user: {user.id}")
|
|
219
|
+
|
|
220
|
+
# Clean up async resources
|
|
221
|
+
await container.cleanup_all_async()
|
|
222
|
+
|
|
223
|
+
if __name__ == "__main__":
|
|
224
|
+
asyncio.run(main())
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
-----
|
|
228
|
+
|
|
229
|
+
## ๐ Transaction Propagation Modes
|
|
230
|
+
|
|
231
|
+
Pico-SQLAlchemy supports the core Spring-inspired semantics:
|
|
232
|
+
|
|
233
|
+
| Mode | Behavior |
|
|
234
|
+
| :--- | :--- |
|
|
235
|
+
| `REQUIRED` | Join existing tx or create new |
|
|
236
|
+
| `REQUIRES_NEW` | Suspend parent and start new tx |
|
|
237
|
+
| `SUPPORTS` | Join if exists, else run without tx |
|
|
238
|
+
| `MANDATORY` | Requires existing tx |
|
|
239
|
+
| `NOT_SUPPORTED`| Run without tx, suspending parent |
|
|
240
|
+
| `NEVER` | Fail if a tx exists |
|
|
241
|
+
|
|
242
|
+
Example:
|
|
243
|
+
|
|
244
|
+
```python
|
|
245
|
+
@transactional(propagation="REQUIRES_NEW")
|
|
246
|
+
async def write_audit(self, entry: AuditEntry):
|
|
247
|
+
...
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
-----
|
|
251
|
+
|
|
252
|
+
## ๐งช Testing with Pico-IoC
|
|
253
|
+
|
|
254
|
+
You can override repositories, engines, or services easily:
|
|
255
|
+
|
|
256
|
+
```python
|
|
257
|
+
import pytest
|
|
258
|
+
import pytest_asyncio
|
|
259
|
+
from pico_ioc import init, configuration, DictSource
|
|
260
|
+
from pico_sqlalchemy import SessionManager, AppBase
|
|
261
|
+
|
|
262
|
+
# In conftest.py
|
|
263
|
+
@pytest_asyncio.fixture
|
|
264
|
+
async def container():
|
|
265
|
+
cfg = configuration(DictSource({"database": {"url": "sqlite+aiosqlite:///:memory:"}}))
|
|
266
|
+
c = init(modules=["pico_sqlalchemy", "myapp"], config=cfg)
|
|
267
|
+
|
|
268
|
+
# Setup the in-memory database
|
|
269
|
+
sm = await c.aget(SessionManager)
|
|
270
|
+
async with sm.engine.begin() as conn:
|
|
271
|
+
await conn.run_sync(AppBase.metadata.create_all)
|
|
272
|
+
|
|
273
|
+
yield c
|
|
274
|
+
|
|
275
|
+
# Clean up all async components
|
|
276
|
+
await c.cleanup_all_async()
|
|
277
|
+
|
|
278
|
+
# In your test
|
|
279
|
+
@pytest.mark.asyncio
|
|
280
|
+
async def test_my_service(container):
|
|
281
|
+
service = await container.aget(UserService)
|
|
282
|
+
user = await service.create("test")
|
|
283
|
+
assert user.id is not None
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
-----
|
|
287
|
+
|
|
288
|
+
## ๐งฌ Example: Custom Database Configurer
|
|
289
|
+
|
|
290
|
+
```python
|
|
291
|
+
from pico_sqlalchemy import DatabaseConfigurer, AppBase
|
|
292
|
+
from pico_ioc import component
|
|
293
|
+
import asyncio
|
|
294
|
+
|
|
295
|
+
@component
|
|
296
|
+
class TableCreationConfigurer(DatabaseConfigurer):
|
|
297
|
+
priority = 10
|
|
298
|
+
def __init__(self, base: AppBase):
|
|
299
|
+
self.base = base
|
|
300
|
+
|
|
301
|
+
def configure(self, engine):
|
|
302
|
+
# This configure method is called by the factory.
|
|
303
|
+
# We need to run the async setup.
|
|
304
|
+
async def setup():
|
|
305
|
+
async with engine.begin() as conn:
|
|
306
|
+
await conn.run_sync(self.base.metadata.create_all)
|
|
307
|
+
|
|
308
|
+
asyncio.run(setup())
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
Pico-SQLAlchemy will detect it and call `configure` during initialization.
|
|
312
|
+
|
|
313
|
+
-----
|
|
314
|
+
|
|
315
|
+
## โ๏ธ How It Works
|
|
316
|
+
|
|
317
|
+
* `SessionManager` is created by Pico-IoC (`SqlAlchemyFactory`)
|
|
318
|
+
* A global session context is established via `contextvars`
|
|
319
|
+
* `@transactional` automatically opens/closes async transactions
|
|
320
|
+
* `@repository` registers a class as a singleton component
|
|
321
|
+
* All dependencies (repositories, services, configurers) are resolved by Pico-IoC
|
|
322
|
+
|
|
323
|
+
No globals. No implicit singletons. No framework coupling.
|
|
324
|
+
|
|
325
|
+
-----
|
|
326
|
+
|
|
327
|
+
## ๐ก Architecture Overview
|
|
328
|
+
|
|
329
|
+
```
|
|
330
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
331
|
+
โ Your App โ
|
|
332
|
+
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโ
|
|
333
|
+
โ
|
|
334
|
+
Constructor Injection
|
|
335
|
+
โ
|
|
336
|
+
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโ
|
|
337
|
+
โ Pico-IoC โ
|
|
338
|
+
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
|
|
339
|
+
โ
|
|
340
|
+
SessionManager / SqlAlchemyFactory
|
|
341
|
+
โ
|
|
342
|
+
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโ
|
|
343
|
+
โ pico-sqlalchemy โ
|
|
344
|
+
โ Transactional Decorators โ
|
|
345
|
+
โ Repository Metadata โ
|
|
346
|
+
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
|
|
347
|
+
โ
|
|
348
|
+
SQLAlchemy
|
|
349
|
+
(Async ORM)
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
-----
|
|
353
|
+
|
|
354
|
+
## ๐ License
|
|
355
|
+
|
|
356
|
+
MIT
|
|
357
|
+
|