Skip to main content

sqlorm

SQL-focused Python ORM

Pypi GitHub Actions Workflow Status Codecov

ORMs and developers often end up in a love/hate relationship. The advantages of ORMs (the mapping of objects) can be negated by their compromises (have to learn their pseudo sql syntax, less control over the generated sql, low performance).

Sqlorm intends to provide a solution where SQL stays front and center and where the behavior of the ORM is what you expect and no more. SQL is seamlessly integrated through functions and model methods using Python doc strings.

  • Thin layer on top of DBAPI 2.0
  • Use SQL as usual
  • Utilities to help you build composable SQL queries
  • ActiveRecord-style ORM that is mostly direct row to object
  • Supports optional model definition via annotations
  • Predictable SQL generation from models
  • Supports relationships
  • Built-in SQL based migration system
  • Low coupling between different parts of the library which can be used directly with DBAPI Connections
  • Easy to understand code
  • VS Code syntax highlighting using extension

Read the documentation

See Flask-SQLORM for a Flask integration.

Installation

pip install sqlorm-py

Example

from sqlorm import Engine, Model, PrimaryKey, sqlfunc, create_table
import datetime

class Task(Model):
    table = "tasks"

    id: PrimaryKey[int]
    title: str
    done: bool
    done_at: datetime.date

    @staticmethod
    def find_todos():
        "SELECT WHERE not done"

    def toggle(self):
        "UPDATE SET done = not done WHERE SELF RETURNING *"

@sqlfunc
def tasks_completion_report(start_date, end_date):
    """SELECT done_at, COUNT(*) count
       FROM tasks
       WHERE done_at >= %(start_date)s AND done_at <= %(end_date)s
       GROUP BY done_at"""

engine = Engine.from_uri("sqlite://:memory:")
with engine:
    create_table(Task)

    task = Task(title="task1")
    task.done = False
    task.save()

    task = Task.create(title="task2", done=False)

    todos = Task.find_todos()

    task = Task.get(1)
    task.toggle()

    task = Task.get(2)
    task.title = "renamed task2"
    task.save()

    report = tasks_completion_report(datetime.date(2024, 1, 1), datetime.date.today())

Metadata

Release files for sqlorm-py 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlorm-py 0.4.2
File Size Uploaded
sqlorm_py-0.4.2.tar.gz 30.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlorm-py 0.4.2
File Interpreter ABI Platform
sqlorm_py-0.4.2-py3-none-any.whl Python 3 none any Details

Total release size: 68.7 kB

Release files / sqlorm_py-0.4.2.tar.gz

Download URL sqlorm_py-0.4.2.tar.gz
Size 30.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5dc8d2051d6b576d8aceaeac3078e2c8b00fcdd6e2ae1b46662df82dc1b5e0b0
BLAKE2b-256 checksum
How to use checksums
6e5a50900cc2faf956bef6b0a4dbecc43839fcdc85bb048620210594ec2986cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / sqlorm_py-0.4.2-py3-none-any.whl

Download URL sqlorm_py-0.4.2-py3-none-any.whl
Size 37.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
03b17bbcbd0345f81f9f4fe071062e1574ed47d268615c5f32414754197d318c
BLAKE2b-256 checksum
How to use checksums
5abad41dfd9c1c20a12a30777561aa4c1d265b578a76c39faf94a291673ff00f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.4.2 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page