-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make compatible to SQLAlchemy and add example
- Loading branch information
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import Optional | ||
|
||
from sqlalchemy import types | ||
from typeid import TypeID, from_uuid | ||
|
||
|
||
class TypeIDType(types.TypeDecorator): | ||
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 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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters