Skip to content

rxQuery

Skitsanos edited this page Jan 12, 2023 · 2 revisions

Starting from v.2.2.20230112 rxQuery was added to the Foxx Builder to showcase a simplified way of building AQL filters.

rxQuery is an adoption of an earlier sandbox project I demoed on https://rx-query.netlify.app/

The idea is to write plain English queries and turn them into AQL filter, so writing something like this:

debug=true AND email?something.com

will produce the following:

FILTER 
  debug == true
  &&
  LIKE(doc.email, '%something.com%', true)

Operations

There are five query operations supported:

= (equal)
~ (not equal)
? (like)
> (greater than)
< (less than)

Using rxQuery

rxQuery is available within context utilities:

const {rxQuery} = module.context.utils;

rxQuery accepts two arguments:

  • qs - (required) query string in URL Encoded format
  • doc - (optional) variable name in FOR/IN loop, by default it is doc

So if your AQL query starts with FOR doc IN users, you run rxQuery(qs), but if you change the record variable name to, let's say, user, eq FOR user IN users, you have to specify it as well in arguments rxQuery(qs, 'user')

Using in Foxx Builder route:

const {query, time, db} = require('@arangodb');

module.exports = {
    contentType: 'application/json',
    name: 'Get users',
    handler: (req, res) =>
    {
        const {rxQuery} = module.context.utils;

        const {skip = 0, pageSize = 25, q} = req.queryParams;

        const filter = rxQuery(q);

        const start = time();

        const cursor = query`      
         LET skip=${Number(skip)}
         LET pageSize=${Number(pageSize)}
        
         LET ds = (
            FOR doc IN users
                ${filter}
                SORT doc._key DESC
                LIMIT skip,pageSize
            RETURN merge(
                UNSET(doc,"_rev","_id", "password"),
                {
                   
                })
        )
                
        RETURN ds          
        `;

        const total = db.users.count({ filter });

        res.send({
            result: {
                data: cursor.toArray(),
                total,
                pageSize
            }, execTime: time() - start
        });
    }
};
Clone this wiki locally