create_collection.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CreateCollectionOperation = void 0;
  4. const constants_1 = require("../cmap/wire_protocol/constants");
  5. const collection_1 = require("../collection");
  6. const error_1 = require("../error");
  7. const command_1 = require("./command");
  8. const indexes_1 = require("./indexes");
  9. const operation_1 = require("./operation");
  10. const ILLEGAL_COMMAND_FIELDS = new Set([
  11. 'w',
  12. 'wtimeout',
  13. 'j',
  14. 'fsync',
  15. 'autoIndexId',
  16. 'pkFactory',
  17. 'raw',
  18. 'readPreference',
  19. 'session',
  20. 'readConcern',
  21. 'writeConcern',
  22. 'raw',
  23. 'fieldsAsRaw',
  24. 'useBigInt64',
  25. 'promoteLongs',
  26. 'promoteValues',
  27. 'promoteBuffers',
  28. 'bsonRegExp',
  29. 'serializeFunctions',
  30. 'ignoreUndefined',
  31. 'enableUtf8Validation'
  32. ]);
  33. /* @internal */
  34. const INVALID_QE_VERSION = 'Driver support of Queryable Encryption is incompatible with server. Upgrade server to use Queryable Encryption.';
  35. /** @internal */
  36. class CreateCollectionOperation extends command_1.CommandOperation {
  37. constructor(db, name, options = {}) {
  38. super(db, options);
  39. this.options = options;
  40. this.db = db;
  41. this.name = name;
  42. }
  43. async execute(server, session) {
  44. const db = this.db;
  45. const name = this.name;
  46. const options = this.options;
  47. const encryptedFields = options.encryptedFields ??
  48. db.client.options.autoEncryption?.encryptedFieldsMap?.[`${db.databaseName}.${name}`];
  49. if (encryptedFields) {
  50. // Creating a QE collection required min server of 7.0.0
  51. // TODO(NODE-5353): Get wire version information from connection.
  52. if (!server.loadBalanced &&
  53. server.description.maxWireVersion < constants_1.MIN_SUPPORTED_QE_WIRE_VERSION) {
  54. throw new error_1.MongoCompatibilityError(`${INVALID_QE_VERSION} The minimum server version required is ${constants_1.MIN_SUPPORTED_QE_SERVER_VERSION}`);
  55. }
  56. // Create auxilliary collections for queryable encryption support.
  57. const escCollection = encryptedFields.escCollection ?? `enxcol_.${name}.esc`;
  58. const ecocCollection = encryptedFields.ecocCollection ?? `enxcol_.${name}.ecoc`;
  59. for (const collectionName of [escCollection, ecocCollection]) {
  60. const createOp = new CreateCollectionOperation(db, collectionName, {
  61. clusteredIndex: {
  62. key: { _id: 1 },
  63. unique: true
  64. }
  65. });
  66. await createOp.executeWithoutEncryptedFieldsCheck(server, session);
  67. }
  68. if (!options.encryptedFields) {
  69. this.options = { ...this.options, encryptedFields };
  70. }
  71. }
  72. const coll = await this.executeWithoutEncryptedFieldsCheck(server, session);
  73. if (encryptedFields) {
  74. // Create the required index for queryable encryption support.
  75. const createIndexOp = new indexes_1.CreateIndexOperation(db, name, { __safeContent__: 1 }, {});
  76. await createIndexOp.execute(server, session);
  77. }
  78. return coll;
  79. }
  80. async executeWithoutEncryptedFieldsCheck(server, session) {
  81. const db = this.db;
  82. const name = this.name;
  83. const options = this.options;
  84. const cmd = { create: name };
  85. for (const n in options) {
  86. if (options[n] != null &&
  87. typeof options[n] !== 'function' &&
  88. !ILLEGAL_COMMAND_FIELDS.has(n)) {
  89. cmd[n] = options[n];
  90. }
  91. }
  92. // otherwise just execute the command
  93. await super.executeCommand(server, session, cmd);
  94. return new collection_1.Collection(db, name, options);
  95. }
  96. }
  97. exports.CreateCollectionOperation = CreateCollectionOperation;
  98. (0, operation_1.defineAspects)(CreateCollectionOperation, [operation_1.Aspect.WRITE_OPERATION]);
  99. //# sourceMappingURL=create_collection.js.map