Integrating GORM uuid implementation using new binary(16)
uuid implementation available in mysql version 8
.
- Wraps
uuid.UUID
inside and converts into binary when needed. - Implements Gorm
Scan
andValue
for data type conversion. - Implements
GormDataType
for data type in sql. MarshalJSON
andUnmarshalJSON
implementation for json encode/decode.
BeforeCreate
hook for creating new uuids whenever new data has to be inserted.
// BeforeCreate ->
func (t *Test) BeforeCreate(tx *gorm.DB) error {
id, err := uuid.NewRandom()
t.ID = BinaryUUID(id)
return err
}
model := Test{}
db.Find(&model, "id = ?", ParseUUID("ed67a4b2-8f77-4d18-8c58-0508e7b207e8"))
log.Printf("data: %+v\n", model)
Will autogenerate uuid automatically because of BeforeCreate
hook added
data := Test{Name: fmt.Sprintf("Time is: %s", time.Now())}
db.Create(&data)
Check this article for more information