GORM Models
go
Generate Go structs with GORM struct tags from SQL schema
Example Output
Generated from the pet-store sample schema:
go
// Generated by @sqldoc/templates/gorm -- DO NOT EDIT
package models
import (
"encoding/json"
"time"
)
type Adoption struct {
Id int32 `gorm:"primaryKey;type:serial;not null;autoIncrement" json:"id,omitempty"`
PetId int32 `gorm:"type:integer;not null;foreignKey:id" json:"pet_id,omitempty"`
OwnerId int32 `gorm:"type:integer;not null;foreignKey:id" json:"owner_id,omitempty"`
AdoptedAt time.Time `gorm:"type:timestamp without time zone;not null;default:now()" json:"adopted_at,omitempty"`
AdoptionFee string `gorm:"type:numeric;not null;default:0" json:"adoption_fee,omitempty"`
}
type AdoptionsAuditLog struct {
Id int64 `gorm:"primaryKey;type:bigserial;not null;autoIncrement" json:"id,omitempty"`
TableName string `gorm:"type:text;not null" json:"table_name,omitempty"`
Operation string `gorm:"type:text;not null" json:"operation,omitempty"`
OldData *json.RawMessage `gorm:"type:jsonb" json:"old_data,omitempty"`
NewData *json.RawMessage `gorm:"type:jsonb" json:"new_data,omitempty"`
ChangedAt time.Time `gorm:"type:timestamp with time zone;not null;default:now()" json:"changed_at,omitempty"`
}
type Category struct {
Id int32 `gorm:"primaryKey;type:serial;not null;autoIncrement" json:"id,omitempty"`
Name string `gorm:"type:character varying;not null" json:"name,omitempty"`
Description *string `gorm:"type:text" json:"description,omitempty"`
}
type LegacyInventory struct {
Id int32 `gorm:"primaryKey;type:serial;not null;autoIncrement" json:"id,omitempty"`
ItemName *string `gorm:"type:character varying" json:"item_name,omitempty"`
OldSku *string `gorm:"type:character varying" json:"old_sku,omitempty"`
Quantity *int32 `gorm:"type:integer;default:0" json:"quantity,omitempty"`
}
type Location struct {
Id int32 `gorm:"primaryKey;type:serial;not null;autoIncrement" json:"id,omitempty"`
Name string `gorm:"type:character varying;not null" json:"name,omitempty"`
Address string `gorm:"type:text;not null" json:"address,omitempty"`
City string `gorm:"type:character varying;not null" json:"city,omitempty"`
Zip *string `gorm:"type:character varying" json:"zip,omitempty"`
}
type MedicalRecord struct {
Id int32 `gorm:"primaryKey;type:serial;not null;autoIncrement" json:"id,omitempty"`
PetId int32 `gorm:"type:integer;not null;foreignKey:id" json:"pet_id,omitempty"`
VisitDate time.Time `gorm:"type:date;not null;default:CURRENT_DATE" json:"visit_date,omitempty"`
Diagnosis string `gorm:"type:text;not null" json:"diagnosis,omitempty"`
Treatment *string `gorm:"type:text" json:"treatment,omitempty"`
VetName *string `gorm:"type:character varying" json:"vet_name,omitempty"`
}
type Owner struct {
Id int32 `gorm:"primaryKey;type:serial;not null;autoIncrement" json:"id,omitempty"`
Name string `gorm:"type:character varying;not null" json:"name,omitempty"`
Email string `gorm:"type:character varying;not null" json:"email,omitempty"`
Phone *string `gorm:"type:character varying" json:"phone,omitempty"`
CreatedAt *time.Time `gorm:"type:timestamp without time zone;default:now()" json:"created_at,omitempty"`
}
type Pet struct {
Id int32 `gorm:"primaryKey;type:serial;not null;autoIncrement" json:"id,omitempty"`
CategoryId *int32 `gorm:"type:integer;foreignKey:id" json:"category_id,omitempty"`
Name string `gorm:"type:character varying;not null" json:"name,omitempty"`
Sku string `gorm:"type:character varying;not null" json:"sku,omitempty"`
Price string `gorm:"type:numeric;not null;default:0" json:"price,omitempty"`
InternalNotes *string `gorm:"type:text" json:"internal_notes,omitempty"`
Status string `gorm:"type:character varying;not null;default:available" json:"status,omitempty"`
CreatedAt *time.Time `gorm:"type:timestamp without time zone;default:now()" json:"created_at,omitempty"`
}
type Review struct {
Id int32 `gorm:"primaryKey;type:serial;not null;autoIncrement" json:"id,omitempty"`
PetId int32 `gorm:"type:integer;not null" json:"pet_id,omitempty"`
OwnerId int32 `gorm:"type:integer;not null" json:"owner_id,omitempty"`
Rating int32 `gorm:"type:integer;not null" json:"rating,omitempty"`
Body *string `gorm:"type:text" json:"body,omitempty"`
LocationId *int32 `gorm:"type:integer;foreignKey:id" json:"location_id,omitempty"`
CreatedAt *time.Time `gorm:"type:timestamp without time zone;default:now()" json:"created_at,omitempty"`
}
type StaffAuditLog struct {
Id int64 `gorm:"primaryKey;type:bigserial;not null;autoIncrement" json:"id,omitempty"`
TableName string `gorm:"type:text;not null" json:"table_name,omitempty"`
Operation string `gorm:"type:text;not null" json:"operation,omitempty"`
OldData *json.RawMessage `gorm:"type:jsonb" json:"old_data,omitempty"`
NewData *json.RawMessage `gorm:"type:jsonb" json:"new_data,omitempty"`
ChangedAt time.Time `gorm:"type:timestamp with time zone;not null;default:now()" json:"changed_at,omitempty"`
}Usage
Add to your sqldoc.config.ts:
typescript
export default {
dialect: 'postgres',
schema: ['./schema.sql'],
codegen: [
{
template: 'gorm',
output: './generated/models.go',
},
],
}Then run:
bash
sqldoc codegen