Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TinyGo SQLite to the API guide #883

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion content/spin/language-support-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ This page contains information about language support for Spin features:
| [Outbound HTTP](/spin/go-components#sending-outbound-http-requests) | Supported |
| [Configuration Variables](/spin/dynamic-configuration#custom-config-variables) | Supported |
| [Key Value Storage](/spin/kv-store-api-guide) | Supported |
| SQLite Storage | Not supported |
| [SQLite Storage](/spin/sqlite-api-guide) | Supported |
| MySQL | Not Supported |
| PostgreSQL | Not Supported |
| [Outbound Redis](/spin/go-components#storing-data-in-redis-from-go-components) | Supported |
Expand Down
56 changes: 55 additions & 1 deletion content/spin/sqlite-api-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 convenience 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 }}

Expand Down