indexes.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.IndexInformationOperation = exports.IndexExistsOperation = exports.ListIndexesOperation = exports.DropIndexOperation = exports.EnsureIndexOperation = exports.CreateIndexOperation = exports.CreateIndexesOperation = exports.IndexesOperation = void 0;
  4. const error_1 = require("../error");
  5. const read_preference_1 = require("../read_preference");
  6. const utils_1 = require("../utils");
  7. const command_1 = require("./command");
  8. const common_functions_1 = require("./common_functions");
  9. const operation_1 = require("./operation");
  10. const VALID_INDEX_OPTIONS = new Set([
  11. 'background',
  12. 'unique',
  13. 'name',
  14. 'partialFilterExpression',
  15. 'sparse',
  16. 'hidden',
  17. 'expireAfterSeconds',
  18. 'storageEngine',
  19. 'collation',
  20. 'version',
  21. // text indexes
  22. 'weights',
  23. 'default_language',
  24. 'language_override',
  25. 'textIndexVersion',
  26. // 2d-sphere indexes
  27. '2dsphereIndexVersion',
  28. // 2d indexes
  29. 'bits',
  30. 'min',
  31. 'max',
  32. // geoHaystack Indexes
  33. 'bucketSize',
  34. // wildcard indexes
  35. 'wildcardProjection'
  36. ]);
  37. function isIndexDirection(x) {
  38. return (typeof x === 'number' || x === '2d' || x === '2dsphere' || x === 'text' || x === 'geoHaystack');
  39. }
  40. function isSingleIndexTuple(t) {
  41. return Array.isArray(t) && t.length === 2 && isIndexDirection(t[1]);
  42. }
  43. function makeIndexSpec(indexSpec, options) {
  44. const key = new Map();
  45. const indexSpecs = !Array.isArray(indexSpec) || isSingleIndexTuple(indexSpec) ? [indexSpec] : indexSpec;
  46. // Iterate through array and handle different types
  47. for (const spec of indexSpecs) {
  48. if (typeof spec === 'string') {
  49. key.set(spec, 1);
  50. }
  51. else if (Array.isArray(spec)) {
  52. key.set(spec[0], spec[1] ?? 1);
  53. }
  54. else if (spec instanceof Map) {
  55. for (const [property, value] of spec) {
  56. key.set(property, value);
  57. }
  58. }
  59. else if ((0, utils_1.isObject)(spec)) {
  60. for (const [property, value] of Object.entries(spec)) {
  61. key.set(property, value);
  62. }
  63. }
  64. }
  65. return { ...options, key };
  66. }
  67. /** @internal */
  68. class IndexesOperation extends operation_1.AbstractOperation {
  69. constructor(collection, options) {
  70. super(options);
  71. this.options = options;
  72. this.collection = collection;
  73. }
  74. async execute(_server, session) {
  75. const coll = this.collection;
  76. const options = this.options;
  77. return (0, common_functions_1.indexInformation)(coll.s.db, coll.collectionName, {
  78. full: true,
  79. ...options,
  80. readPreference: this.readPreference,
  81. session
  82. });
  83. }
  84. }
  85. exports.IndexesOperation = IndexesOperation;
  86. /** @internal */
  87. class CreateIndexesOperation extends command_1.CommandOperation {
  88. constructor(parent, collectionName, indexes, options) {
  89. super(parent, options);
  90. this.options = options ?? {};
  91. this.collectionName = collectionName;
  92. this.indexes = indexes.map(userIndex => {
  93. // Ensure the key is a Map to preserve index key ordering
  94. const key = userIndex.key instanceof Map ? userIndex.key : new Map(Object.entries(userIndex.key));
  95. const name = userIndex.name != null ? userIndex.name : Array.from(key).flat().join('_');
  96. const validIndexOptions = Object.fromEntries(Object.entries({ ...userIndex }).filter(([optionName]) => VALID_INDEX_OPTIONS.has(optionName)));
  97. return {
  98. ...validIndexOptions,
  99. name,
  100. key
  101. };
  102. });
  103. }
  104. async execute(server, session) {
  105. const options = this.options;
  106. const indexes = this.indexes;
  107. const serverWireVersion = (0, utils_1.maxWireVersion)(server);
  108. const cmd = { createIndexes: this.collectionName, indexes };
  109. if (options.commitQuorum != null) {
  110. if (serverWireVersion < 9) {
  111. throw new error_1.MongoCompatibilityError('Option `commitQuorum` for `createIndexes` not supported on servers < 4.4');
  112. }
  113. cmd.commitQuorum = options.commitQuorum;
  114. }
  115. // collation is set on each index, it should not be defined at the root
  116. this.options.collation = undefined;
  117. await super.executeCommand(server, session, cmd);
  118. const indexNames = indexes.map(index => index.name || '');
  119. return indexNames;
  120. }
  121. }
  122. exports.CreateIndexesOperation = CreateIndexesOperation;
  123. /** @internal */
  124. class CreateIndexOperation extends CreateIndexesOperation {
  125. constructor(parent, collectionName, indexSpec, options) {
  126. super(parent, collectionName, [makeIndexSpec(indexSpec, options)], options);
  127. }
  128. async execute(server, session) {
  129. const indexNames = await super.execute(server, session);
  130. return indexNames[0];
  131. }
  132. }
  133. exports.CreateIndexOperation = CreateIndexOperation;
  134. /** @internal */
  135. class EnsureIndexOperation extends CreateIndexOperation {
  136. constructor(db, collectionName, indexSpec, options) {
  137. super(db, collectionName, indexSpec, options);
  138. this.readPreference = read_preference_1.ReadPreference.primary;
  139. this.db = db;
  140. this.collectionName = collectionName;
  141. }
  142. async execute(server, session) {
  143. const indexName = this.indexes[0].name;
  144. const indexes = await this.db
  145. .collection(this.collectionName)
  146. .listIndexes({ session })
  147. .toArray()
  148. .catch(error => {
  149. if (error instanceof error_1.MongoError && error.code === error_1.MONGODB_ERROR_CODES.NamespaceNotFound)
  150. return [];
  151. throw error;
  152. });
  153. if (indexName && indexes.some(index => index.name === indexName))
  154. return indexName;
  155. return super.execute(server, session);
  156. }
  157. }
  158. exports.EnsureIndexOperation = EnsureIndexOperation;
  159. /** @internal */
  160. class DropIndexOperation extends command_1.CommandOperation {
  161. constructor(collection, indexName, options) {
  162. super(collection, options);
  163. this.options = options ?? {};
  164. this.collection = collection;
  165. this.indexName = indexName;
  166. }
  167. async execute(server, session) {
  168. const cmd = { dropIndexes: this.collection.collectionName, index: this.indexName };
  169. return super.executeCommand(server, session, cmd);
  170. }
  171. }
  172. exports.DropIndexOperation = DropIndexOperation;
  173. /** @internal */
  174. class ListIndexesOperation extends command_1.CommandOperation {
  175. constructor(collection, options) {
  176. super(collection, options);
  177. this.options = { ...options };
  178. delete this.options.writeConcern;
  179. this.collectionNamespace = collection.s.namespace;
  180. }
  181. async execute(server, session) {
  182. const serverWireVersion = (0, utils_1.maxWireVersion)(server);
  183. const cursor = this.options.batchSize ? { batchSize: this.options.batchSize } : {};
  184. const command = { listIndexes: this.collectionNamespace.collection, cursor };
  185. // we check for undefined specifically here to allow falsy values
  186. // eslint-disable-next-line no-restricted-syntax
  187. if (serverWireVersion >= 9 && this.options.comment !== undefined) {
  188. command.comment = this.options.comment;
  189. }
  190. return super.executeCommand(server, session, command);
  191. }
  192. }
  193. exports.ListIndexesOperation = ListIndexesOperation;
  194. /** @internal */
  195. class IndexExistsOperation extends operation_1.AbstractOperation {
  196. constructor(collection, indexes, options) {
  197. super(options);
  198. this.options = options;
  199. this.collection = collection;
  200. this.indexes = indexes;
  201. }
  202. async execute(server, session) {
  203. const coll = this.collection;
  204. const indexes = this.indexes;
  205. const info = await (0, common_functions_1.indexInformation)(coll.s.db, coll.collectionName, {
  206. ...this.options,
  207. readPreference: this.readPreference,
  208. session
  209. });
  210. // Let's check for the index names
  211. if (!Array.isArray(indexes))
  212. return info[indexes] != null;
  213. // All keys found return true
  214. return indexes.every(indexName => info[indexName] != null);
  215. }
  216. }
  217. exports.IndexExistsOperation = IndexExistsOperation;
  218. /** @internal */
  219. class IndexInformationOperation extends operation_1.AbstractOperation {
  220. constructor(db, name, options) {
  221. super(options);
  222. this.options = options ?? {};
  223. this.db = db;
  224. this.name = name;
  225. }
  226. async execute(server, session) {
  227. const db = this.db;
  228. const name = this.name;
  229. return (0, common_functions_1.indexInformation)(db, name, {
  230. ...this.options,
  231. readPreference: this.readPreference,
  232. session
  233. });
  234. }
  235. }
  236. exports.IndexInformationOperation = IndexInformationOperation;
  237. (0, operation_1.defineAspects)(ListIndexesOperation, [
  238. operation_1.Aspect.READ_OPERATION,
  239. operation_1.Aspect.RETRYABLE,
  240. operation_1.Aspect.CURSOR_CREATING
  241. ]);
  242. (0, operation_1.defineAspects)(CreateIndexesOperation, [operation_1.Aspect.WRITE_OPERATION]);
  243. (0, operation_1.defineAspects)(CreateIndexOperation, [operation_1.Aspect.WRITE_OPERATION]);
  244. (0, operation_1.defineAspects)(EnsureIndexOperation, [operation_1.Aspect.WRITE_OPERATION]);
  245. (0, operation_1.defineAspects)(DropIndexOperation, [operation_1.Aspect.WRITE_OPERATION]);
  246. //# sourceMappingURL=indexes.js.map