Skip to content

Commit fc5a591

Browse files
Hasura: track views
1 parent 55746d2 commit fc5a591

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

hasura/api.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ func (api *API) post(endpoint string, args map[string]string, body interface{},
8181
}
8282
defer resp.Body.Close()
8383

84-
decoder := json.NewDecoder(resp.Body)
85-
8684
if resp.StatusCode != http.StatusOK {
8785
bodyBytes, err := ioutil.ReadAll(resp.Body)
8886
if err != nil {
@@ -91,7 +89,11 @@ func (api *API) post(endpoint string, args map[string]string, body interface{},
9189
return errors.Errorf("Invalid status code: %s %s", resp.Status, string(bodyBytes))
9290
}
9391

94-
return decoder.Decode(output)
92+
if output == nil {
93+
return nil
94+
}
95+
96+
return json.NewDecoder(resp.Body).Decode(output)
9597
}
9698

9799
// Health
@@ -134,3 +136,15 @@ func (api *API) ReplaceMetadata(data interface{}) error {
134136
}
135137
return errors.Errorf("Can't replace hasura's metadata: %s", resp.Message)
136138
}
139+
140+
// TrackTable -
141+
func (api *API) TrackTable(schema, name string) error {
142+
req := request{
143+
Type: "track_table",
144+
Args: map[string]string{
145+
"schema": schema,
146+
"name": name,
147+
},
148+
}
149+
return api.post("/v1/query", nil, req, nil)
150+
}

hasura/hasura.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

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

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

6565
log.Info("Replacing metadata...")
66-
return api.ReplaceMetadata(metadata)
66+
if err := api.ReplaceMetadata(metadata); err != nil {
67+
return err
68+
}
69+
70+
log.Info("Tracking views...")
71+
for i := range views {
72+
if err := api.TrackTable("public", views[i]); err != nil {
73+
return err
74+
}
75+
}
76+
77+
return nil
6778
}
6879

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

0 commit comments

Comments
 (0)