Skip to main content

SQL adapters for various databases - SQLAlchemy simplifed

Project description

sql-adapters

A Python library that wraps SQLAlchemy core to deliver an opinionated style of development wherein databases are implemented as adapters which are interfaces to the data source.

import sql_adapters.sqlite as sql
from sql_adapters import (
    Column,
    declarative_base,
    delete,
    insert,
    select,
    text,
    update,
)

base = declarative_base()


class _TestTable(base):
    __tablename__ = "test_table"

    id = Column(sql.INTEGER, primary_key=True, autoincrement=True)
    name = Column(sql.TEXT, nullable=False)
    date_created = Column(
        sql.TZDateTime,
        nullable=False,
        default=lambda: datetime.now().astimezone(),
    )


class returns:
    class select_result(NamedTuple):
        id: int
        name: str
        date_created: datetime


class DemoAdapter(sql.SqliteAdapter):
    def __init__(
        self,
        read_only: bool = False,
    ):
        super().__init__("test", mode="ro" if read_only else "rw")

    @staticmethod
    def init():
        adapter = DemoAdapter(read_only=False)
        with adapter:
            base.metadata.create_all(adapter.connection)

    def add_item(self, name: str):
        self.execute(insert(_TestTable).values(name=name))

    def get_item(self, item_id: int) -> returns.select_result:
        stmt = select(_TestTable).where(_TestTable.id == item_id)
        result = self.execute(stmt)
        data = self.read_values(result, returns.select_result)
        return next(data)

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

sql_adapters-0.3.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

sql_adapters-0.3.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file sql_adapters-0.3.0.tar.gz.

File metadata

  • Download URL: sql_adapters-0.3.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.22

File hashes

Hashes for sql_adapters-0.3.0.tar.gz
Algorithm Hash digest
SHA256 1aaf25e43cfc6abb673570b41e9007e5432c9f92bc64f46a36a0dcdb9edec595
MD5 bc7d2a6b5f4bf77e02dfcc55320867a1
BLAKE2b-256 46e710a0f7528bf4c66780f9611eab5f8202b9a468593a55540cb94f5cddd1f8

See more details on using hashes here.

File details

Details for the file sql_adapters-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sql_adapters-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2bc8596a10119c90c1ebf189b0bb3c8ace416c69b23bb7795fbe73690b8ef129
MD5 8e387de3e857697c3a5b06bfb133108b
BLAKE2b-256 db00f874d687aa19857fde32db7f49f5c09f41b8378378f2cefb5a31323e2836

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