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 Answers1

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.

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
  • 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? – Eric Fleming Aug 02 '17 at 22:48
  • 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? – Eric Fleming Aug 02 '17 at 23:26
  • 1
    @EricFleming I'm referencing `1.1.0-beta1` version of `...Extensions.DocumentDB` package. Can this make the difference? – Mikhail Shilkov Aug 03 '17 at 06:24
  • 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? – Eric Fleming Aug 03 '17 at 11:42