Skip to main content

MySQL ORM in Python

Project description

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

mporm is an ORM tool written in Python with only the fundamental CRUD API for MySQL(5.5+) 简体中文


Overview

Features

  • [x] gorm-like API
  • [x] Automatically use uuid as field id
  • [x] Automatically set created_at and updated_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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for mporm, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size mporm-0.0.2-py3-none-any.whl (11.7 kB) File type Wheel Python version py3 Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page