-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy path+page.markdoc
More file actions
311 lines (251 loc) · 7.46 KB
/
+page.markdoc
File metadata and controls
311 lines (251 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
---
layout: article
title: Start with Go
description: Integrating Appwrite with your Go backend application is a quick and simple process. Get your backend up and running with our step-by-step guide.
difficulty: beginner
readtime: 5
back: /docs/quick-starts
---
Learn how to set up your first Go project powered by Appwrite.
{% section #step-1 step=1 title="Create project" %}
Head to the [Appwrite Console](https://cloud.appwrite.io/console).
If this is your first time using Appwrite, create an account and create your first project.
{% only_dark %}

{% /only_dark %}
{% only_light %}

{% /only_light %}
Then, under **Integrate with your server**, add an **API Key** with the following scopes.
{% only_dark %}

{% /only_dark %}
{% only_light %}

{% /only_light %}
| Category {% width=120 %} | Required scopes | Purpose |
|-----------|-----------------------|---------|
| Database | `databases.write` | Allows API key to create, update, and delete [databases](/docs/products/databases/databases). |
| | `tables.write` | Allows API key to create, update, and delete [tables](/docs/products/databases/tables). |
| | `columns.write` | Allows API key to create, update, and delete [columns](/docs/products/databases/tables#columns). |
| | `rows.read` | Allows API key to read [rows](/docs/products/databases/rows). |
| | `rows.write` | Allows API key to create, update, and delete [rows](/docs/products/databases/rows). |
Other scopes are optional.
{% /section %}
{% section #step-2 step=2 title="Create Go project" %}
Create a go application.
```sh
mkdir my-app
cd my-app
go mod init go-appwrite/main
```
{% /section %}
{% section #step-3 step=3 title="Install Appwrite" %}
Install the Go Appwrite SDK.
```sh
go get github.com/appwrite/sdk-for-go
```
{% /section %}
{% section #step-4 step=4 title="Import Appwrite" %}
Find your project ID in the **Settings** page. Also, click on the **View API Keys** button to find the API key that was created earlier.
{% only_dark %}

{% /only_dark %}
{% only_light %}

{% /only_light %}
Create a new file called `app.go`, initialize a function, and initialize the Appwrite Client. Replace `<PROJECT_ID>` with your project ID and `<YOUR_API_KEY>` with your API key. Import the Appwrite dependencies for appwrite, client, databases, and models.
```go
package main
import (
"github.com/appwrite/sdk-for-go/appwrite"
"github.com/appwrite/sdk-for-go/client"
"github.com/appwrite/sdk-for-go/tablesdb"
"github.com/appwrite/sdk-for-go/models"
"github.com/appwrite/sdk-for-go/query"
)
var (
appwriteClient client.Client
todoDatabase *models.Database
todoTable *models.Table
appwriteDatabases *tablesdb.TablesDB
)
func main() {
appwriteClient = appwrite.NewClient(
appwrite.WithProject("<PROJECT_KEY>"),
appwrite.WithKey("<API_KEY>"),
)
}
```
{% /section %}
{% section #step-5 step=5 title="Initialize database" %}
Once the Appwrite Client is initialized, create a function to configure a todo table. Import the id Appwrite dependency by adding `"github.com/appwrite/sdk-for-go/id"` to the imported dependencies list.
```go
func prepareDatabase() {
tablesDB = appwrite.NewTablesDB(appwriteClient)
todoDatabase, _ = tablesDB.Create(
id.Unique(),
"TodosDB",
)
todoTable, _ = tablesDB.CreateTable(
todoDatabase.Id,
id.Unique(),
"Todos",
)
tablesDB.CreateStringColumn(
todoDatabase.Id,
todoTable.Id,
"title",
255,
true,
)
tablesDB.CreateStringColumn(
todoDatabase.Id,
todoTable.Id,
"description",
255,
false,
)
tablesDB.CreateBooleanColumn(
todoDatabase.Id,
todoTable.Id,
"isComplete",
true,
)
}
```
{% /section %}
{% section #step-6 step=6 title="Add rows" %}
Create a function to add some mock data to your new table.
```go
func seedDatabase() {
testTodo1 := map[string]interface{}{
"title": "Buy apples",
"description": "At least 2KGs",
"isComplete": true,
}
testTodo2 := map[string]interface{}{
"title": "Wash the apples",
"isComplete": true,
}
testTodo3 := map[string]interface{}{
"title": "Cut the apples",
"description": "Don't forget to pack them in a box",
"isComplete": false,
}
tablesDB.createRow(
todoDatabase.Id,
todoTable.Id,
id.Unique(),
testTodo1,
)
tablesDB.createRow(
todoDatabase.Id,
todoTable.Id,
id.Unique(),
testTodo2,
)
tablesDB.createRow(
todoDatabase.Id,
todoTable.Id,
id.Unique(),
testTodo3,
)
}
```
{% /section %}
{% section #step-7 step=7 title="Retrieve rows" %}
Create a function to retrieve the mock todo data.
```go
type Todo struct {
Title string `json:"title"`
Description string `json:"description"`
IsComplete bool `json:"isComplete"`
}
type TodoList struct {
*models.DocumentList
Documents []Todo `json:"rows"`
}
func getTodos() {
// Retrieve rows (default limit is 25)
todoResponse, _ := tablesDB.ListRows(
todoDatabase.Id,
todoTable.Id,
)
var todos TodoList
todoResponse.Decode(&todos)
fmt.Println("Todos:")
for _, todo := range todos.Documents {
fmt.Printf("Title: %s\nDescription: %s\nIs Todo Complete: %t\n\n", todo.Title, todo.Description, todo.IsComplete)
}
}
func getCompletedTodos() {
// Use queries to filter completed todos with pagination
todoResponse, _ := tablesDB.ListRows(
todoDatabase.Id,
todoTable.Id,
tablesDB.WithListRowsQueries([]string{
query.Equal("isComplete", true),
query.OrderDesc("$createdAt"),
query.Limit(5),
}),
)
var todos TodoList
todoResponse.Decode(&todos)
fmt.Println("Completed todos (limited to 5):")
for _, todo := range todos.Documents {
fmt.Printf("Title: %s\nDescription: %s\nIs Todo Complete: %t\n\n", todo.Title, todo.Description, todo.IsComplete)
}
}
func getIncompleteTodos() {
// Query for incomplete todos
todoResponse, _ := tablesDB.ListRows(
todoDatabase.Id,
todoTable.Id,
tablesDB.WithListRowsQueries([]string{
query.Equal("isComplete", false),
query.OrderAsc("title"),
}),
)
var todos TodoList
todoResponse.Decode(&todos)
fmt.Println("Incomplete todos (ordered by title):")
for _, todo := range todos.Documents {
fmt.Printf("Title: %s\nDescription: %s\nIs Todo Complete: %t\n\n", todo.Title, todo.Description, todo.IsComplete)
}
}
```
Make sure to update `main()` with the functions you created. Your `main()` function should look something like this:
```go
package main
import (
"fmt"
"github.com/appwrite/sdk-for-go/appwrite"
"github.com/appwrite/sdk-for-go/client"
"github.com/appwrite/sdk-for-go/tablesdb"
"github.com/appwrite/sdk-for-go/id"
"github.com/appwrite/sdk-for-go/models"
"github.com/appwrite/sdk-for-go/query"
)
var (
appwriteClient client.Client
todoDatabase *models.Database
todoTable *models.Table
tablesDB *tablesdb.TablesDB
)
func main() {
appwriteClient = appwrite.NewClient(
appwrite.WithProject("<PROJECT_KEY>"),
appwrite.WithKey("<API_KEY>"),
)
prepareDatabase()
seedDatabase()
getTodos()
getCompletedTodos()
getIncompleteTodos()
}
```
{% /section %}
{% section #step-8 step=8 title="All set" %}
Run your project with `go run .` and view the response in your console.
{% /section %}