Skip to content

Commit

Permalink
fix: add index limit
Browse files Browse the repository at this point in the history
  • Loading branch information
solaris007 committed Dec 19, 2024
1 parent 744f97a commit b713eab
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/spacecat-shared-data-access/src/v2/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
});
});
});

0 comments on commit b713eab

Please sign in to comment.