Skip to main content

Pretty cool IDs for your APIs

Project description

PrettyId

Pretty cool IDs for your APIs.

poetry add prettyid

Synopsis

Generate prefixed IDs, like Stripe does:

>>> from pretty_id import PrettyId
>>> pid = PrettyId.random("task")
PrettyId("task_068n34jrjdth1fqr2nm9a0sh50")
>>> pid.type, pid.id
('task', '068n34jrjdth1fqr2nm9a0sh50')
>>> pid.bytes
b'...'

Backwards compatible with UUID:

>>> pid = PrettyId.from_uuid("1d4e2ea4-c1ab-4a98-8eeb-898051ef0f71", type="task")
PrettyId("task_3n72x961nd59h3qbh6053vrfe4")
>>> pid.uuid
UUID('1d4e2ea4-c1ab-4a98-8eeb-898051ef0f71')

Design

Generated IDs use UUIDv7 underneath:

>>> u = UUID(bytes=pid.bytes)
UUID('01917192-7b9b-7b10-bf5b-15ec95039128')
>>> u.version
7

This means they inherit some useful properties:

  • Natural Sorting: UUIDv7 values are time-sortable, which means you can sort them in increasing order based on when they were generated. Databases often require additional timestamp columns to sort records based on creation time. With PrettyId, you can achieve this sorting using the ID itself, eliminating the need for extra columns.

  • Optimized Indexing: Since UUIDv7 is time-sortable, database indexing mechanisms can better optimize the storage and retrieval processes, leading to faster query times especially for time-based queries.

  • Concurrency and Distribution: In distributed systems, generating unique, sequential IDs can be a challenge. UUIDv7 can be generated concurrently across multiple nodes without the risk of collisions, making it suitable for distributed architectures.

IDs are encoded as lowercase Base32 using Douglas Crockford’s alphabet. This makes them compact, readable, and case-insensitive.

In database, IDs are stored without prefix using a native UUID type or BINARY(16). (We assume that the prefix can be determined from the table name.)

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

prettyid-0.1.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

prettyid-0.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file prettyid-0.1.0.tar.gz.

File metadata

  • Download URL: prettyid-0.1.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.9.12-401.asahi.fc40.aarch64+16k

File hashes

Hashes for prettyid-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f5a45042ad38438ed2be1da83dd04566e08aef54f0c94d8657533dcd7f9fb41b
MD5 1e9031d19ce570fb108149bd05368b2a
BLAKE2b-256 d082fef7c9c531344b0b0f7c5853a9d7d2d8001531ba7259c93539bf6ff56a55

See more details on using hashes here.

File details

Details for the file prettyid-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: prettyid-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.9.12-401.asahi.fc40.aarch64+16k

File hashes

Hashes for prettyid-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 521f002efe2ca1bb464d223a8fcb009e647804b2df5e6eb809066ed4bb4d4f4d
MD5 866acaf110a1eba4bc855ca6f78f2116
BLAKE2b-256 b8f0fa76ea3c4a2f01ca94f7f4f8e2737531ff9a1ed29233be5fb39842fd35d6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page