yokawasa / azure-functions-python-samples

Azure Functions Python Sample Codes. NOTE: The project, hosted in a repository, is no longer actively maintained by its creators or contributors. There won't be any further updates, bug fixes, or support from the original developers in the project.
MIT License
392 stars 181 forks source link

Output Cosmos Multi documents #18

Closed dajor closed 5 years ago

dajor commented 5 years ago

In C# it is possible to output more than one document to cosmos. Is there a way to output in python as well more than one document.

It works with one document but I found now way to pass to doc.set a array

yokawasa commented 5 years ago

@dajor You can set DocumentList to output like this:

def main(docs: func.DocumentList, outdoc: func.Out[func.Document]) -> str:
        ...snip...
        newdoc1_dict = {}
        newdoc1_dict["id"] =  "idxxxxxxxxxxxxx1"
        newdoc1_dict["name"] ="namexxxxxxxx1"
        newdoc1_dict["text"] = "textxxxxxxxxxx1"

        newdoc2_dict={}
        newdoc2_dict["id"] =  "idxxxxxxxxxxxxx2"
        newdoc2_dict["name"] ="namexxxxxxxx2"
        newdoc2_dict["text"] = "textxxxxxxxxxx2"

        newdocs = func.DocumentList()
        newdocs.append(func.Document.from_dict(newdoc1_dict))
        newdocs.append(func.Document.from_dict(newdoc2_dict))

        outdoc.set(newdocs)

I modified my sample code to give DocumentList as output object in this: https://github.com/yokawasa/azure-functions-python-samples/blob/master/v2functions/cosmos-trigger-cosmodb-output-binding/__init__.py

I close this but please feel free to re-open if you want