GORM provides few interfaces that allow users to define well-supported customized data types for GORM, takes json as an example

Implements Customized Data Type

Scanner / Valuer

The customized data type has to implement the Scanner and Valuer interfaces, so GORM knowns to how to receive/save it into the database

For example:

  1. type JSON json.RawMessage
  2. // Scan scan value into Jsonb, implements sql.Scanner interface
  3. func (j *JSON) Scan(value interface{}) error {
  4. bytes, ok := value.([]byte)
  5. if !ok {
  6. return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
  7. }
  8. result := json.RawMessage{}
  9. err := json.Unmarshal(bytes, &result)
  10. *j = JSON(result)
  11. return err
  12. }
  13. // Value return json value, implement driver.Valuer interface
  14. func (j JSON) Value() (driver.Value, error) {
  15. if len(j) == 0 {
  16. return nil, nil
  17. }
  18. return json.RawMessage(j).MarshalJSON()
  19. }

There are many third party packages implement the Scanner/Valuer interface, which can be used with GORM together, for example:

  1. import (
  2. "github.com/google/uuid"
  3. "github.com/lib/pq"
  4. )
  5. type Post struct {
  6. ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4()"`
  7. Title string
  8. Tags pq.StringArray `gorm:"type:text[]"`
  9. }

GormDataTypeInterface

GORM will read column’s database type from tag type, if not found, will check if the struct implemented interface GormDBDataTypeInterface or GormDataTypeInterface and will use its result as data type

  1. type GormDataTypeInterface interface {
  2. GormDataType() string
  3. }
  4. type GormDBDataTypeInterface interface {
  5. GormDBDataType(*gorm.DB, *schema.Field) string
  6. }

The result of GormDataType will be used as the general data type and can be obtained from schema.Field‘s field DataType, which might be helpful when writing plugins or hooks for example:

  1. func (JSON) GormDataType() string {
  2. return "json"
  3. }
  4. type User struct {
  5. Attrs JSON
  6. }
  7. func (user User) BeforeCreate(tx *gorm.DB) {
  8. field := tx.Statement.Schema.LookUpField("Attrs")
  9. if field.DataType == "json" {
  10. // do something
  11. }
  12. }

GormDBDataType usually returns the right data type for current driver when migrating, for example:

  1. func (JSON) GormDBDataType(db *gorm.DB, field *schema.Field) string {
  2. // use field.Tag, field.TagSettings gets field's tags
  3. // checkout https://github.com/go-gorm/gorm/blob/master/schema/field.go for all options
  4. // returns different database type based on driver name
  5. switch db.Dialector.Name() {
  6. case "mysql", "sqlite":
  7. return "JSON"
  8. case "postgres":
  9. return "JSONB"
  10. }
  11. return ""
  12. }

If the struct hasn’t implemented the GormDBDataTypeInterface or GormDataTypeInterface interface, GORM will guess its data type from the struct’s first field, for example, will use string for NullString

  1. type NullString struct {
  2. String string // use the first field's data type
  3. Valid bool
  4. }
  5. type User struct {
  6. Name NullString // data type will be string
  7. }

GormValuerInterface

GORM provides a GormValuerInterface interface, which can allow to create/update from SQL Expr or value based on context, for example:

  1. // GORM Valuer interface
  2. type GormValuerInterface interface {
  3. GormValue(ctx context.Context, db *gorm.DB) clause.Expr
  4. }

Create/Update from SQL Expr

  1. type Location struct {
  2. X, Y int
  3. }
  4. func (loc Location) GormDataType() string {
  5. return "geometry"
  6. }
  7. func (loc Location) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
  8. return clause.Expr{
  9. SQL: "ST_PointFromText(?)",
  10. Vars: []interface{}{fmt.Sprintf("POINT(%d %d)", loc.X, loc.Y)},
  11. }
  12. }
  13. // Scan implements the sql.Scanner interface
  14. func (loc *Location) Scan(v interface{}) error {
  15. // Scan a value into struct from database driver
  16. }
  17. type User struct {
  18. ID int
  19. Name string
  20. Location Location
  21. }
  22. db.Create(&User{
  23. Name: "jinzhu",
  24. Location: Location{X: 100, Y: 100},
  25. })
  26. // INSERT INTO `users` (`name`,`point`) VALUES ("jinzhu",ST_PointFromText("POINT(100 100)"))
  27. db.Model(&User{ID: 1}).Updates(User{
  28. Name: "jinzhu",
  29. Location: Location{X: 100, Y: 100},
  30. })
  31. // UPDATE `user_with_points` SET `name`="jinzhu",`location`=ST_PointFromText("POINT(100 100)") WHERE `id` = 1

You can also create/update with SQL Expr from map, checkout Create From SQL Expr and Update with SQL Expression for details

Value based on Context

If you want to create or update a value depends on current context, you can also implements the GormValuerInterface interface, for example:

  1. type EncryptedString struct {
  2. Value string
  3. }
  4. func (es EncryptedString) GormValue(ctx context.Context, db *gorm.DB) (expr clause.Expr) {
  5. if encryptionKey, ok := ctx.Value("TenantEncryptionKey").(string); ok {
  6. return clause.Expr{SQL: "?", Vars: []interface{}{Encrypt(es.Value, encryptionKey)}}
  7. } else {
  8. db.AddError(errors.New("invalid encryption key"))
  9. }
  10. return
  11. }

Clause Expression

If you want to build some query helpers, you can make a struct that implements the clause.Expression interface:

  1. type Expression interface {
  2. Build(builder Builder)
  3. }

Checkout JSON and SQL Builder for details, the following is an example of usage:

  1. // Generates SQL with clause Expression
  2. db.Find(&user, datatypes.JSONQuery("attributes").HasKey("role"))
  3. db.Find(&user, datatypes.JSONQuery("attributes").HasKey("orgs", "orga"))
  4. // MySQL
  5. // SELECT * FROM `users` WHERE JSON_EXTRACT(`attributes`, '$.role') IS NOT NULL
  6. // SELECT * FROM `users` WHERE JSON_EXTRACT(`attributes`, '$.orgs.orga') IS NOT NULL
  7. // PostgreSQL
  8. // SELECT * FROM "user" WHERE "attributes"::jsonb ? 'role'
  9. // SELECT * FROM "user" WHERE "attributes"::jsonb -> 'orgs' ? 'orga'
  10. db.Find(&user, datatypes.JSONQuery("attributes").Equals("jinzhu", "name"))
  11. // MySQL
  12. // SELECT * FROM `user` WHERE JSON_EXTRACT(`attributes`, '$.name') = "jinzhu"
  13. // PostgreSQL
  14. // SELECT * FROM "user" WHERE json_extract_path_text("attributes"::json,'name') = 'jinzhu'

Customized Data Types Collections

We created a Github repo for customized data types collections https://github.com/go-gorm/datatypes, pull request welcome ;)