Skip to main content

a tiny orm frame

Project description

rain-orm

a tiny orm frame

Install

it's easy to install via pip

pip install rain-orm

Simple Example

assume you have "students" table in "student_management" database

students

id name password class_id
import rain_orm
from rain_orm.column import Int, VarChar

rain_orm.connect(host="localhost", port=3306, user="root", password="123456", database="student_management")


class StudentModel(rain_orm.Table):
    __table__ = "students"
    __fields__ = {
        "id": Int(auto_increment=True, primary_key=True),
        "name": VarChar(30, unique=True),
        "password": VarChar(30),
        "class_id": Int(default=1)
    }

if __name__ == "__main__":
    rain_orm.Table.auto_migrate()
    stu = StudentModel().where("id=?", 1).one()
    print(stu)

result

id: 1
name: cgy
<< StudentModel 1951412882544
	.table = students
	.fields = ['id', 'name', 'password', 'gender', 'origin', 'class_id']
	.instance = {
		id: 1,
		name: cgy,
		password: 123456,
		class_id: 1,
	}
>>

Basic Usage

it's easy to use rain-orm

  1. import rain_orm
  2. connect to database
  3. define model class that inherits class rain_orm.Table
  4. define two attribute "__table__" and "__fields__"
  5. use rain-orm API

Import

import rain_orm

Connect

rain_orm.connect(host="you host", port=3306, user="root", password="your password", database="student_management")

Define Model

each data must have a unique "id"

from rain_orm import Table
from rain_orm.column import Int, VarChar


class ClassModel(Table):
    __table__ = "classes"
    __fields__ = {
        "id": Int(primary_key=True, auto_increment=True),
        "name": VarChar(30)
    }


class StudentModel(Table):
    __table__ = "students"
    __fields__ = {
        "id": Int(primary_key=True, auto_increment=True),
        "name": VarChar(30, not_null=True),
        "class_id": Int(foreign_key=ClassModel.id)
    }

Auto Migrate

# all subclasses that inherit rain_orm.Table
rain_orm.Table.auto_migrate() 
# create StudentModel
rain_orm.Table.auto_migrate(StudentModel)

CRUD Example

Read

# student list
stu_lst = StudentModel().all()
# student whose id equals 1 with all fields
stu1 = StudentModel().where("id = ?", 1).one()
# student whose id equals 1 with all fields
stu2 = StudentModel().where("id = 1").one()

Create

stu = StudentModel(name="name", password="123", class_id=1234)  # set student
ok = stu.create()
print("ok:", ok)
print(stu)

result:

ok: True
<< StudentModel 2449383681440
	.table = students
	.fields = ['id', 'name', 'password', 'class_id']
	.instance = {
		id: 30,
		name: jack,
		password: 123456,
		class_id: 1,
	}
>>

Update

there are 2 ways to update data

StudentModel().where("id = ?", 22).one()  # get student instance first
# 1. set attribute (recommend)
stu.name = "your name"
# 2. call update methods
ok = stu.update("name", "your name")

Delete

StudentModel().where("id = ?", 22).one()  # get student instance first
ok = stu.delete()  # call delete method

Project details


Download files

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

Source Distribution

rain_orm-1.0.9.tar.gz (13.0 kB view hashes)

Uploaded Source

Built Distribution

rain_orm-1.0.9-py3-none-any.whl (15.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page