• Stars
    star
    289
  • Rank 138,074 (Top 3 %)
  • Language
    Go
  • License
    Other
  • Created about 10 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Golang struct-to-table database mapper

Structable: Struct-Table Mapping for Go

Stability: Sustained

Warning: This is the Structable 4 development branch. For a stable release, use version 3.1.0. Structable development happens very slowly.

This library provides basic struct-to-table mapping for Go.

It is based on the Squirrel library.

What It Does

Structable maps a struct (Record) to a database table via a structable.Recorder. It is intended to be used as a back-end tool for building systems like Active Record mappers.

It is designed to satisfy a CRUD-centered record management system, filling the following contract:

  type Recorder interface {
    Bind(string, Record) Recorder // link struct to table
    Interface() interface{}  // Get the struct that has been linked
    Insert() error // INSERT just one record
    Update() error // UPDATE just one record
    Delete() error // DELETE just one record
    Exists() (bool, error) // Check for just one record
    ExistsWhere(cond interface{}, args ...interface{}) (bool, error)
    Load() error  // SELECT just one record
    LoadWhere(cond interface{}, args ...interface{}) error // Alternate Load()
  }

Squirrel already provides the ability to perform more complicated operations.

How To Install It

The usual way...

$ glide get github.com/Masterminds/structable
$ # or...
$ go get github.com/Masterminds/structable

And import it via:

import "github.com/Masterminds/structable"

How To Use It

GoDoc

Structable works by mapping a struct to columns in a database.

To annotate a struct, you do something like this:

  type Stool struct {
    Id		 int	`stbl:"id, PRIMARY_KEY, AUTO_INCREMENT"`
    Legs	 int    `stbl:"number_of_legs"`
    Material string `stbl:"material"`
    Ignored  string // will not be stored. No tag.
  }

To manage instances of this struct, you do something like this:

  stool := new(Stool)
  stool.Material = "Wood"
  db := getDb() // Get a sql.Db. You're on  the hook to do this part.

  // Create a new structable.Recorder and tell it to
  // bind the given struct as a row in the given table.
  r := structable.New(db, "mysql").Bind("test_table", stool)

  // This will insert the stool into the test_table.
  err := r.Insert()

And of course you have Load(), Update(), Delete() and so on.

The target use case for Structable is to use it as a backend for an Active Record pattern. An example of this can be found in the structable_test.go file

Most of Structable focuses on individual objects, but there are helpers for listing objects:

// Get a list of things that have the same type as object.
stool := new(Stool)
items, err := structable.List(stool, offset, limit)

// Customize a list of things that have the same type as object.
fn = func(object structable.Describer, sql squirrel.SelectBuilder) (squirrel.SelectBuilder, error) {
  return sql.Limit(10), nil
}
items, err := structable.ListWhere(stool, fn)

For example, here is a function that uses ListWhere to get collection of definitions from a table described in a struct named Table:

func (s *SchemaInfo) Tables() ([]*Table, error) {

  // Bind a new recorder. We use an empty object just to get the field
  // data for that struct.
	t := &Table{}
	st := structable.New(s.Queryer, s.Driver).Bind(t.TableName(), t)

  // We want to return no more than 10 of these.
	fn := func(d structable.Describer, q squirrel.SelectBuilder) (squirrel.SelectBuilder, error) {
		return q.Limit(10), nil
	}

  // Fetch a list of Table structs.
	items, err := structable.ListWhere(st, fn)
	if err != nil {
		return []*Table{}, err
	}

  // Because we get back a []Recorder, we need to get the original data
  // back out. We have to manually convert it back to its real type.
	tables := make([]*Table, len(items))
	for i, item := range items {
		tables[i] = item.Interface().(*Table)
	}
	return tables, nil
}

Tested On

  • MySQL (5.5)
  • PostgreSQL (9.3, 9.4, 9.6)
  • SQLite 3

What It Does Not Do

It does not...

  • Create or manage schemas.
  • Guess or enforce table or column names. (You have to tell it how to map.)
  • Provide relational mapping.
  • Handle bulk operations (use Squirrel for that)

LICENSE

This software is licensed under an MIT-style license. See LICENSE.txt

More Repositories

1

glide

Package Management for Golang
Go
8,164
star
2

squirrel

Fluent SQL generation for golang
Go
6,406
star
3

sprig

Useful template functions for Go templates.
Go
3,924
star
4

html5-php

An HTML5 parser and serializer for PHP.
HTML
1,443
star
5

semver

Work with Semantic Versions in Go
Go
1,118
star
6

go-in-practice

Repository for Manning Publications Go in Practice
Go
345
star
7

vcs

VCS Repo management through a common interface in Go
Go
191
star
8

learning-helm

HTML
102
star
9

goutils

GoUtils is a Go implementation of some string manipulation libraries of Apache Commons. This is an open source project aimed at providing Go users with utility functions to manipulate strings in various ways.
Go
94
star
10

cookoo

A chain-of-command framework written in Go
Go
60
star
11

vert

Command line version testing: Compare versions at the CLI for use in shell scripts and make files.
Go
59
star
12

go-fileserver

A Go Fileserver where you can specify custom NotFound and Error response handlers.
Go
47
star
13

glide-report

Go
28
star
14

stability

Share the stability of a project
27
star
15

Fortissimo

A Chain Of Command (CoCo) framework for PHP.
PHP
25
star
16

log-go

A Golang logging interface with some reference implementations.
Go
20
star
17

kitt

Make your CLI apps pretty
Go
16
star
18

engine

An HTML theme engine for Go
Go
15
star
19

godir

Go Path Tool: A utility for working with Go and filesystem paths.
Go
12
star
20

codl

Codl: The Cookoo Domain Language
Go
11
star
21

Fortissimo-CLI-Base

A skeleton for creating Fortissimo CLI applications.
PHP
11
star
22

glide.sh

The website for Glide
HTML
10
star
23

convert

A mathematical conversion library.
Go
8
star
24

rmvcsdir

remove version control directories
Go
7
star
25

formenc

Decode form values using a Go unmarshal
Go
6
star
26

httputil

Framework-free HTTP utilities
Go
4
star
27

cookoo-cli-tutorial

A Git-based tutorial for using Cookoo to write command line apps.
Go
3
star
28

Villain

A CMS to be feared
PHP
3
star
29

cookoo-web-tutorial

A Git-based dive into building Cookoo web apps
Shell
3
star
30

Fortissimo-CLI

A CLI Setup for Fortissimo.
PHP
2
star
31

glide-ppa

Shell
2
star
32

Fortissimo-Base

A base setup for Fortissimo based site.
PHP
2
star
33

Fortissimo-Twig

Integration between Fortissimo and Twig
PHP
1
star
34

fortissimo-commons

Common commands for the Fortissimo framework.
PHP
1
star