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

Method addIndex

packages/sql/src/schema/DatabaseTable.ts:1126–1283  ·  view source on GitHub ↗
(
    meta: EntityMetadata,
    index: {
      properties?: string | string[];
      name?: string;
      type?: string;
      expression?: string | IndexCallback<any>;
      where?: string | Dictionary;
      deferMode?: DeferMode | `${DeferMode}`;
      options?: Dictionary;
      columns?: {
        name: string;
        sort?: 'ASC' | 'DESC' | 'asc' | 'desc';
        nulls?: 'FIRST' | 'LAST' | 'first' | 'last';
        length?: number;
        collation?: string;
      }[];
      include?: string | string[];
      fillFactor?: number;
      invisible?: boolean;
      disabled?: boolean;
      clustered?: boolean;
    },
    type: 'index' | 'unique' | 'primary',
  )

Source from the content-addressed store, hash-verified

1124 }
1125
1126 addIndex(
1127 meta: EntityMetadata,
1128 index: {
1129 properties?: string | string[];
1130 name?: string;
1131 type?: string;
1132 expression?: string | IndexCallback<any>;
1133 where?: string | Dictionary;
1134 deferMode?: DeferMode | `${DeferMode}`;
1135 options?: Dictionary;
1136 columns?: {
1137 name: string;
1138 sort?: 'ASC' | 'DESC' | 'asc' | 'desc';
1139 nulls?: 'FIRST' | 'LAST' | 'first' | 'last';
1140 length?: number;
1141 collation?: string;
1142 }[];
1143 include?: string | string[];
1144 fillFactor?: number;
1145 invisible?: boolean;
1146 disabled?: boolean;
1147 clustered?: boolean;
1148 },
1149 type: 'index' | 'unique' | 'primary',
1150 ) {
1151 // If columns are specified but properties are not, derive properties from column names
1152 if (
1153 index.columns?.length &&
1154 !index.expression &&
1155 (!index.properties || Utils.asArray(index.properties).length === 0)
1156 ) {
1157 index = { ...index, properties: index.columns.map(c => c.name) };
1158 }
1159
1160 const properties = Utils.unique(
1161 Utils.flatten(
1162 Utils.asArray(index.properties).map(prop => {
1163 const parts = prop.split('.');
1164 const root = parts[0];
1165
1166 if (meta.properties[prop]) {
1167 if (meta.properties[prop].embeddedPath) {
1168 return [meta.properties[prop].embeddedPath.join('.')];
1169 }
1170
1171 return meta.properties[prop].fieldNames;
1172 }
1173
1174 const rootProp = meta.properties[root];
1175
1176 // inline embedded property index, we need to find the field name of the child property
1177 if (rootProp?.embeddable && !rootProp.object && parts.length > 1) {
1178 const expand = (p: EntityProperty, i: number): string => {
1179 if (parts.length === i) {
1180 return p.fieldNames[0];
1181 }
1182
1183 return expand(p.embeddedProps[parts[i]], i + 1);

Callers 2

fromMetadataMethod · 0.95

Calls 9

getIndexNameMethod · 0.95
processIndexWhereMethod · 0.95
asArrayMethod · 0.80
pushMethod · 0.80
uniqueMethod · 0.65
joinMethod · 0.65
mapMethod · 0.45
flattenMethod · 0.45

Tested by

no test coverage detected