Skip to content

Commit fcdcbb1

Browse files
authored
feat generator: add support for sqlc code generation (#146)
* feat generator: add support for sqlc code generation * add command to add/remove postgres * add docs
1 parent 68927ab commit fcdcbb1

30 files changed

+396
-283
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
run:
2+
deadline: 5m

cmd/mify/cmd/add.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var (
1212
addClientName string
1313
addServiceLanguage string
1414
addFrontendType string
15+
addDatabaseEngine string
1516
)
1617

1718
var addServiceCmd = &cobra.Command{
@@ -71,11 +72,25 @@ var addApiGatewayCmd = &cobra.Command{
7172
},
7273
}
7374

75+
var addDatabase = &cobra.Command{
76+
Use: "database",
77+
Short: "Add database",
78+
Long: `Add a database to workspace (default is Postgres)`,
79+
Run: func(cmd *cobra.Command, args []string) {
80+
for _, ival := range args {
81+
if err := mify.AddPostgres(appContext, workspacePath, ival); err != nil {
82+
fmt.Fprintf(os.Stderr, "failed to add postgres to service: %s\n", err)
83+
os.Exit(2)
84+
}
85+
}
86+
},
87+
}
88+
7489
// addCmd represents the add command
7590
var addCmd = &cobra.Command{
7691
Use: "add",
77-
Short: "Add <service|client|frontend>",
78-
Long: `Add a service, frontend or clients`,
92+
Short: "Add <service|client|frontend|database>",
93+
Long: `Add a service, frontend or clients, or database`,
7994
PersistentPreRun: func(*cobra.Command, []string) {
8095
err := appContext.LoadWorkspace()
8196
if err != nil {
@@ -107,8 +122,18 @@ func init() {
107122
"Template (e.g. nuxtjs, react-ts)",
108123
)
109124

125+
addDatabase.PersistentFlags().StringVarP(
126+
&addDatabaseEngine,
127+
"engine",
128+
"e",
129+
"postgres",
130+
"DB Engine (Postgres, for now)",
131+
)
132+
110133
addCmd.AddCommand(addServiceCmd)
111134
addCmd.AddCommand(addClientCmd)
112135
addCmd.AddCommand(addFrontendCmd)
113136
addCmd.AddCommand(addApiGatewayCmd)
137+
addCmd.AddCommand(addApiGatewayCmd)
138+
addCmd.AddCommand(addDatabase)
114139
}

cmd/mify/cmd/remove.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,33 @@ var removeClientCmd = &cobra.Command{
2020
Run: func(cmd *cobra.Command, args []string) {
2121
for _, ival := range args {
2222
if err := mify.RemoveClient(appContext, workspacePath, ival, removeClientName); err != nil {
23-
fmt.Fprintf(os.Stderr, "failed to add client to service: %s\n", err)
23+
fmt.Fprintf(os.Stderr, "failed to remove client to service: %s\n", err)
2424
os.Exit(2)
2525
}
2626
}
2727
},
2828
}
2929

30-
// addCmd represents the add command
30+
var removeDatabaseCmd = &cobra.Command{
31+
Use: "database",
32+
Short: "Remove database",
33+
Long: `Remove database from service`,
34+
Args: cobra.MinimumNArgs(1),
35+
Run: func(cmd *cobra.Command, args []string) {
36+
for _, ival := range args {
37+
if err := mify.RemovePostgres(appContext, workspacePath, ival); err != nil {
38+
fmt.Fprintf(os.Stderr, "failed to remove database from service: %s\n", err)
39+
os.Exit(2)
40+
}
41+
}
42+
},
43+
}
44+
45+
// removeCmd represents the remove command
3146
var removeCmd = &cobra.Command{
3247
Use: "remove",
33-
Short: "Remove client",
34-
Long: `Remove client from service`,
48+
Short: "Remove client or database",
49+
Long: `Remove client or database from service`,
3550
PersistentPreRun: func(*cobra.Command, []string) {
3651
err := appContext.LoadWorkspace()
3752
if err != nil {
@@ -48,4 +63,5 @@ func init() {
4863
}
4964

5065
removeCmd.AddCommand(removeClientCmd)
66+
removeCmd.AddCommand(removeDatabaseCmd)
5167
}

docs/docs/components/postgres.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ postgres:
1717
After that you will be able to get `pgxpool.Pool` from `MifyServiceContext`
1818
`Postgres()` method. You can use it to directly make queries or use in some library.
1919

20-
Here's an example how you can use it:
20+
### Using without helpers
21+
22+
Here's an example how you can use it, just run queries from pool, which is created on startup:
2123
```go
2224

2325
func (s *PathToApiService) PathToApiGet(ctx *core.MifyRequestContext) (openapi.ServiceResponse, error) {
@@ -26,6 +28,49 @@ func (s *PathToApiService) PathToApiGet(ctx *core.MifyRequestContext) (openapi.S
2628
}
2729
```
2830

31+
### Using with sqlc
32+
33+
After adding postgres you will notice `sql-queries/<service_db_name>` directory
34+
inside `go-services`. Create any file with `.sql` extension and refer to
35+
`queries.sql.example` or sqlc [documentation](https://docs.sqlc.dev/en/latest/tutorials/getting-started-postgresql.html)
36+
for adding queries. After you add them, run `mify generate` to translate them
37+
to go. Generated helpers with lie in `postgres` package, here's an example of
38+
how to use them, assuming you followed sqlc tutorial:
39+
40+
```go
41+
// Call this from service_extra.go
42+
func NewAuthorsStorage(ctx *core.MifyServiceContext) *AuthorsStorage {
43+
return &AuthorsStorage{
44+
pool: ctx.Postgres(),
45+
}
46+
}
47+
48+
func (s *AuthorsStorage) CreateAuthor(
49+
ctx *core.MifyRequestContext, name string, bio string) (domain.Author, error) {
50+
dbConn := postgres.New(s.pool)
51+
tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{})
52+
if err != nil {
53+
return domain.Author{}, err
54+
}
55+
defer tx.Rollback(ctx)
56+
res, err := dbConn.WithTx(tx).CreateAuthor(ctx, postgres.CreateAuthorParams{
57+
Name: name,
58+
Bio: bio,
59+
})
60+
if err != nil {
61+
return domain.Author{}, err
62+
}
63+
if err := tx.Commit(ctx); err != nil {
64+
return domain.Author{}, err
65+
}
66+
return domain.Author{
67+
ID: res.ID,
68+
Name: res.Name,
69+
Bio: res.Bio,
70+
}, nil
71+
}
72+
```
73+
2974
### Migrations
3075

3176
Mify includes a way to apply migrations for a database via dbmate.

docs/docusaurus.config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,16 @@ const config = {
5555
label: 'Docs',
5656
},
5757
{
58-
href: '#',
58+
type: 'doc',
59+
docId: 'cloud/overview',
5960
position: 'right',
6061
label: 'Cloud',
6162
},
63+
{
64+
href: 'https://mify.io/pricing',
65+
label: 'Pricing',
66+
position: 'right',
67+
},
6268
{
6369
href: 'https://github.com/mify-io/mify',
6470
label: 'GitHub',

docs/src/css/custom.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
max-width: 130em;
3030
width: 100%;
3131
margin: 0 auto;
32-
padding: 0 2rem;
3332
}
3433

3534
.navbar__logo {

go.mod

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,27 @@ require (
99
github.com/getkin/kin-openapi v0.88.0
1010
github.com/go-resty/resty/v2 v2.7.0
1111
github.com/google/uuid v1.2.0
12+
github.com/kyleconroy/sqlc v1.17.0
1213
github.com/mitchellh/go-homedir v1.1.0
1314
github.com/otiai10/copy v1.7.0
1415
github.com/pmezard/go-difflib v1.0.0
15-
github.com/spf13/cobra v1.3.0
16+
github.com/spf13/cobra v1.6.1
1617
github.com/spf13/pflag v1.0.5
1718
github.com/spf13/viper v1.10.1
1819
github.com/vbauerster/mpb/v7 v7.3.2
19-
golang.org/x/text v0.3.7
20+
golang.org/x/text v0.7.0
2021
gopkg.in/yaml.v2 v2.4.0
2122
k8s.io/client-go v0.20.6
2223
)
2324

2425
require (
2526
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
2627
github.com/acomagu/bufpipe v1.0.3 // indirect
28+
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220626175859-9abda183db8e // indirect
29+
github.com/benbjohnson/clock v1.1.0 // indirect
30+
github.com/bytecodealliance/wasmtime-go/v5 v5.0.0 // indirect
31+
github.com/cubicdaiya/gonp v1.0.4 // indirect
32+
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
2733
github.com/davecgh/go-spew v1.1.1 // indirect
2834
github.com/emirpasic/gods v1.12.0 // indirect
2935
github.com/fsnotify/fsnotify v1.5.1 // indirect
@@ -34,8 +40,9 @@ require (
3440
github.com/google/gofuzz v1.1.0 // indirect
3541
github.com/hashicorp/hcl v1.0.0 // indirect
3642
github.com/imdario/mergo v0.3.12 // indirect
37-
github.com/inconshreveable/mousetrap v1.0.0 // indirect
43+
github.com/inconshreveable/mousetrap v1.0.1 // indirect
3844
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
45+
github.com/jinzhu/inflection v1.0.0 // indirect
3946
github.com/josharian/intern v1.0.0 // indirect
4047
github.com/json-iterator/go v1.1.12 // indirect
4148
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
@@ -46,6 +53,11 @@ require (
4653
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4754
github.com/modern-go/reflect2 v1.0.2 // indirect
4855
github.com/pelletier/go-toml v1.9.4 // indirect
56+
github.com/pganalyze/pg_query_go/v2 v2.2.0 // indirect
57+
github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63 // indirect
58+
github.com/pingcap/log v0.0.0-20210906054005-afc726e70354 // indirect
59+
github.com/pingcap/tidb/parser v0.0.0-20220725134311-c80026e61f00 // indirect
60+
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
4961
github.com/sergi/go-diff v1.1.0 // indirect
5062
github.com/spf13/afero v1.8.0 // indirect
5163
github.com/spf13/cast v1.4.1 // indirect
@@ -55,17 +67,19 @@ require (
5567
go.uber.org/atomic v1.9.0 // indirect
5668
go.uber.org/goleak v1.1.12 // indirect
5769
go.uber.org/multierr v1.7.0 // indirect
58-
golang.org/x/crypto v0.0.0-20220126234351-aa10faf2a1f8 // indirect
59-
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
70+
golang.org/x/crypto v0.6.0 // indirect
71+
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 // indirect
6072
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
61-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
62-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
73+
golang.org/x/sync v0.1.0 // indirect
74+
golang.org/x/sys v0.5.0 // indirect
75+
golang.org/x/term v0.5.0 // indirect
6376
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
6477
google.golang.org/appengine v1.6.7 // indirect
6578
gopkg.in/inf.v0 v0.9.1 // indirect
6679
gopkg.in/ini.v1 v1.66.3 // indirect
80+
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
6781
gopkg.in/warnings.v0 v0.1.2 // indirect
68-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
82+
gopkg.in/yaml.v3 v3.0.1 // indirect
6983
k8s.io/apimachinery v0.20.6 // indirect
7084
k8s.io/klog/v2 v2.4.0 // indirect
7185
k8s.io/utils v0.0.0-20201110183641-67b214c5f920 // indirect
@@ -101,10 +115,10 @@ require (
101115
github.com/rivo/uniseg v0.2.0 // indirect
102116
github.com/samber/lo v1.37.0
103117
github.com/sirupsen/logrus v1.8.1 // indirect
104-
github.com/stretchr/testify v1.7.0
118+
github.com/stretchr/testify v1.8.0
105119
go.uber.org/zap v1.20.0
106-
golang.org/x/net v0.0.0-20220127074510-2fabfed7e28f // indirect
120+
golang.org/x/net v0.6.0 // indirect
107121
google.golang.org/genproto v0.0.0-20220126215142-9970aeb2e350 // indirect
108122
google.golang.org/grpc v1.44.0 // indirect
109-
google.golang.org/protobuf v1.27.1 // indirect
123+
google.golang.org/protobuf v1.28.1 // indirect
110124
)

0 commit comments

Comments
 (0)