Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit b36ede1

Browse files
committed
Merge branch 'main' of github.com:staticbackendhq/core into main
2 parents 4682cad + 4cbf7a6 commit b36ede1

File tree

8 files changed

+162
-2
lines changed

8 files changed

+162
-2
lines changed

database/memory/base.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ func (m *Memory) BulkCreateDocument(auth internal.Auth, dbName, col string, docs
4848
func (m *Memory) ListDocuments(auth internal.Auth, dbName, col string, params internal.ListParams) (result internal.PagedResult, err error) {
4949
list, err := all[map[string]any](m, dbName, col)
5050
if err != nil {
51+
if errors.Is(err, collectionNotFoundErr) {
52+
return internal.PagedResult{Page: params.Page, Size: params.Size}, nil
53+
}
5154
return
5255
}
5356

@@ -77,6 +80,9 @@ func (m *Memory) ListDocuments(auth internal.Auth, dbName, col string, params in
7780
func (m *Memory) QueryDocuments(auth internal.Auth, dbName, col string, filter map[string]any, params internal.ListParams) (result internal.PagedResult, err error) {
7881
list, err := all[map[string]any](m, dbName, col)
7982
if err != nil {
83+
if errors.Is(err, collectionNotFoundErr) {
84+
return internal.PagedResult{Page: params.Page, Size: params.Size}, nil
85+
}
8086
return
8187
}
8288

database/memory/base_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package memory
33
import (
44
"encoding/json"
55
"fmt"
6+
"reflect"
67
"testing"
78
"time"
89

@@ -305,3 +306,33 @@ func TestListCollections(t *testing.T) {
305306
t.Errorf("expected to have at least one collection got %d", len(results))
306307
}
307308
}
309+
310+
func TestListDocumentsWithNonExistingDB(t *testing.T) {
311+
lp := internal.ListParams{Page: 1, Size: 25}
312+
expected := internal.PagedResult{Page: 1, Size: 25}
313+
result, err := datastore.ListDocuments(adminAuth, "random_name", colName, lp)
314+
if err != nil {
315+
t.Fatal(err)
316+
} else if !reflect.DeepEqual(result, expected) {
317+
t.Fatalf("expected empty result but got %v", result)
318+
}
319+
}
320+
321+
func TestQueryDocumentsWithNonExistingDB(t *testing.T) {
322+
var clauses [][]interface{}
323+
clauses = append(clauses, []interface{}{"title", "=", "where1"})
324+
325+
lp := internal.ListParams{Page: 1, Size: 5}
326+
327+
filters, err := datastore.ParseQuery(clauses)
328+
if err != nil {
329+
t.Fatal(err)
330+
}
331+
expected := internal.PagedResult{Page: 1, Size: 5}
332+
result, err := datastore.QueryDocuments(adminAuth, "random_name", colName, filters, lp)
333+
if err != nil {
334+
t.Fatal(err)
335+
} else if !reflect.DeepEqual(result, expected) {
336+
t.Fatalf("expected empty result but got %v", result)
337+
}
338+
}

database/memory/memory.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
FieldOwnerID = "ownerId"
2020
)*/
2121

22+
var collectionNotFoundErr = errors.New("collection not found")
23+
2224
func init() {
2325
gob.Register(map[string]any{})
2426
gob.Register([]any{})
@@ -89,7 +91,7 @@ func getByID[T any](m *Memory, dbName, col, id string, v T) error {
8991

9092
repo, ok := m.DB[key]
9193
if !ok {
92-
return errors.New("collection not found")
94+
return collectionNotFoundErr
9395
}
9496

9597
b, ok := repo[id]
@@ -106,7 +108,7 @@ func all[T any](m *Memory, dbName, col string) (list []T, err error) {
106108

107109
repo, ok := m.DB[key]
108110
if !ok {
109-
return nil, errors.New("collection not found")
111+
return nil, collectionNotFoundErr
110112
}
111113

112114
for _, v := range repo {

database/mongo/base.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ func (mg *Mongo) ListDocuments(auth internal.Auth, dbName, col string, params in
164164
if err != nil {
165165
return result, err
166166
}
167+
if count == 0 {
168+
return result, nil
169+
}
167170

168171
result.Total = count
169172

@@ -233,6 +236,9 @@ func (mg *Mongo) QueryDocuments(auth internal.Auth, dbName, col string, filter m
233236
if err != nil {
234237
return result, err
235238
}
239+
if count == 0 {
240+
return result, nil
241+
}
236242

237243
result.Total = count
238244

database/mongo/base_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mongo
33
import (
44
"encoding/json"
55
"fmt"
6+
"reflect"
67
"testing"
78
"time"
89

@@ -305,3 +306,33 @@ func TestListCollections(t *testing.T) {
305306
t.Errorf("expected to have at least one collection got %d", len(results))
306307
}
307308
}
309+
310+
func TestListDocumentsWithNonExistingDB(t *testing.T) {
311+
lp := internal.ListParams{Page: 1, Size: 25}
312+
expected := internal.PagedResult{Page: 1, Size: 25}
313+
result, err := datastore.ListDocuments(adminAuth, "random_name", colName, lp)
314+
if err != nil {
315+
t.Fatal(err)
316+
} else if !reflect.DeepEqual(result, expected) {
317+
t.Fatalf("expected empty result but got %v", result)
318+
}
319+
}
320+
321+
func TestQueryDocumentsWithNonExistingDB(t *testing.T) {
322+
var clauses [][]interface{}
323+
clauses = append(clauses, []interface{}{"title", "=", "where1"})
324+
325+
lp := internal.ListParams{Page: 1, Size: 5}
326+
327+
filters, err := datastore.ParseQuery(clauses)
328+
if err != nil {
329+
t.Fatal(err)
330+
}
331+
expected := internal.PagedResult{Page: 1, Size: 5}
332+
result, err := datastore.QueryDocuments(adminAuth, "random_name", colName, filters, lp)
333+
if err != nil {
334+
t.Fatal(err)
335+
} else if !reflect.DeepEqual(result, expected) {
336+
t.Fatalf("expected empty result\nActual: %#v\nExpected: %#v", result, expected)
337+
}
338+
}

database/postgresql/base.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"github.com/lib/pq"
89
"strings"
910
"time"
1011

@@ -118,6 +119,9 @@ func (pg *PostgreSQL) ListDocuments(auth internal.Auth, dbName, col string, para
118119
`, dbName, internal.CleanCollectionName(col), where)
119120

120121
if err = pg.DB.QueryRow(qry, auth.AccountID, auth.UserID).Scan(&result.Total); err != nil {
122+
if !isTableExists(err) {
123+
return result, nil
124+
}
121125
return
122126
}
123127

@@ -167,6 +171,9 @@ func (pg *PostgreSQL) QueryDocuments(auth internal.Auth, dbName, col string, fil
167171
`, dbName, internal.CleanCollectionName(col), where)
168172

169173
if err = pg.DB.QueryRow(qry, auth.AccountID, auth.UserID).Scan(&result.Total); err != nil {
174+
if !isTableExists(err) {
175+
return result, nil
176+
}
170177
return
171178
}
172179

@@ -348,3 +355,12 @@ func scanDocument(rows Scanner, doc *Document) error {
348355
&doc.Created,
349356
)
350357
}
358+
359+
func isTableExists(err error) bool {
360+
if err, ok := err.(*pq.Error); ok {
361+
if err.Code.Name() == "undefined_table" {
362+
return false
363+
}
364+
}
365+
return true
366+
}

database/postgresql/base_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package postgresql
33
import (
44
"encoding/json"
55
"fmt"
6+
"reflect"
67
"testing"
78
"time"
89

@@ -305,3 +306,33 @@ func TestListCollections(t *testing.T) {
305306
t.Errorf("expected to have at least one collection got %d", len(results))
306307
}
307308
}
309+
310+
func TestListDocumentsWithNonExistingDB(t *testing.T) {
311+
lp := internal.ListParams{Page: 1, Size: 25}
312+
expected := internal.PagedResult{Page: 1, Size: 25}
313+
result, err := datastore.ListDocuments(adminAuth, "random_name", colName, lp)
314+
if err != nil {
315+
t.Fatal(err)
316+
} else if !reflect.DeepEqual(result, expected) {
317+
t.Fatalf("expected empty result but got %v", result)
318+
}
319+
}
320+
321+
func TestQueryDocumentsWithNonExistingDB(t *testing.T) {
322+
var clauses [][]interface{}
323+
clauses = append(clauses, []interface{}{"title", "=", "where1"})
324+
325+
lp := internal.ListParams{Page: 1, Size: 5}
326+
327+
filters, err := datastore.ParseQuery(clauses)
328+
if err != nil {
329+
t.Fatal(err)
330+
}
331+
expected := internal.PagedResult{Page: 1, Size: 5}
332+
result, err := datastore.QueryDocuments(adminAuth, "random_name", colName, filters, lp)
333+
if err != nil {
334+
t.Fatal(err)
335+
} else if !reflect.DeepEqual(result, expected) {
336+
t.Fatalf("expected empty result but got %v", result)
337+
}
338+
}

db_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"net/http/httptest"
1010
"net/url"
11+
"reflect"
1112
"testing"
1213
"time"
1314

@@ -179,6 +180,42 @@ func TestDBListCollections(t *testing.T) {
179180
}
180181
}
181182

183+
func TestListDocumentsInvalidDB(t *testing.T) {
184+
req := httptest.NewRequest("GET", "/db/invalid_db_name", nil)
185+
w := httptest.NewRecorder()
186+
187+
req.Header.Set("SB-PUBLIC-KEY", pubKey)
188+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", rootToken))
189+
190+
stdRoot := []middleware.Middleware{
191+
middleware.WithDB(datastore, volatile, getStripePortalURL),
192+
middleware.RequireRoot(datastore),
193+
}
194+
h := middleware.Chain(http.HandlerFunc(database.list), stdRoot...)
195+
196+
h.ServeHTTP(w, req)
197+
198+
resp := w.Result()
199+
defer resp.Body.Close()
200+
if resp.StatusCode > 299 {
201+
b, err := ioutil.ReadAll(resp.Body)
202+
if err != nil {
203+
t.Fatal(err)
204+
}
205+
206+
t.Errorf("got error for list documents: %s", string(b))
207+
}
208+
expected := internal.PagedResult{Page: 1, Size: 25}
209+
210+
var response internal.PagedResult
211+
if err := parseBody(resp.Body, &response); err != nil {
212+
t.Fatal(err)
213+
} else if !reflect.DeepEqual(expected, response) {
214+
t.Errorf("incorrect response is received\nExpected: %#v\nActual: %#v", expected, response)
215+
}
216+
217+
}
218+
182219
func TestDBBulkUpdate(t *testing.T) {
183220
tasks := []Task{
184221
{

0 commit comments

Comments
 (0)