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');
  2. const thirteen = db._query('RETURN 5 + 8').next();

The aql template tag

arangodb.aql

The aql function is a JavaScript template string handler (or template tag).It can be used to write complex AQL queries as multi-line strings withouthaving to worry about bindVars and the distinction between collectionsand regular parameters.

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

Starting with ArangoDB 3.4 queries generated using the aql template tag canbe used inside other aql template strings, allowing arbitrary nesting. Bindparameters of nested queries will be merged automatically.

To find out more about AQL see the AQL documentation.

Examples

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

The aql.literal helper

arangodb.aql.literal

The aql.literal helper can be used to mark strings to be inlined into an AQLquery when using the aql template tag, rather than being treated as a bindparameter.

Any value passed to aql.literal will be treated as part of the AQL query.To avoid becoming vulnerable to AQL injection attacks you should always prefernested aql queries if possible.

Examples

  1. const {aql} = require('@arangodb');
  2. const filterGreen = aql.literal('FILTER d.color == "green"');
  3. const result = db._query(aql`
  4. FOR d IN ${mydata}
  5. ${filterGreen}
  6. RETURN d
  7. `).toArray();

The aql.join helper

arangodb.aql.join

The aql.join helper takes an array of queries generated using the aql tagand combines them into a single query. The optional second argument will beused as literal string to combine the queries.

  1. const {aql} = require('@arangodb');
  2. // Basic usage
  3. const parts = [aql`FILTER`, aql`x`, aql`%`, aql`2`];
  4. const joined = aql.join(parts); // aql`FILTER x % 2`
  5. // Merge without the extra space
  6. const parts = [aql`FIL`, aql`TER`];
  7. const joined = aql.join(parts, ''); // aql`FILTER`;
  8. // Real world example: translate keys into document lookups
  9. const users = db._collection("users");
  10. const keys = ["abc123", "def456"];
  11. const docs = keys.map(key => aql`DOCUMENT(${users}, ${key})`);
  12. const aqlArray = aql`[${aql.join(docs, ", ")}]`;
  13. const result = db._query(aql`
  14. FOR d IN ${aqlArray}
  15. RETURN d
  16. `).toArray();
  17. // Query:
  18. // FOR d IN [DOCUMENT(@@value0, @value1), DOCUMENT(@@value0, @value2)]
  19. // RETURN d
  20. // Bind parameters:
  21. // @value0: "users"
  22. // value1: "abc123"
  23. // value2: "def456"
  24. // Alternative without `aql.join`
  25. const users = db._collection("users");
  26. const keys = ["abc123", "def456"];
  27. const result = db._query(aql`
  28. FOR key IN ${keys}
  29. LET d = DOCUMENT(${users}, key)
  30. RETURN d
  31. `).toArray();
  32. // Query:
  33. // FOR key IN @value0
  34. // LET d = DOCUMENT(@@value1, key)
  35. // RETURN d
  36. // Bind parameters:
  37. // value0: ["abc123", "def456"]
  38. // @value1: "users"

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');
  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();
  9. // Nesting with `aql` works as expected
  10. const {aql} = require('@arangodb');
  11. const filter = aql`FILTER d.num > ${filterValue}`;
  12. const result2 = query`
  13. FOR d IN ${mydata}
  14. ${filter}
  15. RETURN d
  16. `.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`);