A lightweight Object Document Mapper for MongoDB
Project description
Gault
A lightweight Object Document Mapper (ODM) for MongoDB with Python type hints and state tracking.
Features
- Type-safe MongoDB documents with Python type hints
- Field aliasing for database column mapping
- Query operators with Pythonic syntax
- Async and sync managers for CRUD operations
- Aggregation pipeline support with fluent API
- Automatic state tracking and dirty field detection
- Atomic updates with persistence tracking
Installation
pip install gault
Quick Start
from gault import Schema, Model, Field, configure, AsyncManager
# Schema: Persistent documents mapped to MongoDB collections
class Person(Schema, collection="people"):
id: Field[int] = configure(pk=True)
name: Field[str]
age: Field[int] = configure(db_alias="person_age")
# Model: Non-persistent data classes (projections, view models, etc.)
class PersonSummary(Model):
name: Field[str]
total: Field[int]
# Create manager
manager = AsyncManager(database)
# Query and modify
person = await manager.get(Person, filter=Person.id == 1)
person.age = 43
await manager.save(person, atomic=True) # Only updates dirty fields
Querying
# Comparison operators
Person.age >= 18
Person.id.in_([1, 2, 3])
# Logical operators
filter = (Person.age >= 18) & (Person.name == "Alice")
filter = (Person.name == "Alice") | (Person.name == "Bob")
filter = ~(Person.age < 18)
# Field expressions
Person.score.expr.gt(42)
Person.price.expr.gt(Person.discount_price)
Aggregation Pipelines
from gault import Pipeline
from gault.accumulators import Sum, Avg
pipeline = (
Pipeline()
.match(Person.age >= 18)
.group({"total": Sum(1), "avg_age": Avg("$age")}, by="$name")
.sort({"total": -1})
.take(10)
)
async for result in manager.select(PersonSummary, pipeline):
print(result.name, result.total)
Persistence & Dirty Fields
person = await manager.get(Person, filter=Person.id == 1)
person.name = "New Name"
person.age = 50
# Only updates changed fields
await manager.save(person, atomic=True)
Requirements
- Python >= 3.11
- PyMongo >= 4.15.4
Documentation
Full documentation available at johnnoone.github.io/gault.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
gault-0.14.0.tar.gz
(39.5 kB
view details)
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
gault-0.14.0-py3-none-any.whl
(45.4 kB
view details)
File details
Details for the file gault-0.14.0.tar.gz.
File metadata
- Download URL: gault-0.14.0.tar.gz
- Upload date:
- Size: 39.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c03cae0ec759390d291b5dc92fa2fdc448734c027fa21352ce89b1a6343e8884
|
|
| MD5 |
3245983623c3c10dc3225c955ffd57c8
|
|
| BLAKE2b-256 |
8473dea44ead32f37191875dd26f0e54816bcf585eb60683d43d3589f9f0b1ab
|
File details
Details for the file gault-0.14.0-py3-none-any.whl.
File metadata
- Download URL: gault-0.14.0-py3-none-any.whl
- Upload date:
- Size: 45.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9822658e5522b3c9728b7c8144ca08cd8605ac8ada3363a6491dbc1be900aef9
|
|
| MD5 |
6f54057433882ae50f6f45117504eb2f
|
|
| BLAKE2b-256 |
3097c4c80dd561b22416626c9aca3994684af4cea07eac744708c57e4178ac69
|