From 0143ae5728164937a86c26cd771797805198f1c8 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Mon, 9 Jun 2025 16:31:59 -0400 Subject: [PATCH] fix(data-schemas): use partial index for group idOnTheSource uniqueness Replace sparse index with partial filter expression to allow multiple local groups while maintaining unique constraint for external source IDs. The sparse option on compound indexes doesn't work as expected when one field is always present. --- packages/data-schemas/src/schema/group.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/data-schemas/src/schema/group.ts b/packages/data-schemas/src/schema/group.ts index ec98b90119..7de514b776 100644 --- a/packages/data-schemas/src/schema/group.ts +++ b/packages/data-schemas/src/schema/group.ts @@ -44,8 +44,13 @@ const groupSchema = new Schema( { timestamps: true }, ); -// Create indexes for efficient lookups -groupSchema.index({ idOnTheSource: 1, source: 1 }, { unique: true, sparse: true }); +groupSchema.index( + { idOnTheSource: 1, source: 1 }, + { + unique: true, + partialFilterExpression: { idOnTheSource: { $exists: true } }, + }, +); groupSchema.index({ memberIds: 1 }); export default groupSchema;