Submit SqlQuery to Azure DocumentDB Attribute Function

I have an Azure Function that uses an attribute DocumentDB

to connect to a Cosmos database. I am using Azure Tools for Visual Studio 2017. Here is a 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 pass SqlQuery

as one of the attribute parameters DocumentDB

, for example:

    [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 one example and reported that it supposedly works. https://github.com/Azure/Azure-Functions/issues/271 The "error" I receive does not recognize anything with a name SqlQuery

as a possible parameter.

I have looked at the documentation for Azure function input bindings https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-documentdb#input-sample-with-multiple-documents which shows the result in a file function.json

. containing the attribute SqlQuery

. How did it happen?

If it is not possible to pass an attribute to SqlQuery DocumentDB

, what would be the best way to filter the results before not returning the entire collection and then run it through a LINQ query?

+3


source to share


1 answer


You need to specify the 1.1.0-beta

version of the Microsoft.Azure.WebJobs.Extensions.DocumentDB

NuGet package (or later).

In this version, SqlQuery

is a valid attribute parameter DocumentDB

. You compile the code for me if I remove the sign $

in front of the line select

:



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

      

You don't need to $

- it is used for string interpolation in C #, not what you want to do here.

+5


source







All Articles