It's boring to repeatedly create a BeforeCreate hook for each model to generate nanoid automatically.
So I made a simple and lighweight nanoid data type and hope it could be contributed.
User Case Description
import "gorm.io/datatypes"
type UserWithNanoID struct {
ID datatypes.NanoID `gorm:"nanoid" json:"id"`
Name string
}
//Create
user := UserWithNanoID{Name: "nkvi.dev"}
DB.Create(&user)
//Query
result := UserWithNanoID{}
if err := DB.First(&result, "id = ?", user.ID).Error; err != nil {
t.Fatalf("Failed to find user with id")
}
What did this pull request do?
BeforeCreate
hook for each model to generatenanoid
automatically.nanoid
data type and hope it could be contributed.User Case Description