Skip to content

VarthanV/vorm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Contributors Forks Stargazers Issues LinkedIn


vorm

A toy ORM written in python


Bug · Request Feature

Table of Contents

  1. About The Project
  2. Getting Started
  3. Usage and Examples
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgements

About The Project

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

Built With

Getting Started

Installation

Install the package from Github

git clone  https://github.com/VarthanV/vorm.git
cd vorm
python setup.py install

Commonly faced errors

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

Usage and Examples

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

Connecting to database

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)

Creating a model class

  • 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,
);

Migration

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.

Inserting into a table

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"

Querying from a table

# 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

Updating a row in the table

# 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"

Deleting a row from the table

# Deletes the person with the id 1

Person.objects.delete(id=1)

Foreign key

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

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

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.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Vishnu Varthan - Twitter - vishnulatha006@gmail.com

Project Link: https://github.com/VarthanV/vorm

Acknowledgements

These repositories gave me a base idea of how a orm must be

What's next 🚀 ?

  • Adding support for more DB engines.
  • Admin panel (Inspired by Django).
  • Adding more testcases.
  • SQL to vorm style code generator (CLI Tool).

About

A simple toy orm in Python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages