Skip to main content

Entari plugin for SQLAlchemy ORM

Project description

entari-plugin-database

Entari plugin for SQLAlchemy ORM

功能

  • 提供数据库服务支持,管理多个数据库连接
  • 依赖注入会话与 ORM 模型数据
  • 依据类型注解或 SQLDepends 的复杂依赖关系注入
    • 例如 Sequence[Model] 会注入所有模型
    • 声明 SQLDepends(select(Model).where(Model.field == value)) 会注入查询结果
    • 通过配合 arclet.entari.param, 可以在 SQLDepends 的查询语句中使用 param 声明其他的依赖注入参数
  • 自动数据库迁移
    • 通过 alembic 实现
    • 检测到模型变更时,自动生成并应用迁移脚本
    • 根据模型的 __bind_key__ 属性,支持多数据库迁移
    • 允许开发者自定义迁移脚本

多数据库

  • 插件配置项 binds 可以根据 bind_key 声明多个数据库连接
  • ORM 模型可以通过 __bind_key__ 属性指定使用哪个数据库连接
  • 默认数据库连接使用 "" 作为 bind_key

迁移

  • ORM 模型的变更会被自动检测并生成迁移脚本
  • 本插件会根据模型的结构自动计算 revision
  • ORM 模型可以声明 __revision__ 属性来指定特定的 revision
  • 迁移记录将存储在用户目录下的 .entari/data/database/migrations_lock.json 文件中

配置

  • type:数据库类型,默认为 sqlite
  • name:数据库名称或文件路径,默认为 data.db
  • driver:数据库驱动,默认为 aiosqlite;其他类型的数据库驱动参考 SQLAlchemy 文档。
  • host:数据库主机地址。如果是 SQLite 数据库,此项可不填。
  • port:数据库端口号。如果是 SQLite 数据库,此项可不填。
  • username:数据库用户名。如果是 SQLite 数据库,此项可不填。
  • password:数据库密码。如果是 SQLite 数据库,此项可不填。
  • query:数据库连接参数,默认为空字典。可以传入如 {"timeout": "30"} 的参数。
  • session_options:数据库会话选项,默认为 None。可以传入如 {"expire_on_commit": False} 的字典。
  • binds:数据库绑定配置,默认为 None。可以传入如 {"bind1": UrlInfo(...), "bind2": UrlInfo(...)} 的字典。
    plugins:
      database:
        type: sqlite
        binds:
          foo:
            type: mysql
            driver: asyncmy
            host: localhost
            port: 3306
            name: foo_db
            username: root
            password: password
    
  • options:数据库连接选项,默认为 {"echo": None, "pool_pre_ping": True}
  • create_table_at:在指定阶段创建数据库表,默认为 'preparing'。可选值为 'preparing', 'prepared', 'blocking'。

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

entari_plugin_database-0.2.0.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

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

entari_plugin_database-0.2.0-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: entari_plugin_database-0.2.0.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.25.6 CPython/3.11.7 Windows/10

File hashes

Hashes for entari_plugin_database-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1dd5f5f029331561f9e85f23de06847eb69ceda2b1cd01e29e58328d829e333c
MD5 7182720712bece49828d72774a5101c1
BLAKE2b-256 82e83ca3bfa348747862a7a57c93b2be4ecfaf8512f6f9c13f38d8ec38dd71a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for entari_plugin_database-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d2e5c53c7cb2ead06ac4becc69b49c10348bb3064f31c08db9ae6266fc81741b
MD5 149bad6dc5eb45974a9462e6f8c03576
BLAKE2b-256 0e34c6c4a830b57e7891824007e6e9532b389fc86d9ae86264c490e8978c0993

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