- easy to connect database
- easy to do sql operations CRUD
- easy to support different mapper to python class
- easy to generate sql and create sql version
- easy to integrate with other libs
- support async operations
- support supabase
添加以下依赖到pyproject.toml文件
qpydao = { git = "https://github.com/fluent-qa/fluentqa-pdao.git", branch = "main" }
or use PDM:
pdm add "git+https://github.com/fluent-qa/qpydao.git@main"
- 可以使用pydantic model
- 也可以使用sqlachmey/sqlmodel
- 能够支持常用的DAO模式:
- 直接CRUD
- Repository 模式
- Model 支持直接操作数据库
- Async支持+Session封装
- 支持多数据库
- event listener on entity change
- code generation
- CRUD operation
- Module Registration and Auto Injection
- setting files:
settings.toml
file
[default]
key = "value"
databases = { default = { db_url = "postgresql+psycopg://postgres:changeit@127.0.0.1:5432/workspace" } }
pg_url = "postgresql+psycopg://postgres:changeit@127.0.0.1:5432/workspace"
pg_a_url = "postgresql+psycopg_async://postgres:changeit@127.0.0.1:5432/workspace"
db = databases.get_db("default")
def test_query_bind_params():
sql = f'select * from hero where name=:name'
raw_result = dao.plain_query(sql, name="test6")
result = SqlResultMapper.sql_result_to_model(raw_result, Hero)
print(result)
objects = dao.find_by(Hero, **{"name": "test6"})
print(objects)
def test_use_sqlmodel_statement():
s = select(Hero).where(Hero.name == "test6")
result = dao.query_for_model(s)
print(result)
def test_find_by():
result = dao.find_by(Hero, **{"name": "4321"})
print(result)
class HeroRepo(metaclass=RepositoryMeta, base_type=Hero):
@native_sql("select * from hero")
def find_hero(self):
pass
@native_sql("select * from hero where name=:name and age=:age")
def find_hero_by_name_and_age(self, name, age):
pass
@native_sql("select * from hero where name= :name")
def find_hero_by_name(self, name):
...
@native_sql("update hero set name= :new_name where name= :name", modify=True)
def update_name(self, name, new_name):
pass
def test_default_db():
db = databases.get_db("default")
print(db)
def test_repo():
repo = HeroRepo()
print(repo)
result = repo.find_hero()
print(result)
repo.find_hero_by_name(name="new_test")
repo.update_name(name="test", new_name="new_test")
integrate with dl-sql: