Skip to content Skip to sidebar Skip to footer

How To Check If A Container Exists In Cosmos DB Using The Node Sdk?

I want to check if a container exists and if not, initialize it. I was hoping for something like the following: const { endpoint, key, databaseId } = config; const containerName =

Solution 1:

I'm not quite clear on why you would need to do this since typically you only need to do this sort of thing once for the life of your application instance. But I would not recommend doing it this way.

When you query Cosmos to test the existence of a database, container, etc., this hits the master partition for the account. The master partition is kind of like a tiny Cosmos database with all of your account meta data in it.

This master partition is allocated a small amount of the RU/s that manage the metadata operations. So if you app is designed to make these types of calls for every single request, it's quite likely you will get rate limited in your application.

If there is some way you can design this such that it doesn't have to query for the existence of a container then I would pursue that instead.


Solution 2:

You can try this code:

async function check_container_exist(databaseId,containerId) {
    let exist = false;
    const querySpec = {
        query: "SELECT * FROM root r WHERE r.id = @container",
        parameters: [
            {name: "@container", value: containerId}
        ]
    };
    const response = await client.database(databaseId).containers.query(querySpec).fetchNext();
    if(response.resources[0]){
        exist = true;
    }
    return exist;
}

Solution 3:

Interesting question. So i think you have few options

  1. Just call const { container } = await database.containers.createIfNotExists({ id: "Container" }); it will be fast probably few milliseconds, since I went via code at looks like it will always try to read from cosmos :( If you want to still check if container exists sdk has methods(But again no real benefits ):

const iterator = database.containers.readAll();

const { resources: containersList } = await iterator.fetchAll();

  1. Create singleton and first time just initialise all your containers so next time you dont call it, sure if you scale each instance will do the same
  2. My favourite, use terraform/armtemplates/bicep to spin up infrastructure so you code wont need to handle that

Post a Comment for "How To Check If A Container Exists In Cosmos DB Using The Node Sdk?"