A toy ORM written in python
Bug
·
Request Feature
vorm is a toy ORM written in Python , I started it as a fun hobby project to know how ORM's work internally and to better understand it and use it efficiently in my day to day work /projects .Currently vorm supports MYSQL and PostgreSQL DB engines . Feel free to add support to other DB's as well.
Thanks for stopping here (:
Install the package from Github
git clone https://github.com/VarthanV/vorm.git
cd vorm
python setup.py install
If you find mysql is missing or psycopg2 is missing you can install it manually and it will work
pip install mysql-connector-python
pip install psycopg2-binary
vorm has similiar syntax to other popular ORM's like Djang ORM and SQLalchemy , If you have familiarity with these libraries already it will not take much time for you to adopt to its syntax
We need to create a connection to a database , Currently vorm supports PostgreSQL and MYSQL
from vorm.manager import ConnectionManager as db
db_settings = {
"driver": "postgresql",
"user": "root",
"password": "astrongpassword",
"host": "localhost",
"port": 5432,
"database": "students",
}
db.create_connection(db_settings)
-
Each model class in vorm inherits the Base Model class.
-
Each attribute of the model represents a field in your database.
from vorm import base
from vorm import fields
class Person(base.BaseModel):
table_name = 'person'
first_name = fields.CharField(max_length=30)
last_name = fields.CharField(max_length=30)
age = fields.IntegerField(default=15)
table_name
is a required attribute, Your table will be named in the database.
The above model class would create a table like this
CREATE TABLE person (
"id" SERIAL NOT NULL PRIMARY KEY,
"first_name" VARCHAR(30) NOT NULL,
"last_name" VARCHAR(30) NOT NULL ,
"age" int DEFAULT 15 NOT NULL,
);
After you define the model class ,you need to create your defined class as a table in the connected database.
db.migrate(Person)
When this piece of code runs , The person
table will be created in the connected database.
person_1 = Person.objects.insert(first_name="Walter",last_name="White",age=50)
print(f"The persons name is {person.first_name} {person.last_name}")
"Walter White"
# Get all the persons whose age is greater than 30
persons = Person.objects.where(age__gt =30)
print(len(persons))
The where method always returns a list of model_class
, If no results are found it returns an empty array
# Updates the row with the id 1 and returns the updated value
updated_person = Person.objects.update(new_data={"first_name":"Vishnu"},id=1)
print(updated_person.first_name)
"Vishnu"
# Deletes the person with the id 1
Person.objects.delete(id=1)
class Membership(base.BaseModel) :
name = fields.CharField(max_length=100)
person = fields.ForeignKey(Person)
person = Person.objects.where(id=2)
membership = Person.objects.insert(name="Test membership" , person=person)
print(membership.person) # Returns a list of the person object
See the open issues for a list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Vishnu Varthan - Twitter - vishnulatha006@gmail.com
Project Link: https://github.com/VarthanV/vorm
These repositories gave me a base idea of how a orm must be
- Adding support for more DB engines.
- Admin panel (Inspired by Django).
- Adding more testcases.
- SQL to vorm style code generator (CLI Tool).