{"id":20214183,"url":"https://github.com/masterminds/structable","last_synced_at":"2025-04-07T07:17:16.050Z","repository":{"id":57483915,"uuid":"17090116","full_name":"Masterminds/structable","owner":"Masterminds","description":"Golang struct-to-table database mapper","archived":false,"fork":false,"pushed_at":"2020-08-16T11:46:15.000Z","size":92,"stargazers_count":306,"open_issues_count":6,"forks_count":34,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-03-31T06:05:08.960Z","etag":null,"topics":["go","squirrel"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Masterminds.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-02-22T17:34:52.000Z","updated_at":"2025-03-26T14:10:32.000Z","dependencies_parsed_at":"2022-08-28T17:02:15.915Z","dependency_job_id":null,"html_url":"https://github.com/Masterminds/structable","commit_stats":null,"previous_names":["technosophos/structable"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Masterminds%2Fstructable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Masterminds%2Fstructable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Masterminds%2Fstructable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Masterminds%2Fstructable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Masterminds","download_url":"https://codeload.github.com/Masterminds/structable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247608160,"owners_count":20965953,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","squirrel"],"created_at":"2024-11-14T06:14:28.176Z","updated_at":"2025-04-07T07:17:16.033Z","avatar_url":"https://github.com/Masterminds.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Structable: Struct-Table Mapping for Go\n[![Stability:\nSustained](https://masterminds.github.io/stability/sustained.svg)](https://masterminds.github.io/stability/sustained.html)\n\n**Warning:** This is the Structable 4 development branch. For a stable\nrelease, use version 3.1.0. Structable development happens very slowly.\n\nThis library provides basic struct-to-table mapping for Go.\n\nIt is based on the [Squirrel](https://github.com/Masterminds/squirrel) library.\n\n## What It Does\n\nStructable maps a struct (`Record`) to a database table via a\n`structable.Recorder`. It is intended to be used as a back-end tool for\nbuilding systems like Active Record mappers.\n\nIt is designed to satisfy a CRUD-centered record management system,\nfilling the following contract:\n\n```go\n  type Recorder interface {\n    Bind(string, Record) Recorder // link struct to table\n    Interface() interface{}  // Get the struct that has been linked\n    Insert() error // INSERT just one record\n    Update() error // UPDATE just one record\n    Delete() error // DELETE just one record\n    Exists() (bool, error) // Check for just one record\n    ExistsWhere(cond interface{}, args ...interface{}) (bool, error)\n    Load() error  // SELECT just one record\n    LoadWhere(cond interface{}, args ...interface{}) error // Alternate Load()\n  }\n```\n\nSquirrel already provides the ability to perform more complicated\noperations.\n\n## How To Install It\n\nThe usual way...\n\n```\n$ glide get github.com/Masterminds/structable\n$ # or...\n$ go get github.com/Masterminds/structable\n```\n\nAnd import it via:\n\n```\nimport \"github.com/Masterminds/structable\"\n```\n\n## How To Use It\n\n[![GoDoc](https://godoc.org/github.com/Masterminds/structable?status.png)](https://godoc.org/github.com/Masterminds/structable)\n\nStructable works by mapping a struct to columns in a database.\n\nTo annotate a struct, you do something like this:\n\n```go\n  type Stool struct {\n    Id\t\t int\t`stbl:\"id, PRIMARY_KEY, AUTO_INCREMENT\"`\n    Legs\t int    `stbl:\"number_of_legs\"`\n    Material string `stbl:\"material\"`\n    Ignored  string // will not be stored. No tag.\n  }\n```\n\nTo manage instances of this struct, you do something like this:\n\n```go\n  stool := new(Stool)\n  stool.Material = \"Wood\"\n  db := getDb() // Get a sql.Db. You're on  the hook to do this part.\n\n  // Create a new structable.Recorder and tell it to\n  // bind the given struct as a row in the given table.\n  r := structable.New(db, \"mysql\").Bind(\"test_table\", stool)\n\n  // This will insert the stool into the test_table.\n  err := r.Insert()\n```\n\nAnd of course you have `Load()`, `Update()`, `Delete()` and so on.\n\nThe target use case for Structable is to use it as a backend for an\nActive Record pattern. An example of this can be found in the\n`structable_test.go` file\n\nMost of Structable focuses on individual objects, but there are helpers\nfor listing objects:\n\n```go\n// Get a list of things that have the same type as object.\nstool := new(Stool)\nitems, err := structable.List(stool, offset, limit)\n\n// Customize a list of things that have the same type as object.\nfn = func(object structable.Describer, sql squirrel.SelectBuilder) (squirrel.SelectBuilder, error) {\n  return sql.Limit(10), nil\n}\nitems, err := structable.ListWhere(stool, fn)\n```\n\nFor example, here is a function that uses `ListWhere` to get collection\nof definitions from a table described in a struct named `Table`:\n\n```go\nfunc (s *SchemaInfo) Tables() ([]*Table, error) {\n\n  // Bind a new recorder. We use an empty object just to get the field\n  // data for that struct.\n\tt := \u0026Table{}\n\tst := structable.New(s.Queryer, s.Driver).Bind(t.TableName(), t)\n\n  // We want to return no more than 10 of these.\n\tfn := func(d structable.Describer, q squirrel.SelectBuilder) (squirrel.SelectBuilder, error) {\n\t\treturn q.Limit(10), nil\n\t}\n\n  // Fetch a list of Table structs.\n\titems, err := structable.ListWhere(st, fn)\n\tif err != nil {\n\t\treturn []*Table{}, err\n\t}\n\n  // Because we get back a []Recorder, we need to get the original data\n  // back out. We have to manually convert it back to its real type.\n\ttables := make([]*Table, len(items))\n\tfor i, item := range items {\n\t\ttables[i] = item.Interface().(*Table)\n\t}\n\treturn tables, nil\n}\n```\n\n### Tested On\n\n- MySQL (5.5)\n- PostgreSQL (9.3, 9.4, 9.6)\n- SQLite 3\n\n## What It Does Not Do\n\nIt does not...\n\n* Create or manage schemas.\n* Guess or enforce table or column names. (You have to tell it how to\n  map.)\n* Provide relational mapping.\n* Handle bulk operations (use Squirrel for that)\n\n## LICENSE\n\nThis software is licensed under an MIT-style license. See LICENSE.txt\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasterminds%2Fstructable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmasterminds%2Fstructable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasterminds%2Fstructable/lists"}