Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | 400x 400x 400x 400x 400x 400x 182x 2940x 53293x 53293x 53293x 55845x 6326x 49519x 1274x 48245x 502x 964x 47743x 47743x 62183x 53293x 56918x 506279x 56918x 39119x 400x 56422x 56422x 56422x 56422x 56918x 56918x 56918x 56918x 3129x 53293x 53293x 53293x 42019x 56109x 56109x 56109x 56109x 56109x 456x 114x 342x 55995x 55995x 54521x 54063x 400x 8015x 8015x 8015x 8015x 8244x 7786x 7786x 400x 34698x 34698x 34698x 34698x 38988x 38530x 38530x 38530x 38530x 382x 38530x 400x 400x 400x | import type { Document } from '../bson'; import { CursorResponse } from '../cmap/wire_protocol/responses'; import type { Collection } from '../collection'; import { type AbstractCursorOptions } from '../cursor/abstract_cursor'; import { MongoCompatibilityError } from '../error'; import { type OneOrMore } from '../mongo_types'; import type { Server } from '../sdam/server'; import type { ClientSession } from '../sessions'; import { type TimeoutContext } from '../timeout'; import { isObject, maxWireVersion, type MongoDBNamespace } from '../utils'; import { type CollationOptions, CommandOperation, type CommandOperationOptions, type OperationParent } from './command'; import { Aspect, defineAspects } from './operation'; const VALID_INDEX_OPTIONS = new Set([ 'background', 'unique', 'name', 'partialFilterExpression', 'sparse', 'hidden', 'expireAfterSeconds', 'storageEngine', 'collation', 'version', // text indexes 'weights', 'default_language', 'language_override', 'textIndexVersion', // 2d-sphere indexes '2dsphereIndexVersion', // 2d indexes 'bits', 'min', 'max', // geoHaystack Indexes 'bucketSize', // wildcard indexes 'wildcardProjection' ]); /** @public */ export type IndexDirection = | -1 | 1 | '2d' | '2dsphere' | 'text' | 'geoHaystack' | 'hashed' | number; function isIndexDirection(x: unknown): x is IndexDirection { return ( typeof x === 'number' || x === '2d' || x === '2dsphere' || x === 'text' || x === 'geoHaystack' ); } /** @public */ export type IndexSpecification = OneOrMore< | string | [string, IndexDirection] | { [key: string]: IndexDirection } | Map<string, IndexDirection> >; /** @public */ export interface IndexInformationOptions extends ListIndexesOptions { /** * When `true`, an array of index descriptions is returned. * When `false`, the driver returns an object that with keys corresponding to index names with values * corresponding to the entries of the indexes' key. * * For example, the given the following indexes: * ``` * [ { name: 'a_1', key: { a: 1 } }, { name: 'b_1_c_1' , key: { b: 1, c: 1 } }] * ``` * * When `full` is `true`, the above array is returned. When `full` is `false`, the following is returned: * ``` * { * 'a_1': [['a', 1]], * 'b_1_c_1': [['b', 1], ['c', 1]], * } * ``` */ full?: boolean; } /** @public */ export interface IndexDescription extends Pick< CreateIndexesOptions, | 'background' | 'unique' | 'partialFilterExpression' | 'sparse' | 'hidden' | 'expireAfterSeconds' | 'storageEngine' | 'version' | 'weights' | 'default_language' | 'language_override' | 'textIndexVersion' | '2dsphereIndexVersion' | 'bits' | 'min' | 'max' | 'bucketSize' | 'wildcardProjection' > { collation?: CollationOptions; name?: string; key: { [key: string]: IndexDirection } | Map<string, IndexDirection>; } /** @public */ export interface CreateIndexesOptions extends Omit<CommandOperationOptions, 'writeConcern'> { /** Creates the index in the background, yielding whenever possible. */ background?: boolean; /** Creates an unique index. */ unique?: boolean; /** Override the autogenerated index name (useful if the resulting name is larger than 128 bytes) */ name?: string; /** Creates a partial index based on the given filter object (MongoDB 3.2 or higher) */ partialFilterExpression?: Document; /** Creates a sparse index. */ sparse?: boolean; /** Allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher) */ expireAfterSeconds?: number; /** Allows users to configure the storage engine on a per-index basis when creating an index. (MongoDB 3.0 or higher) */ storageEngine?: Document; /** (MongoDB 4.4. or higher) Specifies how many data-bearing members of a replica set, including the primary, must complete the index builds successfully before the primary marks the indexes as ready. This option accepts the same values for the "w" field in a write concern plus "votingMembers", which indicates all voting data-bearing nodes. */ commitQuorum?: number | string; /** Specifies the index version number, either 0 or 1. */ version?: number; // text indexes weights?: Document; default_language?: string; language_override?: string; textIndexVersion?: number; // 2d-sphere indexes '2dsphereIndexVersion'?: number; // 2d indexes bits?: number; /** For geospatial indexes set the lower bound for the co-ordinates. */ min?: number; /** For geospatial indexes set the high bound for the co-ordinates. */ max?: number; // geoHaystack Indexes bucketSize?: number; // wildcard indexes wildcardProjection?: Document; /** Specifies that the index should exist on the target collection but should not be used by the query planner when executing operations. (MongoDB 4.4 or higher) */ hidden?: boolean; } function isSingleIndexTuple(t: unknown): t is [string, IndexDirection] { return Array.isArray(t) && t.length === 2 && isIndexDirection(t[1]); } /** * Converts an `IndexSpecification`, which can be specified in multiple formats, into a * valid `key` for the createIndexes command. */ function constructIndexDescriptionMap(indexSpec: IndexSpecification): Map<string, IndexDirection> { const key: Map<string, IndexDirection> = new Map(); const indexSpecs = !Array.isArray(indexSpec) || isSingleIndexTuple(indexSpec) ? [indexSpec] : indexSpec; // Iterate through array and handle different types for (const spec of indexSpecs) { if (typeof spec === 'string') { key.set(spec, 1); } else if (Array.isArray(spec)) { key.set(spec[0], spec[1] ?? 1); } else if (spec instanceof Map) { for (const [property, value] of spec) { key.set(property, value); } } else Eif (isObject(spec)) { for (const [property, value] of Object.entries(spec)) { key.set(property, value); } } } return key; } /** * Receives an index description and returns a modified index description which has had invalid options removed * from the description and has mapped the `version` option to the `v` option. */ function resolveIndexDescription( description: IndexDescription ): Omit<ResolvedIndexDescription, 'key'> { const validProvidedOptions = Object.entries(description).filter(([optionName]) => VALID_INDEX_OPTIONS.has(optionName) ); return Object.fromEntries( // we support the `version` option, but the `createIndexes` command expects it to be the `v` validProvidedOptions.map(([name, value]) => (name === 'version' ? ['v', value] : [name, value])) ); } /** * @public * The index information returned by the listIndexes command. https://www.mongodb.com/docs/manual/reference/command/listIndexes/#mongodb-dbcommand-dbcmd.listIndexes */ export type IndexDescriptionInfo = Omit<IndexDescription, 'key' | 'version'> & { key: { [key: string]: IndexDirection }; v?: IndexDescription['version']; } & Document; /** @public */ export type IndexDescriptionCompact = Record<string, [name: string, direction: IndexDirection][]>; /** * @internal * * Internally, the driver represents index description keys with `Map`s to preserve key ordering. * We don't require users to specify maps, so we transform user provided descriptions into * "resolved" by converting the `key` into a JS `Map`, if it isn't already a map. * * Additionally, we support the `version` option, but the `createIndexes` command uses the field `v` * to specify the index version so we map the value of `version` to `v`, if provided. */ type ResolvedIndexDescription = Omit<IndexDescription, 'key' | 'version'> & { key: Map<string, IndexDirection>; v?: IndexDescription['version']; }; /** @internal */ export class CreateIndexesOperation extends CommandOperation<string[]> { override options: CreateIndexesOptions; collectionName: string; indexes: ReadonlyArray<ResolvedIndexDescription>; private constructor( parent: OperationParent, collectionName: string, indexes: IndexDescription[], options?: CreateIndexesOptions ) { super(parent, options); this.options = options ?? {}; this.collectionName = collectionName; this.indexes = indexes.map((userIndex: IndexDescription): ResolvedIndexDescription => { // Ensure the key is a Map to preserve index key ordering const key = userIndex.key instanceof Map ? userIndex.key : new Map(Object.entries(userIndex.key)); const name = userIndex.name ?? Array.from(key).flat().join('_'); const validIndexOptions = resolveIndexDescription(userIndex); return { ...validIndexOptions, name, key }; }); } static fromIndexDescriptionArray( parent: OperationParent, collectionName: string, indexes: IndexDescription[], options?: CreateIndexesOptions ): CreateIndexesOperation { return new CreateIndexesOperation(parent, collectionName, indexes, options); } static fromIndexSpecification( parent: OperationParent, collectionName: string, indexSpec: IndexSpecification, options: CreateIndexesOptions = {} ): CreateIndexesOperation { const key = constructIndexDescriptionMap(indexSpec); const description: IndexDescription = { ...options, key }; return new CreateIndexesOperation(parent, collectionName, [description], options); } override get commandName() { return 'createIndexes'; } override async execute( server: Server, session: ClientSession | undefined, timeoutContext: TimeoutContext ): Promise<string[]> { const options = this.options; const indexes = this.indexes; const serverWireVersion = maxWireVersion(server); const cmd: Document = { createIndexes: this.collectionName, indexes }; if (options.commitQuorum != null) { if (serverWireVersion < 9) { throw new MongoCompatibilityError( 'Option `commitQuorum` for `createIndexes` not supported on servers < 4.4' ); } cmd.commitQuorum = options.commitQuorum; } // collation is set on each index, it should not be defined at the root this.options.collation = undefined; await super.executeCommand(server, session, cmd, timeoutContext); const indexNames = indexes.map(index => index.name || ''); return indexNames; } } /** @public */ export type DropIndexesOptions = CommandOperationOptions; /** @internal */ export class DropIndexOperation extends CommandOperation<Document> { override options: DropIndexesOptions; collection: Collection; indexName: string; constructor(collection: Collection, indexName: string, options?: DropIndexesOptions) { super(collection, options); this.options = options ?? {}; this.collection = collection; this.indexName = indexName; } override get commandName() { return 'dropIndexes' as const; } override async execute( server: Server, session: ClientSession | undefined, timeoutContext: TimeoutContext ): Promise<Document> { const cmd = { dropIndexes: this.collection.collectionName, index: this.indexName }; return await super.executeCommand(server, session, cmd, timeoutContext); } } /** @public */ export type ListIndexesOptions = AbstractCursorOptions & { /** @internal */ omitMaxTimeMS?: boolean; }; /** @internal */ export class ListIndexesOperation extends CommandOperation<CursorResponse> { /** * @remarks WriteConcern can still be present on the options because * we inherit options from the client/db/collection. The * key must be present on the options in order to delete it. * This allows typescript to delete the key but will * not allow a writeConcern to be assigned as a property on options. */ override options: ListIndexesOptions & { writeConcern?: never }; collectionNamespace: MongoDBNamespace; constructor(collection: Collection, options?: ListIndexesOptions) { super(collection, options); this.options = { ...options }; delete this.options.writeConcern; this.collectionNamespace = collection.s.namespace; } override get commandName() { return 'listIndexes' as const; } override async execute( server: Server, session: ClientSession | undefined, timeoutContext: TimeoutContext ): Promise<CursorResponse> { const serverWireVersion = maxWireVersion(server); const cursor = this.options.batchSize ? { batchSize: this.options.batchSize } : {}; const command: Document = { listIndexes: this.collectionNamespace.collection, cursor }; // we check for undefined specifically here to allow falsy values // eslint-disable-next-line no-restricted-syntax if (serverWireVersion >= 9 && this.options.comment !== undefined) { command.comment = this.options.comment; } return await super.executeCommand(server, session, command, timeoutContext, CursorResponse); } } defineAspects(ListIndexesOperation, [ Aspect.READ_OPERATION, Aspect.RETRYABLE, Aspect.CURSOR_CREATING ]); defineAspects(CreateIndexesOperation, [Aspect.WRITE_OPERATION]); defineAspects(DropIndexOperation, [Aspect.WRITE_OPERATION]); |