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.
Metadata
Release files for litepolis-database-example 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| litepolis_database_example-0.0.2.tar.gz | 14.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| litepolis_database_example-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.0 kB
Release files / litepolis_database_example-0.0.2.tar.gz
| Download URL | litepolis_database_example-0.0.2.tar.gz |
|---|---|
| Size | 14.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cd21bf635db127d1cd73c811a7af12431b6cfd8a99b9a2ba36cf05c7312f8a19
|
|
BLAKE2b-256 checksum How to use checksums |
419ce951915c73e828ffabc81fbc4f8bad2fae767357712e8c625c1d445fc97c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 7, 2025.
Transparency logRelease files / litepolis_database_example-0.0.2-py3-none-any.whl
| Download URL | litepolis_database_example-0.0.2-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
71b2120843660eb92829ca51d5bd2240dc8686423a479b326190fba70c3aa65d
|
|
BLAKE2b-256 checksum How to use checksums |
7f720ba0ff953ffc920541067a827539f1993224441d8119329676bbb5daedb7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 7, 2025.
Transparency log