Skip to main content

Cloey ORM

Cloey ORM is a lightweight and easy-to-use Object-Relational Mapper (ORM) for Python, designed for simplicity, speed, and elegance when working with databases.

Features

  • Define models with pure Python classes
  • Easy database connection and management
  • Minimal dependencies
  • Supports PostgreSQL and SQLite
  • Simple CRUD operations

Installation

You can install Cloey ORM via pip:

pip install cloey-orm

Quickstart

Import the necessary classes:

from cloey.database import PostgreSQLConnection
from cloey.orm import BaseModel

Set the PostgreSQL connection

BaseModel.set_connection(PostgreSQLConnection(
database="cloey",
user="cloey",
password="secret",
host="localhost",
port=5432
))

Define Models:

class Student(BaseModel):
name: str
email: str

class Subject(BaseModel):
name: str

Create and Query Records:

Subject.create(name="Maths")
Subject.create(name="DSA")

john = Student.create(name="John Doe", email="john.doe@example.com")
jane = Student.create(name="Jane Doe", email="jane.doe@example.com")

user = User.get(email="john.doe@example.com")
print(f"Found user: {user.name}, {user.email}")

Relate tabes:

class Mark(BaseModel):
subject: Subject
mark: float
student: Student

Mark.create(subject_id=dsa.id, mark=10.4, student_id=john.id)
Mark.create(subject_id=maths.id, mark=19.4, student_id=john.id)

Mark.create(subject_id=dsa.id, mark=16.4, student_id=jane.id)
Mark.create(subject_id=maths.id, mark=17.0, student_id=jane.id)


for mark in john.marks:
print(mark.student.name, 'had', mark.mark ,'on', mark.subject.name)

for mark in jane.marks:
print(mark.student.name, 'had', mark.mark ,'on', mark.subject.name)