beloglazov / couchdb-scala

A purely functional Scala client for CouchDB
Apache License 2.0
65 stars 19 forks source link

View Query with Complex Key #14

Closed yeyan closed 8 years ago

yeyan commented 8 years ago
Complex key query

For views with array key is there any way to issue the following query?

intended query parameters:

startkey=["Tag 1"]
endkey=["Tag 1", {}]

URI:

/database/_design/view-name/_view/by-tag-view?startkey=[%22Tag%201%22]&&endkey=[%22Tag%201%22,%20{}]

I have tried issue the query with pure string, but the end key is serialized to a json string.

    xview.startKey(List("Tag 1")).endKey("[\"Tag 1\", {}]").query.attemptRun match {
      // In case of an error (left side of Either), print it
      case -\/(e) => e.printStackTrace()
      // In case of a success (right side of Either), print each object
      case \/-(a) => a.rows.foreach(x => println(x))//println(a)
    }

I have also tried to List with empty object, but this one simple not compile.

    xview.startKey(List("Tag 1")).endKey(List("Tag 1", Map())).query.attemptRun match {
      // In case of an error (left side of Either), print it
      case -\/(e) => e.printStackTrace()
      // In case of a success (right side of Either), print each object
      case \/-(a) => a.rows.foreach(x => println(x))//println(a)
    }

Is there anyway to issue a query with raw strings with couchdb-scala API?

beloglazov commented 8 years ago

Could you please try xview.startKey(("Tag 1")).endKey(("Tag 1", Map())), i.e., using tuples instead of lists? I haven't tried it myself, please let me know if it doesn't work.

ermyas commented 8 years ago

@beloglazov's suggestion for using Tuples should work with slight modifications, either providing type parameter to the Map (wouldn't compile otherwise) or just using a Unit type for the second element in the tuple of endKey