apache / couchdb

Seamless multi-master syncing database with an intuitive HTTP/JSON API, designed for reliability
https://couchdb.apache.org/
Apache License 2.0
6.28k stars 1.03k forks source link

[WIP] Selector-based simple views #5310

Open nickva opened 1 month ago

nickva commented 1 month ago

Try to see what a simple selector-based views might look like. Inspired by comments in the "allow_fallback" pr and the discussion in CouchDB dev meeting.

This is as simple as possible:

Example (DB=http://adm:pass@127.0.0.1:15984)

echo "Recreating DB"
http -q delete $DB/db1
http -b put $DB/db1

echo "Adding some docs"
http -b put $DB/db1/a k:='1' v=x s:='10'
http -b put $DB/db1/b k:='3' v=x s:='11'
http -b put $DB/db1/c k:='4' v=y s:='12'
http -b put $DB/db1/d k:='2' v=x s:='13'
http -b put $DB/db1/e k:='5' v=x s:='14'
http -b put $DB/db1/f w:='1' v=x s:='15'
http -b put $DB/db1/g k:='3' o=o s:='16'

echo "Add two silly selector thingers"
http -b put $DB/db1/_design/s language=selector views:='{
  "v1": {"map": {"selector": {"s":{"$gt":12}}, "keys":["k"], "values":["v"]}},
  "v2": {"map": {"keys":["w"]}}
}'

echo "Query the silly selector thinger"
http -b $DB/db1/_design/s/_view/v1

echo "Query the even sillier one without a selector"
http -b $DB/db1/_design/s/_view/v2

Query the silly selector thinger

{
    "offset": 0,
    "rows": [
        {"id": "d", "key": [2], "value": ["x"]},
        {"id": "e", "key": [5], "value": ["x"]}
    ],
    "total_rows": 2
}

Query the even sillier one without a selector. Pick docs based on keys only.

{
    "offset": 0,
    "rows": [
        {"id": "f", "key": [1], "value": null}
    ],
    "total_rows": 1
}