2

Is there a way to pre-seed or pre-create my needed collections in Azure Cosmos DB? After creating resource on Azure, I am getting an empty database, but my goal is to have my required collections be presented after deploy.

Am I right, that I need to include deploying Azure Cosmos DB into my api deployment pipelines in Azure DevOps?

Thanks for any help.

hoozr
  • 403
  • 4
  • 15

1 Answers1

0

You can create the cosmosdb account using the ARM/Bicep template and then try seeding data with your custom logic.

I have a sample setup for the same.

async function populateDb(data, collectionName) {
  MongoClient.connect(
    endpoint,
    {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      sslValidate: false,
    },
    (error, client) => {
      if (error) {
        throw error;
      } 
      database = client.db("yourdb");
      collection = database.collection(collectionName);
      let batch = collection.initializeOrderedBulkOp();

      for (let i = 0; i <= data.length; i += 1) { 
        if (data[i]) {
          batch.insert(data[i]);
        }
      }
      batch.execute().then(() => console.log("Seeding completed on " + collectionName + " Collection", new Date().toLocaleString()))
        .c 
        });
    });
};
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396