Package: ormar-postgres-extensions
ormar-postgres-extensionsOverviewormar-postgres-extensions is a an extension to theOrmarORM. It enables developers to write models that map to native PostgreSQL types.MotivationOrmaris an amazing async ORM that works withFastAPI. 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.Installationpython-mpipinstallormar-postgres-extensionsUsageFieldsThree native PG fields are provided. TheJSONBandUUIDtypes map to nativeJSONBandUUIDdata types respectively. TheArraytype 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.UUIDfromuuidimportUUIDimportormarimportormar_postgres_extensionsasormar_pg_extclassMyModel(ormar.Model):uuid:UUID=ormar_pg_ext.UUID(unique=True,nullable=False)JSONBimportormarimportormar_postgres_extensionsasormar_pg_extclassJSONBTestModel(ormar.Model):id:int=ormar.Integer(primary_key=True)data:dict=ormar_pg_ext.JSONB()jsonb_contained_byThe maps to thecontainsoperator in Postgres.awaitJSONBTestModel.objects.filter(data__jsonb_contained_by=dict(key="value")).all()jsonb_containsThe maps to thecontainsoperator in Postgres.awaitJSONBTestModel.objects.filter(data__jsonb_contains=dict(key="value")).all()jsonb_has_allThe maps to thehas_alloperator in Postgres.fromsqlalchemy.dialects.postgresqlimportarrayawaitJSONBTestModel.objects.filter(data__jsonb_has_all=array(["key1","key2"])).all()jsonb_has_anyThe maps to thehas_anyoperator in Postgres.fromsqlalchemy.dialects.postgresqlimportarrayawaitJSONBTestModel.objects.filter(data__jsonb_has_any=array(["key1","key2"])).all()jsonb_has_keyThe maps to thehas_keyoperator in Postgres.awaitJSONBTestModel.objects.filter(data__jsonb_has_key="key1").all()ArrayArray field requires a bit more setup to pass the type of the array into the fieldimportormarimportsqlalchemyimportormar_postgres_extensionsasormar_pg_extclassModelWithArray(ormar.Model):classMeta:database=databasemetadata=metadataid: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 functionsarray_contained_byThe maps to thecontained_byoperator in Postgres.awaitModelWithArray.objects.filter(data__array_contained_by=["a"]).all()array_containsThe maps to thecontainsoperator in Postgres.awaitModelWithArray.objects.filter(data__array_contains=["a"]).all()array_overlapThe maps to theoverlapoperator in Postgres.awaitModelWithArray.objects.filter(data__array_overlap=["a"]).all()INET / CIDRfromipaddressimportIPv4Address,IPv6Address,IPv4Interface,IPv6InterfacefromtypingimportUnionimportormarimportormar_postgres_extensionsasormar_pg_extIPAddress=Union[IPv4Address,IPv4Interface,IPv6Address,IPv6Interface,]classINETTestModel(ormar.Model):id:int=ormar.Integer(primary_key=True)inet:IPAddress=ormar_pg_ext.INET()cidr:IPAddress=ormar_pg_ext.CIDR()contained_byThis maps to the<<operatorfromipaddressimportip_interfaceawaitINETTestModel.objects.filter(inet__contained_by=ip_interface("192.168.1.0/24")).all()contained_by_eqThis maps to the<<=operatorfromipaddressimportip_interfaceawaitINETTestModel.objects.filter(inet__contained_by_eq=ip_interface("192.168.1.0/24")).all()contains_subnetThis maps to the>>operatorfromipaddressimportip_interfaceawaitINETTestModel.objects.filter(inet__contains_subnet=ip_interface("192.168.1.0/24")).all()contains_subnet_eqThis maps to the>>=operatorfromipaddressimportip_interfaceawaitINETTestModel.objects.filter(inet__contains_subnet_eq=ip_interface("192.168.1.0/24")).all()contains_or_eqThis maps to the&&operatorfromipaddressimportip_interfaceawaitINETTestModel.objects.filter(inet__contains_or_eq=ip_interface("192.168.1.0/24")).all()MACADDRfromipaddressimportIPv4Address,IPv6Address,IPv4Interface,IPv6InterfacefromtypingimportUnionimportormarimportormar_postgres_extensionsasormar_pg_extclassMacAddrTestModel(ormar.Model):id:int=ormar.Integer(primary_key=True)addr:str=ormar_pg_ext.MACADDR()Uninstallingpipuninstallormar-postgres-extensionsContributingFeel free to open a PR or GitHub issue. Contributions welcome!To develop locally, clone this repository and run. script/bootstrapto install test dependencies. You can then useinvoke --listto see available commands.
To run the tests locally, PostgreSQL needs to be running. This can be easily started viainv database.See contributingguideContributorsYou don't really have to add this section yourself! Simply useall-contributorsby 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!Licenseormar-postgres-extensionsis licensed underApache License Version 2.0.