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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.14

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12

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

Uploaded CPython 3.11Windows x86-64

easyorm_py-1.0.23-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.23-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for easyorm_py-1.0.23-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 c2ae75093035e10c3a66837af2dca8bc259c7f5cb5b746701beb3b2ea7e27920
MD5 13062aada70ef0686b21d7127991af86
BLAKE2b-256 bdb6e16a6292ffd14f0e6fe133e3b246c021e55f78197c027bfa80c04a8f50ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.23-cp314-cp314-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 430c6a100c72fa5635fcf8bdce22f8065a846a2f86bf13ae6fb5d3068ffc18ad
MD5 44704a2db61fa6b1838f1a8da959e78d
BLAKE2b-256 dd4f62b547851af1da40187779e9f45c8bf827ffb92118f9654c72e6059ff1d9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.23-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ce731d14a41cee7135aa3477c06b2d675bcbb3508e6b36179e998d9117f36fa6
MD5 2da0c02024c59c4513163c64b58635f9
BLAKE2b-256 9e72d6e4d6b187b12c5091582663dd251495dead2abbb5b15e15d155c8293f61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.23-cp313-cp313-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f959c1ca602fcb40990c3c7945206a41e0a08072cf27559462a0f4f05c2ed329
MD5 b624ec4182f618cf39d9540e1ef15c3e
BLAKE2b-256 f1f93132c606dcf3a4075ae645c2256e041d90b97df3b6455e0fc59f443577df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.23-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 7a7eac2aaecbf24f1a0b5c20089b77a754dd75f50edbab80ff7616b021e796da
MD5 4eb1280a741dcc33538abbcb91572fd4
BLAKE2b-256 27dd29e8fcb5ee336b39f895b0ac4589fced2610cd47a15b758ee9d515a02d88

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.23-cp312-cp312-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2f286e3461455944d7bdf9aad77cf34bdd5104e5c8436aecbfe83c440948fb4d
MD5 02533accb646e7b0f506d781cd9829a4
BLAKE2b-256 da1a865063cf81d3612e37df7c8a8baf8a13bbba71846dfb67a0a02992a64171

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.23-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 11695a8af196435de006a3945bf7063c98298002a50b97e543a83df83b7147f2
MD5 cb12de70b60da54bc0a5eb223168ebf0
BLAKE2b-256 25300964f60e07d19199887cf2b69da5d0aab4407f165a2496f3d74ca5eb0291

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.23-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 01111b2651f0cebcd59f405057eb79c77a2309628f0db4bd33c471181442fe6a
MD5 79e75fd61b020d5dccf7f6b864062dba
BLAKE2b-256 9f9841448a4680a0d8472ede5b5accaf0ce172e6fe90440ed9bb7fe336d54bd9

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