A Python query builder inspired by Objection.js
Project description
Sustained.py
A Python query builder inspired by Objection.js.
Installation
This package is not available on PyPI and must be installed from source.
pip install .
Usage
from sustained import Model, RelationType, create_model
class Person(Model):
database = 'my_db'
tableSchema = 'public'
tableName = 'persons'
class Animal(Model):
tableName = 'animals'
relationMappings = {
'owner': {
'relation': RelationType.BelongsToOneRelation,
'modelClass': Person,
'join': {
'from': 'animals.ownerId',
'to': 'persons.id'
}
}
}
# Build a query
query = Animal.query().select('animals.name', 'persons.name').leftOuterJoinRelated('owner')
print(query)
# SELECT animals.name, persons.name FROM animals LEFT OUTER JOIN persons ON animals.ownerId = persons.id
# Build a more complex query with a CTE and a raw join
active_owners = Person.query().select('id').where('status', '=', 'active')
query = (
Animal.query()
.with_('active_owners', active_owners)
.join('active_owners', 'animals.ownerId', '=', 'active_owners.id')
.select('animals.name')
)
print(query)
# WITH active_owners AS (SELECT id FROM persons WHERE status = 'active') SELECT animals.name FROM animals JOIN active_owners ON animals.ownerId = active_owners.id
Development
This project uses pre-commit to enforce code quality and run tests before committing code.
Pre-commit Hooks Setup
-
Install pre-commit:
pip install pre-commit
-
Install the Git hooks: From the root of the project directory, run:
pre-commit install
Now, the pre-commit hooks (including black, isort, mypy, and unit tests) will run automatically on every commit.
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
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 sustained-0.0.1.tar.gz.
File metadata
- Download URL: sustained-0.0.1.tar.gz
- Upload date:
- Size: 16.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ebc80253d78b6e28ac40752398907e04e889335f704863832a4e3db01ef368e4
|
|
| MD5 |
dd729f85320c5771f4941d38b92ccea0
|
|
| BLAKE2b-256 |
fffbd4bd8e14b174fded4c19030461554fcc72f365fb420e61d7a58a9aad4a44
|
File details
Details for the file sustained-0.0.1-py3-none-any.whl.
File metadata
- Download URL: sustained-0.0.1-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a3fa3641d0778f4391ee883cce6da3449798a95dda99688f7ed7e1bea61d5a5
|
|
| MD5 |
f1064df1b343da2b9bf02b481807e2dd
|
|
| BLAKE2b-256 |
e97833f473b753ce9451054e976cb02d1beba80858f3e55737054e489fbaa898
|