Documentation ¶
Index ¶
- Variables
- func EachTableGroupToItsOwnPackage() func(rootDir, tableName string) string
- func EachTableToItsOwnPackage(rootDir, tableName string) string
- func GenerateColumnsFromSchema(s *pg.Schema, e ExportOptions) error
- func GenerateSchemaFromDatabase(ctx context.Context, i ImportOptions, e ExportOptions) error
- type ExportOptions
- type ImportOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var GoImportsTool = "goimports"
GoImportsTool is the name of the tool that will be used to format the generated code.
Functions ¶
func GenerateColumnsFromSchema ¶
func GenerateColumnsFromSchema(s *pg.Schema, e ExportOptions) error
GenerateColumnsFromSchema generates Go code for the given schema. The generated code includes a struct for each table which contains struct fields for each column information.
Example Code:
schema := pg.NewSchema() schema.MustRegister("companies", Company{}) schema.MustRegister("customers", Customer{})
opts := &ExportOptions{ RootDir: "./definition", } if err := GenerateColumnsFromSchema(schema, opts); err != nil { t.Fatal(err) }
Usage: definition.Company.Name.String() // returns "name" definition.Customer.Email.String() // returns "email"
Useful for type-safe query builders.
func GenerateSchemaFromDatabase ¶
func GenerateSchemaFromDatabase(ctx context.Context, i ImportOptions, e ExportOptions) error
TODO: Add support for base-type entities. Make base classes, e.g. Accept: { "TargetDater": ["source_id", "target_date"], "Base": "id", "created_at", "updated_at"] }.
Output: $ROOT_DIR/base/target_dater.go $ROOT_DIR/base/base.go
And on each table which contains these columns, replace the column printing with the base.TargetDater and/or base.Base and import this 'base' package to each table's file.
Example ¶
package main import ( "context" "fmt" "os" "reflect" "time" "github.com/kataras/pg" ) type Features []Feature type Feature struct { IsFeatured bool `json:"is_featured"` } type Tag struct { Name string `json:"name"` Value any `json:"value"` } func main() { const ( rootDir = "./_testdata" ) defer func() { os.RemoveAll(rootDir) time.Sleep(1 * time.Second) }() i := ImportOptions{ ConnString: "postgres://postgres:admin!123@localhost:5432/test_db?sslmode=disable", ListTables: pg.ListTablesOptions{ Filter: pg.TableFilterFunc(func(table *pg.Table) bool { columnFilter := func(column *pg.Column) bool { columnName := column.Name switch table.Name { case "blog_posts": switch columnName { case "feature": column.FieldType = reflect.TypeOf(Feature{}) case "other_features": column.FieldType = reflect.TypeOf(Features{}) case "tags": column.FieldType = reflect.TypeOf([]Tag{}) } } return true } table.FilterColumns(columnFilter) return true }), }, } e := ExportOptions{ RootDir: rootDir, // Optionally: // GetFileName: EachTableToItsOwnPackage, GetFileName: EachTableGroupToItsOwnPackage(), } if err := GenerateSchemaFromDatabase(context.Background(), i, e); err != nil { fmt.Println(err.Error()) return } fmt.Println("OK") }
Output: OK
Types ¶
type ExportOptions ¶
type ExportOptions struct { RootDir string FileMode fs.FileMode ToSingular func(string) string GetFileName func(rootDir, tableName string) string GetPackageName func(tableName string) string }
ExportOptions is the options for the GenerateColumnsFromSchema function.
type ImportOptions ¶
type ImportOptions struct { ConnString string ListTables pg.ListTablesOptions }
ExportOptions is the options for the schema export. Used in GenerateSchemaFromDatabase and GenerateColumnsFromSchema.