nilgai
nilgai

Reputation: 1

How to Pass-in a Collection name and Document Key to an AQL query to update the document

From Appsmith with a local ArangoDB database in Windows 10 Desktop environment, cannot get an AQL query to recognise a collection name passed in from an Appsmith Input widget. Works if collection name hard-coded into query and tried every combination of @@, @, ${} etc in LET, IN etc statements to get collection name accepted.

This code works with a hard-coded collection name:

    LET artiKey = "{{TestITartiKey.text}}"LET comSequ = 0
    LET Comment = \[
    {
    "comSequ": 0,
    "comment": "{{InpITcomment.text}}"  
    }
    \]

    FOR doc IN paeAnalysisAttributesNouns
    FILTER doc.\_key == artiKey
    UPDATE artiKey WITH {
    "comSequ": 0,
    Comments: UNION( doc.Comments, Comment )
    }
    IN paeAnalysisAttributesNouns
    RETURN doc

One of many versions that does not work:

    LET @@artiColVar = {{TestITartiColl.text}}
    LET artiKey = {{TestITartiKey.text}}

    LET artiCol = artiColVar
    LET comSequ = 0
    LET Comment = \[
    {
    "comSequ": 0,
    "comment": "{{InpITcomment.text}}"
    }
    \]

    FOR doc IN @artiColVar
    FILTER doc.\_key == artiKey
    UPDATE artiKey WITH {
    "comSequ": 0,
    Comments: UNION( doc.Comments, Comment)
    IN @artiColVar }
    RETURN doc

With following error message:

Your query failed to execute:
com.arangodb.ArangoDBException: Response: 400, Error: 1501 - AQL: syntax error, unexpected bind data source parameter, expecting identifier near '@@artiColVar = paeAnalysisAttrib...' at position 3:5 (while parsing)
{
"actionId":"65fc10a82f6b8a5a1dbb647d"
"requestedAt":"2024-03-21 12:46:05"
"requestParams":{
"Query":{
"value":"//Q2AB1FirstComment LET @@artiColVar = paeAnalysisAttributesNouns LET artiKey = 5095442 LET artiCol = artiColVar LET comSequ = 0 LET Comment = \[ { "comSequ": 0, "comment": "Results in two Account entries" } \] FOR doc IN @artiCol FILTER doc.\_key == artiKey UPDATE artiKey WITH { "comSequ": 0, Comments: UNION( doc.Comments, Comment ) IN @artiCol } RETURN doc "
}
}
}

Any Comment and Help welcomed - it seems such a basic query to need to be able to do!

DESIRED OUTCOME: To add a comment object to a passed-in collection name, document array field, which runs perfectly when the collection name is hard-coded into the AQL query.

Upvotes: 0

Views: 31

Answers (1)

Shahar Shokrani
Shahar Shokrani

Reputation: 8740

Not so familiar with appsmith, but you can use string concatination as a workaround like:

let artiColVar = TestITartiColl.text; // Init the collection name based on the appsmith 

let query = `
    //...
    FOR doc IN ${artiColVar}
`

Make sure to sanitize the inputs in order to avoid Aql Injection.

Upvotes: 0

Related Questions