UUID-compatible Typed IDs with prefixes
Project description
UUID-compatible Typed IDs with prefixes.
typid
is a python library that allows to create specific id types much like
stripe uses them across their platform to distinguish different data models from
their id already. typeid
is compatible to the UUID library and as such can act
as a drop-in replacement.
A type id has the following string representation:
user_nnv5rr3rfk3hgry6xrygr
└──┘ └───────────────────┘
type uuid (base58)
How to use typeid
First generate the Types using generate(...), then use them like so:
from uuid_extensions import uuid7
from typeid import generate
typeids = generate("sk", "pk", "user", "apikey", enable_sqla=True)
# Now they can be used with SkId, PkId, UserId, and ApikeyId
assert str(typeids.SkId(uuid7().hex)).startswith("sk_")
assert str(typeids.PkId(uuid7().hex)).startswith("pk_")
assert str(typeids.UserId(uuid7().hex)).startswith("user_")
assert str(typeids.ApikeyId(uuid7().hex)).startswith("apikey_")
# Obviously, loading a prefixed type id in the correct Type works just fine
assert (typeids.UserId("user_nnv5rr3rfk3hgry6xrygr").hex == "0664c4135ed83faabd4bc0dc33839c9f")
# Use custom suffixes, just in case you need them
other_ids.generate("admin", suffix="IdUseWithCare")
assert str(other_ids.AdminIdUseWithCare(uuid7().hex)).startswith("admin_")
SqlAlchemy Support
The library also allows to create classes specifically for the use with sqlalchemy.
Example
from typeid import generate
typeids = generate("user", enable_sqla=True)
class User(Base):
__tablename__ = "user"
# use typeids.UserId in the Mapper[]
id: Mapped[typeids.UserId] = mapped_column(
# use typeids.UserIdSQLA in the mapped_column!
typeids.UserIdSQLA, primary_key=True, default=uuid.uuid4
)
name: str = Column(String(128), unique=True)
user = User(name="John Doe")
(db,) = get_session()
db.add(user)
db.commit()
db.refresh(user)
print(type(user.id))
# <class 'typeid.UserId'>
print((user.id))
# user_qvnrsem3skqho2hgni2mfk
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
typeid-0.4.1.tar.gz
(4.8 kB
view details)
Built Distribution
File details
Details for the file typeid-0.4.1.tar.gz
.
File metadata
- Download URL: typeid-0.4.1.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.9-arch1-2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc6fa20282ec947994d638b0b76f169504e3f08dbaf69e0bb63ca87c09efc74c |
|
MD5 | 5563d6941a8edcd6efa75ee2b3aebca3 |
|
BLAKE2b-256 | bf964effc69fb355cc0f008e6f9750e3c1fa690578fbc676fada7b96216a9122 |
File details
Details for the file typeid-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: typeid-0.4.1-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.9-arch1-2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f84c3ddc5f25716ef04260ad04520b355dbbbc6e3327b841033c70a9894e7027 |
|
MD5 | c54c09ecf7aa1692ca78965874aba738 |
|
BLAKE2b-256 | 49b6db661d5d7c229624f498c39611524dcb00625df917b780547db5276d4a7f |