Skip to main content

A Python query builder inspired by Objection.js

Project description

Sustained.py

A Python query builder inspired by Objection.js.

Installation

python3 -m pip install sustained

Local Installation from Source

To install sustained from source for local development:

  1. Clone the repository:

    git clone https://github.com/wetherc/sustained.git
    cd sustained
    
  2. Install in editable mode:

    python3 -m pip install -e .
    

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

  1. Install pre-commit:

    pip install pre-commit
    
  2. Install the Git hooks: From the root of the project directory, run:

    pre-commit install
    

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

sustained-1.0.0.tar.gz (38.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sustained-1.0.0-py3-none-any.whl (2.6 kB view details)

Uploaded Python 3

File details

Details for the file sustained-1.0.0.tar.gz.

File metadata

  • Download URL: sustained-1.0.0.tar.gz
  • Upload date:
  • Size: 38.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for sustained-1.0.0.tar.gz
Algorithm Hash digest
SHA256 aedcadbee3c17c23ce82e88605aa76cfa0a1cbd5f67c4701e147b331b30c23a9
MD5 365ac2411c1e7ce0a934b478a209b1f9
BLAKE2b-256 5bd70a8f75f146a5eba6b983dc09b73f9655c9a6fc8a92ab57e2a271700d18a2

See more details on using hashes here.

File details

Details for the file sustained-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: sustained-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 2.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for sustained-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4a5539a5b4a50fc8de91b5ed5a61288a1702c00829f57e69972c879d49999f6a
MD5 9a409e1d95c8ad02867dc54d449bd988
BLAKE2b-256 86ab9d4710537c3cffe8dedf9582193ee6936a23191edc84a1477ccbf5480954

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page