4

I have an Azure Function that uses the DocumentDB attribute to connect to Cosmos DB. I'm using the Azure Functions for Visual Studio 2017 tooling. Here is the simple Function

    [FunctionName("DocumentDbGet")]
    public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get")]HttpRequestMessage req, TraceWriter log,
        [DocumentDB("FunctionJunctionDemo", "Demo")]IEnumerable<DemoModel> items)
    {
        //Can perform operations on the "items" variable, which will be the result of the table/collection you specify
        string name = req.GetQueryNameValuePairs()
            .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
            .Value;

        var item = items.Where(x => x.FirstName == name);
        return req.CreateResponse(HttpStatusCode.OK);
    }

I want to be able to pass a SqlQuery as one of the parameters of the DocumentDB attribute like so:

    [FunctionName("DocumentDbGet")]
    public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get")]HttpRequestMessage req, TraceWriter log,
        [DocumentDB("FunctionJunctionDemo", "Demo", SqlQuery = $"select * from c where c.Id = {Id}")]IEnumerable<DemoModel> items)
    {
        //Can perform operations on the "items" variable, which will be the result of the table/collection you specify
        string name = req.GetQueryNameValuePairs()
            .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
            .Value;

        var item = items.Where(x => x.FirstName == name);
        return req.CreateResponse(HttpStatusCode.OK);
    }

I've only seen 1 example do this and reported it was supposedly working. https://github.com/Azure/Azure-Functions/issues/271 The "error" I receive is it doesn't recognize anything named SqlQuery as a possible parameter.

I have looked at the documentation for Azure Function Input bindings https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-documentdb#input-sample-with-multiple-documents which shows an output in the function.json file containing a sqlQuery attribute. How did that get in there?

If it isn't possible to pass in a SqlQuery in the DocumentDB attribute, what would be the best practice to filter the results up front to avoid returning an entire collection and then running it through a LINQ query?

1 Answer 1

5

You need to reference 1.1.0-beta version of Microsoft.Azure.WebJobs.Extensions.DocumentDB NuGet package (or later).

In that version SqlQuery is a valid parameter of DocumentDB attribute. You code compiles for me, if I remove $ sign before select string:

[DocumentDB("FunctionJunctionDemo", "Demo", SqlQuery = "select * from c where c.Id = {Id}")]

You don't need $ - it's used for string interpolation in C#, not something you want to do here.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi @Mikhail - thanks for the quick answer! So, even if I remove the $ which definitely wasn't needed from the beginning it's still not recognizing it as valid. Maybe I have a version mismatch? I'm using v1.0.0 of Microsoft.Azure.WebJobs.Extensions.DocumentDB which is where the DocumentDB attribute comes in at. Maybe I need to update my Azure CLI?
so I updated the Azure Functions CLI to the latest version, as well as my Visual Studio 2017 Preview to Preview 7, which then allowed me to update the Azure Function tools for Visual Studio from 0.2 to 0.3.30802. None of this helped. Still getting the awesome red squigglies under SqlQuery on my machine. Maybe I'm missing a package altogether?
@EricFleming I'm referencing 1.1.0-beta1 version of ...Extensions.DocumentDB package. Can this make the difference?
AHA! - @Mikhail that is what it was. I did not have the "Include prerelease" checkbox checked in the NuGet package Manager. Once I checked it, I saw 1.1.0-beta1 of the ...Extensions.DocumentDB package. Now it looks like it accepts SqlQuery as a parameter. Could you edit your answer above to include that we need to be on version 1.1.0-beta of this package at the very least to use the SqlQuery param?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.