_ __ ___ _____ ___ _ __ _ __ ___
| '_ ` _ \| __ \/ _ \| `__/| '_ ` _ \
| | | | | | |___| |_| | | | | | | | |
|_| |_| |_|_| \___/|_| |_| |_| |_|

mporm is an ORM tool written in Python with only the fundamental CRUD API for MySQL(5.5+) 简体中文
Overview
Features
Install
pip3 install mporm
Quick Start
from mporm import ORM, DSN, Model, StrField, IntField
ORM.load(DSN(user="xxxx", password="xxxx"))
class Hero(Model):
name = StrField()
age = IntField()
Hero.create()
Hero.add(name="Thor", age=1000)
Hero.where(name="Thor").set(age=1001).update()
Hero.where(name="Thor").find()
Hero.where(name="Thor").delete()
Hero.drop()
Connect to Database
mporm can only connect MySQL database, and has two different ways to load configs of database
Load By DSN
The minimum code that loads by dsn is wriiten as
from mporm import ORM, DSN
ORM.load(DSN(user="xxxx", password="xxxx"))
Because mporm will automatically set other configs as host
= "localhost", port
= 3306, database
= "test", charset
= "utf8"
Of course you can fill all the configs by yourself
Load From Toml File
You can write all the configs in a toml file like
[database]
user = "xxxx"
password = "xxxx"
host = "xxxx"
port = 3306
database = "xxxx"
charset = "xxxx"
Then use load_file
method
from mporm import ORM
ORM.load_file("path/to/toml")
Note that if you use the second way, remember all the 6 configs needs to be written in the toml file.
Table Prefix
You can define a model with an attribute __prefix__
, for example:
from mporm import Model
class Hero(Model):
__prefix__ = "Marvel"
...
Hero.create()
This will create a new table named "marvel_hero"
CRUD Interfaces
We have defined a model like
class Hero(Model):
__prefix__ = "Marvel"
name = StrField()
age = IntField()
Insert
There are two methods you can choose from:
Hero.new(name="Thor", age=1000).insert()
or simply use
Hero.add(name="Thor", age=1000)
The SQL statement that'll be executed is
insert into `marvel_hero` (name, age) values ('Thor', 1000);
Select
Query
Hero.first()
Hero.last()
Hero.take()
Plus they can take an argument
Hero.first(10)
Hero.last(10)
Hero.take(10)
Where
Hero.where(name="Thor", age=1000).find()
Hero.where(name="Thor", age=1000).findone()
Of course, Specified Fields Selecting is available
Hero.where(name="Thor", age=1000).select("name").find()
Or you can simply use
Hero.where(name="Thor", age=1000).filter("name")
Count
Hero.where(name="Thor").count()
Also custom count field is available
Hero.where(name="Thor").count("age")
Advanced
Order
Hero.where(name="Thor").order("age", desc=True).find()
Limit
Hero.where(name="Thor").limit(10).find()
Offset
Hero.where(name="Thor").offset(10).find()
Of course, you can use them like chains
Hero.where(name="Thor").order("age").limit(10).offset(10).select("name", "age").find()
Update
Hero.where(name="Thor").set(age=1001).update()
Delete
Hero.where(name="Thor").delete()
Note that the methods insert()
update()
delete()
return the amount of rows that're affected and method find()
returns a list-typed
query result and not to mention, the method findone()
returns a dict-typed
query result.
Todo
Contribute
You can do anything to help deliver a better MPORM.
License
@ XJJ, 2019~datetime.now()
Released under the MIT License