Skip to content

🔑 Human-friendly IDs for your Go models.

License

Notifications You must be signed in to change notification settings

blackieops/primarykey

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go.b8s.dev/primarykey

Test Suite Go Report Card

primarykey is a replacement for UUID primary keys in your database-driven Go programs. primarykey utilises the shortuuid library to generate binary-compatible UUIDs in a more human-friendly format, allowing you to still benefit from native UUID storage in systems like PostgreSQL but while providing less obnoxious IDs to your users.

Usage

Use primarykey.ID for your ID fields in your model structs:

import "go.b8s.dev/primarykey"

type MyModel struct {
	ID primarykey.ID
}

The ID will be passed to the datastore as a regular binary UUID (eg., uuid in PostgreSQL).

You can pass primarykeys directly into queries:

db.Query("SELECT * FROM my_model WHERE id = $1", myModel.ID)

And marshal them to serializable formats like JSON automatically:

b, _ := json.Marshal(&MyModel{ID: primarykey.New()})
//=> {"ID":"KwSysDpxcBU9FNhGkn2dCf"}

There is also a public interface to encode and decode directly:

newOne := primarykey.New()
primarykey.Encode(newOne) //=> "KwSysDpxcBU9FNhGkn2dCf"

id := primarykey.Decode("KwSysDpxcBU9FNhGkn2dCf") //=> ID
id.UUID() //=> uuid.UUID
id.String() //=> "KwSysDpxcBU9FNhGkn2dCf"

Development

This is a very standard Go project with very minimal dependencies and no required setup. Dependencies are vendored.

To run the test suite:

$ go test .