From 070ff023a0e1e94bbbd0834e8d610f7c84556d39 Mon Sep 17 00:00:00 2001 From: huangsong Date: Tue, 21 Dec 2021 16:33:50 +0800 Subject: [PATCH] add jsonb in postgresql --- docs/fields/field-types.md | 7 +++++++ ormar/fields/model_fields.py | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/docs/fields/field-types.md b/docs/fields/field-types.md index 99c5409..484208e 100644 --- a/docs/fields/field-types.md +++ b/docs/fields/field-types.md @@ -140,6 +140,13 @@ You can pass `timezone=True` for timezone aware database column. * Sqlalchemy column: `sqlalchemy.JSON` * Type (used for pydantic): `pydantic.Json` +### JSONB + +`JSONB()` has no required parameters, only support it in the postgresql. + +* Sqlalchemy column: `sqlalchemy.dialects.postgresql.JSONB` +* Type (used for pydantic): `pydantic.Json` + ### LargeBinary `LargeBinary(max_length)` has a required `max_length` parameter. diff --git a/ormar/fields/model_fields.py b/ormar/fields/model_fields.py index a819b61..2456455 100644 --- a/ormar/fields/model_fields.py +++ b/ormar/fields/model_fields.py @@ -520,6 +520,29 @@ class JSON(ModelFieldFactory, pydantic.Json): return sqlalchemy.JSON() +class JSONB(JSON): + """ + JSONB field factory that construct Field classes and populated their values, + only support postgresql + """ + + _type = pydantic.Json + _sample = '{"jsonb": "jsonb"}' + + @classmethod + def get_column_type(cls, **kwargs: Any) -> Any: + """ + Return proper type of db column for given field type. + Accepts required and optional parameters that each column type accepts. + + :param kwargs: key, value pairs of sqlalchemy options + :type kwargs: Any + :return: initialized column with proper options + :rtype: sqlalchemy Column + """ + return sqlalchemy.dialects.postgresql.JSONB() + + if TYPE_CHECKING: # pragma: nocover # noqa: C901 @overload