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
- Connect to the database anywhere in your codebase by using
Databaseclass constructor
main_db = Database(
db_type=DatabaseType.MYSQL,
host="localhost",
db_name="bookstore",
user="root",
password="",
)- 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")- Save it's structure as a table
Author.create()- 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- 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)- 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)Lets take a more in-depth features of this library
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
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
Currently they are 5 fields supported
DatetimeFieldTextFieldIntegerFieldBooleanField
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 namenull: if set toTrueORM will allow nullish or empty values passedunique: whether field values will be uniquedefault: sets default value on columnmax_size: specifies max size ofTextFieldandIntegerField
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
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
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
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
GNU General Public License