This repository provides a template for creating custom database modules for the LitePolis system using pyproject.toml for modern Python packaging.
The example code within this template uses SQLModel and SQLite for demonstration purposes. You will replace this specific implementation with your chosen database technology (e.g., PostgreSQL, MySQL, MongoDB, Redis, etc.) and corresponding drivers or ORMs/ODMs (e.g., SQLAlchemy, Psycopg2, PyMongo, Django ORM, etc.).
- Folder:
litepolis_database_template/(Rename this to your package name using underscores, e.g.,litepolis_database_mymongodb) - File:
pyproject.toml(Update[project]table:name,version,dependencies,authors,description,urls, etc.) - File:
requirements.txt(Optional: Pin development dependencies if needed) - File:
litepolis_database_template/utils.py(UpdateDEFAULT_CONFIG, connection/session logic relevant to your DB) - File:
litepolis_database_template/Actor.py(UpdateDatabaseActorimplementation/inheritance) - File:
litepolis_database_template/__init__.py(Update imports to expose yourDatabaseActorandDEFAULT_CONFIG) - Files:
litepolis_database_template/Users.py,Conversations.py(Delete or replace these example model/manager files) - Folder:
tests/(Rewrite tests for your implementation) - File:
README.md(This file! Update prerequisites, add specific notes for your implementation)
Follow these steps to adapt the template:
- Clone Repository: Get a local copy of this template. You can use the CLI if you installed
litepolisfrom PyPIlitepolis-cli create database LitePolis-database-mymongodb
- Update
pyproject.toml: Modify the[project]table:- Update
version,authors, anddescription. - Crucially, update
dependencies: List all libraries your module requires to run, including your chosen database driver/ORM (e.g.,"pymongo","psycopg2-binary","redis"). Remove"sqlmodel"if you are not using the example implementation. - Update
[project.urls].Homepage.
- Update
- Install Dependencies: Create a virtual environment and install the dependencies:
python -m venv venv source venv/bin/activate # or venv\Scripts\activate on Windows # Install your package in editable mode (uses pyproject.toml) pip install -e .
- Configure Database (
litepolis_database_template/utils.pyor your renamed equivalent):- Update
DEFAULT_CONFIGwith configuration keys and default values relevant to your database (e.g., connection string/URI, host, port, db name, credentials). - Implement the database connection logic (e.g., creating a client, engine, connection pool).
- Implement session/connection handling functions or context managers suitable for your database and chosen access pattern.
- Adapt the configuration loading logic (checking
PYTEST_CURRENT_TESTvs. defaults) ensuring it reads fromDEFAULT_CONFIGduring tests.
- Update
- Define Data Models:
- Replace the example model files (
Users.py,Conversations.py) for your data models/schemas using your chosen tools (e.g., Pydantic models for MongoDB, SQLAlchemy models, Django models).
- Replace the example model files (
- Implement Database Logic:
- Create "Manager" classes (or use another pattern) for your data models, containing methods for database operations (CRUD, queries, aggregations, etc.).
- Update
litepolis_database_template/Actor.py(DatabaseActor) to utilize your Manager classes (e.g., through inheritance or composition). Remove the example inheritance (UserManager,ConversationManager). Remember, the class name must remainDatabaseActor.
- Write/Update Tests (
tests/):- Delete or heavily modify the existing example test files (
test_Users.py, etc.). - Ensure proper test database setup/teardown procedures (e.g., using a separate test database, mocking, fixtures).
- Run tests using
pytest.
- Delete or heavily modify the existing example test files (
- Document Prerequisites: Add any necessary setup instructions for your specific database to this README (e.g., "Requires a running PostgreSQL server accessible via connection string defined in config", "Ensure MongoDB is running on localhost:27017", "Redis server must be running").
- Release: Once developed and tested, you can build and publish your package to PyPI using tools like
buildandtwine.python -m buildtwine upload dist/*- Once your package is published on PyPI, the LitePolis CLI will be able to download and install it during the setup of other LitePolis services that depend on it.
While changing the implementation, retain these structural concepts:
- A central
DatabaseActorclass inActor.pyas the main entry point for LitePolis. Do not rename this class. - Using a configuration dictionary (
DEFAULT_CONFIGinutils.py) for database settings, exposed by your package's__init__.py. - Having dedicated logic for database connection and session/client management (e.g., in
utils.py). - Organizing database logic for different models/collections into separate "Manager" classes (recommended pattern, but adaptable).
- Comprehensive testing using
pytest(or equivalent) in thetests/directory. - Standard Python packaging via
pyproject.toml(PEP 621).
graph LR
subgraph LitePolis
A[LitePolis Core]
Aa[LitePolis Modules]
end
subgraph Your Database Module (e.g., litepolis-database-mymongodb)
A --> B(DatabaseActor)
Aa --> B(DatabaseActor)
B --> C[YourModelManager1]
B --> D[YourModelManager2]
B --> E[Connection/Utils]
E --> F[DEFAULT_CONFIG]
end
C --> G[Your Database]
D --> G