gormt

gormt is a library designed for the Go programming language. It allows to use custom structs as gorm JSON type columns through go generics. Requires Go version 1.18 or higher.

How to use

Given the following Postgres table:

CREATE TABLE letters (
	id  UUID NOT NULL PRIMARY KEY,
	info JSON NOT NULL
);

where info will contains a JSON like:

{ 
  "sender": "Dvor",
  "recipient": "Magic",
  "content": "Hello World"
}

We implement the model as follows:

type Letter struct {
  ID   string `gorm:"primaryKey"`
  Info gormt.JSON[[]*Info]
}

type Info struct {
  Sender      string
  Recipient   string
  Content     string
}

GORM requires the customized data type to implement the Scanner and Valuer interfaces. This way GORM knowns to how to receive/save it into the database. But using this library you get rid of any complication. You just need to add the label gormt.JSON[[]*CustomType] with your custom type and you are good to go!

GitHub

View Github