Skip to main content

Simple ORM based on Pydantic and SQLite with minimalistic API

Project description

ORMagic - Simple ORM for Python

GitHub License Tests Codecov PyPI - Python Version PyPI - Version Code style: black Linting: Ruff Pydantic SQLite Pytest

The main goal of ORMagic is to provide a simple and easy-to-use ORM for Python, that is easy to understand and use, while still providing the necessary features to interact with a database. The library is in the early stages of development, so it is not recommended to use it in production. Is based on the Pydantic model and extends it with the ability ti save, read, update and delete data from a SQLite database.

Installation

pip install ORMagic

Usage

Define a model

To define a model, create a class that inherits from DBModel and define the fields using Pydantic's field types.

from ormagic import DBModel

class User(DBModel):
    name: str
    age: int
    created_at: datetime = datetime.now()

# Create the table in the database
User.create_table()

Save, read, update and delete data

# Save data to the database, this will create a new record or update an existing one if the primary key is already present
user = User(name="John", age=30)
user.save()

# Read data from the database
user = User.get(id=1)
print(user)
>>> User(id=1, name='John', age=30, created_at=datetime.datetime(2021, 10, 10, 12, 0, 0))

# Delete data from the database
user.delete()

# Update data
user = User.get(id=1)
user.age = 31
user.save()

Define foreign keys

To define a foreign key, use other models as fields in the model. By default, the foreign key will be set to CASCADE, but you can change it by setting the on_delete parameter of the pydantic field to one of the following values: CASCADE, SET_NULL, RESTRICT, SET_DEFAULT, NO_ACTION.

from ormagic import DBModel

class User(DBModel):
    name: str

class Post(DBModel):
    title: str
    content: str
    user: User # Define a foreign key with default on_delete=CASCADE

User.create_table()
Post.create_table()

user = User(name="John")
user.save()

Post(title="Hello", content="World", user=user).save()

# You can also save child models with new parent object in one step, this will save the parent object first and then the child object
Post(title="Hello", content="World", user=User(name="Alice")).save()

Define foreign key with custom on_delete

from ormagic import DBModel

class User(DBModel):
    name: str

class Post(DBModel):
    title: str
    content: str
    user: User(default=None, on_delete="SET_NULL") # Define a foreign key with on_delete=SET_NULL
    user: User(on_delete="RESTRICT") # Define a foreign key with on_delete=RESTRICT
    user: User(default=1, on_delete="SET_DEFAULT") # Define a foreign key with on_delete=SET_DEFAULT
    user: User(on_delete="NO_ACTION") # Define a foreign key with on_delete=NO_ACTION
    user: User(on_delete="CASCADE") # Define a foreign key with on_delete=CASCADE

User.create_table()
Post.create_table()

Features and Roadmap

  • Define table schema using Pydantic models
  • Basic CRUD operations
    • Save data to the database
    • Read data from the database
    • Update data in the database
    • Delete data from the database
  • Relationships between tables
    • One-to-many
      • Create a tables with a foreign key
      • Save data with a foreign key
      • Read data with a foreign key
      • Update data with a foreign key
      • Delete data with a foreign key
        • Cascade
        • Set null
        • Restrict
        • Set default
        • No action
    • One-to-one
    • Many-to-many
  • Custom primary key
  • Bulk operations (save, update, delete)
  • Migrations

License

This project is licensed under the terms of the MIT license

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request.

Why?

There are many ORMs for Python, but most of them are too complex or have too many features that are not needed for simple projects.

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

ormagic-0.3.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

ormagic-0.3.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file ormagic-0.3.0.tar.gz.

File metadata

  • Download URL: ormagic-0.3.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1024-azure

File hashes

Hashes for ormagic-0.3.0.tar.gz
Algorithm Hash digest
SHA256 9f9af761cb19af95f7b847aedfcf18fec06e3edee7ec28517d5cb29857e7dd2e
MD5 94ac33fa74d519c05df41010a1241da4
BLAKE2b-256 3fed5050b6567b4cf80889d9489042a0388a32f3d4e0dcbcec3a0d5510c5e94e

See more details on using hashes here.

File details

Details for the file ormagic-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: ormagic-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1024-azure

File hashes

Hashes for ormagic-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e7d60b4c95938ab577e0fa974a13f6df8dd1b0c06cfc65aa8adf77699d426e3d
MD5 70514f65c96c3c747b876fb87f876857
BLAKE2b-256 93c841de69cd1655ec510d248aeb89fd97632e3fd9684fba36d1df6a131a4f27

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