-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
234 lines (193 loc) · 7.24 KB
/
database.go
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
package turso
import (
"context"
"encoding/json"
"fmt"
"net/http"
"regexp"
)
const (
databaseEndpoint = "v1/organizations/%s/databases"
maxNameLength = 32
regexName = "^[a-z0-9-]+$"
)
// DatabaseService is the interface for the Turso API database endpoint
type DatabaseService service
type databaseService interface {
// ListDatabases lists all databases in the organization
ListDatabases(ctx context.Context) (*ListDatabaseResponse, error)
// CreateDatabase creates a new database
CreateDatabase(ctx context.Context, req CreateDatabaseRequest) (*CreateDatabaseResponse, error)
// GetDatabase gets a database by name
GetDatabase(ctx context.Context, dbName string) (*GetDatabaseResponse, error)
// DeleteDatabase deletes a database by name
DeleteDatabase(ctx context.Context, dbName string) (*DeleteDatabaseResponse, error)
}
// Database is the struct for the Turso Database object
type Database struct {
// Name is the name of the database
Name string `json:"Name"`
// DatabaseID is the ID of the database
DatabaseID string `json:"DbId"`
// Hostname is the hostname of the database`
Hostname string `json:"Hostname"` // this is in the response twice, once with a capital H and once with a lowercase h
// IsSchema is this database controls other child databases then this will be true
IsSchema bool `json:"is_schema"`
// Schema is the name of the parent database that owns the schema for this database
Schema string `json:"schema"`
// BlockedReads is true if reads are blocked
BlockReads bool `json:"block_reads"`
// BlockedWrites is true if writes are blocked
BlockWrites bool `json:"block_writes"`
// AllowAttach is true if the database allows attachments of a child database
AllowAttach bool `json:"allow_attach"`
// Regions is a list of regions the database is available in
Regions []string `json:"regions"`
// PrimaryRegion is the primary region for the database
PrimaryRegion string `json:"primaryRegion"`
// Type is the type of the database
Type string `json:"type"`
// Version is the version of libsql used by the database
Version string `json:"version"`
// Group is the group the database is in
Group string `json:"group"`
// Sleeping is true if the database is sleeping
Sleeping bool `json:"sleeping"`
}
// CreateDatabase is the struct for the Turso API database create request
type CreateDatabase struct {
// DatabaseID is the ID of the database
DatabaseID string `json:"DbId"`
// Name is the name of the database
Name string `json:"Name"`
// Hostname is the hostname of the database
Hostname string `json:"Hostname"`
// IssuedCertCount is the number of certificates issued
IssuedCertCount int `json:"IssuedCertCount"`
// IssuedCertLimit is the limit of certificates that can be issued
IssuedCertLimit int `json:"IssuedCertLimit"`
}
// ListDatabaseResponse is the struct for the Turso API database list response
type ListDatabaseResponse struct {
Databases []*Database `json:"databases"`
}
// GetDatabaseResponse is the struct for the Turso API database get response
type GetDatabaseResponse struct {
Database *Database `json:"database"`
}
// CreateDatabaseResponse is the struct for the Turso API database create response
type CreateDatabaseResponse struct {
Database CreateDatabase `json:"database"`
}
// DeleteDatabaseResponse is the struct for the Turso API database delete response
type DeleteDatabaseResponse struct {
Database string `json:"database"`
}
// CreateDatabaseRequest is the struct for the Turso API database create request
type CreateDatabaseRequest struct {
// Group is the group the database is in
Group string `json:"group"`
// IsSchema is this database controls other child databases then this will be true
IsSchema bool `json:"is_schema"`
// Name is the name of the database
// Must contain only lowercase letters, numbers, dashes. No longer than 32 characters.
Name string `json:"name"`
}
// getDatabaseEndpoint returns the endpoint for the Turso API database service
func getDatabaseEndpoint(baseURL, orgName string) string {
dbEndpoint := fmt.Sprintf(databaseEndpoint, orgName)
return fmt.Sprintf("%s/%s", baseURL, dbEndpoint)
}
// CreateDatabase satisfies the databaseService interface
func (s *DatabaseService) CreateDatabase(ctx context.Context, db CreateDatabaseRequest) (*CreateDatabaseResponse, error) {
// Sanitize the database name
if err := validateDatabaseName(db.Name); err != nil {
return nil, err
}
// Create the database
endpoint := getDatabaseEndpoint(s.client.cfg.BaseURL, s.client.cfg.OrgName)
resp, err := s.client.DoRequest(ctx, http.MethodPost, endpoint, db)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Decode the response
var out CreateDatabaseResponse
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, newBadRequestError("database", "creating", resp.StatusCode)
}
return &out, nil
}
// ListDatabases satisfies the databaseService interface
func (s *DatabaseService) ListDatabases(ctx context.Context) (*ListDatabaseResponse, error) {
endpoint := getDatabaseEndpoint(s.client.cfg.BaseURL, s.client.cfg.OrgName)
resp, err := s.client.DoRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var out ListDatabaseResponse
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, newBadRequestError("databases", "listing", resp.StatusCode)
}
return &out, nil
}
// GetDatabase satisfies the databaseService interface
func (s *DatabaseService) GetDatabase(ctx context.Context, dbName string) (*GetDatabaseResponse, error) {
// get endpoint and append the database name
endpoint := getDatabaseEndpoint(s.client.cfg.BaseURL, s.client.cfg.OrgName)
endpoint = fmt.Sprintf("%s/%s", endpoint, dbName)
resp, err := s.client.DoRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var out *GetDatabaseResponse
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, newBadRequestError("database", "getting", resp.StatusCode)
}
return out, nil
}
// DeleteDatabase satisfies the databaseService interface
func (s *DatabaseService) DeleteDatabase(ctx context.Context, dbName string) (*DeleteDatabaseResponse, error) {
// Delete the database
endpoint := getDatabaseEndpoint(s.client.cfg.BaseURL, s.client.cfg.OrgName)
endpoint = fmt.Sprintf("%s/%s", endpoint, dbName)
resp, err := s.client.DoRequest(ctx, http.MethodDelete, endpoint, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Decode the response
var out DeleteDatabaseResponse
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, newBadRequestError("database", "deleting", resp.StatusCode)
}
return &out, nil
}
// validateDatabaseName validates the database name to ensure it meets the requirements set by the Turso API
func validateDatabaseName(name string) error {
match, err := regexp.MatchString(regexName, name)
if err != nil {
return err
}
if !match {
return ErrInvalidDatabaseName
}
if len(name) > maxNameLength {
return ErrInvalidDatabaseName
}
return nil
}