wexample-orm 2.0.0__tar.gz → 2.0.1__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.
Files changed (41) hide show
  1. wexample_orm-2.0.1/PKG-INFO +117 -0
  2. wexample_orm-2.0.1/README.md +96 -0
  3. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/pyproject.toml +2 -2
  4. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/const/__init__.py +0 -0
  5. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/const/generator.py +0 -0
  6. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/const/import_export.py +0 -0
  7. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/exception/import_reference_not_found_exception.py +0 -0
  8. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/exception/import_unknown_entity_exception.py +0 -0
  9. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/generator/__init__.py +0 -0
  10. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/generator/entity_generator.py +0 -0
  11. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/generator/entity_schema.py +0 -0
  12. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/generator/repository_generator.py +0 -0
  13. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/helper/__init__.py +0 -0
  14. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/helper/doctrine.py +0 -0
  15. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/import_export/__init__.py +0 -0
  16. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/import_export/entity_exporter.py +0 -0
  17. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/import_export/entity_importer.py +0 -0
  18. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/import_export/import_references_bag.py +0 -0
  19. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/import_export/normalized_entity_hydrator.py +0 -0
  20. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/py.typed +0 -0
  21. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/tests/.gitkeep +0 -0
  22. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/tests/test_entity_generator.py +0 -0
  23. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/tests/test_entity_importer.py +0 -0
  24. wexample_orm-2.0.0/PKG-INFO +0 -349
  25. wexample_orm-2.0.0/README.md +0 -328
  26. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/__init__.py +0 -0
  27. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/common/__init__.py +0 -0
  28. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/common/abstract_repositories_manager.py +0 -0
  29. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/entity/__init__.py +0 -0
  30. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/entity/abstract_entity.py +0 -0
  31. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/exception/__init__.py +0 -0
  32. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/exception/repository_session_missing_exception.py +0 -0
  33. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/exception/unknown_repository_exception.py +0 -0
  34. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/repository/__init__.py +0 -0
  35. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/repository/abstract_repository.py +0 -0
  36. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/session/__init__.py +0 -0
  37. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/session/abstract_session_factory.py +0 -0
  38. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/testing/__init__.py +0 -0
  39. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/src/wexample_orm/testing/sqlite.py +0 -0
  40. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/tests/__init__.py +0 -0
  41. {wexample_orm-2.0.0 → wexample_orm-2.0.1}/tests/test_abstract.py +0 -0
@@ -0,0 +1,117 @@
1
+ Metadata-Version: 2.1
2
+ Name: wexample-orm
3
+ Version: 2.0.1
4
+ Summary: Provides SQLAlchemy ORM abstractions — base entity, repository, session factory, and repository manager — with PostgreSQL-first defaults via psycopg
5
+ Author-Email: weeger <contact@wexample.com>
6
+ License: MIT
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Project-URL: homepage, https://github.com/wexample/python-orm
11
+ Requires-Python: >=3.10
12
+ Requires-Dist: attrs>=23.1.0
13
+ Requires-Dist: psycopg[binary]>=3.2
14
+ Requires-Dist: sqlalchemy<3,>=2
15
+ Requires-Dist: uuid-utils>=0.11
16
+ Requires-Dist: wexample-helpers>=20.6.0
17
+ Provides-Extra: dev
18
+ Requires-Dist: pytest; extra == "dev"
19
+ Requires-Dist: pytest-cov; extra == "dev"
20
+ Description-Content-Type: text/markdown
21
+
22
+ # orm
23
+
24
+ Version: 2.0.1
25
+
26
+ Provides SQLAlchemy ORM abstractions — base entity, repository, session factory, and repository manager — with PostgreSQL-first defaults via psycopg
27
+
28
+ ## Table of Contents
29
+
30
+ - [Introduction](#introduction)
31
+ - [Install](#install)
32
+ - [Tests](#tests)
33
+ - [Roadmap](#roadmap)
34
+ - [Status Compatibility](#status-compatibility)
35
+ - [Useful Links](#useful-links)
36
+
37
+ Python Doctrine inspired ORM
38
+
39
+ ## Installation
40
+
41
+ ```bash
42
+ pip install wexample-orm
43
+ ```
44
+
45
+ Requires Python >=3.10.
46
+
47
+ ## Tests
48
+
49
+ This project uses `pytest` for testing and `pytest-cov` for code coverage analysis.
50
+
51
+ ### Installation
52
+
53
+ First, install the required testing dependencies:
54
+ ```bash
55
+ .venv/bin/python -m pip install pytest pytest-cov
56
+ ```
57
+
58
+ ### Basic Usage
59
+
60
+ Run all tests with coverage:
61
+ ```bash
62
+ .venv/bin/python -m pytest --cov --cov-report=html
63
+ ```
64
+
65
+ ### Common Commands
66
+ ```bash
67
+ # Run tests with coverage for a specific module
68
+ .venv/bin/python -m pytest --cov=your_module
69
+
70
+ # Show which lines are not covered
71
+ .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing
72
+
73
+ # Generate an HTML coverage report
74
+ .venv/bin/python -m pytest --cov=your_module --cov-report=html
75
+
76
+ # Combine terminal and HTML reports
77
+ .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing --cov-report=html
78
+
79
+ # Run specific test file with coverage
80
+ .venv/bin/python -m pytest tests/test_file.py --cov=your_module --cov-report=term-missing
81
+ ```
82
+
83
+ ### Viewing HTML Reports
84
+
85
+ After generating an HTML report, open `htmlcov/index.html` in your browser to view detailed line-by-line coverage information.
86
+
87
+ ### Coverage Threshold
88
+
89
+ To enforce a minimum coverage percentage:
90
+ ```bash
91
+ .venv/bin/python -m pytest --cov=your_module --cov-fail-under=80
92
+ ```
93
+
94
+ This will cause the test suite to fail if coverage drops below 80%.
95
+
96
+ ## Known Limitations & Roadmap
97
+
98
+ Current limitations and planned features are tracked in the GitHub issues.
99
+
100
+ See the [project roadmap](https://github.com/wexample/python-orm/issues) for upcoming features and improvements.
101
+
102
+ ## Status & Compatibility
103
+
104
+ **Maturity**: Production-ready
105
+
106
+ **Python Support**: >=3.10
107
+
108
+ **OS Support**: Linux, macOS, Windows
109
+
110
+ **Status**: Actively maintained
111
+
112
+ ## Useful Links
113
+
114
+ - **Homepage**: https://github.com/wexample/python-orm
115
+ - **Issue Tracker**: https://github.com/wexample/python-orm/issues
116
+ - **Discussions**: https://github.com/wexample/python-orm/discussions
117
+ - **PyPI**: [pypi.org/project/wexample-orm](https://pypi.org/project/wexample-orm/)
@@ -0,0 +1,96 @@
1
+ # orm
2
+
3
+ Version: 2.0.1
4
+
5
+ Provides SQLAlchemy ORM abstractions — base entity, repository, session factory, and repository manager — with PostgreSQL-first defaults via psycopg
6
+
7
+ ## Table of Contents
8
+
9
+ - [Introduction](#introduction)
10
+ - [Install](#install)
11
+ - [Tests](#tests)
12
+ - [Roadmap](#roadmap)
13
+ - [Status Compatibility](#status-compatibility)
14
+ - [Useful Links](#useful-links)
15
+
16
+ Python Doctrine inspired ORM
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ pip install wexample-orm
22
+ ```
23
+
24
+ Requires Python >=3.10.
25
+
26
+ ## Tests
27
+
28
+ This project uses `pytest` for testing and `pytest-cov` for code coverage analysis.
29
+
30
+ ### Installation
31
+
32
+ First, install the required testing dependencies:
33
+ ```bash
34
+ .venv/bin/python -m pip install pytest pytest-cov
35
+ ```
36
+
37
+ ### Basic Usage
38
+
39
+ Run all tests with coverage:
40
+ ```bash
41
+ .venv/bin/python -m pytest --cov --cov-report=html
42
+ ```
43
+
44
+ ### Common Commands
45
+ ```bash
46
+ # Run tests with coverage for a specific module
47
+ .venv/bin/python -m pytest --cov=your_module
48
+
49
+ # Show which lines are not covered
50
+ .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing
51
+
52
+ # Generate an HTML coverage report
53
+ .venv/bin/python -m pytest --cov=your_module --cov-report=html
54
+
55
+ # Combine terminal and HTML reports
56
+ .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing --cov-report=html
57
+
58
+ # Run specific test file with coverage
59
+ .venv/bin/python -m pytest tests/test_file.py --cov=your_module --cov-report=term-missing
60
+ ```
61
+
62
+ ### Viewing HTML Reports
63
+
64
+ After generating an HTML report, open `htmlcov/index.html` in your browser to view detailed line-by-line coverage information.
65
+
66
+ ### Coverage Threshold
67
+
68
+ To enforce a minimum coverage percentage:
69
+ ```bash
70
+ .venv/bin/python -m pytest --cov=your_module --cov-fail-under=80
71
+ ```
72
+
73
+ This will cause the test suite to fail if coverage drops below 80%.
74
+
75
+ ## Known Limitations & Roadmap
76
+
77
+ Current limitations and planned features are tracked in the GitHub issues.
78
+
79
+ See the [project roadmap](https://github.com/wexample/python-orm/issues) for upcoming features and improvements.
80
+
81
+ ## Status & Compatibility
82
+
83
+ **Maturity**: Production-ready
84
+
85
+ **Python Support**: >=3.10
86
+
87
+ **OS Support**: Linux, macOS, Windows
88
+
89
+ **Status**: Actively maintained
90
+
91
+ ## Useful Links
92
+
93
+ - **Homepage**: https://github.com/wexample/python-orm
94
+ - **Issue Tracker**: https://github.com/wexample/python-orm/issues
95
+ - **Discussions**: https://github.com/wexample/python-orm/discussions
96
+ - **PyPI**: [pypi.org/project/wexample-orm](https://pypi.org/project/wexample-orm/)
@@ -6,7 +6,7 @@ build-backend = "pdm.backend"
6
6
 
7
7
  [project]
8
8
  name = "wexample-orm"
9
- version = "2.0.0"
9
+ version = "2.0.1"
10
10
  description = "Provides SQLAlchemy ORM abstractions — base entity, repository, session factory, and repository manager — with PostgreSQL-first defaults via psycopg"
11
11
  authors = [
12
12
  { name = "weeger", email = "contact@wexample.com" },
@@ -22,7 +22,7 @@ dependencies = [
22
22
  "psycopg[binary]>=3.2",
23
23
  "sqlalchemy>=2,<3",
24
24
  "uuid-utils>=0.11",
25
- "wexample-helpers>=20.5.0",
25
+ "wexample-helpers>=20.6.0",
26
26
  ]
27
27
 
28
28
  [project.readme]
@@ -1,349 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: wexample-orm
3
- Version: 2.0.0
4
- Summary: Provides SQLAlchemy ORM abstractions — base entity, repository, session factory, and repository manager — with PostgreSQL-first defaults via psycopg
5
- Author-Email: weeger <contact@wexample.com>
6
- License: MIT
7
- Classifier: Programming Language :: Python :: 3
8
- Classifier: License :: OSI Approved :: MIT License
9
- Classifier: Operating System :: OS Independent
10
- Project-URL: homepage, https://github.com/wexample/python-orm
11
- Requires-Python: >=3.10
12
- Requires-Dist: attrs>=23.1.0
13
- Requires-Dist: psycopg[binary]>=3.2
14
- Requires-Dist: sqlalchemy<3,>=2
15
- Requires-Dist: uuid-utils>=0.11
16
- Requires-Dist: wexample-helpers>=20.5.0
17
- Provides-Extra: dev
18
- Requires-Dist: pytest; extra == "dev"
19
- Requires-Dist: pytest-cov; extra == "dev"
20
- Description-Content-Type: text/markdown
21
-
22
- # orm
23
-
24
- Version: 2.0.0
25
-
26
- `wexample-orm` provides four SQLAlchemy base classes — `AbstractEntity`, `AbstractRepository`, `AbstractSessionFactory`, and `AbstractRepositoriesManager` — that impose a Doctrine-style repository pattern on top of SQLAlchemy 2. It targets Python developers in the Wexample stack who connect to PostgreSQL via psycopg and want a consistent, opinionated foundation without rewriting engine, session, and repository boilerplate for every project.
27
-
28
- ## Table of Contents
29
-
30
- - [Installation](#installation)
31
- - [Quickstart](#quickstart)
32
- - [Tests](#tests)
33
- - [Architecture](#architecture)
34
- - [Integration in the Suite](#integration-in-the-suite)
35
- - [Dependencies](#dependencies)
36
- - [Versioning & Compatibility Policy](#versioning--compatibility-policy)
37
- - [License](#license)
38
- - [About us](#about-us)
39
- - [Known Limitations & Roadmap](#known-limitations--roadmap)
40
- - [Status & Compatibility](#status--compatibility)
41
- - [Useful Links](#useful-links)
42
- - [Migration Notes](#migration-notes)
43
-
44
- ## Installation
45
-
46
- ```bash
47
- pip install wexample-orm
48
- ```
49
-
50
- Requires Python >=3.10.
51
-
52
- ## Quickstart
53
-
54
- Install from PyPI:
55
-
56
- ```bash
57
- pip install wexample-orm
58
- ```
59
-
60
- ### Define an entity and a repository
61
-
62
- Subclass `AbstractEntity` to get a BIGINT primary key and a table name derived automatically from the class name (`User` → `user`). Subclass `AbstractRepository` and declare which entity it covers:
63
-
64
- ```python
65
- from sqlalchemy.orm import Mapped, mapped_column
66
- from wexample_orm.entity.abstract_entity import AbstractEntity
67
- from wexample_orm.repository.abstract_repository import AbstractRepository
68
-
69
- class User(AbstractEntity):
70
- name: Mapped[str] = mapped_column()
71
-
72
- class UserRepository(AbstractRepository):
73
- @classmethod
74
- def get_entity_type(cls) -> type[AbstractEntity]:
75
- return User
76
- ```
77
-
78
- ### Connect and persist (PostgreSQL)
79
-
80
- `AbstractSessionFactory` wraps `create_engine` and `sessionmaker`. Pass a SQLAlchemy URL and call `create_session()`:
81
-
82
- ```python
83
- from wexample_orm.session.abstract_session_factory import AbstractSessionFactory
84
-
85
- factory = AbstractSessionFactory(dsn="postgresql+psycopg://user:password@localhost/mydb")
86
- User.metadata.create_all(factory.get_engine())
87
-
88
- session = factory.create_session()
89
- repo = UserRepository(session=session)
90
-
91
- user = repo.save(User(name="alice"))
92
- print(user.id) # 1 — assigned by the database
93
-
94
- found = repo.find(user.id)
95
- print(found.name) # alice
96
- ```
97
-
98
- `save()` calls `session.commit()` by default. Pass `flush=False` to add the row without committing, or `refresh=True` to reload the instance from the database after commit.
99
-
100
- ### In-memory session for local development and tests
101
-
102
- `wexample_orm.testing.in_memory_session` creates a disposable SQLite engine — no PostgreSQL required. It creates all tables on entry and disposes the engine on exit:
103
-
104
- ```python
105
- from wexample_orm.testing import in_memory_session
106
-
107
- with in_memory_session(AbstractEntity) as session:
108
- repo = UserRepository(session=session)
109
- repo.save(User(name="alice"))
110
-
111
- found = repo.find(1)
112
- print(found.name) # alice
113
- ```
114
-
115
- ## Tests
116
-
117
- This project uses `pytest` for testing and `pytest-cov` for code coverage analysis.
118
-
119
- ### Installation
120
-
121
- First, install the required testing dependencies:
122
- ```bash
123
- .venv/bin/python -m pip install pytest pytest-cov
124
- ```
125
-
126
- ### Basic Usage
127
-
128
- Run all tests with coverage:
129
- ```bash
130
- .venv/bin/python -m pytest --cov --cov-report=html
131
- ```
132
-
133
- ### Common Commands
134
- ```bash
135
- # Run tests with coverage for a specific module
136
- .venv/bin/python -m pytest --cov=your_module
137
-
138
- # Show which lines are not covered
139
- .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing
140
-
141
- # Generate an HTML coverage report
142
- .venv/bin/python -m pytest --cov=your_module --cov-report=html
143
-
144
- # Combine terminal and HTML reports
145
- .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing --cov-report=html
146
-
147
- # Run specific test file with coverage
148
- .venv/bin/python -m pytest tests/test_file.py --cov=your_module --cov-report=term-missing
149
- ```
150
-
151
- ### Viewing HTML Reports
152
-
153
- After generating an HTML report, open `htmlcov/index.html` in your browser to view detailed line-by-line coverage information.
154
-
155
- ### Coverage Threshold
156
-
157
- To enforce a minimum coverage percentage:
158
- ```bash
159
- .venv/bin/python -m pytest --cov=your_module --cov-fail-under=80
160
- ```
161
-
162
- This will cause the test suite to fail if coverage drops below 80%.
163
-
164
- ## Architecture
165
-
166
- The package (`wexample-orm`) is a thin layer on top of SQLAlchemy 2.x. It defines four abstract base classes that projects subclass, two domain exceptions, and a test helper. Nothing is concrete out of the box: the library ships patterns, not implementations.
167
-
168
- ### Entity
169
-
170
- src/wexample_orm/entity/abstract_entity.py
171
-
172
- `AbstractEntity` is the SQLAlchemy declarative base for the whole stack. It is decorated with `@as_declarative()`, so every subclass automatically participates in the same `MetaData` registry.
173
-
174
- Two conventions are fixed here and nowhere else:
175
-
176
- - **Table name** — `__tablename__` is set by `get_entity_name()`, which calls `string_to_snake_case(cls.__name__)`. A class named `UserAccount` maps to the table `user_account` with no annotation needed.
177
- - **Primary key** — `id` is a `BigInteger` with `autoincrement=True`. A `with_variant(Integer(), "sqlite")` is bolted on so that the same declaration works in the in-memory SQLite sessions used during testing (SQLite only auto-increments a column typed `INTEGER PRIMARY KEY`, not `BIGINT`).
178
-
179
- ### Repository
180
-
181
- src/wexample_orm/repository/abstract_repository.py
182
-
183
- `AbstractRepository` is the data-access object. It holds one `session` field (default `None`) and exposes:
184
-
185
- - `get_entity_type()` — abstract classmethod every concrete repository must implement; returns the `AbstractEntity` subclass it owns.
186
- - `find(id)` — delegates to `session.get(EntityType, id)`; returns `None` when no session is set.
187
- - `find_one_by(filter_clause)` — `query().filter(clause).first()`.
188
- - `query()` — returns a raw `session.query(EntityType)`; calls `_require_session()` first.
189
- - `save(entity, flush=True, refresh=False)` — `session.add` then `session.commit`; optionally refreshes from DB.
190
- - `_require_session()` — raises `RepositorySessionMissingException` when `session` is `None`; called by any method that cannot function without it.
191
-
192
- A repository can be constructed standalone (session-less `find` returns `None`, everything else raises) or injected with a session directly.
193
-
194
- ### Session factory
195
-
196
- src/wexample_orm/session/abstract_session_factory.py
197
-
198
- `AbstractSessionFactory` builds and caches a SQLAlchemy engine and sessionmaker from a DSN string. The engine is created on first call to `get_engine()` and reused thereafter; the sessionmaker is created on first call to `create_session()`.
199
-
200
- Public API:
201
-
202
- - `dsn` — SQLAlchemy URL, e.g. `postgresql+psycopg://user:pwd@host/db` or `sqlite:///:memory:`.
203
- - `engine_options` / `session_options` — dicts forwarded verbatim to `create_engine()` and `sessionmaker()`.
204
- - `create_session()` — returns a fresh `Session` from the cached maker.
205
- - `get_engine()` — returns the cached engine (creates it on first call).
206
- - `dispose()` — calls `engine.dispose()` and resets both caches; intended for test teardown.
207
- - `_engine_kwargs()` / `_session_kwargs()` — overridable hooks; by default they just copy the option dicts.
208
-
209
- The session factory is decoupled from the rest: nothing in the repository or manager imports it. Callers create a session through it and pass it in manually.
210
-
211
- ### Repositories manager
212
-
213
- src/wexample_orm/common/abstract_repositories_manager.py
214
-
215
- `AbstractRepositoriesManager` is a registry that maps entity types to repository instances. It is the single entry point when a project has multiple repositories and wants to avoid constructing them by hand.
216
-
217
- Fields:
218
-
219
- - `repository_classes` — the list of repository classes the manager may instantiate.
220
- - `session` — the SQLAlchemy session shared across every repository the manager creates.
221
- - `_instances` — private cache (`entity_type → repository instance`); a repository is built once and reused.
222
-
223
- Methods:
224
-
225
- - `get(entity_type)` — looks up the cache; on miss, walks `repository_classes` looking for the class whose `get_entity_type()` matches, instantiates it with `session=self.session`, stores it, and returns it. Raises `UnknownRepositoryException` if nothing matches.
226
- - `get_by_table_name(table_name)` — same lookup but keyed on `entity_type.get_entity_name()` (the snake-case table name).
227
- - `get_classes()` — returns a copy of `repository_classes`.
228
-
229
- ### Call path
230
-
231
- A typical read goes: caller asks the manager for a repository (`manager.get(User)`) → manager instantiates `UserRepository(session=session)` if not cached → caller calls `repo.find(42)` → repository calls `session.get(User, 42)` → SQLAlchemy issues the SQL against the engine that the session factory built.
232
-
233
- A write follows the same path through `repo.save(entity)`, which calls `session.add` then `session.commit`.
234
-
235
- ### Exceptions
236
-
237
- src/wexample_orm/exception/unknown_repository_exception.py
238
-
239
- `UnknownRepositoryException` (error code `ORM_UNKNOWN_REPOSITORY`) is raised by the manager when `get()` receives an entity type for which no repository class is registered.
240
-
241
- src/wexample_orm/exception/repository_session_missing_exception.py
242
-
243
- `RepositorySessionMissingException` (error code `ORM_REPOSITORY_SESSION_MISSING`) is raised by `AbstractRepository._require_session()` when a session-dependent method is called on a repository that has `session=None`.
244
-
245
- Both extend `UndefinedException` from `wexample-helpers` and carry a structured `data` dict alongside the human-readable message.
246
-
247
- ### Testing helper
248
-
249
- src/wexample_orm/testing/sqlite.py
250
-
251
- `in_memory_session(declarative_base)` is a context manager that creates a fresh SQLite in-memory engine, runs `metadata.create_all()` on it, yields a session, then closes the session and disposes the engine. The `declarative_base` argument must be the `@as_declarative()`-decorated class whose `MetaData` holds the tables to create — in practice `AbstractEntity` or a project subclass of it.
252
-
253
- ```python
254
- from wexample_orm.testing import in_memory_session
255
- from wexample_orm.entity.abstract_entity import AbstractEntity
256
-
257
- with in_memory_session(AbstractEntity) as session:
258
- repo = UserRepository(session=session)
259
- repo.save(User(name="alice"))
260
- assert repo.find(1).name == "alice"
261
- ```
262
-
263
- The helper is excluded from the published wheel (`tool.setuptools.packages.find` omits `wexample_orm.testing*`) and is only available in a development install.
264
-
265
- ### Source layout
266
-
267
- ```
268
- src/wexample_orm/
269
- __init__.py # re-exports the three main abstractions
270
- entity/abstract_entity.py # declarative base + PK convention
271
- repository/abstract_repository.py # data-access object
272
- session/abstract_session_factory.py# engine + sessionmaker cache
273
- common/abstract_repositories_manager.py # repository registry
274
- exception/
275
- unknown_repository_exception.py
276
- repository_session_missing_exception.py
277
- testing/sqlite.py # in-memory SQLite helper
278
- ```
279
-
280
- ## Integration in the Suite
281
-
282
- This package is part of the Wexample Suite — a collection of high-quality, modular tools designed to work seamlessly together across multiple languages and environments.
283
-
284
- ### Related Packages
285
-
286
- The suite includes packages for configuration management, file handling, prompts, and more. Each package can be used independently or as part of the integrated suite.
287
-
288
- ## Dependencies
289
-
290
- - attrs: >=23.1.0
291
- - psycopg: >=3.2
292
- - sqlalchemy: <3,>=2
293
- - uuid-utils: >=0.11
294
- - wexample-helpers: >=20.5.0
295
-
296
- ## Versioning & Compatibility Policy
297
-
298
- Wexample packages follow **Semantic Versioning** (SemVer):
299
-
300
- - **MAJOR**: Breaking changes
301
- - **MINOR**: New features, backward compatible
302
- - **PATCH**: Bug fixes, backward compatible
303
-
304
- We maintain backward compatibility within major versions and provide clear migration guides for breaking changes.
305
-
306
- ## License
307
-
308
- This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
309
-
310
- Free to use in both personal and commercial projects.
311
-
312
- ## About us
313
-
314
- [Wexample](https://wexample.com) stands as a cornerstone of the digital ecosystem — a collective of seasoned engineers, researchers, and creators driven by a relentless pursuit of technological excellence. More than a media platform, it has grown into a vibrant community where innovation meets craftsmanship, and where every line of code reflects a commitment to clarity, durability, and shared intelligence.
315
-
316
- This packages suite embodies this spirit. Trusted by professionals and enthusiasts alike, it delivers a consistent, high-quality foundation for modern development — open, elegant, and battle-tested. Its reputation is built on years of collaboration, refinement, and rigorous attention to detail, making it a natural choice for those who demand both robustness and beauty in their tools.
317
-
318
- Wexample cultivates a culture of mastery. Each package, each contribution carries the mark of a community that values precision, ethics, and innovation — a community proud to shape the future of digital craftsmanship.
319
-
320
- Built and maintained by [Romain Weeger](https://weeger.io).
321
-
322
- ## Known Limitations & Roadmap
323
-
324
- Current limitations and planned features are tracked in the GitHub issues.
325
-
326
- See the [project roadmap](https://github.com/wexample/python-orm/issues) for upcoming features and improvements.
327
-
328
- ## Status & Compatibility
329
-
330
- **Maturity**: Production-ready
331
-
332
- **Python Support**: >=3.10
333
-
334
- **OS Support**: Linux, macOS, Windows
335
-
336
- **Status**: Actively maintained
337
-
338
- ## Useful Links
339
-
340
- - **Homepage**: https://github.com/wexample/python-orm
341
- - **Issue Tracker**: https://github.com/wexample/python-orm/issues
342
- - **Discussions**: https://github.com/wexample/python-orm/discussions
343
- - **PyPI**: [pypi.org/project/wexample-orm](https://pypi.org/project/wexample-orm/)
344
-
345
- ## Migration Notes
346
-
347
- When upgrading between major versions, refer to the migration guides in the documentation.
348
-
349
- Breaking changes are clearly documented with upgrade paths and examples.
@@ -1,328 +0,0 @@
1
- # orm
2
-
3
- Version: 2.0.0
4
-
5
- `wexample-orm` provides four SQLAlchemy base classes — `AbstractEntity`, `AbstractRepository`, `AbstractSessionFactory`, and `AbstractRepositoriesManager` — that impose a Doctrine-style repository pattern on top of SQLAlchemy 2. It targets Python developers in the Wexample stack who connect to PostgreSQL via psycopg and want a consistent, opinionated foundation without rewriting engine, session, and repository boilerplate for every project.
6
-
7
- ## Table of Contents
8
-
9
- - [Installation](#installation)
10
- - [Quickstart](#quickstart)
11
- - [Tests](#tests)
12
- - [Architecture](#architecture)
13
- - [Integration in the Suite](#integration-in-the-suite)
14
- - [Dependencies](#dependencies)
15
- - [Versioning & Compatibility Policy](#versioning--compatibility-policy)
16
- - [License](#license)
17
- - [About us](#about-us)
18
- - [Known Limitations & Roadmap](#known-limitations--roadmap)
19
- - [Status & Compatibility](#status--compatibility)
20
- - [Useful Links](#useful-links)
21
- - [Migration Notes](#migration-notes)
22
-
23
- ## Installation
24
-
25
- ```bash
26
- pip install wexample-orm
27
- ```
28
-
29
- Requires Python >=3.10.
30
-
31
- ## Quickstart
32
-
33
- Install from PyPI:
34
-
35
- ```bash
36
- pip install wexample-orm
37
- ```
38
-
39
- ### Define an entity and a repository
40
-
41
- Subclass `AbstractEntity` to get a BIGINT primary key and a table name derived automatically from the class name (`User` → `user`). Subclass `AbstractRepository` and declare which entity it covers:
42
-
43
- ```python
44
- from sqlalchemy.orm import Mapped, mapped_column
45
- from wexample_orm.entity.abstract_entity import AbstractEntity
46
- from wexample_orm.repository.abstract_repository import AbstractRepository
47
-
48
- class User(AbstractEntity):
49
- name: Mapped[str] = mapped_column()
50
-
51
- class UserRepository(AbstractRepository):
52
- @classmethod
53
- def get_entity_type(cls) -> type[AbstractEntity]:
54
- return User
55
- ```
56
-
57
- ### Connect and persist (PostgreSQL)
58
-
59
- `AbstractSessionFactory` wraps `create_engine` and `sessionmaker`. Pass a SQLAlchemy URL and call `create_session()`:
60
-
61
- ```python
62
- from wexample_orm.session.abstract_session_factory import AbstractSessionFactory
63
-
64
- factory = AbstractSessionFactory(dsn="postgresql+psycopg://user:password@localhost/mydb")
65
- User.metadata.create_all(factory.get_engine())
66
-
67
- session = factory.create_session()
68
- repo = UserRepository(session=session)
69
-
70
- user = repo.save(User(name="alice"))
71
- print(user.id) # 1 — assigned by the database
72
-
73
- found = repo.find(user.id)
74
- print(found.name) # alice
75
- ```
76
-
77
- `save()` calls `session.commit()` by default. Pass `flush=False` to add the row without committing, or `refresh=True` to reload the instance from the database after commit.
78
-
79
- ### In-memory session for local development and tests
80
-
81
- `wexample_orm.testing.in_memory_session` creates a disposable SQLite engine — no PostgreSQL required. It creates all tables on entry and disposes the engine on exit:
82
-
83
- ```python
84
- from wexample_orm.testing import in_memory_session
85
-
86
- with in_memory_session(AbstractEntity) as session:
87
- repo = UserRepository(session=session)
88
- repo.save(User(name="alice"))
89
-
90
- found = repo.find(1)
91
- print(found.name) # alice
92
- ```
93
-
94
- ## Tests
95
-
96
- This project uses `pytest` for testing and `pytest-cov` for code coverage analysis.
97
-
98
- ### Installation
99
-
100
- First, install the required testing dependencies:
101
- ```bash
102
- .venv/bin/python -m pip install pytest pytest-cov
103
- ```
104
-
105
- ### Basic Usage
106
-
107
- Run all tests with coverage:
108
- ```bash
109
- .venv/bin/python -m pytest --cov --cov-report=html
110
- ```
111
-
112
- ### Common Commands
113
- ```bash
114
- # Run tests with coverage for a specific module
115
- .venv/bin/python -m pytest --cov=your_module
116
-
117
- # Show which lines are not covered
118
- .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing
119
-
120
- # Generate an HTML coverage report
121
- .venv/bin/python -m pytest --cov=your_module --cov-report=html
122
-
123
- # Combine terminal and HTML reports
124
- .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing --cov-report=html
125
-
126
- # Run specific test file with coverage
127
- .venv/bin/python -m pytest tests/test_file.py --cov=your_module --cov-report=term-missing
128
- ```
129
-
130
- ### Viewing HTML Reports
131
-
132
- After generating an HTML report, open `htmlcov/index.html` in your browser to view detailed line-by-line coverage information.
133
-
134
- ### Coverage Threshold
135
-
136
- To enforce a minimum coverage percentage:
137
- ```bash
138
- .venv/bin/python -m pytest --cov=your_module --cov-fail-under=80
139
- ```
140
-
141
- This will cause the test suite to fail if coverage drops below 80%.
142
-
143
- ## Architecture
144
-
145
- The package (`wexample-orm`) is a thin layer on top of SQLAlchemy 2.x. It defines four abstract base classes that projects subclass, two domain exceptions, and a test helper. Nothing is concrete out of the box: the library ships patterns, not implementations.
146
-
147
- ### Entity
148
-
149
- src/wexample_orm/entity/abstract_entity.py
150
-
151
- `AbstractEntity` is the SQLAlchemy declarative base for the whole stack. It is decorated with `@as_declarative()`, so every subclass automatically participates in the same `MetaData` registry.
152
-
153
- Two conventions are fixed here and nowhere else:
154
-
155
- - **Table name** — `__tablename__` is set by `get_entity_name()`, which calls `string_to_snake_case(cls.__name__)`. A class named `UserAccount` maps to the table `user_account` with no annotation needed.
156
- - **Primary key** — `id` is a `BigInteger` with `autoincrement=True`. A `with_variant(Integer(), "sqlite")` is bolted on so that the same declaration works in the in-memory SQLite sessions used during testing (SQLite only auto-increments a column typed `INTEGER PRIMARY KEY`, not `BIGINT`).
157
-
158
- ### Repository
159
-
160
- src/wexample_orm/repository/abstract_repository.py
161
-
162
- `AbstractRepository` is the data-access object. It holds one `session` field (default `None`) and exposes:
163
-
164
- - `get_entity_type()` — abstract classmethod every concrete repository must implement; returns the `AbstractEntity` subclass it owns.
165
- - `find(id)` — delegates to `session.get(EntityType, id)`; returns `None` when no session is set.
166
- - `find_one_by(filter_clause)` — `query().filter(clause).first()`.
167
- - `query()` — returns a raw `session.query(EntityType)`; calls `_require_session()` first.
168
- - `save(entity, flush=True, refresh=False)` — `session.add` then `session.commit`; optionally refreshes from DB.
169
- - `_require_session()` — raises `RepositorySessionMissingException` when `session` is `None`; called by any method that cannot function without it.
170
-
171
- A repository can be constructed standalone (session-less `find` returns `None`, everything else raises) or injected with a session directly.
172
-
173
- ### Session factory
174
-
175
- src/wexample_orm/session/abstract_session_factory.py
176
-
177
- `AbstractSessionFactory` builds and caches a SQLAlchemy engine and sessionmaker from a DSN string. The engine is created on first call to `get_engine()` and reused thereafter; the sessionmaker is created on first call to `create_session()`.
178
-
179
- Public API:
180
-
181
- - `dsn` — SQLAlchemy URL, e.g. `postgresql+psycopg://user:pwd@host/db` or `sqlite:///:memory:`.
182
- - `engine_options` / `session_options` — dicts forwarded verbatim to `create_engine()` and `sessionmaker()`.
183
- - `create_session()` — returns a fresh `Session` from the cached maker.
184
- - `get_engine()` — returns the cached engine (creates it on first call).
185
- - `dispose()` — calls `engine.dispose()` and resets both caches; intended for test teardown.
186
- - `_engine_kwargs()` / `_session_kwargs()` — overridable hooks; by default they just copy the option dicts.
187
-
188
- The session factory is decoupled from the rest: nothing in the repository or manager imports it. Callers create a session through it and pass it in manually.
189
-
190
- ### Repositories manager
191
-
192
- src/wexample_orm/common/abstract_repositories_manager.py
193
-
194
- `AbstractRepositoriesManager` is a registry that maps entity types to repository instances. It is the single entry point when a project has multiple repositories and wants to avoid constructing them by hand.
195
-
196
- Fields:
197
-
198
- - `repository_classes` — the list of repository classes the manager may instantiate.
199
- - `session` — the SQLAlchemy session shared across every repository the manager creates.
200
- - `_instances` — private cache (`entity_type → repository instance`); a repository is built once and reused.
201
-
202
- Methods:
203
-
204
- - `get(entity_type)` — looks up the cache; on miss, walks `repository_classes` looking for the class whose `get_entity_type()` matches, instantiates it with `session=self.session`, stores it, and returns it. Raises `UnknownRepositoryException` if nothing matches.
205
- - `get_by_table_name(table_name)` — same lookup but keyed on `entity_type.get_entity_name()` (the snake-case table name).
206
- - `get_classes()` — returns a copy of `repository_classes`.
207
-
208
- ### Call path
209
-
210
- A typical read goes: caller asks the manager for a repository (`manager.get(User)`) → manager instantiates `UserRepository(session=session)` if not cached → caller calls `repo.find(42)` → repository calls `session.get(User, 42)` → SQLAlchemy issues the SQL against the engine that the session factory built.
211
-
212
- A write follows the same path through `repo.save(entity)`, which calls `session.add` then `session.commit`.
213
-
214
- ### Exceptions
215
-
216
- src/wexample_orm/exception/unknown_repository_exception.py
217
-
218
- `UnknownRepositoryException` (error code `ORM_UNKNOWN_REPOSITORY`) is raised by the manager when `get()` receives an entity type for which no repository class is registered.
219
-
220
- src/wexample_orm/exception/repository_session_missing_exception.py
221
-
222
- `RepositorySessionMissingException` (error code `ORM_REPOSITORY_SESSION_MISSING`) is raised by `AbstractRepository._require_session()` when a session-dependent method is called on a repository that has `session=None`.
223
-
224
- Both extend `UndefinedException` from `wexample-helpers` and carry a structured `data` dict alongside the human-readable message.
225
-
226
- ### Testing helper
227
-
228
- src/wexample_orm/testing/sqlite.py
229
-
230
- `in_memory_session(declarative_base)` is a context manager that creates a fresh SQLite in-memory engine, runs `metadata.create_all()` on it, yields a session, then closes the session and disposes the engine. The `declarative_base` argument must be the `@as_declarative()`-decorated class whose `MetaData` holds the tables to create — in practice `AbstractEntity` or a project subclass of it.
231
-
232
- ```python
233
- from wexample_orm.testing import in_memory_session
234
- from wexample_orm.entity.abstract_entity import AbstractEntity
235
-
236
- with in_memory_session(AbstractEntity) as session:
237
- repo = UserRepository(session=session)
238
- repo.save(User(name="alice"))
239
- assert repo.find(1).name == "alice"
240
- ```
241
-
242
- The helper is excluded from the published wheel (`tool.setuptools.packages.find` omits `wexample_orm.testing*`) and is only available in a development install.
243
-
244
- ### Source layout
245
-
246
- ```
247
- src/wexample_orm/
248
- __init__.py # re-exports the three main abstractions
249
- entity/abstract_entity.py # declarative base + PK convention
250
- repository/abstract_repository.py # data-access object
251
- session/abstract_session_factory.py# engine + sessionmaker cache
252
- common/abstract_repositories_manager.py # repository registry
253
- exception/
254
- unknown_repository_exception.py
255
- repository_session_missing_exception.py
256
- testing/sqlite.py # in-memory SQLite helper
257
- ```
258
-
259
- ## Integration in the Suite
260
-
261
- This package is part of the Wexample Suite — a collection of high-quality, modular tools designed to work seamlessly together across multiple languages and environments.
262
-
263
- ### Related Packages
264
-
265
- The suite includes packages for configuration management, file handling, prompts, and more. Each package can be used independently or as part of the integrated suite.
266
-
267
- ## Dependencies
268
-
269
- - attrs: >=23.1.0
270
- - psycopg: >=3.2
271
- - sqlalchemy: <3,>=2
272
- - uuid-utils: >=0.11
273
- - wexample-helpers: >=20.5.0
274
-
275
- ## Versioning & Compatibility Policy
276
-
277
- Wexample packages follow **Semantic Versioning** (SemVer):
278
-
279
- - **MAJOR**: Breaking changes
280
- - **MINOR**: New features, backward compatible
281
- - **PATCH**: Bug fixes, backward compatible
282
-
283
- We maintain backward compatibility within major versions and provide clear migration guides for breaking changes.
284
-
285
- ## License
286
-
287
- This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
288
-
289
- Free to use in both personal and commercial projects.
290
-
291
- ## About us
292
-
293
- [Wexample](https://wexample.com) stands as a cornerstone of the digital ecosystem — a collective of seasoned engineers, researchers, and creators driven by a relentless pursuit of technological excellence. More than a media platform, it has grown into a vibrant community where innovation meets craftsmanship, and where every line of code reflects a commitment to clarity, durability, and shared intelligence.
294
-
295
- This packages suite embodies this spirit. Trusted by professionals and enthusiasts alike, it delivers a consistent, high-quality foundation for modern development — open, elegant, and battle-tested. Its reputation is built on years of collaboration, refinement, and rigorous attention to detail, making it a natural choice for those who demand both robustness and beauty in their tools.
296
-
297
- Wexample cultivates a culture of mastery. Each package, each contribution carries the mark of a community that values precision, ethics, and innovation — a community proud to shape the future of digital craftsmanship.
298
-
299
- Built and maintained by [Romain Weeger](https://weeger.io).
300
-
301
- ## Known Limitations & Roadmap
302
-
303
- Current limitations and planned features are tracked in the GitHub issues.
304
-
305
- See the [project roadmap](https://github.com/wexample/python-orm/issues) for upcoming features and improvements.
306
-
307
- ## Status & Compatibility
308
-
309
- **Maturity**: Production-ready
310
-
311
- **Python Support**: >=3.10
312
-
313
- **OS Support**: Linux, macOS, Windows
314
-
315
- **Status**: Actively maintained
316
-
317
- ## Useful Links
318
-
319
- - **Homepage**: https://github.com/wexample/python-orm
320
- - **Issue Tracker**: https://github.com/wexample/python-orm/issues
321
- - **Discussions**: https://github.com/wexample/python-orm/discussions
322
- - **PyPI**: [pypi.org/project/wexample-orm](https://pypi.org/project/wexample-orm/)
323
-
324
- ## Migration Notes
325
-
326
- When upgrading between major versions, refer to the migration guides in the documentation.
327
-
328
- Breaking changes are clearly documented with upgrade paths and examples.