gormuuid

package module
v0.1.1-0...-357841f Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2020 License: MIT Imports: 2 Imported by: 1

README

gormuuid

GoDoc

Provides an embeddable struct to provide a UUID primary key in GORM models. For more information, see the godoc.

Documentation

Overview

Package gormuuid provides a type that can be embedded in a GORM model (http://gorm.io) to provide support for UUID primary keys.

While some databases (ie postgres) have native support for UUIDs, for ease of use and interoperability the column type is a byte slice.

Usage:

package foo

import (
    "github.com/exlibris-fed/gormuuid"
)

type Person struct {
    gormuuid.UUID
    Name string
    // etc
}

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrorNoUUID is the error returned when you attempt to get a UUID when it hasn't been generated.
	ErrorNoUUID = errors.New("UUID has not been created")
)

Functions

This section is empty.

Types

type UUID

type UUID struct {
	ID []byte `gorm:"primary key"`
}

A UUID is a struct that can be embedded to add UUID primary key support to a GORM model.

func (*UUID) BeforeCreate

func (u *UUID) BeforeCreate() (err error)

BeforeCreate ensures that a model has a valid UUID before insertion into the database. If one exists already (ie your implementation needed to specify one) it will be respected.

func (*UUID) UUID

func (u *UUID) UUID() (uuid.UUID, error)

UUID returns the UUID of the model's ID.

As it returns a uuid.UUID object, you can then call any of that package's methods. (see https://godoc.org/github.com/google/uuid). For example, to use as a string:

c := Person{
    Name: "Frank",
}
uuid, _ := p.UUID() // don't ignore errors!
id := uuid.String()

Jump to

Keyboard shortcuts

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