_ __ ___ _____ ___ _ __ _ __ ___
| '_ ` _ \| __ \/ _ \| `__/| '_ ` _ \
| | | | | | |___| |_| | | | | | | | |
|_| |_| |_|_| \___/|_| |_| |_| |_|
mporm is an ORM tool written in Python with only the fundamental CRUD API for MySQL(5.5+) 简体中文
Overview
Features
- gorm-like API
- Automatically use
uuidas fieldid - Automatically set
created_atandupdated_at
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()
# CRUD
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()
## select * from `marvel_hero` order by created_at limit 1;
Hero.last()
## select * from `marvel_hero` order by created_at desc limit 1;
Hero.take()
## select * from `marvel_hero` limit 1;
Plus they can take an argument
Hero.first(10)
## select * from `marvel_hero` order by created_at limit 10;
Hero.last(10)
## select * from `marvel_hero` order by created_at desc limit 10;
Hero.take(10)
## select * from `marvel_hero` limit 10;
Where
Hero.where(name="Thor", age=1000).find()
## select * from `marvel_hero` where name = 'Thor' and age = 1000;
Hero.where(name="Thor", age=1000).findone()
## select * from `marvel_hero` where name = 'Thor' and age = 1000 limit 1;
Of course, Specified Fields Selecting is available
Hero.where(name="Thor", age=1000).select("name").find()
## select name from `marvel_hero` where name = 'Thor' and age = 1000;
Or you can simply use
Hero.where(name="Thor", age=1000).filter("name")
## select name from `marvel_hero` where name = 'Thor' and age = 1000;
Count
Hero.where(name="Thor").count()
## select count(id) from `marvel_hero` where name = 'Thor';
Also custom count field is available
Hero.where(name="Thor").count("age")
## select count(age) from `marvel_hero` where name = 'Thor';
Advanced
Order
Hero.where(name="Thor").order("age", desc=True).find()
## select * from `marvel_hero` where name = 'Thor' order by age desc;
Limit
Hero.where(name="Thor").limit(10).find()
## select * from `marvel_hero` where name = 'Thor' limit 10;
Offset
Hero.where(name="Thor").offset(10).find()
## select * from `marvel_hero` where name = 'Thor' offset 10;
Of course, you can use them like chains
Hero.where(name="Thor").order("age").limit(10).offset(10).select("name", "age").find()
## select name, age from `marvel_hero` where name = 'Thor' order by age asc limit 10 offset 10;
Update
Hero.where(name="Thor").set(age=1001).update()
## update `marvel_hero` set age=1001 where name = 'Thor';
Delete
Hero.where(name="Thor").delete()
## delete from `marvel_hero` where name = "Thor";
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
-
where-like
-
where-or
-
Where-<>
-
custom sql statement execution
Contribute
You can do anything to help deliver a better MPORM.
License
@ XJJ, 2019~datetime.now()
Released under the MIT License
Metadata
Release files for mporm 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mporm-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Release files / mporm-0.0.2-py3-none-any.whl
| Download URL | mporm-0.0.2-py3-none-any.whl |
|---|---|
| Size | 11.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
084dd7a4a31b92944f4efcfebae47418880c51997db75f3a6d7d6349066c5e36
|
|
BLAKE2b-256 checksum How to use checksums |
f8300e680b8904f7c01f73964cc38a26815e80e7dcf1b977a38c62ec50c34c9d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.7.4
|