entimport

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SchemaTpl = parseT("template/schema.tmpl")
	GqlTpl    = parseT("template/graphql.tmpl")
)
View Source
var EntImportCmd = &cli.Command{
	Name:  "entimport",
	Usage: "a tool for creating Ent schemas from existing SQL databases",
	Action: func(c *cli.Context) error {
		var tables []string

		if len(c.StringSlice("tables")) == 1 {
			for _, s := range strings.Split(c.StringSlice("tables")[0], ",") {
				tables = append(tables, s)
			}
		} else {
			tables = c.StringSlice("tables")
		}
		opts := driver.ImportOptions{
			Dialect:       c.String("dialect"),
			DSN:           c.String("dsn"),
			SchemaPath:    c.String("output"),
			Tables:        tables,
			GenGraphql:    c.Bool("gql"),
			GenProtoField: c.Bool("protobuf"),
			CaseInt:       c.Bool("UseInt"),
		}
		return generateSchema(opts)
	},
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "dialect",
			Value:   "mysql",
			Aliases: []string{"d"},
			Usage:   "database dialect",
		},
		&cli.StringFlag{
			Name:    "dsn",
			Usage:   "data source name (connection information)",
			EnvVars: []string{"IMPORT_DSN"},
		},
		&cli.StringFlag{
			Name:    "output",
			Aliases: []string{"o"},
			Value:   "./ent/schema",
			Usage:   "output path for ent schema",
		},
		&cli.StringSliceFlag{
			Name:  "tables",
			Usage: "comma-separated list of tables to inspect (all if empty)",
		},
		&cli.BoolFlag{
			Name:    "gql",
			Aliases: []string{"g"},
			Value:   false,
			Usage:   "generate graphql file",
		},
		&cli.BoolFlag{
			Name:    "protobuf",
			Aliases: []string{"p"},
			Value:   false,
			Usage:   "generate protobuf file",
		},
		&cli.BoolFlag{
			Name:    "UseInt",
			Aliases: []string{"i"},
			Value:   true,
			Usage:   "universal int type",
		},
	},
}
View Source
var Funcs = template.FuncMap{
	"schemaType":       schemaType,
	"fieldTypeName":    fieldTypeName,
	"clearComment":     clearComment,
	"entgqlOrderField": entgqlOrderField,
	"defaultValue":     defaultValue,
	"fieldParameters":  fieldParameters,
	"inc": func(i int) int {
		return i + 1
	},
}

Functions

func WriteGql

func WriteGql(template *gen.Template, types []*gen.Type, output string) error

func WriteSchema

func WriteSchema(template *gen.Template, types []*gen.Type, opts driver.ImportOptions) error

Types

type SchemaImporter

type SchemaImporter interface {
	// SchemaInspect imports a given schema from a data source and returns a list of gen.Type.
	SchemaInspect(context.Context) ([]*gen.Type, error)
}

SchemaImporter is the interface that wraps the Schema.

func NewImport

func NewImport(opts driver.ImportOptions) (SchemaImporter, error)

NewImport - calls the relevant data source importer based on a given dialect.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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