Skip to content

mattn/modelgen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 

Repository files navigation

modelgen

Description

command for generating DB entities for golang

Installation

This package can be installed with the go get command:

go get github.com/mattn/modelgen

Usage

Usage of modelgen:
  -dbi="github.com/mattn/go-sqlite3": Database Driver
  -pkg="": Package Name
  -tag="db": Database Tag
$ modelgen foo bar:string baz:time nothing_but_love:int
type Foo struct {
	Id             int64     `db:"id"`
	Bar            string    `db:"bar"`
	Baz            time.Time `db:"baz"`
	NothingButLove int64     `db:"nothing_but_love"`
}
$ modelgen -pkg=main -dbi=sqlite3 foo bar:string baz:time nothing_but_love:int
package main

import (
	_ "github.com/mattn/go-sqlite3"
	"database/sql"
	"log"
	"time"
)

type Foo struct {
	Id             int64     `db:"id"`
	Bar            string    `db:"bar"`
	Baz            time.Time `db:"baz"`
	NothingButLove int64     `db:"nothing_but_love"`
}

func main() {
	conn, err := sql.Open("sqlite3", "...")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()
}
$ modelgen -pkg=main -dbi=sqlite3 foo bar:string baz:time nothing_but_love:int > main.go
$ vi main.go
$ go build main.go
$ modelgen -gorp -pkg=main blog title:string body:string  
package main

import (
	_ "github.com/mattn/go-sqlite3"
	"database/sql"
	"log"
	"github.com/coopernurse/gorp"
)

type Blog struct {
	Id    int64  `db:"id"`
	Title string `db:"title"`
	Body  string `db:"body"`
}

func main() {
	conn, err := sql.Open("sqlite3", "...")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	dbmap := &gorp.DbMap{Db: conn, Dialect: gorp.SqliteDialect{}}
	dbmap.AddTableWithName(Blog{}, "blogs").SetKeys(true, "id")
	err = dbmap.CreateTablesIfNotExists()
	if err != nil {
		log.Fatal(err)
	}
}

Tips

This is easy to generate json mapped struct.

$ modelgen -tag db,json blog title:string body:string  
type Blog struct {
	Id    int64  `db:"id" json:"id"`
	Title string `db:"title" json:"title"`
	Body  string `db:"body" json:"body"`
}

License

Yasuhiro Matsumoto

MIT: http://mattn.mit-license.org/2014

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Languages