gdmongolite is a zero-boilerplate, schema-first, multi-driver MongoDB toolkit that unifies sync and async drivers, Pydantic validation, migrations, telemetry hooks, and a CLI into one package.
Project description
gdmongolite Documentation
gdmongolite is a zero-boilerplate, schema-first, multi-driver MongoDB toolkit that unifies sync and async drivers, Pydantic validation, migrations, telemetry hooks, and a CLI into one package.
Table of Contents
- Installation
- Configuration
- Defining Schemas
- Connecting to MongoDB
- CRUD Operations
- Migrations
- Interactive Shell
- Model Generation
- Telemetry Hooks
- CLI Reference
- Testing
- Project Layout
- Best Practices
- FAQ
1. Installation
Install from PyPI:
pip install gdmongolite
For development tools:
pip install gdmongolite[dev]
2. Configuration
Create a .env file in your project root:
MONGO_URI="mongodb://localhost:27017"
MONGO_DB="myapp"
MONGO_MAX_POOL=50
gdmongolite reads these automatically via environment variables. You can also override via environment variables.
3. Defining Schemas
All schemas inherit from Schema. Collection names are inferred:
# src/gdmongolite/schema/__init__.py
from gdmongolite import DB, Schema, Email, Positive
db = DB() # Uses .env
class User(Schema):
name: str
email: Email # Validates format
age: Positive # Must be >0
tags: list[str] = [] # Default empty list
Now you can use db.User for operations.
4. Connecting to MongoDB
Instantiate the singleton DB facade:
from gdmongolite import DB
db = DB()
5. CRUD Operations
| Operation | Async |
|---|---|
| Insert one | await db.User.insert(data) |
| Find many | await db.User.find(**filters).to_list() |
Returned value is a MongoDB cursor for find operations. |
6. Migrations
Generate migration scripts when schemas change:
gdmongolite migrate
- Creates timestamped scripts under
migrations/
7. Interactive Shell
Launch REPL with db preloaded:
gdmongolite shell
Inside the shell:
await db.User.insert({"name":"Bob", "email":"bob@x.com", "age":25})
users = await db.User.find().to_list()
8. Model Generation
Scaffold schemas from existing collections:
gdmongolite gen-model --collection products --out src/models/product.py
- Samples documents
- Infers field names and types
9. Telemetry Hooks
Register hooks on query events:
from gdmongolite.core import DBSingleton
@DBSingleton.on("pre_query")
def before(collection, filt, opts):
print(f"About to query {collection}: {filt}")
@DBSingleton.on("post_query")
def after(collection, result):
print(f"{collection} query completed.")
10. CLI Reference
gdmongolite --help
# Commands:
# migrate Generate and apply migration scripts
# shell Launch interactive REPL
# gen-model Scaffold a schema from an existing collection
# test Run test suite
11. Testing
pip install gdmongolite[dev]
pytest --maxfail=1 --disable-warnings -q
12. Project Layout
gdmongolite/
├── src/gdmongolite/
│ ├── __init__.py
│ ├── core.py
│ ├── schema/
│ │ └── __init__.py
│ ├── query.py
│ ├── migrate/
│ │ └── __init__.py
│ ├── telemetry.py
│ ├── cli.py
│ └── models/ # Generated models
├── migrations/ # Auto-generated scripts
├── tests/
├── README.md
├── LICENSE
├── pyproject.toml
└── .env
13. Best Practices
- Keep schemas small and focused
- Version your migrations and commit them
- Use telemetry hooks to capture performance metrics
14. FAQ
Q: How do I handle large result sets?
A: Use cursors with to_list().
Q: Can I use gdmongolite with FastAPI?
A: Absolutely—instantiate db at startup and import your schemas into your routers.
gdmongolite empowers you with a single, coherent API for all MongoDB needs—schema definition, validation, querying, migrations, telemetry, and CLI tooling—ensuring you never touch low-level driver code again.
Author
Ganesh Datta Padamata Email: ganeshdattapadamata@gmail.com PyPI: ganeshdatta999
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 gdmongolite-0.1.1.tar.gz.
File metadata
- Download URL: gdmongolite-0.1.1.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a156fb921da3472842439ee5d05a7c8bdbcc32ab180947f07971dcf3bdd21be
|
|
| MD5 |
db29fbfe376575deaa9e8ae5fb2ab9af
|
|
| BLAKE2b-256 |
42dd5a262f2d3033b34a7dd6b8ad03f58dbd6a72a995ac8e9e4dd54722818527
|
File details
Details for the file gdmongolite-0.1.1-py3-none-any.whl.
File metadata
- Download URL: gdmongolite-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d214078ce4b8bebd508edec714e91eb99556ccbea815409e6a9578bd2527c860
|
|
| MD5 |
034e64d748e48c60d49738332a93bbda
|
|
| BLAKE2b-256 |
683bbdda2741b7dd32b46b1a66601118bbbeff73b7d07f2868fca7f3a6275822
|