JPA-like ORM for Python
Project description
python-jpa
python-jpa is a lightweight, Spring Data JPA-inspired Object-Document Mapper (ODM) for MongoDB built on top of Python 3.13+, Pydantic v2, and PyMongo.
It uses advanced Python metaprogramming (metaclass) to dynamically generate MongoDB queries at runtime based on method naming conventions or explicit query declarations, drastically reducing boilerplate code.
🚀 Features
- JPA-Like Repository Pattern: Declare an interface, and let the metaclass handle query assembly dynamically.
- Query Naming Conventions: Instantly supports
find_by_*,find_all_by_*,exists_by_*, andcount_by_*pattern resolutions. - Custom Query Annotations: Bind complex MongoDB query templates with custom parameter injections using the
@querydecorator. - Data Validation: Fully powered by Pydantic v2 for robust runtime type checking and parsing.
- Zero-Boilerplate ID Mapping: Automatically handles conversions between stringified hexadecimal keys and native BSON
ObjectIdfootprints. - Environment Profiles: Flexible configurations handling multi-stage deployment environments (
dev,prod, etc.) via YAML interpolation.
📁 Directory Structure
.
├── README.md
├── jpa
│ ├── __init__.py
│ ├── config.py # App bootstrapping, YAML parsing & environment mapping
│ └── mongo
│ ├── __init__.py
│ └── interface.py # Repository core interface, custom metadata, and queries
├── pyproject.toml
└── requirements.txt
🛠️ Getting Started
Prerequisites
• Python 3.13 or higher
Installation & Virtual Env Setup
1. Clone the repository and navigate to its root: cd python-jpa
2. Spin up a virtual environment and update your pip core dependencies: python3 -m venv venv source venv/bin/activate # On Windows use: venv\Scripts\activate pip install --upgrade pip
3. Install the project library dependencies: pip install -r requirements.txt
⚙️ Configuration Management
The framework supports multi-profile YAML configurations with real-time environment substitution syntax (e.g., ${ENV_VAR:default_value}).
Create a resources/ directory at the root of your execution workspace and include your application sheets:
1. Master Configuration (resources/settings.yml)
app:
profile: ${APP_PROFILE:dev} # Swaps profile to target environment configuration
2. Environment Profile Configuration (resources/settings-dev.yml)
mongodb:
uri: ${MONGO_URI:mongodb://localhost:27017}
database: ${MONGO_DB_NAME:jpa_database}
framework:
logging:
enabled: true
level: "DEBUG"
📖 Usage Example
Here is a quick overview of how you can configure a domain entity and generate auto-implemented interface pipelines:
1. Define your Document Model
from pydantic import Field
from jpa.mongo.interface import DocumentModel, document
@document(name="users")
class User(DocumentModel):
id: str = Field(alias="_id", default=None)
username: str
email: str
age: int
2. Declare your Interface Repository
By extending IRepository[T], method naming patterns are captured and converted into database interactions seamlessly.
from jpa.mongo.interface import IRepository, query
from typing import List, Optional
class UserRepository(IRepository[User]):
# 1. Query generation by structural method naming convention
def find_by_username(self, username: str) -> Optional[User]: ...
def count_by_age(self, age: int) -> int: ...
# 2. Templated declaration using placeholder substitutions
@query(definition={"email": "?0", "age": {"$gte": "?1"}})
def find_by_email_and_min_age(self, email: str, min_age: int) -> List[User]: ...
3. Execute CRUD Actions
from jpa.config import close_db_connection
# Initialize repository instance
user_repo = UserRepository()
# Save a document
new_user = User(username="pratyush", email="pratyush@example.com", age=25)
user_id = user_repo.save(new_user)
# Fetch using automatic query generation
user = user_repo.find_by_username("pratyush")
print(f"Found User: {user.email if user else 'Not Found'}")
# Clean up connections on process termination
close_db_connection()
📜 License
This project is open-source software licensed under the MIT License.
Project details
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 python_ppa-0.1.3.tar.gz.
File metadata
- Download URL: python_ppa-0.1.3.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ef233f06c9cbf10067f707141974654ae6c376aaf157fdd9c28bbf446d849d4
|
|
| MD5 |
70d3cb45f95cb6e1b6f28bc16577f1bd
|
|
| BLAKE2b-256 |
f22f1ee64e54d91c90191f8a91a93b9e0ebfee1e13afd3fe13339a84e2598306
|
Provenance
The following attestation bundles were made for python_ppa-0.1.3.tar.gz:
Publisher:
deploy.yml on vector-forces/python-jpa
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_ppa-0.1.3.tar.gz -
Subject digest:
4ef233f06c9cbf10067f707141974654ae6c376aaf157fdd9c28bbf446d849d4 - Sigstore transparency entry: 1776056521
- Sigstore integration time:
-
Permalink:
vector-forces/python-jpa@ab8660f1db96df050ed1587ba6e6368412ad4859 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/vector-forces
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy.yml@ab8660f1db96df050ed1587ba6e6368412ad4859 -
Trigger Event:
release
-
Statement type:
File details
Details for the file python_ppa-0.1.3-py3-none-any.whl.
File metadata
- Download URL: python_ppa-0.1.3-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e267a4236f2915a361ce90a5a0d81c63910e14b2acfd72030516cd7e3756bcf
|
|
| MD5 |
ae9ded2455156fb19bc13ba4b9d5e82d
|
|
| BLAKE2b-256 |
ef2b9e1aa6cf0c791e359ac90919baa61b77026e3d133447dead480e3a8eff02
|
Provenance
The following attestation bundles were made for python_ppa-0.1.3-py3-none-any.whl:
Publisher:
deploy.yml on vector-forces/python-jpa
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_ppa-0.1.3-py3-none-any.whl -
Subject digest:
7e267a4236f2915a361ce90a5a0d81c63910e14b2acfd72030516cd7e3756bcf - Sigstore transparency entry: 1776056714
- Sigstore integration time:
-
Permalink:
vector-forces/python-jpa@ab8660f1db96df050ed1587ba6e6368412ad4859 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/vector-forces
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy.yml@ab8660f1db96df050ed1587ba6e6368412ad4859 -
Trigger Event:
release
-
Statement type: