跳转到主要内容

用于与SQLAlchemy一起工作的意见性实用工具

项目描述

sqla-utils

用于与SQLAlchemy一起工作的意见性实用工具

MIT License PyPI - Python Version GitHub pypi GitHub Workflow Status

内容

事务包装器

FIXME

DBObjectBase

DBObjectBase 是映射类的基类。

示例

from datetime import datetime
from sqlalchemy import Column, DateTime, Integer, String
from sqla_utils import DBObjectBase, Transaction

class DBAppointment(DBObjectBase):
    __tablename__ = "appointments"

    id = Column(Integer, primary_key=True)
    date = Column(DateTime, nullable=False)
    description = Column(String(1000), nullable=False, default="")

预约项可以按如下方式查询

from sqla_utils import begin_transaction

with begin_transaction() as t:
    app123 = DBAppointment.fetch_by_id(t, 123)
    great_apps = DBAppointment.fetch_all(t, DBAppointment.description.like("%great%"))

建议添加自定义查询、创建和更新方法

class DBAppointment(DBObjectBase):
    ...

    @classmethod
    def create(cls, t: Transaction, date: datetime, description: str) -> DBAppointment:
        o = cls()
        o.date = date
        o.description = description
        t.add(o)
        return o

    @classmethod
    def fetch_all_after(cls, t: Transaction, date: datetime) -> List[DBAppointment]:
        return cls.fetch_all(t, cls.start >= dates.start)

    def update_description(self, t: Transaction, new_description: str) -> None:
        self.description = new_description
        t.changed(self)

数据库构建器

FIXME

pytest 实用工具

sqla_utils.test 模块包含了一些用于与pytest和SQLAlchemy一起工作的实用工具。

FIXME

项目详情


下载文件

下载适合您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

sqla_utils-0.6.1.tar.gz (12.5 kB 查看散列值)

上传时间 源代码

构建发行版

sqla_utils-0.6.1-py3-none-any.whl (14.6 kB 查看散列值)

上传时间 Python 3

支持