Skip to main content

A new kind or ORM that do not abstract away your database or SQL queries.

Project description

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())

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

sqlorm_py-0.4.0.tar.gz (30.3 kB view details)

Uploaded Source

Built Distribution

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

sqlorm_py-0.4.0-py3-none-any.whl (37.2 kB view details)

Uploaded Python 3

File details

Details for the file sqlorm_py-0.4.0.tar.gz.

File metadata

  • Download URL: sqlorm_py-0.4.0.tar.gz
  • Upload date:
  • Size: 30.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.17

File hashes

Hashes for sqlorm_py-0.4.0.tar.gz
Algorithm Hash digest
SHA256 544da5667fec2bd4af4a6355428970b5d3a5aceddc3175b458365cd4edc49981
MD5 582d1e4e3671d6bbcccb1959fd05199d
BLAKE2b-256 c53a2609f5b354960c69122e6c13a997745aeef2eaa127da9c84b6dd63b4f6a6

See more details on using hashes here.

File details

Details for the file sqlorm_py-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: sqlorm_py-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 37.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.17

File hashes

Hashes for sqlorm_py-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1cafda5cb973af69b11f4026dbb16a6a549f205bac9a1b259109d11f6d23395e
MD5 01432397247d2339145fb609dfc5e917
BLAKE2b-256 2774944f1db49530b20c416a44e5f9d7bd5e0fef369465a52c9f406b30792b80

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