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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file rain_orm-1.0.9.tar.gz.

File metadata

  • Download URL: rain_orm-1.0.9.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for rain_orm-1.0.9.tar.gz
Algorithm Hash digest
SHA256 6351c248a2c00c947252324a57ce3cbdb12e8ad7a4a2e556561661343c94cbe5
MD5 a31ee6d97ad1ace7b5afb81943ccd952
BLAKE2b-256 3bfcc59a9827390a3f03a26515716898d6064f2bd6b27267e2b1c1b7181be421

See more details on using hashes here.

File details

Details for the file rain_orm-1.0.9-py3-none-any.whl.

File metadata

  • Download URL: rain_orm-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for rain_orm-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 8fe115111a08b533ce82d47a6edc7a4c8e2bf00c7d9b6d716d37d2bef70c710a
MD5 5372ec5a21c1af1e41ed4d6baf0e6cf4
BLAKE2b-256 837375b6251e3fe2a620a609ee5f0b5049a0b0329287b6627a5acc672fa58476

See more details on using hashes here.

Supported by

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