Giter Site home page Giter Site logo

ormysql's Introduction

Build Status

orMysql, 顾名思义是一个针对mysql的orm,

可以看成sqlalchemy的精简版,但是也能满足大部分的需求。 它的mysql客户端使用PyMySQL, orMysql在内部实现了一个连接池,可以回收使用宝贵的连接。 具体像下面这样👇

from orMysql.db import db
from orMysql.model import Model

class User(Model):
    __tablename__ = 'test.user'
    id_ = IntFiled(name="id", doc="用户id", primary_key=True)
    age = IntFiled(name="age", doc="用户年龄")
    name = StringFiled(name="name", doc="用户姓名")

    @classmethod
    def add(cls, age, name):
        user = cls(age=age, name=name)
        db.session.add(user)

    @classmethod
    def get(cls, id):
        return cls.query.filter(cls.id_==id).first()

    def update(self, **kw):
        db.session.update(self, **kw)

用法说明

在使用之前需要先设置以下环境变量来使用数据库:

OR_MYSQL_HOST=127.0.0.1   # 数据库host
OR_MYSQL_PORT=33062       # 数据库port
OR_MYSQL_USER=root        # 数据库user
OR_MYSQL_PASSWORD=123456  # 数据库密码
OR_MYSQL_DB=              # 选择的数据库,可以在表中指定
OR_MYSQL_CHARSET=utf8mb4  # 客户端编码
OR_MYSQL_MAX_POOL_SIZE=5  # 连接池大小,默认为5
OR_MYSQL_MAX_LIFE_TIME=3600 # 连接最大存活时间,单位秒
OR_MYSQL_TRY_TIMES=3  # 获取连接尝试的次数i,默认三次

如果在环境变量里面没有设置, 可以在代码里调用orMysql.db.db.setup_db设置。

具体用法见测试

本项目可能存在一些坑,欢迎拍砖🧱。

本项目license是MIT。具体见LICENSE

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.