Skip to main content

Lightweight query access-pattern declaration for ORMs

Project description

query-patterns

Declare query-access patterns in code and verify matching DB indexes. Supports SQLAlchemy and Django, both schema-based and database introspection modes.

Motivation

As projects grow, the number and variety of database queries increase. Over time, it becomes difficult to maintain a consistent set of query-access patterns across the codebase, and even harder to verify whether each pattern is backed by an appropriate database index. Relying on manual checks or memory often leads to:

  • Missing or outdated indexes that cause silent performance regressions
  • Inconsistent query patterns across teams or modules
  • Schema changes that unintentionally break previously optimized queries
  • Performance issues that surface only in production traffic

query-patterns addresses these problems by allowing you to declare expected query patterns in code and validate them against either your ORM schema or a running database instance — all via a simple CLI command.

What it does

  • Collects all @query_pattern declarations from your Python modules
  • Extracts index definitions from:
    • SQLAlchemy
      • ORM schema (MetaData)
      • Actual DB (Inspector)
    • Django
      • ORM schema (Model._meta.indexes)
      • Actual DB (connection.introspection)
  • Compares (table, columns) tuples
  • Can be integrated into CI to enforce index coverage

Install

pip install query-patterns

Declare a pattern

from query_patterns import query_pattern


# Declare query pattern using table/column names
class RepoA:
    @query_pattern(table="users", columns=["email"])
    def find(self, email): ...


# Declare query pattern using ORM models(works with both SQLAlchemy and Django models)
from models import User

class RepoB:
    @query_pattern(table=User, columns=[User.email])
    def find(self, email): ...

a. SQLAlchemy Command

# Reads indexes from MetaData
query-patterns sqlalchemy \
  --metadata myapp.db.metadata \
  --module myapp.repo
  
# Auto-discover modules
query-patterns sqlalchemy \
  --metadata myapp.db.metadata
  
# Reads actual indexes from the database
query-patterns sqlalchemy \
  --source db \
  --engine-url postgresql://user:pass@localhost/mydb \
  --module myapp.repo

b. Django Command

# Reads Model._meta.indexes from installed apps
query-patterns django \
  --settings config.settings \
  --module myapp.repo
  
# Auto-discover modules 
query-patterns django \
  --settings config.settings
  
# Reads actual DB indexes using Django introspection
query-patterns django \
  --source db \
  --settings config.settings \
  --module myapp.repo

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

query_patterns-0.1.8.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

query_patterns-0.1.8-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file query_patterns-0.1.8.tar.gz.

File metadata

  • Download URL: query_patterns-0.1.8.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for query_patterns-0.1.8.tar.gz
Algorithm Hash digest
SHA256 a22ff3f4fe9607b86ea950c61d14a8030f75a94a82b15d8068927d84f5adf89e
MD5 5437220260f24be3d2ee36d0a344014e
BLAKE2b-256 8cad496e064a6ba0f952d8baf4aef03303b03a0e9eeb88494ff85469e1acdb45

See more details on using hashes here.

File details

Details for the file query_patterns-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: query_patterns-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for query_patterns-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 8f5004034fe92812b71dda8a7ce59dc18e4b626f9ead230fd3cdfce8329e3980
MD5 83e99c5b68775f6df0c4fc74267eefd6
BLAKE2b-256 ed7899e07be4906010bb7581e5de4ce5f072e1125ccb1ccaa9d40e6c2f91bde2

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