|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -const ReadPreference = require('../read_preference'); |
4 |
| -const { Aspect, defineAspects, OperationBase } = require('./operation'); |
5 |
| -const { executeCommand } = require('./db_ops'); |
| 3 | +const { Aspect, defineAspects } = require('./operation'); |
6 | 4 | const { MongoError } = require('../error');
|
| 5 | +const CommandOperationV2 = require('./command_v2'); |
| 6 | +const { maxWireVersion, parseIndexOptions } = require('../utils'); |
7 | 7 |
|
8 |
| -class CreateIndexesOperation extends OperationBase { |
9 |
| - constructor(collection, indexSpecs, options) { |
10 |
| - super(options); |
| 8 | +const validIndexOptions = new Set([ |
| 9 | + 'unique', |
| 10 | + 'partialFilterExpression', |
| 11 | + 'sparse', |
| 12 | + 'background', |
| 13 | + 'expireAfterSeconds', |
| 14 | + 'storageEngine', |
| 15 | + 'collation' |
| 16 | +]); |
11 | 17 |
|
| 18 | +class CreateIndexesOperation extends CommandOperationV2 { |
| 19 | + constructor(parent, collection, indexes, options) { |
| 20 | + super(parent, options); |
12 | 21 | this.collection = collection;
|
13 |
| - this.indexSpecs = indexSpecs; |
| 22 | + |
| 23 | + // createIndex can be called with a variety of styles: |
| 24 | + // coll.createIndex('a'); |
| 25 | + // coll.createIndex({ a: 1 }); |
| 26 | + // coll.createIndex([['a', 1]]); |
| 27 | + // createIndexes is always called with an array of index spec objects |
| 28 | + if (!Array.isArray(indexes) || Array.isArray(indexes[0])) { |
| 29 | + this.onlyReturnNameOfCreatedIndex = true; |
| 30 | + // TODO: remove in v4 (breaking change); make createIndex return full response as createIndexes does |
| 31 | + |
| 32 | + const indexParameters = parseIndexOptions(indexes); |
| 33 | + // Generate the index name |
| 34 | + const name = typeof options.name === 'string' ? options.name : indexParameters.name; |
| 35 | + // Set up the index |
| 36 | + const indexSpec = { name, key: indexParameters.fieldHash }; |
| 37 | + // merge valid index options into the index spec |
| 38 | + for (let optionName in options) { |
| 39 | + if (validIndexOptions.has(optionName)) { |
| 40 | + indexSpec[optionName] = options[optionName]; |
| 41 | + } |
| 42 | + } |
| 43 | + this.indexes = [indexSpec]; |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + this.indexes = indexes; |
14 | 48 | }
|
15 | 49 |
|
16 |
| - execute(callback) { |
17 |
| - const coll = this.collection; |
18 |
| - const indexSpecs = this.indexSpecs; |
19 |
| - let options = this.options; |
| 50 | + execute(server, callback) { |
| 51 | + const options = this.options; |
| 52 | + const indexes = this.indexes; |
20 | 53 |
|
21 |
| - const capabilities = coll.s.topology.capabilities(); |
| 54 | + const serverWireVersion = maxWireVersion(server); |
22 | 55 |
|
23 | 56 | // Ensure we generate the correct name if the parameter is not set
|
24 |
| - for (let i = 0; i < indexSpecs.length; i++) { |
25 |
| - if (indexSpecs[i].name == null) { |
26 |
| - const keys = []; |
| 57 | + for (let i = 0; i < indexes.length; i++) { |
| 58 | + // Did the user pass in a collation, check if our write server supports it |
| 59 | + if (indexes[i].collation && serverWireVersion < 5) { |
| 60 | + callback( |
| 61 | + new MongoError( |
| 62 | + `Server ${server.name}, which reports wire version ${serverWireVersion}, does not support collation` |
| 63 | + ) |
| 64 | + ); |
| 65 | + return; |
| 66 | + } |
27 | 67 |
|
28 |
| - // Did the user pass in a collation, check if our write server supports it |
29 |
| - if (indexSpecs[i].collation && capabilities && !capabilities.commandsTakeCollation) { |
30 |
| - return callback(new MongoError('server/primary/mongos does not support collation')); |
31 |
| - } |
| 68 | + if (indexes[i].name == null) { |
| 69 | + const keys = []; |
32 | 70 |
|
33 |
| - for (let name in indexSpecs[i].key) { |
34 |
| - keys.push(`${name}_${indexSpecs[i].key[name]}`); |
| 71 | + for (let name in indexes[i].key) { |
| 72 | + keys.push(`${name}_${indexes[i].key[name]}`); |
35 | 73 | }
|
36 | 74 |
|
37 | 75 | // Set the name
|
38 |
| - indexSpecs[i].name = keys.join('_'); |
| 76 | + indexes[i].name = keys.join('_'); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + const cmd = { createIndexes: this.collection, indexes }; |
| 81 | + |
| 82 | + if (options.commitQuorum != null) { |
| 83 | + if (serverWireVersion < 9) { |
| 84 | + callback( |
| 85 | + new MongoError('`commitQuorum` option for `createIndexes` not supported on servers < 4.4') |
| 86 | + ); |
| 87 | + return; |
39 | 88 | }
|
| 89 | + cmd.commitQuorum = options.commitQuorum; |
40 | 90 | }
|
41 | 91 |
|
42 |
| - options = Object.assign({}, options, { readPreference: ReadPreference.PRIMARY }); |
43 |
| - |
44 |
| - // Execute the index |
45 |
| - executeCommand( |
46 |
| - coll.s.db, |
47 |
| - { |
48 |
| - createIndexes: coll.collectionName, |
49 |
| - indexes: indexSpecs |
50 |
| - }, |
51 |
| - options, |
52 |
| - callback |
53 |
| - ); |
| 92 | + // collation is set on each index, it should not be defined at the root |
| 93 | + this.options.collation = undefined; |
| 94 | + |
| 95 | + super.executeCommand(server, cmd, (err, result) => { |
| 96 | + if (err) { |
| 97 | + callback(err); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + callback(null, this.onlyReturnNameOfCreatedIndex ? indexes[0].name : result); |
| 102 | + }); |
54 | 103 | }
|
55 | 104 | }
|
56 | 105 |
|
57 |
| -defineAspects(CreateIndexesOperation, Aspect.WRITE_OPERATION); |
| 106 | +defineAspects(CreateIndexesOperation, [Aspect.WRITE_OPERATION, Aspect.EXECUTE_WITH_SELECTION]); |
58 | 107 |
|
59 | 108 | module.exports = CreateIndexesOperation;
|
0 commit comments