ouid

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2025 License: MIT Imports: 12 Imported by: 1

README

OUID - UUID based on ObjectID For Go

What & Why

MongoDB's ObjectID is a sortable unique identifier. It uses 12 bytes to store data, including a timestamp, machine identifier, process ID, and a counter.

Its hex string is more compact and readable than a UUID. However, if you directly use this string as the primary key in relational databases (Postgres, MySQL, SQLite, etc.), it can lead to index bloat and performance degradation.

Almost all relational databases natively support the UUID type, and when used as a primary key, it offers better distribution and performance characteristics compared to character types.

Coincidentally, a UUID requires 16 bytes of storage, which is 4 bytes more than an ObjectID. Therefore, we can generate a UUID by appending 4 bytes of data to an ObjectID. This way, we keep the benefits of ObjectID while gaining the performance advantages of UUID in relational databases.

OUID (ObjectUUID) is a UUID type designed based on this idea: it appends 4 bytes of empty data to the tail of an ObjectID, forming a 16-byte identifier that conforms to the UUID standard.

OUID implements the Scanner and Valuer interfaces from database/sql, so it can be used directly for read and write operations in relational databases.

In the database, it is stored as a UUID type, like this:

693a436b-4218-921b-4e0d-1e2100000000

In the application, it is represented by default as the hex string form of the ObjectID:

693a436b4218921b4e0d1e21

If needed, you can also call the OUID.UUID() method to obtain the standard UUID string representation.

If you are migrating from MongoDB to a relational database, or you want to use an ObjectID-like identifier in a relational database, OUID is a good choice.

Installation

go get github.com/unvgo/ouid

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidHex = errors.New("the provided hex string is not a valid ObjectID")

ErrInvalidHex indicates that a hex string cannot be converted to an ObjectID.

View Source
var MarshalJSON = func(val any) ([]byte, error) {
	return json.Marshal(val)
}
View Source
var NilObjectID objectID

NilObjectID is the zero value for ObjectID.

View Source
var UnmarshalJSON = func(data []byte, val any) error {
	return json.Unmarshal(data, val)
}

Functions

func Validate

func Validate(s string) error

Types

type OUID

type OUID uuid.UUID

OUID will transform MongoDB's ObjectID (12 bytes) into UUID format (16 bytes). The last 4 bytes are always zero.

var (
	Nil OUID
)

func FromObjectID

func FromObjectID(oid objectID) OUID

FromObjectID converts MongoDB's ObjectID to OUID

func FromObjectIDHex

func FromObjectIDHex(oidHex string) (OUID, error)

func FromUUIDString

func FromUUIDString(s string) (OUID, error)

FromUUIDString creates OUID from a UUID string

The UUID string must be a valid OUID (the last 4 bytes are zero).

func New

func New() OUID

func (OUID) Hex

func (cu OUID) Hex() string

Hex returns objectID hex string

func (OUID) IsZero

func (cu OUID) IsZero() bool

func (OUID) MarshalJSON

func (cu OUID) MarshalJSON() ([]byte, error)

MarshalJSON outputs OUID as a JSON string (ObjectID hex).

func (OUID) MarshalText

func (cu OUID) MarshalText() ([]byte, error)

MarshalText returns the OUID as text (hex of ObjectID part).

func (*OUID) Scan

func (c *OUID) Scan(value any) error

Scan implements sql.Scanner (for reading from DB)

func (OUID) String

func (c OUID) String() string

String returns the OUID as a string (same as Hex).

func (OUID) ToObjectID

func (cu OUID) ToObjectID() objectID

ToObjectID converts OUID back to MongoDB's ObjectID

func (OUID) UUID

func (cu OUID) UUID() uuid.UUID

UUID returns the OUID as a uuid.UUID.

func (*OUID) UnmarshalJSON

func (cu *OUID) UnmarshalJSON(b []byte) error

UnmarshalJSON parses JSON into OUID. Supports:

  • null → leave unchanged
  • "507f1f77bcf86cd799439011" (24 hex chars)
  • {"$oid": "..."} (Mongo Extended JSON)
  • "" → Nil

func (*OUID) UnmarshalText

func (cu *OUID) UnmarshalText(b []byte) error

UnmarshalText parses OUID from hex text.

func (OUID) Value

func (c OUID) Value() (driver.Value, error)

Value implements driver.Valuer (for writing into DB)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL