PostgreSQL specific extensions to the Ormar ORM
Project description
ormar-postgres-extensions
Overview
ormar-postgres-extensions is a an extension to theOrmar ORM. It enables developers to write models that map to native PostgreSQL types.
Motivation
Ormar is an amazing async ORM that works with FastAPI. However, it is agnostic to the underlying database used meaning that we cannot use native PostgreSQL types such as UUID or JSONB columns. The aim of this library is to provide Ormar fields that can be used to generate database columns with native PG types.
Installation
python -m pip install ormar-postgres-extensions
Usage
Fields
Three native PG fields are provided. The JSONB
and UUID
types map to native JSONB
and UUID
data types respectively. The Array
type can be used to create an array column. Using these in an Ormar model is as simple as importing the fields and using them in the model.
from uuid import UUID
import ormar
from ormar_postgres_extensions import PostgresUUID
class MyModel(ormar.Model):
uid: UUID = PostgresUUID(unique=True, nullable=False)
Array Fields
Array fields require a bit more setup to pass the type of the array into the field
import ormar
import sqlalchemy
from ormar_postgres_extensions import Array
class ModelWithArray(ormar.Model):
class Meta:
database = database
metadata = metadata
id: int = ormar.Integer(primary_key=True)
data: list = Array(item_type=sqlalchemy.String())
Arrays have access to three special methods that map to specific PostgreSQL array functions
array_contained_by
The maps to the contained_by
operator in Postgres.
await ModelWithArray.objects.filter(
ModelWithArray.data.array_contained_by(["a"])
).all()
array_contains
The maps to the contains
operator in Postgres.
await ModelWithArray.objects.filter(
ModelWithArray.data.array_contains(["a"])
).all()
array_overlap
The maps to the overlap
operator in Postgres.
await ModelWithArray.objects.filter(
ModelWithArray.data.array_overlap(["a"])
).all()
Uninstalling
pip uninstall ormar-postgres-extensions
Contributing
Feel free to open a PR or GitHub issue. Contributions welcome!
To develop locally, clone this repository and run . script/bootstrap
to install test dependencies. You can then use invoke --list
to see available commands.
To run the tests locally, PostgreSQL needs to be running. This can be easily started via inv database
.
See contributing guide
Contributors
You don't really have to add this section yourself! Simply use all-contributors by adding comments in your PRs like so:
Evert Timberg 🤔 🚇 🚧 📖 ⚠️ |
@all-contributors please add <username> for <contribution type>
Find out more about All-Contributors on their website!
License
ormar-postgres-extensions
is licensed under Apache License Version 2.0.
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
Built Distribution
Hashes for ormar-postgres-extensions-2.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe7f6fc120066ca5ccd6108e67b17aa74d0b6c70456316b8d28ba4119acd3a8e |
|
MD5 | 758ff0f9df0bf42bedcbe12379352e84 |
|
BLAKE2b-256 | 28a85b3bd64665dc8dee57a0a1fda9a7b8306251af7578d0cb13a0873b9a1095 |
Hashes for ormar_postgres_extensions-2.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86aeaa7e65c5e462dbb9fa793c684c34bb5dc52cc8771e36f2b4d2f14cfba2e6 |
|
MD5 | 1842f78400a93a1e24976fd8c9cc9acb |
|
BLAKE2b-256 | ebd1068e7b9ab47fbd63bfcef0f27c6350dd37255a48b54f234161c91e401ec3 |