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.22-cp314-cp314-win_amd64.whl (416.2 kB view details)

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.14

easyorm_py-1.0.22-cp313-cp313-win_amd64.whl (405.5 kB view details)

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13

easyorm_py-1.0.22-cp312-cp312-win_amd64.whl (411.0 kB view details)

Uploaded CPython 3.12Windows x86-64

easyorm_py-1.0.22-cp312-cp312-manylinux2014_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.12

easyorm_py-1.0.22-cp311-cp311-win_amd64.whl (417.4 kB view details)

Uploaded CPython 3.11Windows x86-64

easyorm_py-1.0.22-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.22-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for easyorm_py-1.0.22-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 5ad7cab57195af7496964c41a6b5c636128746e6dbe360daefc7808d80bfbab5
MD5 430d5a673b8aba4a7667747d28496d72
BLAKE2b-256 fef1535c3c376661d3020fa399f3c40a7220577a32b4107bd28383cff55a2032

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.22-cp314-cp314-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 81b16d7f0ca5e7c5ad1d8b375a933ebfc27e8728f3092afa268fad343225f835
MD5 62468f6d9515dbc57428fe087fcf4640
BLAKE2b-256 9fc60174bd23b1dda962a92d0b0c6f48ae52c3cd0cb323ba2b86123dbd086a0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.22-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1cf58da626564b27abf1bb924a923513d10b3ad90a8bb75bf709cf635179d1b3
MD5 4ace3a9e27064d9b73c8aa435302c914
BLAKE2b-256 ff9a9407099aec44d175a2eb47299364bf6b3e40befc10adb33c97aae234f3bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.22-cp313-cp313-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 17bd2f1201f712039dc9dc847f354490f0da996e83202fb0df415fa7f094d508
MD5 0bbf9f95896b8ba2065a8422a8d7f796
BLAKE2b-256 3a7b0f69dc237f681610b4ccd222b683e17235460e4a8fd21d1d4db2182b7856

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.22-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 784a4ff9f1dda126a397ac69594866084718aa5d1c258918df04d383534e7e0c
MD5 0790c85a3ecfc4e4d852c78eeddc7e02
BLAKE2b-256 749068c034f42095746591ecc15c491a813fd3bbe0f541516f400bccfc8fd6d9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.22-cp312-cp312-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d592220adc785e52075dab424909172761039797c1ded61e24c2485107bf882f
MD5 666aadcfe11a33b14bbbe8a8dc64ecbb
BLAKE2b-256 49f13a44ab7eecf75c98efb3f67039797791751b286d6acbf03977a40672904d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.22-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 5ead94809258d14ac456d6f98a6a5026263c19d811e8dc8660f31c5d8135b433
MD5 adb1dd5b9a740ad6549e5db427e82250
BLAKE2b-256 9cb96abbd7c861017ffe2f1a9862f42a50f2fda32f530bf836de4695aa0df966

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.22-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 df413aa29ae17a151829c6244d01d96d97458c81fc8355468875bd0230cbb723
MD5 d3f87ecd319c500344f4cd1009dfbe4f
BLAKE2b-256 61ffdd93d48a1df0329bc349a7544e805e4a64a150934151e338ad83ffb025d0

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