Skip to content

Commit

Permalink
Dev (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
redjax authored Jan 26, 2024
2 parents 6e9be2c + 88460bd commit 6b9dc1e
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/red_utils/ext/sqlalchemy_utils/custom_types/type_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ class SomeModel(Base):
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.engine.interfaces import Dialect
import sqlalchemy.orm as so
from sqlalchemy.types import CHAR, TypeDecorator
from sqlalchemy.types import CHAR
from sqlalchemy import TypeDecorator, types

import json



class CompatibleUUID(TypeDecorator):
"""Define a custom UUID, overriding SQLAlchemy's UUId type.
Expand Down Expand Up @@ -81,3 +86,26 @@ def process_result_value(self, value: Any | None, dialect: Dialect) -> Any | Non
if not isinstance(value, uuid.UUID):
value = uuid.UUID(value)
return value



class CustomJSON(TypeDecorator):
"""Class to handle storing JSON in a database."""

@property
def python_type(self):
return object

impl = types.String

def process_bind_param(self, value, dialect):
return json.dumps(value)

def process_literal_param(self, value, dialect):
return value

def process_result_value(self, value, dialect):
try:
return json.loads(value)
except (ValueError, TypeError):
return None

0 comments on commit 6b9dc1e

Please sign in to comment.