Skip to content

Commit

Permalink
Hasura: track views
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Jun 23, 2021
1 parent 55746d2 commit fc5a591
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
20 changes: 17 additions & 3 deletions hasura/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ func (api *API) post(endpoint string, args map[string]string, body interface{},
}
defer resp.Body.Close()

decoder := json.NewDecoder(resp.Body)

if resp.StatusCode != http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand All @@ -91,7 +89,11 @@ func (api *API) post(endpoint string, args map[string]string, body interface{},
return errors.Errorf("Invalid status code: %s %s", resp.Status, string(bodyBytes))
}

return decoder.Decode(output)
if output == nil {
return nil
}

return json.NewDecoder(resp.Body).Decode(output)
}

// Health
Expand Down Expand Up @@ -134,3 +136,15 @@ func (api *API) ReplaceMetadata(data interface{}) error {
}
return errors.Errorf("Can't replace hasura's metadata: %s", resp.Message)
}

// TrackTable -
func (api *API) TrackTable(schema, name string) error {
req := request{
Type: "track_table",
Args: map[string]string{
"schema": schema,
"name": name,
},
}
return api.post("/v1/query", nil, req, nil)
}
15 changes: 13 additions & 2 deletions hasura/hasura.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// Create - creates hasura models
func Create(hasura config.Hasura, cfg config.Database, models ...interface{}) error {
func Create(hasura config.Hasura, cfg config.Database, views []string, models ...interface{}) error {
api := New(hasura.URL, hasura.Secret)

log.Info("Waiting hasura is up...")
Expand Down Expand Up @@ -63,7 +63,18 @@ func Create(hasura config.Hasura, cfg config.Database, models ...interface{}) er
metadata["tables"] = dataTables

log.Info("Replacing metadata...")
return api.ReplaceMetadata(metadata)
if err := api.ReplaceMetadata(metadata); err != nil {
return err
}

log.Info("Tracking views...")
for i := range views {
if err := api.TrackTable("public", views[i]); err != nil {
return err
}
}

return nil
}

// Generate - creates hasura table structure in JSON from `models`. `models` should be pointer to your table models. `cfg` is DB config from YAML.
Expand Down

0 comments on commit fc5a591

Please sign in to comment.