easy_orm
Project description
example
1. Load db config
db_test = {
"primary": "default",
"raw_sql": True,
"mapper_xml_path": "test_xml",
"default": {
"db_type": "mysql",
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "123456",
"database": "template",
},
"test": {
"db_type": "mysql",
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "123456",
"database": "template",
}
}
from easy_orm import EasyOrmConfig, DBConfig, DataSource
db_config = DBConfig(**db_test)
EasyOrmConfig(db_config)
# add datasource
ds = {
"test_ds": DataSource(
db_type="mysql",
host="127.0.0.1",
port=3306,
user="root",
password="123456",
database="test_ds"
)
}
EasyOrmConfig.add_datasource_config(ds)
# remove
EasyOrmConfig.remove_datasource("test_ds")
2. edit xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "">
<mapper namespace="TestMapper">
<select id="find_data_by_ids">
select id, username from test where id in
<foreach collection="id_list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
<select id="test_page">
select id, username from test
</select>
<select id="test_where" resultType="model.user.User">
select id, username from test
<where>
<if test="params.get('id') is not None">
id = #{id}
</if>
</where>
</select>
<insert id="add_data">
insert into test (nickname, username) values ('xxx', 'xxx1')
</insert>
<update id="update_data_by_id">
update test set nickname = #{nickname} where id = #{id}
</update>
<delete id="delete_data_by_id">
delete from test where id = #{id}
</delete>
</mapper>
3. Extend BaseMapper and Use
# TestMapper.py
class BaseMapper(Generic[T]):
@classmethod
def session(cls):
"""
get db session
"""
def add(self, data: Any):
"""
add
:param data:
:return:
"""
def add_all(self, data: list):
"""
add_all
:param data:
:return:
"""
def select_page_by_orm(self, statement: Select, page: Page=Page(1, 10)):
"""
:param statement: Select statement
:param page: Page Page(1, 10)
:return: {"list": [], "total": 0}
"""
def select_page_by_ori_sql(self, sql_str: str, sql_param: dict | None=None, page: Page=Page(1, 10)):
"""
:param sql_str: str ori_sql
:param sql_param: dict {}
:param page: Page Page(1, 10)
:return: {"list": [], "total": 0}
"""
def select_all_by_ori_sql(self, sql_str: str, sql_param: dict | None=None):
"""
:param sql_str: str ori_sql
:param sql_param: dict {}
:return: []
"""
def select_all(self, statement=None):
"""
select all
:param statement:
:return:
"""
def select_one(self, statement=None):
"""
select one
:param statement:
:return:
"""
def select_by_id(self, id: int):
"""
select by id
:param id:
:return:
"""
def update_by_id(self, data: dict, selective: bool=False):
"""
upate by id
:param data:
:param selective:
:return:
"""
def delete_by_id(self, id: int):
"""
delete by id
:param id:
:return:
"""
...
from sqlalchemy import select, text
from easy_orm import BaseMapper, Page, select_one, delete, datasource, db_session, EasyOrmConfig
from model.user import User, UserModel
class TestMapper(BaseMapper[User]):
def find_data_by_ids(self, id_list: list): ...
@select_one('select * from test where id = #{id}')
def find_data_one(self, id: int): ...
def test_page(self, page: Page): ...
def test_where(self, id: int) -> list[User]: ...
def add_data(self): ...
def update_data_by_id(self, nickname: str, id: int): ...
@delete('delete from test where id = #{id}')
def delete_data_by_id2(self, id: int): ...
def select_page_orm_test(self):
page = Page(1, 10)
stat = select(UserModel).where(UserModel.id > 1)
return self.select_page_by_orm(stat, page)
def select_page_ori_sql_test(self):
page = Page(1, 10)
sql_str = "select * from user where id > :id"
return self.select_page_by_ori_sql(sql_str, {"id": 1}, page)
def select_all_ori_sql_test(self):
sql_str = "select * from user where id > :id"
return self.select_all_by_ori_sql(sql_str, {"id": 1})
class TestService:
@datasource("default", True)
def test_find_user_one(self):
TestMapper().find_data_one(1)
self.test_find_user_one2()
@datasource("test", True)
def test_find_user_one2(self):
TestMapper().find_data_one(1)
def test(self):
id = TestMapper().insert({"field": "test"})
TestMapper().select_by_id(1)
TestMapper().update_by_id({'id': 1, "field": "test2"})
TestMapper().delete_by_id(1)
def test_ori_session(self):
with db_session() as session:
d = session.execute(text("select * from user")).first()
print(d._asdict())
def test_add_source(self):
with TestMapper.switch_datasource("test_ds"):
TestMapper().select_by_id(1)
if __name__ == '__main__':
EasyOrmConfig.add_datasource_config(ds)
# remove
EasyOrmConfig.remove_datasource("test_ds")
test_service = TestService()
test_service.test_find_user_one()
test_mapper = TestMapper()
test_mapper.find_data_by_ids([1, 2, 3])
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file easyorm_py-1.0.15-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: easyorm_py-1.0.15-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 442.4 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
165da956d0aedbf32a906b7fa7d66554cf8db437cc44104391ea4e951bb0de71
|
|
| MD5 |
0fed90cb948653b8876d209ab14c6ba0
|
|
| BLAKE2b-256 |
86552a9d878c17690d5dafa5a9cfae2b6882f0bcbcffa711184e1312d76912a6
|
File details
Details for the file easyorm_py-1.0.15-cp313-cp313-manylinux2014_x86_64.whl.
File metadata
- Download URL: easyorm_py-1.0.15-cp313-cp313-manylinux2014_x86_64.whl
- Upload date:
- Size: 3.1 MB
- Tags: CPython 3.13
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
535c8440826b4f331461e43d54c3d027dd0ff05e91d03101beb52d967e37603a
|
|
| MD5 |
3fc58fa9a830c9a85d6eafa1a98a4831
|
|
| BLAKE2b-256 |
9092999581c8f39e5ab4201a096c8c9dd137d315df8f949628560c421867f2d0
|
File details
Details for the file easyorm_py-1.0.15-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: easyorm_py-1.0.15-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 448.8 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4de22e0251a82b46062f75b296a3fd252604d3dc6aa0e87b9ebabdcf0eeaaf85
|
|
| MD5 |
9b3d875bd0e9f61edb60231eeb778be3
|
|
| BLAKE2b-256 |
462efb07c64cf5bc8e487dc2413f2d78ec90710e972dd3eb11d8609712c69c3b
|
File details
Details for the file easyorm_py-1.0.15-cp312-cp312-manylinux2014_x86_64.whl.
File metadata
- Download URL: easyorm_py-1.0.15-cp312-cp312-manylinux2014_x86_64.whl
- Upload date:
- Size: 3.1 MB
- Tags: CPython 3.12
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e170e11ba05a14fdfeb59d7d12ec5798ff940d0ab815c3117dc0bd7a6bca3890
|
|
| MD5 |
4e435859a9e856d9d07cc0baa287d1e7
|
|
| BLAKE2b-256 |
6384d10c1751e18b16129ff57906a608f8b8ed2b2076fca21207a727e0d3ff0e
|
File details
Details for the file easyorm_py-1.0.15-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: easyorm_py-1.0.15-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 458.6 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4f02cd813d97123e5c04461ea86ce90d595d8e4389690c91ddb4412e2d298f0
|
|
| MD5 |
02bcdf9553a5bb1101cf404de76f8657
|
|
| BLAKE2b-256 |
7248ef792151205ff3a0ae245a153db50fb485df914961688c864d6092704d9d
|
File details
Details for the file easyorm_py-1.0.15-cp311-cp311-manylinux2014_x86_64.whl.
File metadata
- Download URL: easyorm_py-1.0.15-cp311-cp311-manylinux2014_x86_64.whl
- Upload date:
- Size: 3.1 MB
- Tags: CPython 3.11
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b18720d5b21140d66bdfcf4c6bddab076c0961ed75b4d9f216dce67ad0649342
|
|
| MD5 |
c3eedfe4679f315351876048b6a0ca90
|
|
| BLAKE2b-256 |
eda49993c9b83969a15609effa297e0a088f2c105b7ac0c8234264fd7785c3d0
|