MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / validateIndexUsage

Method validateIndexUsage

packages/core/src/EntityManager.ts:2732–2796  ·  view source on GitHub ↗
(
    meta: EntityMetadata<Entity>,
    where: FilterQuery<Entity> | Dictionary,
    options: { using?: string | string[]; orderBy?: any },
  )

Source from the content-addressed store, hash-verified

2730 }
2731
2732 private validateIndexUsage<Entity extends object>(
2733 meta: EntityMetadata<Entity>,
2734 where: FilterQuery<Entity> | Dictionary,
2735 options: { using?: string | string[]; orderBy?: any },
2736 ): void {
2737 if (!options.using) {
2738 return;
2739 }
2740
2741 const indexNames = Utils.asArray(options.using);
2742 const allIndexes = [...meta.indexes, ...meta.uniques];
2743
2744 const indexMap = new Map<string, string[]>();
2745
2746 for (const idx of allIndexes) {
2747 if (idx.name) {
2748 indexMap.set(idx.name, Utils.asArray(idx.properties ?? []) as string[]);
2749 }
2750 }
2751
2752 for (const prop of meta.props) {
2753 if (typeof prop.index === 'string') {
2754 indexMap.set(prop.index, [prop.name]);
2755 }
2756
2757 if (typeof prop.unique === 'string') {
2758 indexMap.set(prop.unique, [prop.name]);
2759 }
2760 }
2761
2762 const allowedProps = new Set<string>();
2763
2764 for (const name of indexNames) {
2765 const props = indexMap.get(name);
2766
2767 if (!props) {
2768 const available = [...indexMap.keys()];
2769 throw new Error(
2770 `Index '${name}' not found on entity '${meta.className}'. ` +
2771 (available.length > 0 ? `Available indexes: ${available.join(', ')}` : 'No named indexes defined.'),
2772 );
2773 }
2774
2775 for (const prop of props) {
2776 allowedProps.add(prop);
2777 }
2778 }
2779
2780 this.validateWhereKeysForIndex(where, allowedProps, indexNames);
2781
2782 if (options.orderBy) {
2783 const orderMaps = Array.isArray(options.orderBy) ? options.orderBy : [options.orderBy];
2784
2785 for (const orderMap of orderMaps) {
2786 for (const key of Object.keys(orderMap)) {
2787 if (!allowedProps.has(key)) {
2788 throw new Error(
2789 `Property '${key}' in orderBy is not covered by index '${indexNames.join("', '")}'. ` +

Callers 3

findMethod · 0.80
streamMethod · 0.80
findOneMethod · 0.80

Calls 8

asArrayMethod · 0.80
addMethod · 0.80
setMethod · 0.65
getMethod · 0.65
joinMethod · 0.65
hasMethod · 0.65
keysMethod · 0.45

Tested by

no test coverage detected