Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 68083ee

Browse files
committed
chore(internal): support rendering enum types from the DMMF
This will be helpful for transaction isolation levels support #878
1 parent 3de2b22 commit 68083ee

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

src/prisma/generator/models.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ def to_params(self) -> Dict[str, Any]:
353353
"""Get the parameters that should be sent to Jinja templates"""
354354
params = vars(self)
355355
params['type_schema'] = Schema.from_data(self)
356+
params['client_types'] = ClientTypes.from_data(self)
356357

357358
# add utility functions
358359
for func in [
@@ -628,11 +629,22 @@ def engine_type_validator(cls, value: EngineType) -> EngineType:
628629
assert_never(value)
629630

630631

632+
class EnumType(BaseModel):
633+
name: str
634+
values: List[object]
635+
636+
637+
class EnumTypes(BaseModel):
638+
prisma: List[EnumType]
639+
640+
641+
class PrismaSchema(BaseModel):
642+
enum_types: EnumTypes = FieldInfo(alias='enumTypes')
643+
644+
631645
class DMMF(BaseModel):
632646
datamodel: 'Datamodel'
633-
634-
# TODO
635-
prisma_schema: Any = FieldInfo(alias='schema')
647+
prisma_schema: PrismaSchema = FieldInfo(alias='schema')
636648

637649

638650
class Datamodel(BaseModel):
@@ -1182,4 +1194,4 @@ class DefaultData(GenericData[_EmptyModel]):
11821194
TemplateError,
11831195
PartialTypeGeneratorError,
11841196
)
1185-
from .schema import Schema
1197+
from .schema import Schema, ClientTypes

src/prisma/generator/schema.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from enum import Enum
2-
from typing import Any, Dict, List, Type, Tuple, Union
2+
from typing import Any, Dict, List, Type, Tuple, Union, Optional
33
from typing_extensions import ClassVar
44

55
from pydantic import BaseModel
66

7+
from .utils import to_constant_case
78
from .models import Model as ModelInfo, AnyData, PrimaryKey
89
from .._compat import (
910
PYDANTIC_V2,
@@ -18,6 +19,7 @@ class Kind(str, Enum):
1819
alias = 'alias'
1920
union = 'union'
2021
typeddict = 'typeddict'
22+
enum = 'enum'
2123

2224

2325
class PrismaType(BaseModel):
@@ -45,6 +47,11 @@ class PrismaUnion(PrismaType):
4547
subtypes: List[PrismaType]
4648

4749

50+
class PrismaEnum(PrismaType):
51+
kind: Kind = Kind.enum
52+
members: List[Tuple[str, str]]
53+
54+
4855
class PrismaAlias(PrismaType):
4956
kind: Kind = Kind.alias
5057
to: str
@@ -143,6 +150,28 @@ def order_by(self) -> PrismaType:
143150
return PrismaType.from_subtypes(subtypes, name=f'{model}OrderByInput')
144151

145152

153+
class ClientTypes(BaseModel):
154+
transaction_isolation_level: Optional[PrismaEnum]
155+
156+
@classmethod
157+
def from_data(cls, data: AnyData) -> 'ClientTypes':
158+
enum_types = data.dmmf.prisma_schema.enum_types.prisma
159+
160+
tx_isolation = next((t for t in enum_types if t.name == 'TransactionIsolationLevel'), None)
161+
if tx_isolation is not None:
162+
tx_isolation = PrismaEnum(
163+
name='TransactionIsolationLevel',
164+
members=[
165+
(to_constant_case(str(value)), str(value))
166+
for value in tx_isolation.values
167+
],
168+
)
169+
170+
return cls(
171+
transaction_isolation_level=tx_isolation,
172+
)
173+
174+
146175
model_rebuild(Schema)
147176
model_rebuild(PrismaType)
148177
model_rebuild(PrismaDict)

src/prisma/generator/templates/types.py.jinja

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ from .utils import _NoneType
6565
},
6666
total={{ type.total }}
6767
)
68+
{% elif type.kind == 'enum' %}
69+
class {{ type.name }}(StrEnum):
70+
{% for name, value in type.members %}
71+
{{ name }} = "{{ value }}"
72+
{% endfor %}
6873
{% else %}
6974
{{ raise_err('Unhandled type kind: %s' % type.kind) }}
7075
{% endif %}

0 commit comments

Comments
 (0)