An ORM for MySql.
Project description
MySql ORM
A simple Object Relational Mapper for MySql. Works with existing tables and the schema of the tables are automatically inferred.
Install
pip install orm-mysql
Connect to MySql
from orm import Table
Table.connect(config_dict={
'host': '<host_here>',
'port': 3306,
'user': '<user>',
'password': '<password>',
'database': '<database>'
})
Create a class for a table
Create a class that inherits from Table
. Initialize the class variable table_name
with the name of the table, here 'student'
from orm import Table
Table.connect(config_dict=CONFIG)
class Student(Table):
table_name = 'student'
OR use get_table()
from orm import Table, get_table
Table.connect(config_dict=CONFIG)
Student = get_table('student')
Insert data
Using save()
new_student = Student(name='hrushi', age=19, gender='M')
new_student.age = 20
new_student.save()
Using create()
new_student = Student.create(name='hrushi', age=19, gender='M')
Query data
Using where()
students = Student.where(age=19, gender='F')
for stu in students:
print(stu.name)
Using find()
Find a single record based on PRIMARY KEY.
# find student where id(PK) = 2
student = Student.find(2)
print(student.name)
Delete a record
student = Student.find(2)
student.destroy()
Relations
has_many
and belongs_to
can be used to show relationships between tables.
from orm import Table, has_many, belongs_to
class Article(Table):
table_name = 'articles'
relations = [
belongs_to(name='author', _class='Author', foreign_key='author_id', primary_key='id')
]
class Author(Table):
table_name = 'authors'
relations = [
has_many(name='articles', _class='Article', foreign_key='author_id')
]
Get all the books written a Author
auth = Author.find(2)
articles = auth.articles()
for art in articles:
print(art.title)
Get Author of a book
art = Article.find(5)
author = art.author()
print(author.name)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
orm-mysql-1.0.1.tar.gz
(5.3 kB
view details)
Built Distribution
File details
Details for the file orm-mysql-1.0.1.tar.gz
.
File metadata
- Download URL: orm-mysql-1.0.1.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 294a4a67e5df286cf9751ea24bc88ad982309158bb3e2c74a36c08874da0b811 |
|
MD5 | 82acfa47461e37e46e09c5aed4972504 |
|
BLAKE2b-256 | 3f3b90ec88bad0d1e0b95a6c24a92a20b79c2457f8c41a682d869e81b3c36163 |
File details
Details for the file orm_mysql-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: orm_mysql-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9efa979da0067739254d3d1b7a8ba5fddbb6ff2d23f01384f8824c15a02b8cf1 |
|
MD5 | 29c8d953cc725ee5085c047ead027942 |
|
BLAKE2b-256 | 3904059c1bf16ce4497f87402509fde714c3c8bfa9702ec3c49ada1e35af0d5c |