From c73c2948cee57e3a51c6eab0c547bfc37db6fc05 Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Sun, 8 Aug 2021 12:30:01 -0400 Subject: [PATCH] breaking: Rename PostgresJSONB to JSONB and PostgresUUID to UUID --- README.md | 2 +- src/ormar_postgres_extensions/__init__.py | 4 +- .../fields/__init__.py | 4 +- src/ormar_postgres_extensions/fields/array.py | 37 +++++++++++++++++++ src/ormar_postgres_extensions/fields/jsonb.py | 2 +- src/ormar_postgres_extensions/fields/uuid.py | 2 +- tests/fields/test_jsonb.py | 6 +-- tests/fields/test_uuid.py | 6 +-- 8 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 src/ormar_postgres_extensions/fields/array.py diff --git a/README.md b/README.md index 681e545..14ac187 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ python -m pip install ormar-postgres-extensions ### Fields -Two native PG fields are provided. The `PostgresJSONB` and `PostgresUUID` types map to native `JSONB` and `UUID` data types respectively. Using these in an Ormar model is as simple as importing the fields and using them in the model. +Two native PG fields are provided. The `JSONB` and `UUID` types map to native `JSONB` and `UUID` data types respectively. Using these in an Ormar model is as simple as importing the fields and using them in the model. ```python from uuid import UUID diff --git a/src/ormar_postgres_extensions/__init__.py b/src/ormar_postgres_extensions/__init__.py index d8aba2f..020338b 100644 --- a/src/ormar_postgres_extensions/__init__.py +++ b/src/ormar_postgres_extensions/__init__.py @@ -1,4 +1,4 @@ from .fields import ( # noqa: F401 - PostgresJSONB, - PostgresUUID, + JSONB, + UUID, ) diff --git a/src/ormar_postgres_extensions/fields/__init__.py b/src/ormar_postgres_extensions/fields/__init__.py index cb31770..ce68cae 100644 --- a/src/ormar_postgres_extensions/fields/__init__.py +++ b/src/ormar_postgres_extensions/fields/__init__.py @@ -1,2 +1,2 @@ -from .jsonb import PostgresJSONB # noqa: F401 -from .uuid import PostgresUUID # noqa: F401 +from .jsonb import JSONB # noqa: F401 +from .uuid import UUID # noqa: F401 diff --git a/src/ormar_postgres_extensions/fields/array.py b/src/ormar_postgres_extensions/fields/array.py new file mode 100644 index 0000000..0d499e4 --- /dev/null +++ b/src/ormar_postgres_extensions/fields/array.py @@ -0,0 +1,37 @@ +from typing import Any + +import ormar +from sqlalchemy.dialects import postgresql +from sqlalchemy.types import TypeDecorator + + +class PostgresArrayTypeDecorator(TypeDecorator): + def __init__(self, item_type, *args, **kwargs): + super().__init__(*args, **kwargs) + self._impl = postgresql.ARRAY(item_type) + + @property + def impl(self): + return self._impl + + +class PostgresArray(ormar.fields.model_fields.ModelFieldFactory, list): + _type = list + _sample = [] + + def __new__( # type: ignore + cls, *, item_type, **kwargs: Any + ) -> ormar.fields.BaseField: + kwargs = { + **kwargs, + **{ + k: v + for k, v in locals().items() + if k not in ["cls", "__class__", "kwargs"] + }, + } + return super().__new__(cls, **kwargs) + + @classmethod + def get_column_type(cls, **kwargs: Any) -> PostgresArrayTypeDecorator: + return PostgresArrayTypeDecorator(item_type=kwargs["item_type"]) diff --git a/src/ormar_postgres_extensions/fields/jsonb.py b/src/ormar_postgres_extensions/fields/jsonb.py index be234f1..9e70085 100644 --- a/src/ormar_postgres_extensions/fields/jsonb.py +++ b/src/ormar_postgres_extensions/fields/jsonb.py @@ -9,7 +9,7 @@ class PostgresJSONBTypeDecorator(TypeDecorator): impl = postgresql.JSONB -class PostgresJSONB(ormar.JSON): +class JSONB(ormar.JSON): """ Custom JSON field uses a native PG JSONB type """ diff --git a/src/ormar_postgres_extensions/fields/uuid.py b/src/ormar_postgres_extensions/fields/uuid.py index 9e7b763..a6b614f 100644 --- a/src/ormar_postgres_extensions/fields/uuid.py +++ b/src/ormar_postgres_extensions/fields/uuid.py @@ -39,7 +39,7 @@ def process_result_value( return value -class PostgresUUID(ormar.UUID): +class UUID(ormar.UUID): """ Custom UUID field for the schema that uses a native PG UUID type """ diff --git a/tests/fields/test_jsonb.py b/tests/fields/test_jsonb.py index 54a37a5..2aaed8a 100644 --- a/tests/fields/test_jsonb.py +++ b/tests/fields/test_jsonb.py @@ -4,7 +4,7 @@ import ormar import pytest -from ormar_postgres_extensions.fields import PostgresJSONB +from ormar_postgres_extensions.fields import JSONB from tests.database import ( database, metadata, @@ -17,7 +17,7 @@ class Meta: metadata = metadata id: int = ormar.Integer(primary_key=True) - data: dict = PostgresJSONB() + data: dict = JSONB() class NullableJSONBTestModel(ormar.Model): @@ -26,7 +26,7 @@ class Meta: metadata = metadata id: int = ormar.Integer(primary_key=True) - data: Optional[dict] = PostgresJSONB(nullable=True) + data: Optional[dict] = JSONB(nullable=True) @pytest.mark.asyncio diff --git a/tests/fields/test_uuid.py b/tests/fields/test_uuid.py index bb61363..3f614e7 100644 --- a/tests/fields/test_uuid.py +++ b/tests/fields/test_uuid.py @@ -7,7 +7,7 @@ import ormar import pytest -from ormar_postgres_extensions.fields import PostgresUUID +from ormar_postgres_extensions.fields import UUID as UUIDField from tests.database import ( database, metadata, @@ -20,7 +20,7 @@ class Meta: metadata = metadata id: int = ormar.Integer(primary_key=True) - uid: UUID = PostgresUUID(default=uuid4) + uid: UUID = UUIDField(default=uuid4) class NullableUUIDTestModel(ormar.Model): @@ -29,7 +29,7 @@ class Meta: metadata = metadata id: int = ormar.Integer(primary_key=True) - uid: Optional[UUID] = PostgresUUID(nullable=True) + uid: Optional[UUID] = UUIDField(nullable=True) @pytest.mark.asyncio