A comprehensive projection system for defining how data should be shaped, aggregated, grouped, ordered, and limited in database queries
Project description
Fractal Projections
A comprehensive projection system for defining how data should be shaped, aggregated, grouped, ordered, and limited in database queries.
This library complements fractal-specifications (which handles filtering) by providing database-agnostic data shaping capabilities.
Features
- Database-Agnostic: Write projections once, use them across different databases
- Type-Safe: Fully typed Python API for better IDE support and fewer errors
- Flexible: Support for field selection, aggregations, grouping, ordering, and limiting
- Multi-Database Support: Built-in builders for:
- PostgreSQL
- MongoDB
- Firestore
- Elasticsearch
Installation
pip install fractal-projections
For specific database support:
# PostgreSQL
pip install fractal-projections[postgres]
# MongoDB
pip install fractal-projections[mongo]
# Firestore
pip install fractal-projections[firestore]
# Elasticsearch
pip install fractal-projections[elasticsearch]
# All databases
pip install fractal-projections[all]
Quick Start
from fractal_projections import (
QueryProjection,
FieldProjection,
ProjectionList,
OrderingProjection,
LimitProjection,
)
from fractal_projections.builders import PostgresProjectionBuilder
from fractal_specifications.generic.operators import EqualsSpecification
# Define a database-agnostic projection
query = QueryProjection(
filter=EqualsSpecification("status", "active"),
projection=ProjectionList([
FieldProjection("id"),
FieldProjection("name"),
FieldProjection("created_at"),
]),
ordering=OrderingProjection("created_at", descending=True),
limit=LimitProjection(10),
)
# Convert to database-specific query
builder = PostgresProjectionBuilder("users")
sql, params = builder.build(query)
print(sql)
# SELECT id, name, created_at FROM users WHERE status = %s ORDER BY created_at DESC LIMIT 10
# params = ['active']
Core Concepts
Field Projection
Select specific fields from your data:
from fractal_projections import FieldProjection, ProjectionList
projection = ProjectionList([
FieldProjection("user_id"),
FieldProjection("email"),
])
Aggregation
Perform aggregations like COUNT, SUM, AVG:
from fractal_projections import AggregateProjection, AggregateFunction
projection = AggregateProjection(
field="revenue",
function=AggregateFunction.SUM,
alias="total_revenue"
)
Grouping
Group results by one or more fields:
from fractal_projections import GroupingProjection
grouping = GroupingProjection(["organization_id", "status"])
Ordering
Sort results by fields:
from fractal_projections import OrderingProjection, OrderingList
ordering = OrderingList([
OrderingProjection("created_at", descending=True),
OrderingProjection("name", descending=False),
])
Limiting
Limit and offset results:
from fractal_projections import LimitProjection
limit = LimitProjection(limit=20, offset=10)
Architecture
The library follows a builder pattern:
- Projections: Database-agnostic definitions of how data should be shaped
- Builders: Database-specific converters that translate projections into native queries
QueryProjection (agnostic)
↓
PostgresProjectionBuilder → SQL
MongoProjectionBuilder → MongoDB Pipeline
FirestoreProjectionBuilder → Firestore Query
ElasticsearchProjectionBuilder → ES Query DSL
This separation allows you to:
- Write business logic once
- Switch databases without changing application code
- Get optimized native queries for each backend
Advanced Usage
See the examples.py file for comprehensive examples including:
- Complex aggregations with grouping
- Multi-field ordering
- Combining filters with projections
- Database-specific optimizations
Development
# Clone the repository
git clone https://github.com/Fractal-Forge/fractal-projections.git
cd fractal-projections
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Format code
black .
isort .
ruff check --fix .
# Lint code
ruff check .
mypy fractal_projections
License
MIT License - see LICENSE file for details
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Related Projects
- fractal-specifications - Database-agnostic filtering system
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 fractal_projections-0.2.0.tar.gz.
File metadata
- Download URL: fractal_projections-0.2.0.tar.gz
- Upload date:
- Size: 33.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b163943ece1eb7036627b4d42cb19463dddfd1315c29603aa09a3e6eceb7e03c
|
|
| MD5 |
44b6a1d03edb4eff82b7c27a548bfee8
|
|
| BLAKE2b-256 |
7e313f8c2c7c386e694714c65c3fe869cde191447718c8ebbd8086f659dee7fd
|
File details
Details for the file fractal_projections-0.2.0-py3-none-any.whl.
File metadata
- Download URL: fractal_projections-0.2.0-py3-none-any.whl
- Upload date:
- Size: 25.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
107d39e0e86d695596c8cf8e2b92f6309f0261d9bf4c068add6d55554c0da352
|
|
| MD5 |
8a54b35e1e3deb529378be0594c613ee
|
|
| BLAKE2b-256 |
78caa952578b530626c58993a28c0f6b60fc1c6859cb92e1d8b4e0af89861449
|