Skip to content

Commit d13f3e9

Browse files
committed
Add README and examples
1 parent 6f64a0e commit d13f3e9

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# quickdb
2+
3+
## Usage
4+
5+
```go
6+
package main
7+
8+
import (
9+
"context"
10+
"log"
11+
"os"
12+
13+
"github.com/sqlc-dev/quickdb"
14+
pb "github.com/sqlc-dev/quickdb/v1"
15+
)
16+
17+
func main() {
18+
ctx := context.Background()
19+
projectID := os.Getenv("SQLC_PROJECT_ID")
20+
authToken := os.Getenv("SQLC_AUTH_TOKEN")
21+
client, err := quickdb.NewClient(projectID, authToken)
22+
if err != nil {
23+
log.Fatal(err)
24+
}
25+
26+
resp, err := client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{
27+
Engine: "postgresql",
28+
})
29+
if err != nil {
30+
log.Fatal(err)
31+
}
32+
33+
log.Println(resp.Uri)
34+
35+
_, err = client.DropEphemeralDatabase(ctx, &pb.DropEphemeralDatabaseRequest{
36+
DatabaseId: resp.DatabaseId,
37+
})
38+
if err != nil {
39+
log.Fatal(err)
40+
}
41+
}
42+
```

examples/main_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package example
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/sqlc-dev/quickdb"
12+
pb "github.com/sqlc-dev/quickdb/v1"
13+
)
14+
15+
func createDatabase(ctx context.Context, path string) (*pb.CreateEphemeralDatabaseResponse, error) {
16+
projectID := os.Getenv("SQLC_PROJECT_ID")
17+
authToken := os.Getenv("SQLC_AUTH_TOKEN")
18+
if projectID == "" || authToken == "" {
19+
return nil, fmt.Errorf("missing project id and auth token")
20+
}
21+
22+
client, err := quickdb.NewClient(projectID, authToken)
23+
if err != nil {
24+
return nil, fmt.Errorf("new client: %w", err)
25+
}
26+
27+
var migrations []string
28+
files, err := os.ReadDir(path)
29+
if err != nil {
30+
return nil, fmt.Errorf("new client: %w", err)
31+
}
32+
for _, f := range files {
33+
contents, err := os.ReadFile(filepath.Join(path, f.Name()))
34+
if err != nil {
35+
return nil, fmt.Errorf("read file: %s", err)
36+
}
37+
migrations = append(migrations, string(contents))
38+
}
39+
40+
resp, err := client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{
41+
Engine: "postgresql",
42+
Region: quickdb.GetClosestRegion(),
43+
Migrations: migrations,
44+
})
45+
if err != nil {
46+
return nil, fmt.Errorf("create db: %w", err)
47+
}
48+
49+
return resp, nil
50+
51+
// cleanup := func() {
52+
// client.DropEphemeralDatabase(ctx, &pb.DropEphemeralDatabaseRequest{
53+
// DatabaseId: resp.DatabaseId,
54+
// })
55+
// }
56+
}
57+
58+
func TestMain(m *testing.M) {
59+
ctx := context.Background()
60+
resp, err := createDatabase(ctx, "path/to/migrations")
61+
if err != nil {
62+
log.Fatal(err)
63+
}
64+
log.Println(resp.Uri)
65+
66+
os.Exit(m.Run())
67+
}

0 commit comments

Comments
 (0)