Skip to content

Commit

Permalink
fix(typegen): use default schemas only if flag is not specified (#1947)
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge authored Feb 14, 2024
1 parent 65a9e32 commit e3ce7a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
30 changes: 12 additions & 18 deletions internal/gen/types/typescript/typescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ import (
)

func Run(ctx context.Context, useLocal bool, useLinked bool, projectId string, dbUrl string, schemas []string, postgrestV9Compat bool, fsys afero.Fs) error {
coalesce := func(args ...[]string) []string {
for _, arg := range args {
if len(arg) != 0 {
return arg
}
// Generating types on `projectId` and `dbUrl` should work without `supabase init`
// i.e. only load config on `--local` or `--linked` flags.
if useLocal || useLinked {
if err := utils.LoadConfigFS(fsys); err != nil {
return err
}
return []string{}
}

// Generating types on `projectId` and `dbUrl` should work without `supabase
// init` - i.e. we shouldn't try to load the config for these cases.
// Add default schemas if --schema flag is not specified
if len(schemas) == 0 {
schemas = utils.RemoveDuplicates(append([]string{"public"}, utils.Config.Api.Schemas...))
}
included := strings.Join(schemas, ",")

if projectId != "" {
included := strings.Join(coalesce(schemas, []string{"public"}), ",")
resp, err := utils.GetSupabase().GetTypescriptTypesWithResponse(ctx, projectId, &api.GetTypescriptTypesParams{
IncludedSchemas: &included,
})
Expand Down Expand Up @@ -68,7 +68,7 @@ func Run(ctx context.Context, useLocal bool, useLinked bool, projectId string, d
Env: []string{
"PG_META_DB_URL=" + escaped,
"PG_META_GENERATE_TYPES=typescript",
"PG_META_GENERATE_TYPES_INCLUDED_SCHEMAS=" + strings.Join(coalesce(schemas, []string{"public"}), ","),
"PG_META_GENERATE_TYPES_INCLUDED_SCHEMAS=" + included,
fmt.Sprintf("PG_META_GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS=%v", !postgrestV9Compat),
},
Cmd: []string{"node", "dist/server/server.js"},
Expand All @@ -83,11 +83,6 @@ func Run(ctx context.Context, useLocal bool, useLinked bool, projectId string, d
)
}

// only load config on `--local` or `--linked`
if err := utils.LoadConfigFS(fsys); err != nil {
return err
}

if useLocal {
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
return err
Expand All @@ -103,7 +98,7 @@ func Run(ctx context.Context, useLocal bool, useLinked bool, projectId string, d
[]string{
"PG_META_DB_HOST=" + utils.DbId,
"PG_META_GENERATE_TYPES=typescript",
"PG_META_GENERATE_TYPES_INCLUDED_SCHEMAS=" + strings.Join(coalesce(schemas, utils.Config.Api.Schemas, []string{"public"}), ","),
"PG_META_GENERATE_TYPES_INCLUDED_SCHEMAS=" + included,
fmt.Sprintf("PG_META_GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS=%v", !postgrestV9Compat),
},
[]string{"node", "dist/server/server.js"},
Expand All @@ -118,7 +113,6 @@ func Run(ctx context.Context, useLocal bool, useLinked bool, projectId string, d
return err
}

included := strings.Join(coalesce(schemas, utils.Config.Api.Schemas, []string{"public"}), ",")
resp, err := utils.GetSupabase().GetTypescriptTypesWithResponse(ctx, projectId, &api.GetTypescriptTypesParams{
IncludedSchemas: &included,
})
Expand Down
6 changes: 3 additions & 3 deletions internal/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,8 @@ func LoadConfigFS(fsys afero.Fs) error {
}
}
// Append required schemas if they are missing
Config.Api.Schemas = removeDuplicates(append([]string{"public", "storage"}, Config.Api.Schemas...))
Config.Api.ExtraSearchPath = removeDuplicates(append([]string{"public"}, Config.Api.ExtraSearchPath...))
Config.Api.Schemas = RemoveDuplicates(append([]string{"public", "storage"}, Config.Api.Schemas...))
Config.Api.ExtraSearchPath = RemoveDuplicates(append([]string{"public"}, Config.Api.ExtraSearchPath...))
// Validate db config
if Config.Db.Port == 0 {
return errors.New("Missing required field in config: db.port")
Expand Down Expand Up @@ -789,7 +789,7 @@ func WriteConfig(fsys afero.Fs, _test bool) error {
return InitConfig(InitParams{}, fsys)
}

func removeDuplicates(slice []string) (result []string) {
func RemoveDuplicates(slice []string) (result []string) {
set := make(map[string]struct{})
for _, item := range slice {
if _, exists := set[item]; !exists {
Expand Down

0 comments on commit e3ce7a6

Please sign in to comment.