Foreword & Background

During golang development, when we use ORM, we often need to map database tables to a Golang struct, which carries the corresponding ORM tag, as defined in the following struct definition:

type InsInfo struct {
  Connections  string    `gorm:"column:connections"`
  CPU          int       `gorm:"column:cpu"`
  CreateTime   time.Time `gorm:"column:create_time"`
  Env          int       `gorm:"column:env"`
  ID           int64     `gorm:"column:id; primary_key"`
  IP           string    `gorm:"column:ip"`
  Organization string    `gorm:"column:organization"`
  Pass         string    `gorm:"column:pass"`
  Port         string    `gorm:"column:port"`
  RegionId     string    `gorm:"column:regionid"`
  ServerIP     string    `gorm:"column:server_ip"`
  Status       int       `gorm:"column:status"`
  Type         string    `gorm:"column:type"`
  UUID         string    `gorm:"column:uuid"`}Copy the code

This is the struct map of the database table corresponding to GORM. Even if the data table does not have many fields, it is also some repetitive work if it is written manually. For relational databases like MySQL, we usually use ORM to operate the database, so we wonder if MySQL’s data table can automatically generate the struct definition of Golang, and reduce the repetitive development work (leave work early).

The status quo

I have investigated some tools at present, such as Chrome plugin SQL2Struct(a Chrome plugin that automatically generates Golang structure according to SQL statements), but I feel that it is cumbersome to use. Every time I need to enter the database, execute SQL statements and get the copy of the construction statement into the browser, then I can use it. In the hope of providing an out-of-the-box environment, providing web interface, we only need to fill in the database information, we can generate the corresponding ORM struct with one key, so this project was born: github.com/hantmac/fuc…

The principle of

Mysql has its own database information_SCHEMA, and there is a table COLUMNS, whose COLUMNS contain database name, table name, field name, and field type, etc. We can use the data of this table to read the field information of the corresponding table, and then according to the syntax rules of Golang, Generate the corresponding struct. Specific not detailed expansion, interested can go to see the source code.

The Web version

Connecting to a Local Database

Docker-compose up -d: docker-compose up -d: docker-compose up -d: docker-compose up -d: docker-compose up -d: docker-compose up -d: docker-compose up -d

The database on the server

If your database on a network server, you need to modify the backend interface IP: port, and then to build Docker image, push into his image of the warehouse, and then modify the Docker – compose. Yaml, perform Docker – compose up – d. Modify the position is: fuckdb/frontend/SRC/config/index. Js.

let APIdb2struct

if(process.env.NODE_ENV === "development"){
  APIdb2struct = "http://0.0.0.0:8000"// Change to the IP address of the deployment server}else{
  APIdb2struct = "http://0.0.0.0:8000"// Change to the IP address of the deployment server}export default {
  APIdb2struct
}

Copy the code

Just fill in the database information, the package name and struct name of the Golang code you want, and click Generate to get the structure mapping for GORM.

Just Ctrl+C&Ctrl+V in your project. We know that golang struct tag has many functions, there are also many tag options, such as JSON, XML, etc., will add more tag option support later.

Web version of the features of the database information caching function, can remember the database information you filled in before, save a lot of repeated operations, you do not have to fill in the tedious database name, table name, just one key, you can get the corresponding code, With the jSON-to-go plugin (github.com/mholt/json-…) , the development efficiency has been greatly improved. At present, this tool has been used in our group, and the feedback is good, saving a lot of repetitive work, especially in the development of the use of the same library of multiple tables, can soon complete the database table ->strcut mapping.

Take a look at a demo video.

episode

A few days ago, some students came to us and said that the Web version of FuckDB could not be used after deployment. After a long time of solving the problem, users could not deploy it, but the feedback still felt that the deployment was a bit complicated. Reflection and for a tool software, some users don’t want to do some complex deployment process or not familiar with deployment operations, may be want to temporarily use, so you should make tools more lightweight, more out of the box, so the overnight wrote a fuckdb lite, more easy to use, more convenient installation process, One minute to get the code you want.

fuckdb Lite

The principle of

Based on Cobra (github.com/spf13/cobra), the core code inherits from the Web version.

The installation

Listen to user feedback, the installation process is extremely simple, Mac users can directly brew install installation

brew tap hantmac/tap && brew install fuckdb
Copy the code
  • Linux users:The curl https://github.com/hantmac/fuckdb/releases/download/v0.2/fuckdb_linux.tar.gzDownload, decompress, install
  • For Windows emMM users, go to GitHub release and download it manually

use

fuckdb --help
From mysql schema generate golang struct with gorm, json tag

Usage:
  fuckdb [command]

Available Commands:
  generate    use `fuckdb generate` to generate fuckdb.json
  go          fuckdb go to generate golang struct with gorm and json tag
  help        Help about any command

Flags:
  -h, --help   help for fuckdb

Use "fuckdb [command] --help" for more information about a command.
Copy the code

Two main commands are currently available, fuckDB generate and FUCkDB GO, which we’ll look at in turn.

fuckdb generate
Copy the code

Create a fuckdb.json file that stores MySQL information, edit fuckdb.json, and fill in your MySQL information. This file can be reused by simply changing the table name.

{
  "db": {
    "host": "localhost"."port": 3306,
    "password": "password"."user": "root"."table": "tableName"."database": "example"."packageName": "packageName"."structName": "structName"."jsonAnnotation": true."gormAnnotation": true}}Copy the code

After you modify the file, you’re done. Go, go, go!

perform

fuckdb go
Copy the code

Enjoy Your Code!

Here is a demonstration operation (agree to get the code in one minute, never more than one second)

Compared to the previous web version of the installation is simply too convenient, my mother no longer need to worry about my overtime.

The ps: fuckdb.json file must be in the operation directory.

Welcome to try & feedback &Contribute. Code address: github.com/hantmac/fuc…


Official information * Latest technology * exclusive interpretationCopy the code