YeSQL is a SQL-first data manipulation library that will replace your ORM.
OTHER License
Say yes to SQL with yesql.
yesql eliminates boilerplate without the baggage of an expensive or clunky ORM. Simply write your SQL, point yesql to the directory, and it does all the rest.
pip install -U --pre yesql
or
poetry add --allow-prereleases yesql
yesql currently supports the following database drivers:
You can select your driver as an extra when installing yesql (recommended):
pip install -U --pre "yesql[psycopg]"
or
poetry add --allow-prereleases yesql -E asyncpg
from __future__ import annotations
import dataclasses
import datetime
import pathlib
import yesql
QUERIES = pathlib.Path(__file__).resolve().parent / "queries"
@dataclasses.dataclass(slots=True, kw_only=True)
class Post:
id: int | None = None
slug: str | None = None
title: str | None = None
subtitle: str | None = None
tagline: str | None = None
body: str | None = None
tags: set[str] = dataclasses.field(default_factory=set)
publication_date: datetime.date | None = None
created_at: datetime.datetime | None = None
updated_at: datetime.datetime | None = None
class PostsRepository(yesql.SyncQueryRepository[Post]):
"""An asyncio-native service for querying blog posts."""
class metadata(yesql.QueryMetadata):
__querylib__ = QUERIES
__tablename__ = "posts"
__exclude_fields__ = frozenset(("slug",))
posts = PostsRepository()
posts.initialize()
new_post = Post(
title="My Great Blog Post",
subtitle="It's super great. Trust me...",
tagline="You'll be glad you read it.",
tags={"tips", "tricks", "cool stuff"},
)
saved_post = posts.create(instance=new_post)
yesql ships with simple CLI for generating type-stubs. This allows for more exact static type-analysis and enables auto-complete for your IDE.
Usage:
yesql stubgen
You can optionally supply any number of paths to directories or python modules. The command will default to the current working directory on the filesystem.
If you don't have black installed in your development environment, you should add
the cli
extra as a development dependency.
ORMs are bad for you. They are a leaky abstraction that cannot solve the problem they set out to do - which is to abstract out the details of working with a database.
ORMs are slow. ORMs depend upon a high level of abstraction in order to work across database clients. They also attempt to bridge the gap of data validation and state management. By attempting to hide the details of managing state from the end user, they suffer from large computational costs and predict
ORMs are wasteful. As mentioned above, ORMs use a huge amount of resources to auto-magically determine state for anything and everything currently pulled into your application's memory. Because this is implicit, this takes a large amount of work to do right. In general, your application is already aware of "clean" and "dirty" states. Your application should be in charge of managing it.
ORMs encourage bad data modeling. ORMs vastly simplify recursive data modeling (see: N+1 problem) which encourages lazy solutions to simple problems and can result in extreme service degradation without reasonable means of mitigation.
yesql takes a SQL-first approach to data management:
Focus on your SQL and your database.
Explicit state management.
Plain Ol' Data Objects.