Skip to content

Commit

Permalink
add SQLAlchemy example
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Oct 2, 2023
1 parent e426fad commit 33eba55
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions examples/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from typing import Optional

from sqlalchemy import types
from sqlalchemy.util import generic_repr
from typeid import TypeID, from_uuid


class TypeIDType(types.TypeDecorator):
"""
A SQLAlchemy TypeDecorator that allows storing TypeIDs in the database.
The prefix will not be persisted, instead the database-native UUID field will be used.
At retrieval time a TypeID will be constructed based on the configured prefix and the
UUID value from the database.
Usage:
# will result in TypeIDs such as "user_01h45ytscbebyvny4gc8cr8ma2"
id = mapped_column(
TypeIDType("user"),
primary_key=True,
default=lambda: TypeID("user")
)
"""
impl = types.Uuid

cache_ok = True

prefix: Optional[str]

def __init__(self, prefix: Optional[str], *args, **kwargs):
self.prefix = prefix
super().__init__(*args, **kwargs)

def __repr__(self) -> str:
# Customize __repr__ to ensure that auto-generated code e.g. from alembic includes
# the right __init__ params (otherwise by default prefix will be omitted because
# uuid.__init__ does not have such an argument).
return generic_repr(
self,
to_inspect=TypeID(self.prefix),
)

def process_bind_param(self, value: TypeID, dialect):
if self.prefix is None:
assert value.prefix is None
else:
assert value.prefix == self.prefix

return value.uuid

def process_result_value(self, value, dialect):
return from_uuid(value, self.prefix)

0 comments on commit 33eba55

Please sign in to comment.