MCPcopy Index your code
hub / github.com/Masterminds/structable

github.com/Masterminds/structable @3.1.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 3.1.0 ↗ · + Follow
107 symbols 380 edges 7 files 46 documented · 43%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Structable: Struct-Table Mapping for Go

Warning: Structable 3.0.0 has new interfaces, and is incompatible with older versions of Structable.

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
    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...

$ 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() // 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

Tested On

  • MySQL (5.5)
  • PostgreSQL (9.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

Extension points exported contracts — how you extend this code

Recorder (Interface)
A Recorder is responsible for managing the persistence of a Record. A Recorder is bound to a struct, which it then exami [1 …
structable.go
Haecceity (Interface)
Haecceity implements John Duns Scotus. Actually, it is responsible for testing whether a thing exists, and is what we t [1 …
structable.go
Record (Interface)
* Record describes a struct that can be stored. Example: type Stool struct { Id int `stbl:"id PRIMARY_KEY AUTO_
structable.go
Saver (Interface)
(no doc) [2 implementers]
structable.go
Loader (Interface)
(no doc) [1 implementers]
structable.go

Core symbols most depended-on inside this repo

Bind
called by 24
structable.go
New
called by 21
structable.go
Insert
called by 8
structable.go
Update
called by 8
structable.go
LoadWhere
called by 6
structable.go
Load
called by 5
structable.go
Delete
called by 5
structable.go
Exists
called by 5
structable.go

Shape

Method 45
Function 44
Struct 13
Interface 5

Languages

Go100%

Modules by API surface

structable.go33 symbols
structable_test.go25 symbols
schema2struct/schema2struct.go16 symbols
sqlite_pod_test.go11 symbols
sqlite_ptr_test.go10 symbols
example/fence.go8 symbols
example/users.go4 symbols

For agents

$ claude mcp add structable \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact