Skip to main content

UUID-compatible Typed IDs with prefixes

Documentation

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")

# 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")

class User(Base):
    __tablename__ = "user"

    # use typeids.UserId in the Mapper[]
    id: Mapped[typeids.UserId] = mapped_column(
        # use typeids.UserId in the mapped_column!
        typeids.UserId, 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

Metadata

Release files for typeid 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for typeid 1.0.0
File Size Uploaded
typeid-1.0.0.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for typeid 1.0.0
File Interpreter ABI Platform
typeid-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.4 kB

Release files / typeid-1.0.0.tar.gz

Download URL typeid-1.0.0.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f23b9def9805b3bcf4c23d2624060ee1d202db3c297896538b1d3012491cff89
BLAKE2b-256 checksum
How to use checksums
3167fca92b4902d9b6317486116845422a8024db0c112a2e0cf7cef18573e099
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.5 Linux/6.10.6-arch1-1

Release files / typeid-1.0.0-py3-none-any.whl

Download URL typeid-1.0.0-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d99aa4cdd805c8d52cfb58cffff20ae45b6c6fc128316729a0de542feab2e893
BLAKE2b-256 checksum
How to use checksums
503b02245193ebb8621b38bb715c5cdce72f76b35298f327a258b934ea4d398a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.5 Linux/6.10.6-arch1-1

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page