Options
All
  • Public
  • Public/Protected
  • All
Menu

Namespace aql

Index

Functions

Functions

join

  • Constructs AqlQuery objects from an array of arbitrary values.

    Note: Nesting aql template strings is a much safer alternative for most use cases. This low-level helper function only exists to complement the aql tag when constructing complex queries from dynamic arrays of query fragments.

    example
    const users = db.collection("users");
    const filters = [];
    if (adminsOnly) filters.push(aql`FILTER user.admin`);
    if (activeOnly) filters.push(aql`FILTER user.active`);
    const result = await db.query(aql`
      FOR user IN ${users}
      ${aql.join(filters)}
      RETURN user
    `);
    
    example
    const users = db.collection("users");
    const keys = ["jreyes", "ghermann"];
    
    // BAD! NEEDLESSLY COMPLEX!
    const docs = keys.map(key => aql`DOCUMENT(${users}, ${key}`));
    const result = await db.query(aql`
      FOR user IN [
        ${aql.join(docs, ", ")}
      ]
      RETURN user
    `);
    // Query:
    //   FOR user IN [
    //     DOCUMENT(@@value0, @value1), DOCUMENT(@@value0, @value2)
    //   ]
    //   RETURN user
    // Bind parameters:
    //   @value0 -> "users"
    //   value1 -> "jreyes"
    //   value2 -> "ghermann"
    
    // GOOD! MUCH SIMPLER!
    const result = await db.query(aql`
      FOR key IN ${keys}
      LET user = DOCUMENT(${users}, key)
      RETURN user
    `);
    // Query:
    //   FOR user IN @value0
    //   LET user = DOCUMENT(@@value1, key)
    //   RETURN user
    // Bind parameters:
    //   value0 -> ["jreyes", "ghermann"]
    //   @value1 -> "users"
    

    Parameters

    • values: AqlValue[]

      Array of values to join. These values will behave exactly like values interpolated in an aql template string.

    • sep: string = " "

      Seperator to insert between values. This value will behave exactly like a value passed to aql.literal, i.e. it will be inlined as-is, rather than being converted into a bind parameter.

    Returns GeneratedAqlQuery

literal

  • Marks an arbitrary scalar value (i.e. a string, number or boolean) as safe for being inlined directly into AQL queries when used in an aql template string, rather than being converted into a bind parameter.

    Note: Nesting aql template strings is a much safer alternative for most use cases. This low-level helper function only exists to help with rare edge cases where a trusted AQL query fragment must be read from a string (e.g. when reading query fragments from JSON) and should only be used as a last resort.

    example
    // BAD! DO NOT DO THIS!
    const sortDirection = aql.literal('ASC');
    
    // GOOD! DO THIS INSTEAD!
    const sortDirection = aql`ASC`;
    
    example
    // BAD! DO NOT DO THIS!
    const filterColor = aql.literal('FILTER d.color == "green"');
    const result = await db.query(aql`
      FOR d IN some-collection
      ${filterColor}
      RETURN d
    `);
    
    // GOOD! DO THIS INSTEAD!
    const color = "green";
    const filterColor = aql`FILTER d.color === ${color}`;
    const result = await db.query(aql`
      FOR d IN some-collection
      ${filterColor}
      RETURN d
    `);
    
    example
    // WARNING: We explicitly trust the environment variable to be safe!
    const filter = aql.literal(process.env.FILTER_STATEMENT);
    const users = await db.query(aql`
      FOR user IN users
      ${filter}
      RETURN user
    `);
    

    Parameters

    • value: string | number | boolean | AqlLiteral | null | undefined

    Returns AqlLiteral

Generated using TypeDoc