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


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_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 easy_orm import BaseMapper, Page, select_one, delete, datasource, db_session, EasyOrmConfig
from model.user import User

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): ...

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.14-cp313-cp313-win_amd64.whl (435.7 kB view details)

Uploaded CPython 3.13Windows x86-64

easyorm_py-1.0.14-cp313-cp313-manylinux2014_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.13

easyorm_py-1.0.14-cp312-cp312-win_amd64.whl (442.2 kB view details)

Uploaded CPython 3.12Windows x86-64

easyorm_py-1.0.14-cp312-cp312-manylinux2014_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.12

easyorm_py-1.0.14-cp311-cp311-win_amd64.whl (451.5 kB view details)

Uploaded CPython 3.11Windows x86-64

easyorm_py-1.0.14-cp311-cp311-manylinux2014_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.11

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.14-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b8832d6a2c887aa9f8d79c6e9368632cf5b97acbc2e38db33b71733f1debadee
MD5 3e7a492cc919f826f94123aebb19abf1
BLAKE2b-256 916c0b446f9c13ff263249a4155551660f39bf0c087dcf29d590b84eee30e69c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.14-cp313-cp313-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 78617286ba7dfc8fd573434e1ae539bb55a627a9418428941aeeb7f0bdb432e3
MD5 09c843470652efe52d4a2de43734fc33
BLAKE2b-256 0f2288233859771150959f7d8f99abd006f3016e426ba709029ff92632303181

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.14-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4068f31fceff09525277ce3df3769de14f61dc6c62df29cbd51e2ca28f293df1
MD5 03d9ed7a7988a57b0a8802debe27ce4a
BLAKE2b-256 9722954443a13b496cf32276712c325e164d1a8ff401b24402138aa20ea9709a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.14-cp312-cp312-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b95ab14866ddb2ce6b6536ff100bfa594d9deee625782bd3602ab3e6388b3294
MD5 489346c786afb4f0797c91bfd59b2171
BLAKE2b-256 b3e0e9d5c2f1dc4baa0d1ab3b1f51af8c712853de7d9e7083c2d8a7cc3681018

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.14-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d4c017b6b1078cc8f16a7fc47fad9875c44eee74b470359eae95f4470ef1c955
MD5 c2869fa7b419311782517919e2bb768f
BLAKE2b-256 49758ea88c8c0176a2010197b84342180b1a82fe7cfa10578c9a045ced465b14

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.14-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f704e6001da4e110e1aa5b4f83d749e02648ac023543caa5084373ec5a4b7996
MD5 a5414c1c08aa7f4d85c17c31f53be5e1
BLAKE2b-256 731512dc9082e6048da3824ac771fb86c7bf6831f77ff6a04cf4f57fdae694b5

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