Skip to content

Commit

Permalink
Merge pull request #873 from go-kivik/allDocsMulti
Browse files Browse the repository at this point in the history
multi-query _all_docs & POST _all_docs
  • Loading branch information
flimzy authored Feb 4, 2024
2 parents 9ddd03f + fe78a6e commit e415338
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
15 changes: 13 additions & 2 deletions x/server/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"path/filepath"
"strconv"
"time"

Expand Down Expand Up @@ -228,8 +229,18 @@ loop:
func (s *Server) allDocs() httpe.HandlerWithError {
return httpe.HandlerWithErrorFunc(func(w http.ResponseWriter, r *http.Request) error {
req := map[string]interface{}{}
if err := s.bind(r, &req); err != nil {
return err
if _, last := filepath.Split(r.URL.Path); last == "queries" {
var jsonReq struct {
Queries []map[string]interface{} `json:"queries"`
}
if err := s.bind(r, &jsonReq); err != nil {
return err
}
req["queries"] = jsonReq.Queries
} else {
if err := s.bind(r, &req); err != nil {
return err
}
}
db := chi.URLParam(r, "db")
rows := s.client.DB(db).AllDocs(r.Context(), options(r))
Expand Down
87 changes: 86 additions & 1 deletion x/server/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,92 @@ func Test_allDocs(t *testing.T) {
return client
}(),
wantStatus: http.StatusOK,
// wantBodyRE: "xxx",
wantJSON: map[string]interface{}{
"offset": 0,
"rows": []interface{}{
map[string]interface{}{
"id": "foo",
"key": "foo",
"value": map[string]interface{}{
"rev": "1-beea34a62a215ab051862d1e5d93162e",
},
},
},
"total_rows": 99,
},
},
{
name: "multi queries defaults",
authUser: userAdmin,
method: http.MethodPost,
path: "/db1/_all_docs/queries",
headers: map[string]string{
"Content-Type": "application/json",
},
body: strings.NewReader(`{"queries": [{"keys": ["foo", "bar"]}]}`),
client: func() *kivik.Client {
client, mock, err := mockdb.New()
if err != nil {
t.Fatal(err)
}
db := mock.NewDB()
mock.ExpectDB().WillReturn(db)
db.ExpectSecurity().WillReturn(&driver.Security{})
mock.ExpectDB().WillReturn(db)
db.ExpectAllDocs().WillReturn(mockdb.NewRows().
AddRow(&driver.Row{
ID: "foo",
Key: []byte(`"foo"`),
Value: strings.NewReader(`{"rev": "1-beea34a62a215ab051862d1e5d93162e"}`),
}).
TotalRows(99),
)
return client
}(),
wantStatus: http.StatusOK,
wantJSON: map[string]interface{}{
"offset": 0,
"rows": []interface{}{
map[string]interface{}{
"id": "foo",
"key": "foo",
"value": map[string]interface{}{
"rev": "1-beea34a62a215ab051862d1e5d93162e",
},
},
},
"total_rows": 99,
},
},
{
name: "POST _all_docs",
authUser: userAdmin,
method: http.MethodPost,
path: "/db1/_all_docs",
headers: map[string]string{
"Content-Type": "application/json",
},
body: strings.NewReader(`{"keys": ["foo", "bar"]}`),
client: func() *kivik.Client {
client, mock, err := mockdb.New()
if err != nil {
t.Fatal(err)
}
db := mock.NewDB()
mock.ExpectDB().WillReturn(db)
db.ExpectSecurity().WillReturn(&driver.Security{})
mock.ExpectDB().WillReturn(db)
db.ExpectAllDocs().WillReturn(mockdb.NewRows().
AddRow(&driver.Row{
ID: "foo",
Key: []byte(`"foo"`),
Value: strings.NewReader(`{"rev": "1-beea34a62a215ab051862d1e5d93162e"}`),
}).
TotalRows(99),
)
return client
}(),
wantStatus: http.StatusOK,
wantJSON: map[string]interface{}{
"offset": 0,
"rows": []interface{}{
Expand Down
4 changes: 2 additions & 2 deletions x/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ func (s *Server) routes(mux *chi.Mux) {
admin.Put("/", e(s.createDB()))
admin.Delete("/", e(s.deleteDB()))
member.Get("/_all_docs", e(s.allDocs()))
member.Post("/_all_docs/queries", e(s.notImplemented()))
member.Post("/_all_docs", e(s.notImplemented()))
member.Post("/_all_docs/queries", e(s.allDocs()))
member.Post("/_all_docs", e(s.allDocs()))
member.Get("/_design_docs", e(s.notImplemented()))
member.Post("/_design_docs", e(s.notImplemented()))
member.Post("/_design_docs/queries", e(s.notImplemented()))
Expand Down

0 comments on commit e415338

Please sign in to comment.