Thanks to visit codestin.com
Credit goes to github.com

Skip to content

πŸ—„οΈ Python Persistence API: A JPA-inspired abstraction layer. Unifies SQL & NoSQL access via a robust Repository Pattern and Entity Manager.

License

Notifications You must be signed in to change notification settings

soltani-a/soltania-python-persistence-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

soltania-python-persistence-api

Python Package Manager License

πŸ—„οΈ Python Persistence API: A JPA (Java Persistence API) and Spring Data inspired abstraction layer.

This framework unifies database access (SQL & NoSQL) via a robust implementation of the Repository Pattern and the Entity Manager. The current implementation focuses on Apache TinkerPop (Gremlin) for Graph Databases.


πŸ“‘ Table of Contents


πŸ— Architecture & Concepts

This project ports solid concepts from the Java ecosystem to Python:

  1. Domain Entities (@Entity): Uses Pydantic to define strongly typed data models with runtime validation.
  2. Entity Manager (EntityManager): An agnostic interface managing the object lifecycle (persist, find, remove) and the underlying driver connection.
  3. Repositories (Repository): A business abstraction layer hiding query complexity (Gremlin, SQL) behind simple methods (save, find_by_id, find_fastest_path).

Simplified Class Diagram

classDiagram
    class BaseEntity {
        +ID id
        +datetime created_at
        +datetime updated_at
    }
    class Station {
        +str name
        +int zone
    }
    BaseEntity <|-- Station
    
    class EntityManager {
        <<Interface>>
        +persist(entity)
        +find_by_property(key, value)
        +create_relationship(from, to, rel)
    }
    class GremlinEntityManager {
        +DriverRemoteConnection connection
    }
    EntityManager <|-- GremlinEntityManager
    
    class MetroRepository {
        +save_station(station)
        +find_fastest_path(start, end)
    }
    MetroRepository --> EntityManager : uses

Loading

##πŸš€ InstallationThis project uses uv for lightning-fast dependency management.

  1. Clone the repository:
git clone [https://github.com/your-user/soltania-python-persistence-api.git](https://github.com/your-user/soltania-python-persistence-api.git)
cd soltania-python-persistence-api
  1. Install dependencies and environment:
uv sync

This command automatically creates the .venv folder and installs everything needed.


##βš™οΈ Configuration GuideThis project uses a hierarchical configuration system inspired by Spring Boot. Variables are defined in src/soltania_persistence/config.py.

###πŸ“‹ Available Variables| Variable | Description | Default Value | | --- | --- | --- | | GREMLIN_HOST | IP Address of the Tinkerpop/Gremlin server | localhost | | GREMLIN_PORT | Server Port | 8182 | | GREMLIN_PROTOCOL | ws (WebSocket) or wss (Secure) | ws |

###πŸš€ Source Priority1. CLI Arguments (e.g. --gremlin_host=10.0.0.1) 2. Environment Variables (export GREMLIN_HOST=...) 3. Internal .env File (Project root) 4. Default Values (Code)


##πŸš‡ Running the Demos (Paris Metro)The project includes a complete example modeling the Paris Metro Network located in src/soltania_persistence/examples/metro_network.

###1. Initialize the DatabaseYou must first clean and populate the database with the provided line data (JSON).

# Clear database (Drop)
uv run src/soltania_persistence/examples/metro_network/main.py drop

# Import data (Load)
uv run src/soltania_persistence/examples/metro_network/main.py load

###2. Calculate an ItineraryRun the pathfinding algorithm between any two stations.

Example 1: A simple trip

uv run src/soltania_persistence/examples/metro_network/main.py "Mairie des Lilas" "ChΓ’telet"

Example 2: A complex trip (with transfer)

uv run src/soltania_persistence/examples/metro_network/main.py "Mairie des Lilas" "Chelles - Gournay"

###πŸ“Έ Real-world OutputHere is an actual execution trace. Notice how the engine intelligently detects transfers:

πŸš€ FASTEST ROUTE (43 min 30 sec)
==================================================
πŸ“ START : Mairie des Lilas

   ⬇️  TAKE πŸš‡ METRO 11
      β–ͺ️ Porte des Lilas
      β–ͺ️ TΓ©lΓ©graphe
      β–ͺ️ Place des FΓͺtes
      β–ͺ️ Jourdain
      β–ͺ️ PyrΓ©nΓ©es
      β–ͺ️ Belleville
      β–ͺ️ Goncourt
      β–ͺ️ RΓ©publique
      β–ͺ️ Arts et MΓ©tiers
      β–ͺ️ Rambuteau
      β–ͺ️ HΓ΄tel de Ville
      β–ͺ️ ChΓ’telet

   πŸ”„ TRANSFER : Take πŸš„ RER A
      β–ͺ️ Gare de Lyon
      β–ͺ️ Nation
      β–ͺ️ Vincennes
      β–ͺ️ Val de Fontenay
      β–ͺ️ Neuilly-Plaisance
      β–ͺ️ Bry-sur-Marne
      β–ͺ️ Noisy-le-Grand - Mont d'Est
      β–ͺ️ Noisy - Champs

   πŸ”„ TRANSFER : Take πŸš‡ METRO 16
      β–ͺ️ Chelles - Gournay
==================================================
🏁 ARRIVAL : Chelles - Gournay


##πŸ“‚ Project StructureThe project follows a modular "Domain-Driven" structure.

soltania-python-persistence-api/
β”œβ”€β”€ pyproject.toml               # Dependencies
β”œβ”€β”€ .env                         # Local config
β”œβ”€β”€ src/
β”‚   └── soltania_persistence/
β”‚       β”œβ”€β”€ config.py            # βš™οΈ Configuration Engine
β”‚       β”œβ”€β”€ core/                # 🧱 Framework Core (Entities, Interfaces)
β”‚       β”œβ”€β”€ provider/            # πŸ”Œ Drivers (Tinkerpop/Gremlin)
β”‚       └── examples/
β”‚           └── metro_network/   # πŸš‡ Domain Example: Transport
β”‚               β”œβ”€β”€ data/        # JSON Data (lines.json)
β”‚               β”œβ”€β”€ models/      # Nodes (Station) & Edges (Connection)
β”‚               β”œβ”€β”€ repositories/# Gremlin Logic (Pathfinding)
β”‚               β”œβ”€β”€ services/    # ETL/Importer Logic
β”‚               └── main.py      # Entry Point (CLI)
└── tests/                       # πŸ§ͺ Unit & Integration Tests


##πŸ’» Usage (Code Snippet)Here is how you would use the framework in your own code:

from soltania_persistence.provider.tinkerpop.manager import GremlinEntityManager
from soltania_persistence.config import settings
from my_app.repositories import UserRepository

# 1. Initialize Manager
em = GremlinEntityManager(settings.gremlin_url)
repo = UserRepository(em)

# 2. Persist Data
user = User(username="admin", email="[email protected]")
repo.save(user)

# 3. Query Data
found_user = repo.find_by_username("admin")

##πŸ§ͺ Tests```bash

Run unit tests

uv run pytest -m "not integration"

Run all tests (requires running Gremlin server)

uv run pytest

About

πŸ—„οΈ Python Persistence API: A JPA-inspired abstraction layer. Unifies SQL & NoSQL access via a robust Repository Pattern and Entity Manager.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages