github.com/go-kivik/kivik/v4@v4.3.2/pouchdb/rows.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 //go:build js 14 15 package pouchdb 16 17 import ( 18 "encoding/json" 19 "io" 20 "strings" 21 22 "github.com/gopherjs/gopherjs/js" 23 24 "github.com/go-kivik/kivik/v4/driver" 25 "github.com/go-kivik/kivik/v4/pouchdb/bindings" 26 ) 27 28 var jsJSON *js.Object 29 30 func init() { 31 jsJSON = js.Global.Get("JSON") 32 } 33 34 type rows struct { 35 *js.Object 36 Off int64 `js:"offset"` 37 TRows int64 `js:"total_rows"` 38 USeq string `js:"update_seq"` 39 } 40 41 var _ driver.Rows = &rows{} 42 43 func (r *rows) Close() error { 44 r.Delete("rows") // Free up memory used by any remaining rows 45 return nil 46 } 47 48 func (r *rows) Next(row *driver.Row) (err error) { 49 defer bindings.RecoverError(&err) 50 if r.Get("rows") == js.Undefined || r.Get("rows").Length() == 0 { 51 return io.EOF 52 } 53 next := r.Get("rows").Call("shift") 54 row.ID = next.Get("id").String() 55 row.Key = json.RawMessage(jsJSON.Call("stringify", next.Get("key")).String()) 56 row.Value = strings.NewReader(jsJSON.Call("stringify", next.Get("value")).String()) 57 if doc := next.Get("doc"); doc != js.Undefined { 58 row.Doc = strings.NewReader(jsJSON.Call("stringify", doc).String()) 59 } 60 return nil 61 } 62 63 func (r *rows) Offset() int64 { 64 return r.Off 65 } 66 67 func (r *rows) TotalRows() int64 { 68 return r.TRows 69 } 70 71 func (r *rows) UpdateSeq() string { 72 return "" // PouchDB doesn't support this option 73 }