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.
This commit is contained in:
Danny Avila 2025-06-09 16:31:59 -04:00
parent eed43e6662
commit 0143ae5728
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956

View file

@ -44,8 +44,13 @@ const groupSchema = new Schema<IGroup>(
{ 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;