44 "bytes"
55 "encoding/json"
66 "fmt"
7+ "io"
78 "io/fs"
89 "net/http"
910 "time"
@@ -18,18 +19,17 @@ type LinkDocument struct {
1819 Owner string `json:"owner"`
1920}
2021
21- type StatsMap struct {
22- Stats [][2 ]interface {} `json:"$map"`
23- }
22+ type StatsMap = map [string ]interface {}
2423
2524type ConvexDB struct {
2625 url string
2726 token string
2827}
2928
3029type UdfExecution struct {
31- Path string `json:"path"`
32- Args []interface {} `json:"args"`
30+ Path string `json:"path"`
31+ Args map [string ]interface {} `json:"args"`
32+ Format string `json:"format"`
3333}
3434
3535type ConvexResponse struct {
@@ -43,7 +43,7 @@ func NewConvexDB(url string, token string) *ConvexDB {
4343}
4444
4545func (c * ConvexDB ) mutation (args * UdfExecution ) error {
46- args .Args = append ( args . Args , c .token )
46+ args .Args [ "token" ] = c .token
4747 url := fmt .Sprintf ("%s/api/mutation" , c .url )
4848 encodedArgs , err := json .Marshal (args )
4949 if err != nil {
@@ -73,7 +73,7 @@ func (c *ConvexDB) mutation(args *UdfExecution) error {
7373}
7474
7575func (c * ConvexDB ) query (args * UdfExecution ) (json.RawMessage , error ) {
76- args .Args = append ( args . Args , c .token )
76+ args .Args [ "token" ] = c .token
7777 url := fmt .Sprintf ("%s/api/query" , c .url )
7878 encodedArgs , err := json .Marshal (args )
7979 if err != nil {
@@ -84,7 +84,8 @@ func (c *ConvexDB) query(args *UdfExecution) (json.RawMessage, error) {
8484 return nil , err
8585 }
8686 if resp .StatusCode != 200 {
87- return nil , fmt .Errorf ("unexpected status code from Convex: %d" , resp .StatusCode )
87+ body , _ := io .ReadAll (resp .Body )
88+ return nil , fmt .Errorf ("unexpected status code from Convex: %d: %s" , resp .StatusCode , body )
8889 }
8990
9091 defer resp .Body .Close ()
@@ -103,7 +104,7 @@ func (c *ConvexDB) query(args *UdfExecution) (json.RawMessage, error) {
103104}
104105
105106func (c * ConvexDB ) LoadAll () ([]* Link , error ) {
106- args := UdfExecution {"load:loadAll" , [ ]interface {}{}}
107+ args := UdfExecution {"load:loadAll" , map [ string ]interface {}{}, "json" }
107108 resp , err := c .query (& args )
108109 if err != nil {
109110 return nil , err
@@ -130,7 +131,7 @@ func (c *ConvexDB) LoadAll() ([]*Link, error) {
130131}
131132
132133func (c * ConvexDB ) Load (short string ) (* Link , error ) {
133- args := UdfExecution {"load:loadOne" , [ ]interface {}{linkID (short )}}
134+ args := UdfExecution {"load:loadOne" , map [ string ]interface {}{"normalizedId" : linkID (short )}, "json" }
134135 resp , err := c .query (& args )
135136 if err != nil {
136137 return nil , err
@@ -166,12 +167,12 @@ func (c *ConvexDB) Save(link *Link) error {
166167 LastEdit : float64 (link .LastEdit .Unix ()),
167168 Owner : link .Owner ,
168169 }
169- args := UdfExecution {"store" , [ ]interface {}{document }}
170+ args := UdfExecution {"store" , map [ string ]interface {}{"link" : document }, "json" }
170171 return c .mutation (& args )
171172}
172173
173174func (c * ConvexDB ) LoadStats () (ClickStats , error ) {
174- args := UdfExecution {"stats:loadStats" , [ ]interface {}{}}
175+ args := UdfExecution {"stats:loadStats" , map [ string ]interface {}{}, "json" }
175176 response , err := c .query (& args )
176177 if err != nil {
177178 return nil , err
@@ -184,12 +185,12 @@ func (c *ConvexDB) LoadStats() (ClickStats, error) {
184185 return nil , err
185186 }
186187 clicks := make (ClickStats )
187- for _ , entry := range stats . Stats {
188- num , err := entry [ 1 ] .(json.Number ).Float64 ()
188+ for k , v := range stats {
189+ num , err := v .(json.Number ).Float64 ()
189190 if err != nil {
190191 return nil , err
191192 }
192- clicks [entry [ 0 ].( string ) ] = int (num )
193+ clicks [k ] = int (num )
193194 }
194195 return clicks , nil
195196}
@@ -199,6 +200,6 @@ func (c *ConvexDB) SaveStats(stats ClickStats) error {
199200 for id , clicks := range stats {
200201 mungedStats [linkID (id )] = clicks
201202 }
202- args := UdfExecution {"stats:saveStats" , [ ]interface {}{mungedStats }}
203+ args := UdfExecution {"stats:saveStats" , map [ string ]interface {}{"stats" : mungedStats }, "json" }
203204 return c .mutation (& args )
204205}
0 commit comments