From f73f153852c2bb74392146653bcdc77d5ce080e6 Mon Sep 17 00:00:00 2001 From: Maria Date: Tue, 20 May 2025 14:41:06 +0200 Subject: [PATCH] FIO-10043: automatically adding indexes for CosmosDB --- src/db/index.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/db/index.js b/src/db/index.js index 0afc83278..edcf189de 100644 --- a/src/db/index.js +++ b/src/db/index.js @@ -304,6 +304,29 @@ module.exports = function(formio) { formio.util.log('Compound indexes that contain nested paths are not supported.'); config.mongoFeatures.compoundIndexWithNestedPath = formio.mongoFeatures.compoundIndexWithNestedPath = false; } + + // check CosmosDB indexes + try { + // Checking whether indexes need to be created for CosmosDB to function + await featuresTest.dropIndexes(); + await featuresTest.insertOne({title: 'Test Title', nested: {test: 'value'}}); + await featuresTest.find().sort({title: 1}).limit(1).toArray(); + } + catch (err) { + // Create indexes if they don't exist + const collections = await db.listCollections().toArray(); + + for (const {name} of collections) { + const collection = db.collection(name); + const indexes = await collection.indexes(); + + const hasWildcard = indexes.some(idx => idx.key && idx.key["$**"] === 1); + + if (!hasWildcard) { + await collection.createIndex({"$**": 1}); + } + } + } await featuresTest.drop(); } catch (err) {