cogen

package module
v0.0.0-...-1e22c6c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2022 License: MIT Imports: 2 Imported by: 0

README

cogen

co-generator, or codegen.

cogen is a DDL codegen for GORM structs.

Example

package main

import (
	"fmt"

	"github.com/anqur/cogen"
)

type Item struct {
	ID    uint64 `gorm:"column:id;primary_key;auto_increment;not null;type:bigint(20) unsigned"`
	Name  string `gorm:"column:name;not null;type:varchar(64);default:\"\";uniqueindex"`
	Price uint32 `gorm:"column:price;not null;type:uint;default:0"`
}

func (Item) TableName() string {
	return "t_item"
}

func main() {
	g, err := cogen.MySQL(Item{})
	if err != nil {
		panic(err)
	}
	fmt.Println(g.String())
}

Why

  • It's hard to do that in GORM. We need some hacks and tricks
  • I believe that DB schemas are mostly not the real business entities without invariants. Let's generate the DDLs from some intermediate ORM structs!

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cogen

type Cogen interface {
	fmt.Stringer
}

func MySQL

func MySQL(data interface{}, opts ...Option) (Cogen, error)
Example
package main

import (
	"fmt"

	"github.com/anqur/cogen"
)

type Item struct {
	ID    uint64 `gorm:"column:id;primary_key;auto_increment;not null;type:bigint(20) unsigned"`
	Name  string `gorm:"column:name;not null;type:varchar(64);default:\"\";uniqueindex"`
	Price uint32 `gorm:"column:price;not null;type:uint;default:0"`
}

func main() {
	g, err := cogen.MySQL(Item{}, cogen.WithTableName("t_item"))
	if err != nil {
		panic(err)
	}
	fmt.Println(g.String())
}
Output:

CREATE TABLE `t_item` (`id` bigint(20) unsigned NOT NULL,`name` varchar(64) NOT NULL DEFAULT '',`price` int unsigned NOT NULL DEFAULT 0,PRIMARY KEY (`id`),UNIQUE INDEX idx_name (`name`))

type Option

type Option interface {
	Apply(s *internal.Setting)
}

func WithTableName

func WithTableName(name string) Option

func WithTableNameInIndex

func WithTableNameInIndex() Option

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL