How to use container.items.bulk()

Viewed 30

I need to send bulk patch requests in azure functions. I've read their very minimalistic documentation on a function called "bulk( )" and here's what I currently have

const response = await container.items.bulk([
    {
        operationType: "Patch",
        id: "FOO-123",
        partitionKey: "FOO-123",
        resourceBody: {
            "op": "add",
            "path": "/payments/-",
            "value": {bar: "foo"}
        }
    }
])

Here's the item I'm trying to manipulate in CosmosDB

{
    "id": "FOO-123",
    "code": "FOO-123",
    "payments": [
        {
            "foo": "bar"
        }
    ]
}

When logging "response" I get this

[ { statusCode: 400, requestCharge: 1 } ]
1 Answers

I got it eventually

const response = await container.items.bulk([
    {
        operationType: "Patch",
        id: "FOO-123",
        partitionKey: "FOO-123",
        resourceBody: {
            operations: {
                "op": "add",
                "path": "/payments/-",
                "value": {bar: "foo"}
            }
        }
    }
])

Got this after reading more into the function. It needed an array of something called "OperationInput" as an argument. Found this documentation which states that a type OperationInput can be a number of other types.

I assumed I needed to use PatchOperationInput for patching. A PatchOperationInput has a key called "resourceBody" which needs to be of type PatchRequestBody which can either be

{ condition?: string, operations: PatchOperation[] }

or

PatchOperation[]

For some reason the second one doesn't work, but the first one does.

Related