Skip to main content

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.

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

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


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-0.1.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

orm_mysql-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file orm-mysql-0.1.0.tar.gz.

File metadata

  • Download URL: orm-mysql-0.1.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.2

File hashes

Hashes for orm-mysql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 621690c9304bc6eb2cbff84d92ad63bb240f68c4350f2cedbeeeff46a469e6e7
MD5 2a522c9cb65d943a0b67e5269f8777c9
BLAKE2b-256 e83a59235b171248475a2db7cc744ef43e93e18ea300f18da2ebff4f3b019e51

See more details on using hashes here.

File details

Details for the file orm_mysql-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: orm_mysql-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.2

File hashes

Hashes for orm_mysql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f3443ba093f287b568a71d896ca4e879ce2a476a75110f85451fc874a15eba61
MD5 71d491b81d156ef50acf34ab8857c963
BLAKE2b-256 b79c1d3a85d91f156938cfd5e13661cef355550b2233d4a8012ada60d5de0b53

See more details on using hashes here.

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