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

Golang SDK #166

Merged
merged 47 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
b89a8cf
Init Golang SDK
philkra Oct 20, 2023
1febb18
search table
philkra Nov 24, 2023
4ec4c4f
examples contrinued
philkra Nov 24, 2023
aaae6e2
delete snippets
philkra Nov 24, 2023
dea9b31
Add python sdk schema edit references (#167)
kostasb Oct 24, 2023
8837c0b
Point GH actions to FE (#168)
snide Oct 24, 2023
88b6f44
fix missing commas (#169)
kostasb Oct 26, 2023
61be3f3
Fix pricing docs URL (#170)
philkra Oct 30, 2023
5a3aa33
add updates for api docs (#165)
Nov 1, 2023
212cca2
Vercel deploys via github action (#173)
richardgill Nov 1, 2023
4e3dfd8
clarify Postman Bearer Token (#175)
kostasb Nov 7, 2023
2241de3
Update sparse-checkout in workflows (#176)
SferaDev Nov 7, 2023
ee816f4
Clarify main branch in Workflow (#178)
kostasb Nov 21, 2023
4060825
Update docs for search change (#177)
SferaDev Nov 21, 2023
11571ba
more create examples
philkra Nov 24, 2023
770b50a
replace tabs with spaces
philkra Nov 24, 2023
94775ea
align var names
philkra Nov 24, 2023
5f649d9
fix bulk insert example
philkra Nov 24, 2023
f1cc86a
one more
philkra Nov 24, 2023
761891b
linting
philkra Nov 24, 2023
73367e5
update samples
philkra Nov 25, 2023
f80e4ac
one more update example
philkra Nov 27, 2023
879cc30
vector search examples
philkra Nov 28, 2023
1b35647
ask samples
philkra Nov 28, 2023
2d2612b
trx snippet
philkra Nov 29, 2023
ed74dcb
update numeric ops
philkra Nov 29, 2023
2e49ab0
overview page
philkra Nov 29, 2023
399b714
change alert to warning
philkra Nov 29, 2023
6003a41
Update 040-SDK/030-Go/010-overview.mdx
philkra Nov 30, 2023
25c66db
Update 040-SDK/030-Go/010-overview.mdx
philkra Nov 30, 2023
c954c6b
Update 020-Getting-started/040-workflow.mdx
philkra Nov 30, 2023
e889d25
Update 020-Getting-started/040-workflow.mdx
philkra Nov 30, 2023
82e0f66
Update 020-Getting-started/040-workflow.mdx
philkra Nov 30, 2023
09c72f1
Update 040-SDK/020-Python/020-examples.mdx
philkra Nov 30, 2023
713a8dc
Merge branch 'main' into golang-sdk
philkra Nov 30, 2023
ecb62c9
add search items
philkra Nov 30, 2023
e28acd7
Update 040-SDK/030-Go/010-overview.mdx
philkra Nov 30, 2023
f85303c
make linter happy
philkra Nov 30, 2023
fcefe8e
Update 040-SDK/030-Go/010-overview.mdx
philkra Nov 30, 2023
c84f1f8
Apply suggestions from code review
philkra Dec 1, 2023
1c33631
indent
philkra Dec 4, 2023
b8aee2e
add search skel
philkra Dec 4, 2023
a9fbb14
indent
philkra Dec 4, 2023
608f291
add response struct
philkra Dec 4, 2023
0d3ab02
linter
philkra Dec 4, 2023
b9f2115
Apply suggestions from code review
philkra Dec 5, 2023
462659e
add install
philkra Dec 6, 2023
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
6 changes: 3 additions & 3 deletions 020-Getting-started/040-workflow.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ xata pull my_feature

On completion, notice Xata updated our client files in `src/xata.ts` and added migration files in the `.xata/migrations` folder. The later files are needed for GitHub to pick up on the changes we've made. Each time you make changes to your database branch, you will need to run `xata pull my_feature`.

### Commit your code, and create a GitHub PR
### Commit code and create a pull request

Once you are happy with your changes you should commit your files and create a GitHub PR for your Git branch. Xata's GitHub application will look for new files in your `.xata/migrations` folder so make sure they are included in the commit. Regardless of whether migrations are present, Xata will generate a preview branch for any PR you deploy using a `preview-{branch_name}` naming system. This is done by forking the `main` branch, applying schema migrations from the Pull Request branch and copying over sample data from `main` (excluding file attachments). The Xata app bot will leave a comment with the details.
Once you are happy with your changes you should commit your files and create a GitHub pull request for your Git branch. Xata's GitHub application will look for new files in your `.xata/migrations` folder so make sure they are included in the commit. Regardless of whether migrations are present, Xata will generate a preview branch for any pull request you deploy using a `preview-{branch_name}` naming system. This is done by forking the `main` branch, applying schema migrations from the pull request branch and copying over sample data from `main` (excluding file attachments). The Xata app bot will leave a comment with the details.

If you also have Vercel or Netlify configured for your repository you should see their bot make a similar comment. As long as your integrations were set up properly, the preview deployment that your host provides should be using the preview database that Xata provides. The workflow should be the same whether you are using the Netlify of Vercel integration.
If you also have Vercel or Netlify configured for your repository, you should see their bot make a similar comment. As long as your integrations were set up properly, the preview deployment that your host provides should use the preview database that Xata provides. The workflow should be the same whether you are using the Netlify of Vercel integration.

![Xata bot on GitHub](images/xata-bot.png)

Expand Down
2 changes: 1 addition & 1 deletion 040-SDK/020-Python/020-examples.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ print(to_rfc339(date_with_tz, tz_europe_vienna))

The Xata SDK provides schema editing operations under the [Table](https://xata-py.readthedocs.io/en/latest/api.html#xata.api.table.Table) class.

You can create tables, add and delete columns, or set the full table schema at once.
You can create tables, add and delete columns, or set the full table schema.

```python
from xata.client import XataClient
Expand Down
75 changes: 75 additions & 0 deletions 040-SDK/030-Go/010-overview.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
title: Xata SDK for Go
navTitle: SDK overview
keywords: ['go', 'golang', 'sdk']
description: Get to know Xata using Go
slug: sdk/go/overview
published: true
---

<Alert status="warning">
The Go SDK is currently in *alpha*. This means that it does not include all endpoints and is subject to potential
breaking changes before reaching its `1.0.0` stable General Availability (GA) release.
</Alert>

The Go SDK is available on GitHub: https://github.com/xataio/xata-go

## Install

```bash
go get github.com/xataio/xata-go@latest
```

## Configuration

The client needs a `XATA_API_KEY` environment variables for authentication and the `.xatarc` file for the database URL. Refer to [our installation guide](/docs/getting-started/installation#xatarc) to initialize your project with the `.xatarc` file.

## Example

After setting up your project and adding Xata credentials, create an example Go file.

The following example code demonstrates using the SDK to connect to Xata, retrieve a list of workspaces, and then print out details of the first workspace in the list.

In your text editor, paste the following code into your `example.go` file and save the file:

```go
package main

import (
"context"
"fmt"

"github.com/xataio/xata-go/xata"
)

func main() {
workspaces, _ := xata.NewWorkspacesClient()
resp, _ := workspaces.List(context.TODO())
fmt.Printf("%#v\n", *resp.Workspaces[0])
}
```

philkra marked this conversation as resolved.
Show resolved Hide resolved
In the terminal, enter `go run .` to run the code.

The code snippet calls `workspaces.List(ctx)` API to list all workspaces and prints the details of the first workspace with `fmt.Printf("%#v\n", *resp.Workspaces[0])`.

### Runnable Example

You can run the snippet from above on your machine by, pulling the example code from the xataio/xata-go repository

```bash
wget -O example.go "https://raw.githubusercontent.com/xataio/xata-go/main/examples/list_workspaces.go"
philkra marked this conversation as resolved.
Show resolved Hide resolved
go mod edit -module=example.com/mod
go mod download github.com/xataio/xata-go@main
```

Run the code with the command:

```bash
XATA_API_KEY="<INSERT_YOUR_API_KEY>" go run example.go
```

## Known Limitations

- [Numeric operations](/docs/sdk/update#numeric-operations) are currently not supported using the update API. As a workaround, you can use the transaction API [example](/docs/sdk/transaction).
- Not all Xata endpoints are available at the alpha state of the SDK. You can check the coverage [here](https://github.com/xataio/xata-go/issues/1).
155 changes: 123 additions & 32 deletions 040-SDK/101-get.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ published: true

The general form of a query is:

<TabbedCode tabs={['TypeScript', 'Python', 'SQL', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'SQL', 'JSON']}>

```ts
import { getXataClient } from "./xata";

const client = getXataClient();
const data = await client
.db[table]
.select([...])
.filter({ ... })
.sort({ ... })
.page({ ... })
.getMany();
.db[table]
.select([...])
.filter({ ... })
.sort({ ... })
.page({ ... })
.getMany();
```

```python
Expand All @@ -38,6 +38,26 @@ data = xata.data().query("{table_name}", {
...
},
"page": {
...
}
})
```

```go
searchClient, _ := xata.NewSearchAndFilterClient()
data, _ := searchClient.Query(context.TODO(), xata.QueryTableRequest{
TableName: "{tabel_name}",
Payload: xata.QueryTableRequestPayload{
Columns: []string{"col_1", ".."},
Filter: &xata.FilterExpression{
// ..
},
Sort: xata.NewSortExpressionFromStringSortOrderMapList(
// ..
),
Page: &xata.SearchPageConfig{
// ..
},
}
})
```
Expand All @@ -62,6 +82,7 @@ data = xata.data().query("{table_name}", {
...
},
"page": {
...
}
}
```
Expand All @@ -72,7 +93,7 @@ For the REST API example, note that a POST request is used, even though the data

All the requests are optional, so the simplest query request looks like this:

<TabbedCode tabs={['TypeScript', 'Python', 'SQL', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'SQL', 'JSON']}>

```ts
const teams = await xata.db.Teams.getMany();
Expand All @@ -82,6 +103,13 @@ const teams = await xata.db.Teams.getMany();
teams = xata.data().query("Teams")
```

```go
client, _ := xata.NewSearchAndFilterClient()
teams, _ := data.Query(context.TODO(), xata.QueryTableRequest{
TableName: "Teams",
})
```

philkra marked this conversation as resolved.
Show resolved Hide resolved
```jsonc
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql

Expand All @@ -98,7 +126,7 @@ teams = xata.data().query("Teams")

The response looks like this:

<TabbedCode tabs={['TypeScript', 'Python', 'SQL', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'SQL', 'JSON']}>
```ts
[
{
Expand All @@ -112,26 +140,32 @@ The response looks like this:
```

```python
"records": [
{
"id": "rec_c8hng2h26un90p8sr7k0",
"name": "Matrix",
"owner": {
"id": "myid"
},
"xata": {
"version": 0,
"createdAt": "2023-05-15T08:21:31.96526+01:00",
"updatedAt": "2023-05-15T21:58:54.072595+01:00"
}
}
],
"meta": {
"page": {
"cursor": "jMq7DcIwEIDhnjH-2sWRAsItAT2KkOU8bAgB3Zkqyu6IDei_",
"more": false
"records": [
{
"id": "rec_c8hng2h26un90p8sr7k0",
"name": "Matrix",
"owner": {
"id": "myid"
},
"xata": {
"version": 0,
"createdAt": "2023-05-15T08:21:31.96526+01:00",
"updatedAt": "2023-05-15T21:58:54.072595+01:00"
}
}
],
"meta": {
"page": {
"cursor": "jMq7DcIwEIDhnjH-2sWRAsItAT2KkOU8bAgB3Zkqyu6IDei_",
"more": false
}
}
```

```go
// type QueryTableResponse
Meta *RecordsMetadata
Records []*Record
```

```json
Expand Down Expand Up @@ -226,7 +260,7 @@ Unless there's a specific reason to prefer another method, getMany() is generall

You can retrieve a record with a given ID using a request like this:

<TabbedCode tabs={['TypeScript', 'Python', 'SQL', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'SQL', 'JSON']}>

```ts
const user = await xata.db.Users.read('myid');
Expand All @@ -236,6 +270,17 @@ const user = await xata.db.Users.read('myid');
user = xata.records().get("Users", "myid")
```

```go
client, _ := xata.NewRecordsClient()
user, _ := client.Get(context.TODO(), xata.GetRecordRequest{
RecordRequest: xata.RecordRequest{
DatabaseName: xata.String("{db}"),
TableName: "Users",
},
RecordID: "myid",
})
```

```jsonc
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql

Expand All @@ -257,7 +302,7 @@ By default, the Query API returns all columns of the queried table. For link col

For example, if you are only interested in the name and the city of the user, you can make a request like this:

<TabbedCode tabs={['TypeScript', 'Python', 'SQL', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'SQL', 'JSON']}>

```ts
const users = await xata.db.Users.select(['name', 'city']).getMany();
Expand All @@ -269,6 +314,16 @@ users = xata.data().query("Users", {
})
```

```go
client, err := xata.NewSearchAndFilterClient()
users, _ := client.Query(context.TODO(), xata.QueryTableRequest{
TableName: "Users",
Payload: xata.QueryTableRequestPayload{
Columns: []string{"name", "city"},
},
})
```

```jsonc
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql

Expand Down Expand Up @@ -310,7 +365,7 @@ It's worth noting that the special columns [id](/docs/concepts/data-model#id), [

The same syntax can be used to select columns from a linked table, therefore adding new columns to the response. For example, to query all the columns of the teams table and also add all the columns of the owner user, you can use:

<TabbedCode tabs={['TypeScript', 'Python', 'SQL', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'SQL', 'JSON']}>

```ts
const teams = await xata.db.Teams.select(['*', 'owner.*']).getMany();
Expand All @@ -322,6 +377,16 @@ users = xata.data().query("Users", {
})
```

```go
client, err := xata.NewSearchAndFilterClient()
users, _ := client.Query(context.TODO(), xata.QueryTableRequest{
TableName: "Users",
Payload: xata.QueryTableRequestPayload{
Columns: []string{"*", "owner.*"},
},
})
```

```jsonc
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql

Expand All @@ -341,7 +406,7 @@ users = xata.data().query("Users", {

You can do this transitively as well, for example:

<TabbedCode tabs={['TypeScript', 'Python', 'SQL', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'SQL', 'JSON']}>
```ts
const posts = await xata.db.Posts.select([
"title",
Expand All @@ -360,6 +425,20 @@ posts = xata.data().query("Posts", {
})
```

```go
client, err := xata.NewSearchAndFilterClient()
users, _ := client.Query(context.TODO(), xata.QueryTableRequest{
TableName: "Users",
Payload: xata.QueryTableRequestPayload{
Columns: []string{
"title",
"author.*",
"author.team.*",
},
},
})
```

```jsonc
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql

Expand All @@ -382,7 +461,7 @@ In this example, the `author` is a link column from `Posts` to `Users` and `team

By default, when retrieving a record by its ID, only the ID column of linked records is included in the response. However, you have the option to request the linked columns to be expanded. In the following examples, the code is using the `.read()` method to retrieve a record with the ID `myid` from the `Posts` table. The code specifies it wants the `author` column to be expanded and to retrieve more information from the `author` linked record.

<TabbedCode tabs={['TypeScript', 'Python', 'SQL', 'JSON']}>
<TabbedCode tabs={['TypeScript', 'Python', 'Go', 'SQL', 'JSON']}>

```ts
const user = await xata.db.Posts.read('myid', ['author.*']);
Expand All @@ -392,6 +471,18 @@ const user = await xata.db.Posts.read('myid', ['author.*']);
user = xata.records().get("Posts", "myid", columns=["author.*"])
```

```go
client, _ := xata.NewRecordsClient()
record, _ := client.Get(context.TODO(), xata.GetRecordRequest{
RecordRequest: xata.RecordRequest{
DatabaseName: xata.String("{db}"),
TableName: "Users",
},
RecordID: "myid",
Columns: []string{"author.*"},
})
```

```jsonc
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql

Expand Down
Loading