Skip to main content

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
from contextlib import contextmanager
from sqlalchemy.sql.selectable import Select
from sqlalchemy.sql.dml import Delete, Update
from easy_orm import Page


class BaseMapper(Generic[T]):


    @classmethod
    @contextmanager
    def db_session(cls, datasource_name:str=None):
        """
        get ori db session
        :param datasource_name:
        :return:
        """

    @classmethod
    @contextmanager
    def switch_datasource(cls, datasource_name: str):
        """
        switch_datasource by datasource_name
        :param datasource_name:
        :return:
        """
        
        
    def add(self, data: Any):
        """
        add
        :param data:
        :return:
        """


    def add_all(self, data: list):
        """
        add_all
        :param data:
        :return:
        """
        
    def save_batch(self, data: list):
        """
        save_batch  orm2.0
        :param data:
        :return:
        """
        
    def insert(self, data: Any):
        """
        insert  orm2.0
        :param data:
        :return:
        """
    
    
    def delete_by_id(self, id: int):
        """
        delete by id
        :param id: int
        :return: rows
        """
    
    def delete(self, del_statement: Delete):
        """
        delete orm 2.0 statement
        :param del_statement: 
        :return: rows
        """
    
    def update_by_id(self, data: dict, selective: bool = False):
        """
        update by id
        :param data: dict
        :param selective: bool
        :return: rows
        """
    
    def update(self, update: Update):
        """
        update orm 2.0 statement
        :param update: 
        :return: rows
        """
    
    def bulk_update_mappings(self, table_model=None, data_list: list[dict]|None=None):
        """
        update orm2.0 or origin_sql statement
        :param table_model:
        :param data_list:
        :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:
        """



        
    
from sqlalchemy import select, update, delete, text
from easy_orm import BaseMapper, Page, select_one, delete, datasource, db_session, EasyOrmConfig
from model.user import User, UserModel

class TestMapper(BaseMapper[UserModel]):

    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[UserModel]: ...

    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_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_sql(sql_str, {"id": 1})
    
    def delete_test(self):
        stat = delete(UserModel).where(UserModel.id == 1)
        return self.delete(stat)
    
    def update_test(self):
        stat = update(UserModel).where(UserModel.id == 1).values(name="test")
        return self.update(stat)
    

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

easyorm_py-1.0.25-cp314-cp314-win_amd64.whl (416.0 kB view details)

Uploaded CPython 3.14Windows x86-64

easyorm_py-1.0.25-cp314-cp314-manylinux2014_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.14

easyorm_py-1.0.25-cp313-cp313-win_amd64.whl (405.0 kB view details)

Uploaded CPython 3.13Windows x86-64

easyorm_py-1.0.25-cp313-cp313-manylinux2014_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.13

easyorm_py-1.0.25-cp312-cp312-win_amd64.whl (410.5 kB view details)

Uploaded CPython 3.12Windows x86-64

easyorm_py-1.0.25-cp312-cp312-manylinux2014_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.12

easyorm_py-1.0.25-cp311-cp311-win_amd64.whl (417.0 kB view details)

Uploaded CPython 3.11Windows x86-64

easyorm_py-1.0.25-cp311-cp311-manylinux2014_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.11

File details

Details for the file easyorm_py-1.0.25-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for easyorm_py-1.0.25-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 344e76df6a2cfaf516cfd72237aed00d3dbc265d91f49c2d0f0d65d7e3311c9e
MD5 478354d849d73b585542c73a5b074244
BLAKE2b-256 82beec7e64859cb1c0d11d7938952300cd92daad3ec4b5d8eadc5fffd8d961a0

See more details on using hashes here.

File details

Details for the file easyorm_py-1.0.25-cp314-cp314-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for easyorm_py-1.0.25-cp314-cp314-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dc1c4a4dc29110162eda44826fc6e8b91249219636898e1337e2b3a249150522
MD5 540e787c8fad8d676ba65378793c40ca
BLAKE2b-256 678312f5e93ef6acb7bdd70501be76c524c1f185bba83c08044aaaf25f9825d7

See more details on using hashes here.

File details

Details for the file easyorm_py-1.0.25-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for easyorm_py-1.0.25-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 81c9b4ef46c030e5e27007a8ec1223aefaee895bd38a8354af9ea60373a71b7e
MD5 27db7a282f6da4abf05f30c31d6369e0
BLAKE2b-256 8abb32266f5d268619326f6d4d7fdafb7f4a45e733c229f3c807fe06786e9e08

See more details on using hashes here.

File details

Details for the file easyorm_py-1.0.25-cp313-cp313-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for easyorm_py-1.0.25-cp313-cp313-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 68fdc411c9786ab0b46e6f35d5517c8a26ea854d3cdd72a0c40da55f07c709dd
MD5 af0444edd04572c18dd44f934aa4334b
BLAKE2b-256 e0af93377b5b142f3efda9596798043714e2ad5fe5994f97bad8b74fab68a729

See more details on using hashes here.

File details

Details for the file easyorm_py-1.0.25-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for easyorm_py-1.0.25-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 b5d4590322a8e3fcdec03f55a8a180834403760f3c16d3529269f8636fa74d66
MD5 1c59b5d6877c2a8db07035aac6be3a5c
BLAKE2b-256 83285f4acd001bc4e73a6d269e26e21522b9e26a2d32acae62c0d90a8de9a561

See more details on using hashes here.

File details

Details for the file easyorm_py-1.0.25-cp312-cp312-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for easyorm_py-1.0.25-cp312-cp312-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 68ad2ee8bb14f26b35f3e522e4a7a33c76037e62d6027a9cebdb98bc81209610
MD5 d70993f8de6ff6922e17d478a5868fd6
BLAKE2b-256 b5f20532f28375cc6101a67cf96ef157eae65c205741b2038aa952faec294363

See more details on using hashes here.

File details

Details for the file easyorm_py-1.0.25-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for easyorm_py-1.0.25-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 236c3a60aaf52b3095b04fdea7203b4153db40249f287b30d091371f2fe2aaef
MD5 b33725f513afe0fe7eadc8e33aeb3cbe
BLAKE2b-256 0368fe180adf03cda8a1304f7d0b2f1a8ae8e0a81e7f158dacb81caf4219c62f

See more details on using hashes here.

File details

Details for the file easyorm_py-1.0.25-cp311-cp311-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for easyorm_py-1.0.25-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a73c01c099bf795419cc55d997ef7f7f7420576caef7bdbc97c47bda773f77c6
MD5 e94508d639023fc95df30cce639b937a
BLAKE2b-256 e51930d160534a420b6f515a029b480a6c2f129ca9fee5ae0eb7388688df926f

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