Proposal: Separation of Concerns in library.py (Repository Pattern) #1384
Replies: 1 comment
-
|
There's a thread in the Discord server currently talks about everything going with proposed refactors to help align with these roadmap goals. For the sake of clarity I'll paste over some of my responses to other proposals and questions and things in general that also address a lot of what you bring up here:
Computerdores then suggested we do the core license split and interface layer inside the current repo before splitting it off, which ultimately I agreed with and is how we've been carrying things out since these messages, including full REUSE compliance. Responding to comments wanting to start a Rust rewrite now:
Ultimately incremental changes are the way to go, as long as they don't currently interfere with other roadmap items being worked on in open PRs, open feature branches, or upcoming milestones. I've recently given the feature roadmap a refresh that should also better align with which release cycles I see new features landing, with 9.6.x involving a lot of heavy lifting on database changes that have been needing to be made for a while. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone!
First, I want to say thank you for the great work on TagStudio. I really loved the concept and the vision behind the app, so much so that it inspired me to create a similar project called TagIt for my final university project.
While building my project, I spent a lot of time thinking about architecture, and after diving into TagStudio's core backend recently, I wanted to propose a structural enhancement for src/tagstudio/core/library/alchemy/library.py.
Specifically, I think this refactor could lay a much better basis and cleaner architecture for the upcoming Core Library + CLI milestone.
Aligning with the Core Library + CLI Milestone
Currently, the Library class has grown into a very large, centralized module. It handles several distinct responsibilities all at once:
Database connection and session lifecycle.
Complex SQLAlchemy queries and CRUD operations.
Schema migrations (e.g., __apply_db..._migration methods).
Core business logic and file handling.
I saw that the goal for the Core Library milestone is to create a separated core library that could interface with scripts, external programs, and a new CLI.
Having the database lifecycle, migrations, and raw SQL queries tightly coupled with the business logic makes it very difficult to extract a clean, lightweight core. By separating these concerns now, it will be significantly easier to expand the codebase, build out the CLI for bulk operations, and foster wider adoption without getting tangled in the internal SQLAlchemy implementation.
Proposed Architecture (Repository Pattern)
In my project, I implemented a strict Separation of Concerns architecture, and I'd love to propose a refactoring plan to bring a similar pattern to TagStudio to prepare for the above purpose.
We could break library.py down into smaller, specialized components:
Repositories (e.g., tag_repository.py, entry_repository.py): Pure data-access classes. They would take a SQLAlchemy Session and handle the raw SQL/ORM execution.
Migration Engine (migrations.py): Extracting the __apply_db_migration functions into a dedicated migration runner to keep the main library initialization clean.
Library Service (library.py): The main class remains the entry point for the app (and eventually the CLI), but instead of writing SQL and managing the session directly, it delegates to the repositories. It becomes a pure business logic coordinator.
Next Steps
I know rewriting a core file is a massive undertaking and carries risks, so I wanted to open a discussion before touching any code.
Is this architectural direction something the core team is interested in to help facilitate the core library + CLI?
If so, would you be open to an incremental PR? (For example, we could start by just extracting the Migration logic, or just moving the Tag-specific database queries into a TagRepository to see how it feels).
I'd love to give back to this project. Let me know your thoughts!
Beta Was this translation helpful? Give feedback.
All reactions