Skip to content

R3VANEK/simple-orm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple ORM in Python

forthebadge forthebadge

Table of Contents

Description

This is very simple Python library providing basic ORM funcitonalities. It was created as an exercise in more object-oriented Python programming. Currently supported db engine is MySQL

Overview

  1. Connect to the database anywhere in your codebase by using Database class constructor
main_db = Database(
    db_type=DatabaseType.MYSQL,
    host="localhost",
    db_name="bookstore",
    user="root",
    password="",
)
  1. Create your model classes defining db schema
@config(db=main_db)
class Author(Model):

    table_name = "authors"

    name = TextField(db_column="name", default="John")
    surname = TextField(db_column="surname", default="Doe")
  1. Save it's structure as a table
Author.create()
  1. Create, update or delete its instances as rows in your table. 0% SQL needed
    author = Author()
    author.name = "Jane"
    author.surname = "Smith"
    author.save() # will create new record in table

    author.name = "John"
    author.save() # will update existing record

    author.delete() # easy deletion of corelated record
  1. Perform easy to write CRUD queries
fetched_authors = (
    Select(Author)
    .Where((Author.surname != "Smith") | (Author.name == "Jane"))
    .execute()
)
# will print a list of Author objects
print(fetched_authors)
  1. Support of one-to-many relationships via ForeignKeyField
@config(db=main_db)
class Book(Model):

    table_name = "books"

    name = TextField(db_column="name")
    description = TextField(db_column="description", null=True)
    author = ForeignKeyField(to=Author, reverse_name="books")

book1 = Book()
book1.name = "Amazing book"
book1.author = author # assign already saved instance of author
book1.save()

# will print a list of Book objects corelated to the concrete author instance
print(author.books)

Dive in

Lets take a more in-depth features of this library

Connect to your db

Right now, library only supports connecting to the MySQL database. You can do this anywhere in your code by declaring an instance of Database class. Its written as a singleton so don't worry about making unnecessary connections

Models

To consider a class a model class, you need to import Model and config decorator from db.classes and then make your class as in the overview. Each model class must define table_name and at least one Field attribute. Other attributes or methods will not interfere with library so feel free to declare your e.g constructors

Fields

Currently they are 5 fields supported

  • DatetimeField
  • TextField
  • IntegerField
  • BooleanField

and ForeignKeyField which we will talk more about in a moment. Each field by default has ability to define these properties:

  • db_column: specifies name of column in db. May be different than attribute name
  • null: if set to True ORM will allow nullish or empty values passed
  • unique: whether field values will be unique
  • default: sets default value on column
  • max_size: specifies max size of TextField and IntegerField

To further assist a developer catch bugs quickier and more reliable than waiting for the db error, each field has built-in type safety, meaning if you provide to the e.g DatetimeField attribute something else than datetime, you will receive error on the assigment

Foreign Keys

By using ForeignKeyField you can introduce one-to-many relation on your models. This type of field has as well ability to define reverse name of relation for easy access to data no matter the direction. To link another model, it must be in saved state beforehand, otherwise you will get an error. Adding or removing objects from the related_name side is not yet supported

Query engine

You can perform queries by using special classes found in db.queries. The result of select query is a list of objects, whereas for the update, delete ones, you will receive number of affected rows. As shown in the Overview section, ORM supports linking multiples of conditions to where clause by easy comparisons on class attributes

TODO

As this project was created for the WUST studies project its far from complete. Few things require further testing and implementing including:

  • more complex comparisons to where clauses of queries
  • many-to-many relation support
  • reverse foreign key updating records
  • joins on queries
  • further testing of various field attributes

License

GNU General Public License

About

Minimalistic ORM in Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages