All Projects → fraenky8 → Tables To Go

fraenky8 / Tables To Go

Licence: mit
convert your database tables to structs easily

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Tables To Go

Poodle
🔥 A fast and beautiful command line tool to build API requests.
Stars: ✭ 129 (+108.06%)
Mutual labels:  golang-application, developer-tools
Wago
Automate the actions you do after saving code.
Stars: ✭ 60 (-3.23%)
Mutual labels:  developer-tools
Gogeom
This is a Geometrical library for Go Language. Which includes multiple Geometrical calculations like Circle, Lines etc in different forms
Stars: ✭ 47 (-24.19%)
Mutual labels:  golang-application
Grpcc
A gRPC cli interface for easy testing against gRPC servers
Stars: ✭ 1,078 (+1638.71%)
Mutual labels:  developer-tools
Pulse
Dynamic, api-driven stats collector service for realtime stats publishing and historical aggregation with Influxdb.
Stars: ✭ 48 (-22.58%)
Mutual labels:  developer-tools
Rdpgw
Remote Desktop Gateway in Go for deploying on Linux/BSD/Kubernetes
Stars: ✭ 59 (-4.84%)
Mutual labels:  golang-application
Puphpeteer
A Puppeteer bridge for PHP, supporting the entire API.
Stars: ✭ 1,014 (+1535.48%)
Mutual labels:  developer-tools
Git Notify
🙉 📣 Communicate important updates to your team via git commit messages
Stars: ✭ 1,091 (+1659.68%)
Mutual labels:  developer-tools
Blindfold
🔎 Gitignore file generator written in rust
Stars: ✭ 60 (-3.23%)
Mutual labels:  developer-tools
Linessorter Xcode Extension
Xcode Extension that helps you keep your import statements and long code lists organized and uniform
Stars: ✭ 54 (-12.9%)
Mutual labels:  developer-tools
Cells
Future-proof content collaboration platform
Stars: ✭ 1,059 (+1608.06%)
Mutual labels:  golang-application
Ship
Aids you creating a `Dockerfile` by transforming its (interactive shell) history into `RUN` instructions
Stars: ✭ 50 (-19.35%)
Mutual labels:  developer-tools
Dotfiles
🖥️ Automated Configuration, Preferences and Software Installation for macOS
Stars: ✭ 1,103 (+1679.03%)
Mutual labels:  developer-tools
Shon
A simple tool to convert json or yaml into a shell-compliant data structure.
Stars: ✭ 47 (-24.19%)
Mutual labels:  developer-tools
Xxv
The XXV visual hex viewer for the terminal.
Stars: ✭ 61 (-1.61%)
Mutual labels:  developer-tools
Css Semdiff
Semantic diff tool for CSS
Stars: ✭ 43 (-30.65%)
Mutual labels:  developer-tools
Spm Agent Nodejs
NodeJS Monitoring Agent
Stars: ✭ 51 (-17.74%)
Mutual labels:  developer-tools
Http Prompt
An interactive command-line HTTP and API testing client built on top of HTTPie featuring autocomplete, syntax highlighting, and more. https://twitter.com/httpie
Stars: ✭ 8,329 (+13333.87%)
Mutual labels:  developer-tools
Trymodule
➰ It's never been easier to try nodejs modules!
Stars: ✭ 1,115 (+1698.39%)
Mutual labels:  developer-tools
Logvac
Simple, lightweight, api-driven log aggregation service with realtime push capabilities and historical persistence.
Stars: ✭ 61 (-1.61%)
Mutual labels:  developer-tools

Tables-to-Go

convert your database tables to structs easily

A small and convenient tool supporting development against a changing database schema.

Tables change, run the tool, get your structs!

Go Report Card GoDoc Build & Test Code Coverage

Install

This project provides a make file but can also simply installed with the usual go-get command:

go get github.com/fraenky8/tables-to-go

To enable SQLite3 support, run the make file:

make sqlite3

See this PR why it's disabled by default.

Getting Started

tables-to-go -v -of ../path/to/my/models

This gets all tables of a local running PostgreSQL database. Therefore it uses the database postgres, schema public and user postgres with no password. Flag -v is verbose mode, -of is the output file path where the go files containing the structs will get created (default: current working directory).

Features

  • convert your tables to structs
  • table with name a_foo_bar will become file AFooBar.go with struct AFooBar
  • properly formated files with imports
  • automatically typed struct fields, either with sql.Null* or primitve pointer types
  • struct fields with db-tags for ready to use in database code
  • partial support for Masterminds/structable
    • only primary key & auto increment columns supported
    • struct fields with stbl tags
    • ability to generate structs only for Masterminds/structable:
      • without db-tags
      • with or without structable.Recorder
  • currently supported:
    • PostgreSQL (9.5 tested)
    • MySQL (5.5+, 8 tested)
    • SQLite (3 tested)
  • currently the following basic data types are supported:
    • numeric: integer, serial, double, real, float
    • character: varying, text, char, varchar, binary, varbinary, blob
    • date/time: timestamp, date, datetime, year, time with time zone, timestamp with time zone, time without time zone, timestamp without time zone
    • others: boolean

Examples

Assuming you have the following table definition (PostgreSQL):

CREATE TABLE some_user_info  (
  id SERIAL NOT NULL PRIMARY KEY,
  first_name VARCHAR(20),
  last_name  VARCHAR(20) NOT NULL,
  height DECIMAL
);

Run the following command (default local PostgreSQL instance):

tables-to-go

The following file SomeUserInfo.go with default package dto (data transfer object) will be created:

package dto

import (
	"database/sql"
)

type SomeUserInfo struct {
	ID        int             `db:"id"`
	FirstName sql.NullString  `db:"first_name"`
	LastName  string          `db:"last_name"`
	Height    sql.NullFloat64 `db:"height"`
}

The column id got autmatically converted to upper-case to follow the idiomatic go guidelines. See here for more details. Words which gets converted can be found here.
This behaviour can be disabled by providing the command-line flag -no-initialism.

Running on remote database server (eg. [email protected])

tables-to-go -v -t mysql -h 192.168.99.100 -d testdb -u root -p mysecretpassword

PostgreSQL example with different default schema but default database postgres:

tables-to-go -v -t pg -h 192.168.99.100 -s test -u postgres -p mysecretpassword

Note: since database type pg is default, following command will be equivalent:

tables-to-go -v -h 192.168.99.100 -s test -u postgres -p mysecretpassword

You can also specify the package or prefix and suffix.

tables-to-go -v -t mysql -h 192.168.99.100 -d testdb -u root -p mysecretpassword -pn models -pre model_ -suf _model

With same table given above, following file with Name ModelSomeUserInfoModel.go will be created:

package models

import (
	"database/sql"
)

type ModelSomeUserInfoModel struct {
	ID        int             `db:"id"`
	FirstName sql.NullString  `db:"first_name"`
	LastName  string          `db:"last_name"`
	Height    sql.NullFloat64 `db:"height"`
}

Where Are The JSON-Tags?

This is a common question asked by contributors and bug reporters.

Fetching data from a database and representation of this data in the end (JSON, HTML template, cli, ...) are two different concerns and should be decoupled. Therefore, this tool will not generate json tags for the structs.

There are tools like gomodifytags which enables you to generate json tags for existing structs. The call for this tool applied to the example above looks like the following:

gomodifytags -file SomeUserInfo.go -w -all -add-tags json

This adds the json tags directly to the file:

type SomeUserInfo struct {
	ID        int             `db:"id" json:"id"`
	FirstName sql.NullString  `db:"first_name" json:"first_name"`
	LastName  string          `db:"last_name" json:"last_name"`
	Height    sql.NullFloat64 `db:"height" json:"height"`
}

Command-line Flags

Print usage with -? or -help

tables-to-go -help
  -?	shows help and usage
  -d string
    	database name (default "postgres")
  -f
      force, skip tables that encounter errors but construct all others
  -format string
    	format of struct fields (columns): camelCase (c) or original (o) (default "c")
  -fn-format string
      format of the filename: camelCase (c, default) or snake_case (s)
  -h string
    	host of database (default "127.0.0.1")
  -help
    	shows help and usage
  -no-initialism
      	disable the conversion to upper-case words in column names
  -null string
       	representation of NULL columns: sql.Null* (sql) or primitive pointers (native|primitive)  (default "sql")
  -of string
    	output file path (default "current working directory")
  -p string
    	password of user
  -pn string
    	package name (default "dto")
  -port string
    	port of database host, if not specified, it will be the default ports for the supported databases
  -pre string
    	prefix for file- and struct names
  -s string
    	schema name (default "public")
  -structable-recorder
    	generate a structable.Recorder field
  -suf string
    	suffix for file- and struct names
  -t string
    	type of database to use, currently supported: [pg mysql] (default "pg")
  -tags-no-db
    	do not create db-tags
  -tags-structable
    	generate struct with tags for use in Masterminds/structable (https://github.com/Masterminds/structable)
  -tags-structable-only
    	generate struct with tags ONLY for use in Masterminds/structable (https://github.com/Masterminds/structable)
  -u string
    	user to connect to the database (default "postgres")
  -v	verbose output
  -vv
    	more verbose output

Contributing

If you find any issues or missing a feature, feel free to contribute or make suggestions! You can fork the repository and use a feature branch too. Feel free to send me a pull request. The PRs have to come with appropriate unit tests, documentation of the added functionality and updated README with optional examples.

To start developing clone via git or use go's get command to fetch this project.

This project uses go modules so make sure when adding new dependencies to update the go.mod file and the vendor directory:

go mod tidy
go mod vendor

Licensing

The code in this project is licensed under MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].