ArangoDB Module

const arangodb = require('@arangodb')

Note: This module should not be confused with the arangojs JavaScript driver which can be used to access ArangoDB from outside the database. Although the APIs share similarities and the functionality overlaps, the two are not compatible with each other and can not be used interchangeably.

The db object

arangodb.db

The db object represents the current database and lets you access collections and run queries. For more information see the db object reference.

Examples

  1. const db = require('@arangodb').db;
  2. const thirteen = db._query('RETURN 5 + 8').next();

The aql template string handler

arangodb.aql

The aql function is a JavaScript template string handler. It can be used to write complex AQL queries as multi-line strings without having to worry about bindVars and the distinction between collections and regular parameters.

To use it just prefix a JavaScript template string (the ones with backticks instead of quotes) with its import name (e.g. aql) and pass in variables like you would with a regular template string. The string will automatically be converted into an object with query and bindVars attributes which you can pass directly to db._query to execute. If you pass in a collection it will be automatically recognized as a collection reference and handled accordingly.

You can also use the aql.literal helper to mark strings containing AQL snippetsthat should be inlined directly into the query rather than be treated as bind variables.

aql.literal allows you to pass a arbitrary strings into your AQL and thus will openyou to AQL injection attacks if you are passing in untrusted user input unsanitized.

To find out more about AQL see the AQL documentation.

Examples

  1. const aql = require('@arangodb').aql;
  2. const filterValue = 23;
  3. const mydata = db._collection('mydata');
  4. const result = db._query(aql`
  5. FOR d IN ${mydata}
  6. FILTER d.num > ${filterValue}
  7. RETURN d
  8. `).toArray();
  9. const filterGreen = aql.literal('FILTER d.color == "green"');
  10. const result2 = db._query(aql`
  11. FOR d IN ${mydata}
  12. ${filterGreen}
  13. RETURN d
  14. `).toArray()

The query helper

arangodb.query

In most cases you will likely use the aql template handler to create a query you directly pass todb._query. To make this even easier ArangoDB provides the query template handler, which behavesexactly like aql but also directly executes the query and returns the result cursor instead ofthe query object:

  1. const query = require('@arangodb').query;
  2. const filterValue = 23;
  3. const mydata = db._collection('mydata');
  4. const result = query`
  5. FOR d IN ${mydata}
  6. FILTER d.num > ${filterValue}
  7. RETURN d
  8. `.toArray();

The errors object

arangodb.errors

This object provides useful objects for each error code ArangoDB might use in ArangoError errors. This is helpful when trying to catch specific errors raised by ArangoDB, e.g. when trying to access a document that does not exist. Each object has a code property corresponding to the errorNum found on ArangoError errors.

For a complete list of the error names and codes you may encounter see the appendix on error codes.

Examples

  1. const errors = require('@arangodb').errors;
  2. try {
  3. someCollection.document('does-not-exist');
  4. } catch (e) {
  5. if (e.isArangoError && e.errorNum === errors.ERROR_ARANGO_DOCUMENT_NOT_FOUND.code) {
  6. throw new Error('Document does not exist');
  7. }
  8. throw new Error('Something went wrong');
  9. }

The time function

arangodb.time

This function provides the current time in seconds as a floating point value with microsecond precisison.

This function can be used instead of Date.now() when additional precision is needed.

Examples

  1. const time = require('@arangodb').time;
  2. const start = time();
  3. db._query(someVerySlowQuery);
  4. console.log(`Elapsed time: ${time() - start} secs`);