1
votes

I am using Azure Functions and Cosmos DB SQL to create a serverless application with javascript.

I have the following database schema of a user item:

{
    "id": "user_id_2",
    "username": "username_2",
    "pass": "pass_1",
    "feed": [],
    "followed": [
        "username_1",
        "username_3",
        "username_4"
    ],
    "followers": [
        "username_3",
        "username_4"
    ]
}

Currently when a user_1 follows another user_2 I update the database document for user_1 - no problem. But now I also need to update the document for user_2, particularly the array of Strings - followers. How can I do that through an azure function with bindings? The only way I came up with is to query the database for the whole document, update it in the client-side and then PUT back in the database, overwriting the previous document. However, this seems ridiculous...

1

1 Answers

1
votes

Cosmos DB does not support partial updates at this moment, so pulling the document, adding the item to the array and then doing the PUT is the only option.

Having said that, the problem with your data design is that followed and followers are unbounded arrays, where your user size can grow unchecked. The bigger the size, the more RUs operations will take.

Please see When not to embed here https://docs.microsoft.com/azure/cosmos-db/modeling-data#when-not-to-embed

I wrote a design doc for social apps back when I worked in creating a social application: https://docs.microsoft.com/azure/cosmos-db/social-media-apps

Ideally, the relationship of A follows B would be a document of its own. You could store them on a Graph account for optimal performance, since what you are really building here is a graph, and the queries you will be running are "Who follows B?" or "Who follows my followers?".