Function 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.

    Parameters

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

    Returns AqlLiteral

    Example

    // BAD! DO NOT DO THIS!
    const sortDirection = literal('ASC');

    // GOOD! DO THIS INSTEAD!
    const sortDirection = aql`ASC`;

    Example

    // BAD! DO NOT DO THIS!
    const filterColor = 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 = literal(process.env.FILTER_STATEMENT);
    const users = await db.query(aql`
    FOR user IN users
    ${filter}
    RETURN user
    `);