|
| 1 | +## When should you create each component? |
| 2 | + |
| 3 | +### Bind manager |
| 4 | + |
| 5 | +The `SQLAlchemyBindManager` object holds all the SQLAlchemy Engines, which |
| 6 | +are supposed to be global objects. Therefore it should be created on application |
| 7 | +startup and be globally accessible. |
| 8 | + |
| 9 | +From SQLAlchemy documentation: |
| 10 | + |
| 11 | +> The Engine is intended to normally be a permanent fixture established up-front |
| 12 | +> and maintained throughout the lifespan of an application. |
| 13 | +
|
| 14 | +### Repositories |
| 15 | + |
| 16 | +[SQLAlchemy documentation](https://docs.sqlalchemy.org/en/20/orm/session_basics.html#when-do-i-construct-a-session-when-do-i-commit-it-and-when-do-i-close-it) |
| 17 | +recommends we create `Session` object at the beginning of a logical operation where |
| 18 | +database access is potentially anticipated. |
| 19 | + |
| 20 | +The repository starts a `Session` for each _operation_, in order to maintain isolation. |
| 21 | +This means you can create a repository object almost whenever you want. |
| 22 | + |
| 23 | +/// details | There two exceptions: creating repositories in global variables or concurrent asyncio tasks |
| 24 | + type: warning |
| 25 | +The repository creates and maintain the lifecycle of a session object to avoid |
| 26 | +emitting unnecessary queries to refresh the model state on new session. |
| 27 | + |
| 28 | +The session is not thread safe, therefore the repository is not thread safe as well. |
| 29 | + |
| 30 | +Check the [Notes on multithreaded applications](/manager/session/#note-on-multithreaded-applications) |
| 31 | + |
| 32 | +The `AsyncSession` [is not safe on concurrent asyncio tasks](https://docs.sqlalchemy.org/en/20/orm/session_basics.html#is-the-session-thread-safe-is-asyncsession-safe-to-share-in-concurrent-tasks), |
| 33 | +therefore the same repository instance can't be used in multiple asyncio tasks like |
| 34 | +when using `asyncio.gather()` |
| 35 | +/// |
| 36 | + |
| 37 | +Even using multiple repository instances will work fine, however as they will have completely |
| 38 | +different sessions, it's likely that the second repository will fire additional SELECT queries |
| 39 | +to get the state of the object prior to saving it. |
| 40 | + |
| 41 | +/// details | Example |
| 42 | +```python |
| 43 | +from sqlalchemy import String |
| 44 | +from sqlalchemy.orm import Mapped, mapped_column |
| 45 | +from sqlalchemy_bind_manager import SQLAlchemyBindManager ,SQLAlchemyConfig |
| 46 | +from sqlalchemy_bind_manager.repository import SQLAlchemyRepository |
| 47 | + |
| 48 | +config = SQLAlchemyConfig( |
| 49 | + engine_url="sqlite:///./sqlite.db", |
| 50 | + engine_options=dict(connect_args={"check_same_thread": False}, echo=True), |
| 51 | + session_options=dict(expire_on_commit=False), |
| 52 | +) |
| 53 | + |
| 54 | +sa_manager = SQLAlchemyBindManager(config={}) |
| 55 | + |
| 56 | +class MyModel(sa_manager.get_bind().model_declarative_base): |
| 57 | + id: Mapped[int] = mapped_column(primary_key=True) |
| 58 | + name: Mapped[str] = mapped_column(String(30)) |
| 59 | + |
| 60 | +def update_my_model(): |
| 61 | + # Create 2 instances of the same repository |
| 62 | + repo = SQLAlchemyRepository(sa_manager.get_bind(), model_class=MyModel) |
| 63 | + repo2 = SQLAlchemyRepository(sa_manager.get_bind(), model_class=MyModel) |
| 64 | + |
| 65 | + o = repo.get(1) |
| 66 | + o.name = "John" |
| 67 | + |
| 68 | + repo2.save(o) |
| 69 | + |
| 70 | +update_my_model() |
| 71 | +``` |
| 72 | +/// |
| 73 | + |
| 74 | +The recommendation is of course to use the same repository instance, where possible, |
| 75 | +and structure your code in a way to match the single repository instance approach. |
| 76 | + |
| 77 | +For example a strategy similar to this would be optimal, if possible: |
| 78 | + |
| 79 | +* Create repositories |
| 80 | +* Retrieve all the models you need |
| 81 | +* Do the changes you need, as per business logic |
| 82 | +* Save all the changed models as needed |
| 83 | + |
| 84 | +/// tip | Using multiple repository instances is the only way to safely use concurrent asyncio tasks |
| 85 | + |
| 86 | +/// |
| 87 | + |
| 88 | +### Unit of work |
| 89 | + |
| 90 | +The Unit of Work session management follows the **same exact rules as the repository**, |
| 91 | +therefore you should approach the creation af a `UnitOfWork` object in the same way. |
0 commit comments