Skip to main content

easy_orm

Project description

example

1. Load db config

db_test = {
    "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
db_config = DBConfig(db_test)
EasyOrmConfig(db_config)

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

    
if __name__ == '__main__':
    
    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.10-cp313-cp313-manylinux2014_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.13

easyorm_py-1.0.10-cp312-cp312-manylinux2014_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.12

easyorm_py-1.0.10-cp311-cp311-manylinux2014_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.11

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.10-cp313-cp313-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 58c80fd45e63c0b4c6983a5266cca3f9cb4d68196e5a7287205a1b05ebe3aa9d
MD5 38ffa68f83e5f31f1112799321f6f50c
BLAKE2b-256 e3397bae5be449d2dbebac6ee2dcec45a49f9609739c94c9fe2d4aaa38110d43

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.10-cp312-cp312-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 146b087e32ca4136f45a7feca3c18d95b304cd457ed6f75494e14c5096e5211d
MD5 1bbabb15bbcaf4eaf963eca904804cf8
BLAKE2b-256 af0236a4aa8d5ecc3c7118123c9d9d27719e75138070c109bca65eb6eb297d16

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easyorm_py-1.0.10-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 58fadbfd654ad3a8ab17a1bafa86ee332315eb6700af76558d9a78d40a50106e
MD5 cafa72cb18e54e4e7bbcbc71752fee90
BLAKE2b-256 da37c766f5a90128c80507929094e93cbf1cf8f2fecfad9807b92025f2ab2f1b

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