Documentation
¶
Overview ¶
TypeIDs are a draft standard for *type-safe, globally unique identifiers* based on the upcoming UUIDv7 standard. Their properties, particularly k-sortability, make them suitable primary identifiers for classic database systems like PostgreSQL. However, k-sortability may not always be desirable. For instance, you might require an identifier with high randomness entropy for security reasons. Additionally, in distributed database systems like CockroachDB, having a k-sortable primary key can lead to hotspots and performance issues.
While this package draws inspiration from the original typeid Go package (go.jetpack.io/typeid), it provides multiple ID types:
- typeid.Sortable is based on UUIDv7 and is k-sortable. Its implementation adheres to the draft standard. The suffix part is encoded in **lowercase** crockford base32.
- typeid.Random is also based on UUIDv4 and is completely random. Unlike `typeid.Sortable`, the suffix part is encoded in **uppercase** crockford base32.
Please refer to the respective type documentation for more details.
Database Support ¶
ID types in this package can be used with database/sql and github.com/jackc/pgx.
When using the standard library sql, IDs will be stored as their string representation and can be scanned and valued accordingly. When using pgx, both TEXT and UUID columns can be used directly. However, note that the type information is lost when using UUID columns, unless you take additional steps at the database layer. Be mindful of your identifier semantics, especially in complex JOIN queries.
Usage ¶
To create a new ID type, define a prefix type that implements the Prefix interface. Then, define a TypeAlias for your ID type to Random or Sortable with your prefix type as generic argument.
Example:
import "github.com/sumup/typeid"
type UserPrefix struct{}
func (UserPrefix) Prefix() string {
return "user"
}
type UserID = typeid.Sortable[UserPrefix]
userID, err := typeid.New[UserID]()
if err != nil {
fmt.Println("create user id:", err)
}
fmt.Println(userID) // --> user_01hf98sp99fs2b4qf2jm11hse4
Index ¶
- Constants
- Variables
- func FromString[T instance[P], P Prefix](s string) (T, error)
- func FromUUID[T instance[P], P Prefix](u uuid.UUID) (T, error)
- func FromUUIDBytes[T instance[P], P Prefix](bytes []byte) (T, error)
- func FromUUIDStr[T instance[P], P Prefix](uuidStr string) (T, error)
- func Must[T any](tid T, err error) T
- func MustNew[T instance[P], P Prefix]() T
- func New[T instance[P], P Prefix]() (T, error)
- func Nil[T instance[P], P Prefix]() T
- type Prefix
- type Random
- func (r Random[P]) MarshalText() ([]byte, error)
- func (tid Random) Prefix() string
- func (r *Random[P]) Scan(src any) error
- func (r *Random[P]) ScanText(v pgtype.Text) error
- func (r *Random[P]) ScanUUID(v pgtype.UUID) error
- func (r Random[P]) String() string
- func (r Random[P]) TextValue() (pgtype.Text, error)
- func (Random[P]) Type() string
- func (r Random[P]) UUID() uuid.UUID
- func (r Random[P]) UUIDValue() (pgtype.UUID, error)
- func (r *Random[P]) UnmarshalText(text []byte) error
- func (r Random[P]) Value() (driver.Value, error)
- type Sortable
- func (r Sortable[P]) MarshalText() ([]byte, error)
- func (tid Sortable) Prefix() string
- func (s *Sortable[P]) Scan(src any) error
- func (s *Sortable[P]) ScanText(v pgtype.Text) error
- func (s *Sortable[P]) ScanUUID(v pgtype.UUID) error
- func (s Sortable[P]) String() string
- func (s Sortable[P]) TextValue() (pgtype.Text, error)
- func (Sortable[P]) Type() string
- func (r Sortable[P]) UUID() uuid.UUID
- func (s Sortable[P]) UUIDValue() (pgtype.UUID, error)
- func (r *Sortable[P]) UnmarshalText(text []byte) error
- func (s Sortable[P]) Value() (driver.Value, error)
Constants ¶
const (
// MaxPrefixLen is the maximum string length of a [Prefix]. Any generation or parsing of an ID type with a longer prefix will fail.
MaxPrefixLen = 63
)
Variables ¶
var (
ErrParse = errors.New("parse typeid")
)
Functions ¶
func FromString ¶
func FromUUIDBytes ¶
func FromUUIDStr ¶
func Must ¶
Must returns a TypeID if the error is nil, otherwise panics. This is a helper function to ease intialization in tests, etc. For generating a new id, use MustNew
Example:
testID := typeid.Must(typeid.FromString[UserID]("user_01hf98sp99fs2b4qf2jm11hse4"))
func MustNew ¶
func MustNew[T instance[P], P Prefix]() T
MustNew returns a generated TypeID if the error is null, otherwise panics. Equivalent to:
typeid.Must(typeid.New[IDType]())
Types ¶
type Random ¶
type Random[P Prefix] struct { // contains filtered or unexported fields }
Random represents an unique identifier that is entirely random. Internally, it's based on UUIDv4.
func (Random[P]) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface. Internally it use Random.String
func (*Random[P]) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. It parses a TypeID string using FromString
type Sortable ¶
type Sortable[P Prefix] struct { // contains filtered or unexported fields }
Sortable represents an unique identifier that is k-sortable. Internally, it's based on UUIDv7.
func (Sortable[P]) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface. Internally it use Random.String
func (*Sortable[P]) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. It parses a TypeID string using FromString