https://github.com/blackieops/primarykey
🔑 Human-friendly IDs for your Go models.
https://github.com/blackieops/primarykey
database go golang library shortuuid uuid
Last synced: about 2 months ago
JSON representation
🔑 Human-friendly IDs for your Go models.
- Host: GitHub
- URL: https://github.com/blackieops/primarykey
- Owner: blackieops
- License: mit
- Created: 2022-09-24T16:12:35.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-11-03T17:42:48.000Z (over 1 year ago)
- Last Synced: 2025-01-05T17:32:38.402Z (over 1 year ago)
- Topics: database, go, golang, library, shortuuid, uuid
- Language: Go
- Homepage:
- Size: 36.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `go.b8s.dev/primarykey`
[](https://github.com/blackieops/primarykey/actions/workflows/tests.yml)
[](https://goreportcard.com/report/go.b8s.dev/primarykey)
`primarykey` is a replacement for UUID primary keys in your database-driven Go
programs. `primarykey` utilises the [`shortuuid`][1] 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.
[1]: https://github.com/lithammer/shortuuid
## Usage
Use `primarykey.ID` for your ID fields in your model structs:
```go
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:
```go
db.Query("SELECT * FROM my_model WHERE id = $1", myModel.ID)
```
And marshal them to serializable formats like JSON automatically:
```go
b, _ := json.Marshal(&MyModel{ID: primarykey.New()})
//=> {"ID":"KwSysDpxcBU9FNhGkn2dCf"}
```
There is also a public interface to encode and decode directly:
```go
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 .
```