indexes.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.IndexInformationOperation = exports.IndexExistsOperation = exports.ListIndexesCursor = exports.ListIndexesOperation = exports.DropIndexesOperation = exports.DropIndexOperation = exports.EnsureIndexOperation = exports.CreateIndexOperation = exports.CreateIndexesOperation = exports.IndexesOperation = void 0;
  4. const abstract_cursor_1 = require("../cursor/abstract_cursor");
  5. const error_1 = require("../error");
  6. const read_preference_1 = require("../read_preference");
  7. const utils_1 = require("../utils");
  8. const command_1 = require("./command");
  9. const common_functions_1 = require("./common_functions");
  10. const execute_operation_1 = require("./execute_operation");
  11. const operation_1 = require("./operation");
  12. const VALID_INDEX_OPTIONS = new Set([
  13. 'background',
  14. 'unique',
  15. 'name',
  16. 'partialFilterExpression',
  17. 'sparse',
  18. 'hidden',
  19. 'expireAfterSeconds',
  20. 'storageEngine',
  21. 'collation',
  22. 'version',
  23. // text indexes
  24. 'weights',
  25. 'default_language',
  26. 'language_override',
  27. 'textIndexVersion',
  28. // 2d-sphere indexes
  29. '2dsphereIndexVersion',
  30. // 2d indexes
  31. 'bits',
  32. 'min',
  33. 'max',
  34. // geoHaystack Indexes
  35. 'bucketSize',
  36. // wildcard indexes
  37. 'wildcardProjection'
  38. ]);
  39. function makeIndexSpec(indexSpec, options) {
  40. const indexParameters = (0, utils_1.parseIndexOptions)(indexSpec);
  41. // Generate the index name
  42. const name = typeof options.name === 'string' ? options.name : indexParameters.name;
  43. // Set up the index
  44. const finalIndexSpec = { name, key: indexParameters.fieldHash };
  45. // merge valid index options into the index spec
  46. for (const optionName in options) {
  47. if (VALID_INDEX_OPTIONS.has(optionName)) {
  48. finalIndexSpec[optionName] = options[optionName];
  49. }
  50. }
  51. return finalIndexSpec;
  52. }
  53. /** @internal */
  54. class IndexesOperation extends operation_1.AbstractOperation {
  55. constructor(collection, options) {
  56. super(options);
  57. this.options = options;
  58. this.collection = collection;
  59. }
  60. execute(server, session, callback) {
  61. const coll = this.collection;
  62. const options = this.options;
  63. (0, common_functions_1.indexInformation)(coll.s.db, coll.collectionName, { full: true, ...options, readPreference: this.readPreference, session }, callback);
  64. }
  65. }
  66. exports.IndexesOperation = IndexesOperation;
  67. /** @internal */
  68. class CreateIndexesOperation extends command_1.CommandOperation {
  69. constructor(parent, collectionName, indexes, options) {
  70. super(parent, options);
  71. this.options = options !== null && options !== void 0 ? options : {};
  72. this.collectionName = collectionName;
  73. this.indexes = indexes;
  74. }
  75. execute(server, session, callback) {
  76. const options = this.options;
  77. const indexes = this.indexes;
  78. const serverWireVersion = (0, utils_1.maxWireVersion)(server);
  79. // Ensure we generate the correct name if the parameter is not set
  80. for (let i = 0; i < indexes.length; i++) {
  81. // Did the user pass in a collation, check if our write server supports it
  82. if (indexes[i].collation && serverWireVersion < 5) {
  83. callback(new error_1.MongoCompatibilityError(`Server ${server.name}, which reports wire version ${serverWireVersion}, ` +
  84. 'does not support collation'));
  85. return;
  86. }
  87. if (indexes[i].name == null) {
  88. const keys = [];
  89. for (const name in indexes[i].key) {
  90. keys.push(`${name}_${indexes[i].key[name]}`);
  91. }
  92. // Set the name
  93. indexes[i].name = keys.join('_');
  94. }
  95. }
  96. const cmd = { createIndexes: this.collectionName, indexes };
  97. if (options.commitQuorum != null) {
  98. if (serverWireVersion < 9) {
  99. callback(new error_1.MongoCompatibilityError('Option `commitQuorum` for `createIndexes` not supported on servers < 4.4'));
  100. return;
  101. }
  102. cmd.commitQuorum = options.commitQuorum;
  103. }
  104. // collation is set on each index, it should not be defined at the root
  105. this.options.collation = undefined;
  106. super.executeCommand(server, session, cmd, err => {
  107. if (err) {
  108. callback(err);
  109. return;
  110. }
  111. const indexNames = indexes.map(index => index.name || '');
  112. callback(undefined, indexNames);
  113. });
  114. }
  115. }
  116. exports.CreateIndexesOperation = CreateIndexesOperation;
  117. /** @internal */
  118. class CreateIndexOperation extends CreateIndexesOperation {
  119. constructor(parent, collectionName, indexSpec, options) {
  120. // createIndex can be called with a variety of styles:
  121. // coll.createIndex('a');
  122. // coll.createIndex({ a: 1 });
  123. // coll.createIndex([['a', 1]]);
  124. // createIndexes is always called with an array of index spec objects
  125. super(parent, collectionName, [makeIndexSpec(indexSpec, options)], options);
  126. }
  127. execute(server, session, callback) {
  128. super.execute(server, session, (err, indexNames) => {
  129. if (err || !indexNames)
  130. return callback(err);
  131. return callback(undefined, indexNames[0]);
  132. });
  133. }
  134. }
  135. exports.CreateIndexOperation = CreateIndexOperation;
  136. /** @internal */
  137. class EnsureIndexOperation extends CreateIndexOperation {
  138. constructor(db, collectionName, indexSpec, options) {
  139. super(db, collectionName, indexSpec, options);
  140. this.readPreference = read_preference_1.ReadPreference.primary;
  141. this.db = db;
  142. this.collectionName = collectionName;
  143. }
  144. execute(server, session, callback) {
  145. const indexName = this.indexes[0].name;
  146. const cursor = this.db.collection(this.collectionName).listIndexes({ session });
  147. cursor.toArray((err, indexes) => {
  148. /// ignore "NamespaceNotFound" errors
  149. if (err && err.code !== error_1.MONGODB_ERROR_CODES.NamespaceNotFound) {
  150. return callback(err);
  151. }
  152. if (indexes) {
  153. indexes = Array.isArray(indexes) ? indexes : [indexes];
  154. if (indexes.some(index => index.name === indexName)) {
  155. callback(undefined, indexName);
  156. return;
  157. }
  158. }
  159. super.execute(server, session, callback);
  160. });
  161. }
  162. }
  163. exports.EnsureIndexOperation = EnsureIndexOperation;
  164. /** @internal */
  165. class DropIndexOperation extends command_1.CommandOperation {
  166. constructor(collection, indexName, options) {
  167. super(collection, options);
  168. this.options = options !== null && options !== void 0 ? options : {};
  169. this.collection = collection;
  170. this.indexName = indexName;
  171. }
  172. execute(server, session, callback) {
  173. const cmd = { dropIndexes: this.collection.collectionName, index: this.indexName };
  174. super.executeCommand(server, session, cmd, callback);
  175. }
  176. }
  177. exports.DropIndexOperation = DropIndexOperation;
  178. /** @internal */
  179. class DropIndexesOperation extends DropIndexOperation {
  180. constructor(collection, options) {
  181. super(collection, '*', options);
  182. }
  183. execute(server, session, callback) {
  184. super.execute(server, session, err => {
  185. if (err)
  186. return callback(err, false);
  187. callback(undefined, true);
  188. });
  189. }
  190. }
  191. exports.DropIndexesOperation = DropIndexesOperation;
  192. /** @internal */
  193. class ListIndexesOperation extends command_1.CommandOperation {
  194. constructor(collection, options) {
  195. super(collection, options);
  196. this.options = options !== null && options !== void 0 ? options : {};
  197. this.collectionNamespace = collection.s.namespace;
  198. }
  199. execute(server, session, callback) {
  200. const serverWireVersion = (0, utils_1.maxWireVersion)(server);
  201. const cursor = this.options.batchSize ? { batchSize: this.options.batchSize } : {};
  202. const command = { listIndexes: this.collectionNamespace.collection, cursor };
  203. // we check for undefined specifically here to allow falsy values
  204. // eslint-disable-next-line no-restricted-syntax
  205. if (serverWireVersion >= 9 && this.options.comment !== undefined) {
  206. command.comment = this.options.comment;
  207. }
  208. super.executeCommand(server, session, command, callback);
  209. }
  210. }
  211. exports.ListIndexesOperation = ListIndexesOperation;
  212. /** @public */
  213. class ListIndexesCursor extends abstract_cursor_1.AbstractCursor {
  214. constructor(collection, options) {
  215. super(collection.s.db.s.client, collection.s.namespace, options);
  216. this.parent = collection;
  217. this.options = options;
  218. }
  219. clone() {
  220. return new ListIndexesCursor(this.parent, {
  221. ...this.options,
  222. ...this.cursorOptions
  223. });
  224. }
  225. /** @internal */
  226. _initialize(session, callback) {
  227. const operation = new ListIndexesOperation(this.parent, {
  228. ...this.cursorOptions,
  229. ...this.options,
  230. session
  231. });
  232. (0, execute_operation_1.executeOperation)(this.parent.s.db.s.client, operation, (err, response) => {
  233. if (err || response == null)
  234. return callback(err);
  235. // TODO: NODE-2882
  236. callback(undefined, { server: operation.server, session, response });
  237. });
  238. }
  239. }
  240. exports.ListIndexesCursor = ListIndexesCursor;
  241. /** @internal */
  242. class IndexExistsOperation extends operation_1.AbstractOperation {
  243. constructor(collection, indexes, options) {
  244. super(options);
  245. this.options = options;
  246. this.collection = collection;
  247. this.indexes = indexes;
  248. }
  249. execute(server, session, callback) {
  250. const coll = this.collection;
  251. const indexes = this.indexes;
  252. (0, common_functions_1.indexInformation)(coll.s.db, coll.collectionName, { ...this.options, readPreference: this.readPreference, session }, (err, indexInformation) => {
  253. // If we have an error return
  254. if (err != null)
  255. return callback(err);
  256. // Let's check for the index names
  257. if (!Array.isArray(indexes))
  258. return callback(undefined, indexInformation[indexes] != null);
  259. // Check in list of indexes
  260. for (let i = 0; i < indexes.length; i++) {
  261. if (indexInformation[indexes[i]] == null) {
  262. return callback(undefined, false);
  263. }
  264. }
  265. // All keys found return true
  266. return callback(undefined, true);
  267. });
  268. }
  269. }
  270. exports.IndexExistsOperation = IndexExistsOperation;
  271. /** @internal */
  272. class IndexInformationOperation extends operation_1.AbstractOperation {
  273. constructor(db, name, options) {
  274. super(options);
  275. this.options = options !== null && options !== void 0 ? options : {};
  276. this.db = db;
  277. this.name = name;
  278. }
  279. execute(server, session, callback) {
  280. const db = this.db;
  281. const name = this.name;
  282. (0, common_functions_1.indexInformation)(db, name, { ...this.options, readPreference: this.readPreference, session }, callback);
  283. }
  284. }
  285. exports.IndexInformationOperation = IndexInformationOperation;
  286. (0, operation_1.defineAspects)(ListIndexesOperation, [
  287. operation_1.Aspect.READ_OPERATION,
  288. operation_1.Aspect.RETRYABLE,
  289. operation_1.Aspect.CURSOR_CREATING
  290. ]);
  291. (0, operation_1.defineAspects)(CreateIndexesOperation, [operation_1.Aspect.WRITE_OPERATION]);
  292. (0, operation_1.defineAspects)(CreateIndexOperation, [operation_1.Aspect.WRITE_OPERATION]);
  293. (0, operation_1.defineAspects)(EnsureIndexOperation, [operation_1.Aspect.WRITE_OPERATION]);
  294. (0, operation_1.defineAspects)(DropIndexOperation, [operation_1.Aspect.WRITE_OPERATION]);
  295. (0, operation_1.defineAspects)(DropIndexesOperation, [operation_1.Aspect.WRITE_OPERATION]);
  296. //# sourceMappingURL=indexes.js.map