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.2.0.tar.gz (4.4 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.2.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sql_adapters-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b289afe08513fa5aceb94bbcf4c05f84588a40beffa86753757d700c7557ed3d
MD5 d51fabc4fbac134774f7dfc1dfb0a77d
BLAKE2b-256 eb49d54345c1a5f0fe463faf66f0ffa42a25a0a0490bbb34627936f18d9f5ec3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sql_adapters-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d3bab14b848241592b63c7c35496e430d2730978b25c69f1d4150852d2438f85
MD5 c5db8034f9d0bb8e8b09b4ba4642c8f8
BLAKE2b-256 a2844ed2f405f01211000f7c7c7a168f71b685cdc686a21a2d1c83823b1984f9

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