home shape

AQB Update: Write More Readable Queries with ArangoDB

The latest update to the AQL Query Builder for JavaScript addresses a major pain point: the “prefix notation” or LISP style syntax of AQL operator methods. Instead of calling the operator methods on the query builder object itself, you can now directly call them as methods on value objects.

Let’s say you want to write a query that takes all non-admin users with a power level over 9000 and returns their score rounded to the closest 100. In plain AQL this could be written like this:

FOR user IN users
FILTER user.isAdmin == false && user.powerLevel > 9000
RETURN ROUND(user.score / 100) * 100

Trying to construct this query using the query builder would previously have resulted in code like this:

var qb = require('aqb');
var query = qb.for('user').in('users').filter(
  qb.and(
    qb.eq('user.isAdmin', false), 
    qb.gt('user.powerLevel', 9000)
  )
).return(
  qb.times(qb.ROUND(qb.div('user.score', 100)), 100)
);

As of AQB 1.10.0, you can instead write the following:

var qb = require('aqb');
var query = qb.for('user').in('users').filter(
  qb.ref('user.isAdmin').eq(false)
  .and(qb.ref('user.powerLevel').gt(9000))
).return(
  qb.ROUND(qb.ref('user.score').div(100)).times(100)
);

Note that these changes are entirely cosmetic. Both queries translate to the same AQL and the old syntax is still supported. For more information on the AQL Query Builder for JavaScript see AQB on GitHub.

AQB 1.10.0 is available in the upcoming 2.6 release of ArangoDB and also in the latest development version. If you want to use the new features today you can install the latest version of AQB from NPM. AQB also works in the browser and on the server with Node.js or io.js using the official JavaScript driver for ArangoDB.

Alan Plum avatar 1418721602 92x92

Alan Plum

Alan is an experienced web developer who feels equally at home in the backend and frontend. At ArangoDB he works on everything regarding JavaScript, with a special focus on Foxx.

Leave a Comment





Get the latest tutorials, blog posts and news: