From 529f6677f95b69ef69ed387d6a3ea8b3a9a5cc29 Mon Sep 17 00:00:00 2001 From: Adam Reese Date: Tue, 12 Sep 2023 15:50:29 -0700 Subject: [PATCH] Add TinyGo SQLite to the API guide Signed-off-by: Adam Reese --- content/spin/sqlite-api-guide.md | 56 +++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/content/spin/sqlite-api-guide.md b/content/spin/sqlite-api-guide.md index 292bc911d..d28266876 100644 --- a/content/spin/sqlite-api-guide.md +++ b/content/spin/sqlite-api-guide.md @@ -152,7 +152,61 @@ def handle_request(request): {{ startTab "TinyGo"}} -The Go SDK doesn't currently surface the SQLite API. +The Go SDK is implemented as a driver for the standard library's [database/sql](https://pkg.go.dev/database/sql) interface. + +```go +package main + +import ( + "encoding/json" + "net/http" + + spinhttp "github.com/fermyon/spin/sdk/go/http" + "github.com/fermyon/spin/sdk/go/sqlite" +) + +type Todo struct { + ID string + Description string + Due string +} + +func init() { + spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) { + db := sqlite.Open("default") + defer db.Close() + + _, err := db.Exec("INSERT INTO todos (description, due) VALUES (?, ?)", "Try out Spin SQLite", "Friday") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + rows, err := db.Query("SELECT id, description, due FROM todos") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + var todos []*Todo + for rows.Next() { + var todo Todo + if err := rows.Scan(&todo.ID, &todo.Description, &todo.Due); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + todos = append(todos, &todo) + } + json.NewEncoder(w).Encode(todos) + }) +} + +func main() {} +``` + +**General Notes** + +A convince function `sqlite.Open()` is provided to create a database connection. Because the `http.Handle` function is inside the `init()` function the Spin SQLite driver cannot be initialized the same way as other drivers using [sql.Open](https://pkg.go.dev/database/sql#Open). {{ blockEnd }}