ormar-postgres-extensions is a an extension to theOrmar ORM. It enables developers to write models that map to native PostgreSQL types.
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.
python -m pip install ormar-postgres-extensions
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
import ormar_postgres_extensions as ormar_pg_ext
class MyModel(ormar.Model):
uuid: UUID = ormar_pg_ext.UUID(unique=True, nullable=False)
import ormar
import ormar_postgres_extensions as ormar_pg_ext
class JSONBTestModel(ormar.Model):
id: int = ormar.Integer(primary_key=True)
data: dict = ormar_pg_ext.JSONB()
Array field requires a bit more setup to pass the type of the array into the field
import ormar
import sqlalchemy
import ormar_postgres_extensions as ormar_pg_ext
class ModelWithArray(ormar.Model):
class Meta:
database = database
metadata = metadata
id: int = ormar.Integer(primary_key=True)
data: list = ormar_pg_ext.ARRAY(item_type=sqlalchemy.String())
Arrays have access to three special methods that map to specific PostgreSQL array functions
The maps to the contained_by
operator in Postgres.
await ModelWithArray.objects.filter(data__array_contained_by=["a"]).all()
The maps to the contains
operator in Postgres.
await ModelWithArray.objects.filter(data__array_contains=["a"]).all()
The maps to the overlap
operator in Postgres.
await ModelWithArray.objects.filter(data__array_overlap=["a"]).all()
pip uninstall ormar-postgres-extensions
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
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!
ormar-postgres-extensions
is licensed under Apache License Version 2.0.