Skip to main content

A simple and easy-to-use ORM for managing your database queries.

Project description

Morgan ORM for Python

A simple and easy-to-use ORM for managing your database queries.

Motivation

I decided to create an ORM for two main reasons:

  • To build an ORM that is extremely simple to use, without complex configurations—just set up your database access and you're ready to go.
  • As a learning experience to understand how an ORM works internally.

I've worked with Laravel for many years, so in a way, I'm drawing inspiration from Eloquent for this ORM.

Usage

Using the query builder from a model

Creating a model

# Import required packages
from morgan.connection import DatabaseConfig, ConnectionType
from morgan.orm import Model

# Create your model inheriting from Model
class User(Model):
    table = "users"     # Name of your table
    primary_key = "id"  # Name of your primary key

    # Configuration for the database (now it only supports SQLite)
    db_config = DatabaseConfig(connector=ConnectionType.SQLite, database_url="morgan.db")

Some available operations

# Get all the users
users = User.all()

# Get user by primary key
user = User.get_by_pk(pk=92)

# Get users by filtering results
users = User.where("status = ?", 1).get()

# Limiting results
users = User.where("status = ?", 1).limit(5).get()

# Ordering results
users = User.where("status = ?", 1).order_by("created_at", "DESC").get()

# Creating records
user = User.create_one(username="admin", email="admin@email.com", status=1)

# Or...
users_created = User.create_many([
    {"username": "admin", "email": "admin@email.com", "status": 1},
    {"username": "staff", "email": "staff@email.com", "status": 1},
    {"username": "guest", "email": "guest@email.com", "status": 1},
])

# Updating records
User.where("status = ?", 0).update("status = ?", 1).exec()

# Deleting records
User.where("email = ?", "user@email.com").delete().exec()

Working with an instance

# Creating a new user
user = User(username="admin", email="admin@email.com", status=1)
user.save()

# Or...
user = User()
user.username = "admin"
user.email = "admin@email.com"
user.status = 1
user.save()

# Updating an existing record
user = User.get_by_pk(pk=8)
user.email = "support@email.com"
user.save()

# Deleting a record
user = User.get_by_pk(pk=9)
user.delete()

Roadmap

  • Add support for methods such as create_one and create_many in the QueryBuilder.
  • Add support for logging queries.
  • Add unit tests.
  • Add support for MySQL/MariaDB and PostgreSQL.
  • Add support for statements like GROUP BY and HAVING.
  • Add support for relationships such as one-to-one, one-to-many, and many-to-one.

Future plans include:

  • Support for migrations.
  • CLI tool for creating migrations and models files.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_morgan_orm-0.2.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

py_morgan_orm-0.2.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file py_morgan_orm-0.2.0.tar.gz.

File metadata

  • Download URL: py_morgan_orm-0.2.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for py_morgan_orm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ca93791a015e3bcb6c9b523af76bca2f27b1ba40d3aac269fbe9133456e88210
MD5 ade25571683766cb614b51c16443a941
BLAKE2b-256 2b7201ad9aeb0f2fc682a15dd499c0bd6db91016438734e7f242a5a317bc0e61

See more details on using hashes here.

File details

Details for the file py_morgan_orm-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for py_morgan_orm-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4edc94185b48d0f962b5527d24a71326a6a26ad59691c5b81800f0525deae9f9
MD5 fcaeecfbd5b7c138b71a4e5ab02a2446
BLAKE2b-256 8856cce159133bfe27a002576b1c5055a12031afb27201f35c9d019d5f06bc2f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page