Git Product home page Git Product logo

tabler's Introduction

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.

tabler's People

Contributors

tristanwietsma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

ekanna

tabler's Issues

Coding Style

Hi @tristanwietsma

func (c *Column) Init(name, tag string) error {
    (*c).Name = name

Why do you use (*c).Name?
Why not c.Name ?

Just curious to know.

other attributes

  • selectable: a field can be selected based on equality test
  • default: a field has a default value

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.