January 2, 2023 · couchdb, javascript and language

tango provides a textual syntax for logical and relational expressions compatible with Mango selectors.

In my previous post, I introduced couchilla, a lightweight command-line tool for bundling CouchDB design documents. While MapReduce views are powerful, the /db/_find API typically provides faster querying in most cases by using Mango selector expressions in JSON format. tango introduces a textual syntax for these queries.

For example, in Mango, a query to find movies directed by Roy Andersson after 2007 is expressed in a JSON structure resembling a syntax tree:

{
  "$and": [
    {
      "director": {
        "$eq": "Roy Andersson"
      }
    },
    {
      "year": {
        "$gt": 2007
      }
    }
  ]
}

With tango, this can instead be written as:

director == "Roy Andersson" && year > 2007

This syntax includes standard C comparison operators and supports parentheses to specify explicit precedence. At present, tango does not support unary operators or the $in operator for matching elements within arrays.

The Shunting Yard algorithm, as implemented here and devised by Edsger W. Dijkstra, is a linear-time algorithm for parsing expressions using a technique known as operator precedence parsing. It uses a stack to manage operators and a queue to output expressions in Reverse Polish Notation or to construct an Abstract Syntax Tree (AST).