Coder Social home page Coder Social logo

cs475's Introduction

CS475 Project: Object Relational Mapper (ORM)

This project is a simple, stand-alone and lightweight implementation of an Object Relational Mapper (ORM) for Python. The ORM is a library that allows developers to interact with a relational database using Python objects. The ORM will handle the mapping of Python objects to database tables and vice versa.

Installation

You can install the ORM by running the following command:

pip install borm

How to use

The first step to is to design your models (tables). For each model you can inherit from models.Model and define its fields as class attributes of available fields from the fields module.

from borm import Model
from borm.fields import UUIDField, StringField, IntegerField


class MyModel(Model):
    id = UUIDField(primary_key=True)
    name = StringField()
    rating = IntegerField()

As appears in the example above, you can define the primary key by setting the primary_key attribute to True in only one field. Then you can start data manipulation by creating instances of your models, etc.

from uuid import uuid4

id_1 = uuid4()
my_model = MyModel.objects.create(id=id_1, name='My Model', rating=5)
print(my_model.id)
print(my_model.name)
print(my_model.rating)
id_2 = uuid4()
my_model_2 = MyModel.objects.create(id=id_2, name='My Model 2', rating=4)
print(my_model_2.id)
print(my_model_2.name)
print(my_model_2.rating)
models = MyModel.objects.all()
for model in models:
    print(model.id)
    print(model.name)
    print(model.rating)
specific_model = MyModel.objects.get(id=id_1)
print(specific_model.id)
models_with_rating_4 = MyModel.objects.filter(rating=4)
for model in models_with_rating_4:
    print(model.id)
    print(model.name)
    print(model.rating)

Furthermore, you may manipulate and update the data in the database by updating the fields of the model instances and calling the save method to apply the changes to the database.

my_model.rating = 3
my_model.save()

Finally, you can delete a model instance by calling the delete method.

my_model._delete()

Unit Testing

TODO

cs475's People

Contributors

baranerf avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.