Skip to content

Commit

Permalink
update cli and bump sqlc version to v1.27.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sxwebdev authored Aug 13, 2024
1 parent 44ccf98 commit 23a44f0
Show file tree
Hide file tree
Showing 38 changed files with 859 additions and 596 deletions.
63 changes: 17 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,24 @@ go install github.com/tkcrm/pgxgen/cmd/pgxgen@latest

Print all available commands

```bash
```shell
pgxgen help
```

### Version

```bash
# Print current version
pgxgen version

# Check for new version
pgxgen check-version
COMMANDS:
crud Generate crud sql's
gomodels Generate golang models based on existed structs
keystone Generate mobx keystone models
ts Generate types for typescript, based on go structs
sqlc Generate sqlc code
check-version Check for a new version
version Print the version
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--pgxgen-config value Absolute or relative path to pgxgen.yaml file (default: "pgxgen.yaml")
--sqlc-config value Absolute or relative path to sqlc.yaml file (default: "sqlc.yaml")
--help, -h show help
--version, -v print the version
```
### Configure `pgxgen`
Expand Down Expand Up @@ -253,38 +259,14 @@ gen_typescript_from_structs:
- "GetOrganizationRequest"
```
### Generate `CRUD` queries for existing tables

```bash
pgxgen crud
```

### Generate models based on sqlc models

```bash
pgxgen gomodels
```

### Generate keystone models based on go models

```bash
pgxgen keystone
```

#### Install `@tkcrm/ui` in your frontend
### Install `@tkcrm/ui` in your frontend
If you generate mobx keystone models install `@tkcrm/ui` in your frontend project
```bash
npm i @tkcrm/ui --save-dev
```
### Generate typescript types based on go structs

```bash
pgxgen ts
```

### Configure `sqlc`
At root of your project create a `sqlc.yaml` file with the configuration described below.
Expand Down Expand Up @@ -319,14 +301,3 @@ sql:
emit_enum_valid_method: true
emit_all_enum_values: true
```

### Generate `sqlc`

```bash
pgxgen sqlc generate
```

## Roadmap

- Generate models without sqlc
- Generate `.proto` files with CRUD services
165 changes: 113 additions & 52 deletions cmd/pgxgen/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"fmt"
"os"
"runtime"

"github.com/cristalhq/acmd"
"github.com/cristalhq/flagx"
"github.com/tkcrm/pgxgen/internal/config"
"github.com/tkcrm/pgxgen/internal/crud"
"github.com/tkcrm/pgxgen/internal/gomodels"
Expand All @@ -13,74 +13,135 @@ import (
"github.com/tkcrm/pgxgen/internal/typescript"
"github.com/tkcrm/pgxgen/internal/ver"
"github.com/tkcrm/pgxgen/pkg/logger"
"github.com/urfave/cli/v2"
)

var (
appName = "pgxgen"
version = "v0.3.5"
)

func main() {
logger := logger.New()

// custom config file path
var cf config.Flags
fset := flagx.NewFlagSet(appName, os.Stdout)
fset.String(&cf.PgxgenConfigFilePath, "pgxgen-config", "", "pgxgen.yaml", "absolute or relative path to sqlc.yaml file")
fset.String(&cf.SqlcConfigFilePath, "sqlc-config", "", "sqlc.yaml", "absolute or relative path to pgxgen.yaml file")
func getBuildVersion() string {
return fmt.Sprintf(
"\nrelease: %s\ngo version: %s",
version,
runtime.Version(),
)
}

// parse flags
if err := fset.Parse(os.Args[1:]); err != nil {
logger.Fatalf("failed to parse flags: %s", err)
func loadConfig(c *cli.Context) (config.Config, error) {
cf := config.Flags{
PgxgenConfigFilePath: c.String("pgxgen-config"),
SqlcConfigFilePath: c.String("sqlc-config"),
}

cfg, err := config.LoadConfig(cf, version)
if err != nil {
logger.Fatalf("load config error: %s", err)
return cfg, fmt.Errorf("load config error: %w", err)
}

cmds := []acmd.Command{
{
Name: "crud",
Description: "generate crud sql's",
ExecFunc: crud.CmdFunc(logger, cfg),
},
{
Name: "gomodels",
Description: "generate golang models based on existed structs",
ExecFunc: gomodels.CmdFunc(logger, cfg),
},
{
Name: "keystone",
Description: "generate mobx keystone models",
ExecFunc: keystone.CmdFunc(logger, cfg),
},
{
Name: "ts",
Description: "generate types for typescript, based on go structs",
ExecFunc: typescript.CmdFunc(logger, cfg),
},
{
Name: "sqlc",
Description: "generate sqlc code",
ExecFunc: sqlc.CmdFunc(logger, cfg),
return cfg, nil
}

func main() {
logger := logger.New()

app := &cli.App{
Name: appName,
Version: getBuildVersion(),
Usage: "Generate GO models, DB CRUD, Mobx Keystone models and typescript code based on DDL",
Suggest: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pgxgen-config",
Usage: "Absolute or relative path to pgxgen.yaml file",
Value: "pgxgen.yaml",
},
&cli.StringFlag{
Name: "sqlc-config",
Usage: "Absolute or relative path to sqlc.yaml file",
Value: "sqlc.yaml",
},
},
{
Name: "check-version",
Description: "check for new version",
ExecFunc: ver.CmdFunc(logger, cfg),
Commands: []*cli.Command{
{
Name: "crud",
Usage: "Generate crud sql's",
Action: func(c *cli.Context) error {
cfg, err := loadConfig(c)
if err != nil {
return err
}
return crud.CmdFunc(c, logger, cfg)
},
},
{
Name: "gomodels",
Usage: "Generate golang models based on existed structs",
Action: func(c *cli.Context) error {
cfg, err := loadConfig(c)
if err != nil {
return err
}
return gomodels.CmdFunc(c, logger, cfg)
},
},
{
Name: "keystone",
Usage: "Generate mobx keystone models",
Action: func(c *cli.Context) error {
cfg, err := loadConfig(c)
if err != nil {
return err
}
return keystone.CmdFunc(c, logger, cfg)
},
},
{
Name: "ts",
Usage: "Generate types for typescript, based on go structs",
Action: func(c *cli.Context) error {
cfg, err := loadConfig(c)
if err != nil {
return err
}
return typescript.CmdFunc(c, logger, cfg)
},
},
{
Name: "sqlc",
Usage: "Generate sqlc code",
Action: func(c *cli.Context) error {
cfg, err := loadConfig(c)
if err != nil {
return err
}
return sqlc.CmdFunc(c, logger, cfg)
},
},
{
Name: "check-version",
Usage: "Check for a new version",
Action: func(c *cli.Context) error {
cfg, err := loadConfig(c)
if err != nil {
return err
}
return ver.CmdFunc(c, logger, cfg)
},
},
{
Name: "version",
Usage: "Print the version",
Action: func(c *cli.Context) error {
fmt.Printf("%s version%s\n", appName, c.App.Version)
return nil
},
},
},
}

r := acmd.RunnerOf(cmds, acmd.Config{
AppName: "pgxgen",
AppDescription: "Generate GO models, DB CRUD, Mobx Keystone models and typescript code based on DDL",
PostDescription: "pgxgen crud",
Version: version,
Args: append([]string{os.Args[0]}, fset.Args()...),
})

if err := r.Run(); err != nil {
if err := app.Run(os.Args); err != nil {
logger.Fatalf("error: %s", err)
}
}
Loading

0 comments on commit 23a44f0

Please sign in to comment.