diff --git a/packages/spacecat-shared-data-access/src/v2/models/base/base.collection.js b/packages/spacecat-shared-data-access/src/v2/models/base/base.collection.js index db7435ef..929e2464 100755 --- a/packages/spacecat-shared-data-access/src/v2/models/base/base.collection.js +++ b/packages/spacecat-shared-data-access/src/v2/models/base/base.collection.js @@ -38,6 +38,14 @@ function isValidParent(parent, child) { return child.record?.[foreignKey] === parent.record?.[foreignKey]; } +/** + * Finds the index name by the keys provided. The index is searched + * keys to match the combination of partition and sort keys. If no + * index is found, we fall back to the "all" index, then the "primary". + * @param {Schema} schema - The schema to search for the index. + * @param {Object} keys - The keys to search for. + * @return {*|string} - The index name. + */ function findIndexNameByKeys(schema, keys) { const keyNames = Object.keys(keys); diff --git a/packages/spacecat-shared-data-access/src/v2/models/base/schema.builder.js b/packages/spacecat-shared-data-access/src/v2/models/base/schema.builder.js index 817e12c8..1ff05876 100755 --- a/packages/spacecat-shared-data-access/src/v2/models/base/schema.builder.js +++ b/packages/spacecat-shared-data-access/src/v2/models/base/schema.builder.js @@ -302,6 +302,10 @@ class SchemaBuilder { ...other, ]; + if (orderedIndexes.length > 5) { + throw new Error('Cannot have more than 5 indexes.'); + } + this.indexes = { primary: this.rawIndexes.primary }; let indexCounter = 0; diff --git a/packages/spacecat-shared-data-access/src/v2/readme.md b/packages/spacecat-shared-data-access/src/v2/readme.md index e9cb2c05..c0610202 100755 --- a/packages/spacecat-shared-data-access/src/v2/readme.md +++ b/packages/spacecat-shared-data-access/src/v2/readme.md @@ -135,7 +135,7 @@ const userSchema = new SchemaBuilder(User, UserCollection) validate: (value) => value.includes('@'), }) .addAttribute('name', { type: 'string', required: true }) - .addAllIndexWithComposite('email') + .addAllIndex(['email']) .addReference('belongs_to', 'Organization') // Adds organizationId and byOrganizationId index .build(); diff --git a/packages/spacecat-shared-data-access/test/unit/v2/models/base/schema.builder.test.js b/packages/spacecat-shared-data-access/test/unit/v2/models/base/schema.builder.test.js index 61cf42e5..b01d8f5d 100755 --- a/packages/spacecat-shared-data-access/test/unit/v2/models/base/schema.builder.test.js +++ b/packages/spacecat-shared-data-access/test/unit/v2/models/base/schema.builder.test.js @@ -431,5 +431,15 @@ describe('SchemaBuilder', () => { ], }); }); + + it('throws error if more than 5 indexes are added', () => { + instance.addAllIndex(['baseURL']); + instance.addIndex({ composite: ['deliveryType'] }, { composite: ['updatedAt'] }); + instance.addIndex({ field: 'someField', composite: ['deliveryType'] }, { composite: ['updatedAt'] }); + instance.addIndex({ field: 'someField', composite: ['deliveryType'] }, { composite: ['updatedAt'] }); + instance.addIndex({ field: 'someField', composite: ['deliveryType'] }, { composite: ['updatedAt'] }); + instance.addIndex({ field: 'someField', composite: ['deliveryType'] }, { composite: ['updatedAt'] }); + expect(() => instance.build()).to.throw('Cannot have more than 5 indexes.'); + }); }); });