Skip to content

Commit 422634e

Browse files
authored
Bump to convex 1.3.1 and add format param to api calls (#2)
1 parent 35783ca commit 422634e

File tree

21 files changed

+809
-1094
lines changed

21 files changed

+809
-1094
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
FROM --platform=$BUILDPLATFORM cgr.dev/chainguard/go:1.20 as build
1+
FROM --platform=$BUILDPLATFORM cgr.dev/chainguard/wolfi-base as build
2+
RUN apk update && apk add build-base git openssh go-1.20
23

34
WORKDIR /work
45

convex.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
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

2524
type ConvexDB struct {
2625
url string
2726
token string
2827
}
2928

3029
type 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

3535
type ConvexResponse struct {
@@ -43,7 +43,7 @@ func NewConvexDB(url string, token string) *ConvexDB {
4343
}
4444

4545
func (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

7575
func (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

105106
func (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

132133
func (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

173174
func (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
}

convex.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
{
2-
"project": "golink",
3-
"team": "convex",
4-
"prodUrl": "https://merry-grouse-70.convex.cloud",
5-
"functions": "src/convex/",
6-
"authInfo": []
2+
"functions": "src/convex/"
73
}

convex_test.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,29 @@ import (
1010

1111
"github.com/google/go-cmp/cmp"
1212
"github.com/google/go-cmp/cmp/cmpopts"
13+
"github.com/joho/godotenv"
1314
)
1415

1516
func clear(c *ConvexDB) {
16-
c.mutation(&UdfExecution{Path: "clear", Args: []interface{}{}})
17+
c.mutation(&UdfExecution{Path: "clear", Args: map[string]interface{}{}, Format: "json"})
18+
}
19+
20+
func getDbUrl() string {
21+
envLocal, err := godotenv.Read(".env.local")
22+
if err != nil {
23+
return "https://feeble-gull-946.convex.cloud"
24+
}
25+
url := envLocal["VITE_CONVEX_URL"]
26+
if len(url) == 0 {
27+
url = "https://feeble-gull-946.convex.cloud"
28+
}
29+
return url
1730
}
1831

1932
// Test saving and loading links for SQLiteDB
2033
func Test_Convex_SaveLoadLinks(t *testing.T) {
21-
db := NewConvexDB("https://feeble-gull-946.convex.cloud", "test")
34+
url := getDbUrl()
35+
db := NewConvexDB(url, "test")
2236
clear(db)
2337
defer clear(db)
2438

@@ -56,11 +70,11 @@ func Test_Convex_SaveLoadLinks(t *testing.T) {
5670

5771
// Test saving and loading stats for SQLiteDB
5872
func Test_Convex_SaveLoadStats(t *testing.T) {
59-
db := NewConvexDB("https://feeble-gull-946.convex.cloud", "test")
73+
url := getDbUrl()
74+
db := NewConvexDB(url, "test")
6075
clear(db)
6176
defer clear(db)
6277

63-
6478
// preload some links
6579
links := []*Link{
6680
{Short: "a"},

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require (
4040
github.com/illarion/gonotify v1.0.1 // indirect
4141
github.com/insomniacslk/dhcp v0.0.0-20221215072855-de60144f33f8 // indirect
4242
github.com/jmespath/go-jmespath v0.4.0 // indirect
43+
github.com/joho/godotenv v1.5.1 // indirect
4344
github.com/josharian/native v1.1.1-0.20230202152459-5c7d0dd6ab86 // indirect
4445
github.com/jsimonetti/rtnetlink v1.1.2-0.20220408201609-d380b505068b // indirect
4546
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
115115
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
116116
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
117117
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
118+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
119+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
118120
github.com/josharian/native v1.0.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
119121
github.com/josharian/native v1.0.1-0.20221213033349-c1e37c09b531/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
120122
github.com/josharian/native v1.1.1-0.20230202152459-5c7d0dd6ab86 h1:elKwZS1OcdQ0WwEDBeqxKwb7WB62QX8bvZ/FJnVXIfk=

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
"typescript": "^4.9.5"
1313
},
1414
"dependencies": {
15-
"convex": "^0.9.1"
15+
"convex": "^1.3.1"
1616
}
1717
}

src/convex/_generated/api.d.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
11
/* eslint-disable */
22
/**
3-
* Generated API.
3+
* Generated `api` utility.
44
*
55
* THIS CODE IS AUTOMATICALLY GENERATED.
66
*
7-
* Generated by convex@0.9.1.
8-
* To regenerate, run `npx convex codegen`.
7+
* Generated by convex@1.3.1.
8+
* To regenerate, run `npx convex dev`.
99
* @module
1010
*/
1111

12-
import type { ApiFromModules } from "convex/api";
12+
import type {
13+
ApiFromModules,
14+
FilterApi,
15+
FunctionReference,
16+
} from "convex/server";
1317
import type * as clear from "../clear";
1418
import type * as load from "../load";
1519
import type * as stats from "../stats";
1620
import type * as store from "../store";
1721

1822
/**
19-
* A type describing your app's public Convex API.
23+
* A utility for referencing Convex functions in your app's API.
2024
*
21-
* This `API` type includes information about the arguments and return
22-
* types of your app's query and mutation functions.
23-
*
24-
* This type should be used with type-parameterized classes like
25-
* `ConvexReactClient` to create app-specific types.
25+
* Usage:
26+
* ```js
27+
* const myFunctionReference = api.myModule.myFunction;
28+
* ```
2629
*/
27-
export type API = ApiFromModules<{
30+
declare const fullApi: ApiFromModules<{
2831
clear: typeof clear;
2932
load: typeof load;
3033
stats: typeof stats;
3134
store: typeof store;
3235
}>;
36+
export declare const api: FilterApi<
37+
typeof fullApi,
38+
FunctionReference<any, "public">
39+
>;
40+
export declare const internal: FilterApi<
41+
typeof fullApi,
42+
FunctionReference<any, "internal">
43+
>;

src/convex/_generated/api.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* eslint-disable */
2+
/**
3+
* Generated `api` utility.
4+
*
5+
* THIS CODE IS AUTOMATICALLY GENERATED.
6+
*
7+
* Generated by [email protected].
8+
* To regenerate, run `npx convex dev`.
9+
* @module
10+
*/
11+
12+
import { anyApi } from "convex/server";
13+
14+
/**
15+
* A utility for referencing Convex functions in your app's API.
16+
*
17+
* Usage:
18+
* ```js
19+
* const myFunctionReference = api.myModule.myFunction;
20+
* ```
21+
*/
22+
export const api = anyApi;
23+
export const internal = anyApi;

src/convex/_generated/dataModel.d.ts

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
*
55
* THIS CODE IS AUTOMATICALLY GENERATED.
66
*
7-
* Generated by convex@0.9.1.
8-
* To regenerate, run `npx convex codegen`.
7+
* Generated by convex@1.3.1.
8+
* To regenerate, run `npx convex dev`.
99
* @module
1010
*/
1111

12-
import type { DataModelFromSchemaDefinition } from "convex/schema";
12+
import type { DataModelFromSchemaDefinition } from "convex/server";
1313
import type { DocumentByName, TableNamesInDataModel } from "convex/server";
14-
import { GenericId, GenericIdConstructor } from "convex/values";
14+
import type { GenericId } from "convex/values";
1515
import schema from "../schema";
1616

1717
/**
@@ -24,7 +24,7 @@ export type TableNames = TableNamesInDataModel<DataModel>;
2424
*
2525
* @typeParam TableName - A string literal type of the table name (like "users").
2626
*/
27-
export type Document<TableName extends TableNames> = DocumentByName<
27+
export type Doc<TableName extends TableNames> = DocumentByName<
2828
DataModel,
2929
TableName
3030
>;
@@ -37,28 +37,13 @@ export type Document<TableName extends TableNames> = DocumentByName<
3737
*
3838
* Documents can be loaded using `db.get(id)` in query and mutation functions.
3939
*
40-
* **Important**: Use `myId.equals(otherId)` to check for equality.
41-
* Using `===` will not work because two different instances of `Id` can refer
42-
* to the same document.
40+
* IDs are just strings at runtime, but this type can be used to distinguish them from other
41+
* strings when type checking.
4342
*
4443
* @typeParam TableName - A string literal type of the table name (like "users").
4544
*/
4645
export type Id<TableName extends TableNames> = GenericId<TableName>;
4746

48-
/**
49-
* An identifier for a document in Convex.
50-
*
51-
* Convex documents are uniquely identified by their `Id`, which is accessible
52-
* on the `_id` field. To learn more, see [Document IDs](https://docs.convex.dev/using/document-ids).
53-
*
54-
* Documents can be loaded using `db.get(id)` in query and mutation functions.
55-
*
56-
* **Important**: Use `myId.equals(otherId)` to check for equality.
57-
* Using `===` will not work because two different instances of `Id` can refer
58-
* to the same document.
59-
*/
60-
export declare const Id: GenericIdConstructor<TableNames>;
61-
6247
/**
6348
* A type describing your Convex data model.
6449
*

0 commit comments

Comments
 (0)