diff --git a/hasura/api.go b/hasura/api.go index edba74d..c7372b6 100644 --- a/hasura/api.go +++ b/hasura/api.go @@ -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 { @@ -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 @@ -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) +} diff --git a/hasura/hasura.go b/hasura/hasura.go index 04e4a43..4776bbb 100644 --- a/hasura/hasura.go +++ b/hasura/hasura.go @@ -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...") @@ -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.