Skip to main content

A lightweight CRUD toolkit built on SQLAlchemy 2.x.

Project description

alchetools

🚀 一个基于 SQLAlchemy 的轻量级 CRUD 工具库,封装了常用的增删改查、分页、过滤、批量操作等功能,帮助你更快速地构建数据库访问层。


✨ 功能特性

  • ✅ 通用 save_or_update(新增或更新记录)
  • ✅ 支持批量插入/更新
  • ✅ 分页查询与总数统计
  • ✅ 自动格式化时间字段为字符串
  • ✅ 支持动态条件过滤(精确 / 模糊)
  • ✅ 支持动态排序(asc / desc)

📦 安装依赖

pip install sqlalchemy

📁 使用说明

1. 新增或更新一条记录

from alchetools import DbTools

model_instance = DbTools.save_or_update(
    session=session,
    request_body={"id": 1, "name": "Tom"},
    model=UserModel  # 替换为你的 SQLAlchemy 模型类
)

2. 批量插入或更新记录

from alchetools import DbTools

data = [{"id": 1, "name": "Tom"}, {"id": 2, "name": "Jerry"}]

instances = DbTools.bulk_insert(session, data, UserModel)

3. 分页查询数据

from alchetools import DbTools

query = session.query(UserModel)
result = DbTools.find_list_page(query, page_size=10, page_index=1)

4. 查询全部数据(带时间格式化)

from alchetools import DbTools

records = DbTools.query_all(session.query(UserModel))

5. 应用条件过滤

from alchetools import DbTools

filters = {
    "name": {"value": "Tom", "is_fuzzy": True},
    "age": {"value": 18}
}

filtered_query = DbTools.apply_filters(session.query(UserModel), UserModel, json.dumps(filters))

6. 获取排序后的查询对象

sorted_query = get_sorted_query(session, UserModel, sort_by="created_at", order="desc")

🛠 公共方法列表

方法名 说明
save_or_update 新增或根据主键更新记录
bulk_insert 批量新增或更新记录
query_all 查询所有记录并格式化时间
find_list_page 分页查询
pagination_function 分页查询(另一个别名)
apply_filters 动态构建 SQL 查询条件
get_sorted_query 获取按字段排序的查询对象
format_model_data 将 SQLAlchemy 模型转为字典
convert_timestamps_in_dict 格式化时间戳/时间字段为字符串

🧪 日志支持

setup_logger() 使用方法如下 :

# 示例:utils/logger.py
from alchetools.uilts import setup_logger
setup_logger().info("正常日志")
setup_logger().error("错误日志")
setup_logger().warning("警告日志")

🔐 兼容性说明

  • ✅ 支持 SQLAlchemy 2.0.29
  • ✅ Python 3.8 及以上版本

📄 License

MIT License. 自由用于个人或商业项目。


🤝 欢迎贡献

欢迎提交 issue 或 PR,一起让 alchetools 更好用!

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

alchetools-0.1.3.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

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

alchetools-0.1.3-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file alchetools-0.1.3.tar.gz.

File metadata

  • Download URL: alchetools-0.1.3.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for alchetools-0.1.3.tar.gz
Algorithm Hash digest
SHA256 9670ae75c118d6037a310260e01bb00ad9a685d657f3be88ccb3be592fb63d6d
MD5 1f4d2f9e3b14218c103e75921ea040bc
BLAKE2b-256 aebe5fd601412492675c2590338888e01ffcb1985c0d69c24128a251624fd59e

See more details on using hashes here.

File details

Details for the file alchetools-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: alchetools-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for alchetools-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f4ed9f632c839079920a095f45556016789c6a48da9e5ad244c1a07f0f5b58fd
MD5 def644308043fbe1252c8926a5510408
BLAKE2b-256 24ba0c5ed92a3b230c7ccf623abd6c8a60fc567a24b0548c58c5a1dca6261e6d

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