Skip to content

Commit

Permalink
feat(client): add root support for filtering by None (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertCraigie authored Apr 12, 2022
1 parent 6a72543 commit 7c6e796
Show file tree
Hide file tree
Showing 18 changed files with 718 additions and 233 deletions.
2 changes: 2 additions & 0 deletions src/prisma/generator/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,8 @@ def where_input_type(self) -> str:
return f"'types.{typ}ListFilter'"

if typ in FILTER_TYPES:
if self.is_optional:
return f"Union[None, {self._actual_python_type}, 'types.{typ}Filter']"
return f"Union[{self._actual_python_type}, 'types.{typ}Filter']"

return self.python_type
Expand Down
7 changes: 5 additions & 2 deletions src/prisma/generator/templates/fields.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ class Base64:
@classmethod
def _internal_from_prisma(
cls,
value: Union[str, 'Base64', List[Union[str, 'Base64']]]
) -> Union['Base64', List['Base64']]:
value: Union[None, str, 'Base64', List[Union[str, 'Base64']]]
) -> Union[None, 'Base64', List['Base64']]:
if value is None:
return None

if isinstance(value, Base64):
return value

Expand Down
2 changes: 1 addition & 1 deletion src/prisma/generator/templates/models.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class {{ model.name }}(BaseModel):
{% for field in model.get_fields_of_type('Bytes') %}
@validator('{{ field.name }}', pre=True, allow_reuse=True)
@classmethod
def _prisma_{{ field.name }}_bytes_validator(cls, value: Any) -> Union[fields.Base64, List[fields.Base64]]:
def _prisma_{{ field.name }}_bytes_validator(cls, value: Any) -> Union[None, fields.Base64, List[fields.Base64]]:
return fields.Base64._internal_from_prisma(value)
{% endfor %}

Expand Down
28 changes: 19 additions & 9 deletions tests/data/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,23 @@ model Profile {

// model that just exists for testing different schema types
model Types {
id Int @id @default(autoincrement())
bool_ Boolean @default(false)
string String @default("")
bytes Bytes @default("")
bigint BigInt @default(0)
integer Int @default(0)
float_ Float @default(0)
datetime DateTime @default(now())
decimal Decimal @default(1)
id Int @id @default(autoincrement())
bool_ Boolean @default(false)
string String @default("")
bytes Bytes @default("")
bigint BigInt @default(0)
integer Int @default(0)
float_ Float @default(0)
datetime_ DateTime @default(now())
decimal_ Decimal @default(1)
optional_int Int? @default(0)
optional_bool Boolean? @default(false)
optional_string String? @default("")
optional_bytes Bytes? @default("")
optional_bigint BigInt? @default(0)
optional_integer Int? @default(0)
optional_float Float? @default(0)
optional_datetime DateTime? @default(now())
optional_decimal Decimal? @default(1)
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,11 @@ class Base64:
@classmethod
def _internal_from_prisma(
cls,
value: Union[str, 'Base64', List[Union[str, 'Base64']]]
) -> Union['Base64', List['Base64']]:
value: Union[None, str, 'Base64', List[Union[str, 'Base64']]]
) -> Union[None, 'Base64', List['Base64']]:
if value is None:
return None

if isinstance(value, Base64):
return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ class Lists(BaseModel):

@validator('bytes', pre=True, allow_reuse=True)
@classmethod
def _prisma_bytes_bytes_validator(cls, value: Any) -> Union[fields.Base64, List[fields.Base64]]:
def _prisma_bytes_bytes_validator(cls, value: Any) -> Union[None, fields.Base64, List[fields.Base64]]:
return fields.Base64._internal_from_prisma(value)

class A(BaseModel):
Expand Down Expand Up @@ -1455,7 +1455,7 @@ class D(BaseModel):

@validator('binary', pre=True, allow_reuse=True)
@classmethod
def _prisma_binary_bytes_validator(cls, value: Any) -> Union[fields.Base64, List[fields.Base64]]:
def _prisma_binary_bytes_validator(cls, value: Any) -> Union[None, fields.Base64, List[fields.Base64]]:
return fields.Base64._internal_from_prisma(value)

class E(BaseModel):
Expand Down
Loading

0 comments on commit 7c6e796

Please sign in to comment.