/tabler

Go generate syntactic sugar for SQL-backed structs

Primary LanguageGoMIT LicenseMIT

tabler

Go generate syntactic sugar for SQL-backed structs

Introduction

Given a struct with tagged fields, tabler will generate methods that return strings for the following actions:

  • Create Table
  • Drop Table
  • Insert Row
  • Select Row

See the example for more information.

Installation

go get github.com/tristanwietsma/tabler

Use

Add the go:generate directive to files with SQL-backed structs.

//go:generate tabler $GOFILE

Add the @table decorator to the comment block for all target structs. Tag each field with the data type (columnType) and label the primary keys.

// @table
type User struct {
    ID      string    `tabler:"columnType=uuid&primary=true"`
    Email   string    `tabler:"columnType=varchar(128)"`
    Created time.Time `tabler:"columnType=timestamp"`
}

Run generate and tabler will produce *_tabler.go files for those files containing decorated structs.

go generate
go build

Tags

Requirements

  • Every field must have a tabler key in the tag in order to be included as a column.
  • Struct fields without a tabler key will be ignored.
  • A columnType attribute is required for every field.
  • Every table must have at least one primary key.

Foreign Key Convention

Fields matching the pattern <something>ID are assumed to be foreign keys. For example:

// @table
type Profile struct {
    UserID    string `tabler:"columnType=uuid&primary=true"`
    Attribute string `tabler:"columnType=varchar(64)&primary=true"`
    Value     string `tabler:"columnType=varchar(256)"`
}

In the above, UserID will be defined as userid uuid REFERENCES user(id) in the table creation statement.