The database module for LitePolis
Project description
LitePolis Database Example
This repository provides a working example of a database module for LitePolis, using SQLite as the database backend. It demonstrates a structured approach to database interactions using SQLModel and a Manager/Actor pattern, separating database logic from potential API layers.
This example is based on the LitePolis-database-template and showcases how to implement database operations for Users, Conversations, and Comments. You can use this example as a starting point to build your own custom database modules for LitePolis.
While this README focus on how to build your own database module, our Wiki shows how to use this database module 🚀
Core Concepts: Manager Pattern
This module utilizes a Manager pattern to organize database operations:
- SQLModels: Define the database table structure (e.g.,
User,Conversation,CommentinUsers.py,Conversations.py,Comments.py). - Manager Classes: Each model has a corresponding Manager class (e.g.,
UserManager,ConversationManager) located in the same file. These classes contain static methods that encapsulate the specific CRUD (Create, Read, Update, Delete) logic for that model, using a shared database session (with_sessionfromutils.py). - DatabaseActor: A central
DatabaseActorclass (Actor.py) inherits from all Manager classes. This provides a single, unified interface to access all database operations for the module.
graph LR
subgraph litepolis_router_database_sqlite
DA[DatabaseActor] --> UM(UserManager)
DA --> CM(ConversationManager)
DA --> CMM(CommentManager)
UM --> User(User SQLModel)
UM --> utils(utils.py: get_session)
CM --> Conv(Conversation SQLModel)
CM --> utils
CMM --> Comm(Comment SQLModel)
CMM --> utils
end
style DA fill:#f9f,stroke:#333,stroke-width:2px
style UM fill:#ccf,stroke:#333,stroke-width:1px
style CM fill:#ccf,stroke:#333,stroke-width:1px
style CMM fill:#ccf,stroke:#333,stroke-width:1px
This pattern promotes separation of concerns: the database module focuses solely on data persistence logic, while the main LitePolis application (or a separate router module) would handle API endpoints and call methods on the DatabaseActor.
Getting Started
Follow these steps to understand and adapt this example for your own LitePolis database module:
-
Clone the Repository: Clone this repository to your local machine.
-
Install Dependencies: Install the required Python packages. Note that
fastapiis not a direct dependency of this module anymore; it depends onlitepolisandsqlmodel.pip install -r requirements.txt
-
Examine
setup.py: This file contains metadata about the package.name: Change to your package's unique name (e.g.,litepolis-mydatabase).version,description,author,url: Update these fields.install_requires: Should list runtime dependencies likelitepolisandsqlmodel. Add any others your specific logic requires.
-
Understand the Structure (
litepolis_database_example/):utils.py: Sets up the database engine (create_engine) and provides thewith_sessioncontext manager for database interactions. It also definesDEFAULT_CONFIG.Users.py,Conversations.py,Comments.py: Each file defines:- A SQLModel class for the data table (e.g.,
User). - A Manager class (e.g.,
UserManager) with static methods for CRUD operations usingwith_session.
- A SQLModel class for the data table (e.g.,
Actor.py: Defines theDatabaseActorclass, inheriting from all Manager classes.__init__.py: Exports the key components (DatabaseActor, SQLModels,DEFAULT_CONFIG) for use by other parts of LitePolis.
-
Adapt and Extend:
-
Rename the
litepolis_database_examplefolder to your desired package name (e.g.,litepolis_database_mydatabase). Update imports accordingly. -
Create New Models/Managers: For new data types, create a new Python file (e.g.,
Products.py). Inside, define yourProductSQLModel and aProductManagerclass with static CRUD methods usingget_session. -
Update
Actor.py: Add your newProductManagerto the inheritance list ofDatabaseActor.from .Products import ProductManager # ... other imports ... class DatabaseActor(UserManager, ConversationManager, CommentManager, ProductManager): pass
classDiagram class DatabaseActor { +create_user() +create_conversation() +read_users() +read_conversations() } DatabaseActor --|> UserManager DatabaseActor --|> ConversationManager DatabaseActor --|> CommentManager -
Update
__init__.py: Export your newProductmodel and potentially theProductManagerif needed externally. Update the__all__list. -
Update
setup.py: Adjust metadata (name,version, etc.). -
Update
requirements.txt: Add any new dependencies required by your managers.
-
-
Testing (
testsfolder):- The tests (
test_Users.py,test_Conversations.py,test_Comments.py,test_Actor.py) demonstrate how to test the database logic by directly calling methods on theDatabaseActor. - When adapting this example, update existing tests and add new ones for your models, ensuring you test the methods within your new Manager classes via the
DatabaseActor. - Run tests using:
pytest
- The tests (
-
Integration with LitePolis: This module provides the
DatabaseActor. The main LitePolis application or a dedicated LitePolis router module would import and use thisDatabaseActorto interact with the database, potentially exposing operations via API endpoints. -
Deployment:
- Release your package to PyPI so the LitePolis package manager can fetch it.
- Document any pre-requirements for your specific database backend (e.g., setting up PostgreSQL, MySQL). The example uses SQLite (
database.db), which requires no external setup.
Key Libraries
- SQLModel: Simplifies database interactions by combining Pydantic and SQLAlchemy. https://sqlmodel.tiangolo.com/
- Pytest: Used for writing and running tests. https://docs.pytest.org/en/stable/
- LitePolis: The framework this module is designed to integrate with. https://github.com/NewJerseyStyle/LitePolis.
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 litepolis_database_example-0.0.2.tar.gz.
File metadata
- Download URL: litepolis_database_example-0.0.2.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd21bf635db127d1cd73c811a7af12431b6cfd8a99b9a2ba36cf05c7312f8a19
|
|
| MD5 |
ac7d9a25f7d56d2b8c5dd7f52b3232de
|
|
| BLAKE2b-256 |
419ce951915c73e828ffabc81fbc4f8bad2fae767357712e8c625c1d445fc97c
|
Provenance
The following attestation bundles were made for litepolis_database_example-0.0.2.tar.gz:
Publisher:
python-publish.yml on NewJerseyStyle/LitePolis-database-example
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
litepolis_database_example-0.0.2.tar.gz -
Subject digest:
cd21bf635db127d1cd73c811a7af12431b6cfd8a99b9a2ba36cf05c7312f8a19 - Sigstore transparency entry: 193369666
- Sigstore integration time:
-
Permalink:
NewJerseyStyle/LitePolis-database-example@606bc65365a941d85a32105aab483d54bbb8a056 -
Branch / Tag:
refs/tags/example-publish - Owner: https://github.com/NewJerseyStyle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@606bc65365a941d85a32105aab483d54bbb8a056 -
Trigger Event:
release
-
Statement type:
File details
Details for the file litepolis_database_example-0.0.2-py3-none-any.whl.
File metadata
- Download URL: litepolis_database_example-0.0.2-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71b2120843660eb92829ca51d5bd2240dc8686423a479b326190fba70c3aa65d
|
|
| MD5 |
31459601004a2804c3c9b510bbe435bc
|
|
| BLAKE2b-256 |
7f720ba0ff953ffc920541067a827539f1993224441d8119329676bbb5daedb7
|
Provenance
The following attestation bundles were made for litepolis_database_example-0.0.2-py3-none-any.whl:
Publisher:
python-publish.yml on NewJerseyStyle/LitePolis-database-example
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
litepolis_database_example-0.0.2-py3-none-any.whl -
Subject digest:
71b2120843660eb92829ca51d5bd2240dc8686423a479b326190fba70c3aa65d - Sigstore transparency entry: 193369668
- Sigstore integration time:
-
Permalink:
NewJerseyStyle/LitePolis-database-example@606bc65365a941d85a32105aab483d54bbb8a056 -
Branch / Tag:
refs/tags/example-publish - Owner: https://github.com/NewJerseyStyle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@606bc65365a941d85a32105aab483d54bbb8a056 -
Trigger Event:
release
-
Statement type: