create_collection.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.CommandCallbackOperation {
  37. constructor(db, name, options = {}) {
  38. super(db, options);
  39. this.options = options;
  40. this.db = db;
  41. this.name = name;
  42. }
  43. executeCallback(server, session, callback) {
  44. (async () => {
  45. const db = this.db;
  46. const name = this.name;
  47. const options = this.options;
  48. const encryptedFields = options.encryptedFields ??
  49. db.client.options.autoEncryption?.encryptedFieldsMap?.[`${db.databaseName}.${name}`];
  50. if (encryptedFields) {
  51. // Creating a QE collection required min server of 7.0.0
  52. // TODO(NODE-5353): Get wire version information from connection.
  53. if (!server.loadBalanced &&
  54. server.description.maxWireVersion < constants_1.MIN_SUPPORTED_QE_WIRE_VERSION) {
  55. throw new error_1.MongoCompatibilityError(`${INVALID_QE_VERSION} The minimum server version required is ${constants_1.MIN_SUPPORTED_QE_SERVER_VERSION}`);
  56. }
  57. // Create auxilliary collections for queryable encryption support.
  58. const escCollection = encryptedFields.escCollection ?? `enxcol_.${name}.esc`;
  59. const ecocCollection = encryptedFields.ecocCollection ?? `enxcol_.${name}.ecoc`;
  60. for (const collectionName of [escCollection, ecocCollection]) {
  61. const createOp = new CreateCollectionOperation(db, collectionName, {
  62. clusteredIndex: {
  63. key: { _id: 1 },
  64. unique: true
  65. }
  66. });
  67. await createOp.executeWithoutEncryptedFieldsCheck(server, session);
  68. }
  69. if (!options.encryptedFields) {
  70. this.options = { ...this.options, encryptedFields };
  71. }
  72. }
  73. const coll = await this.executeWithoutEncryptedFieldsCheck(server, session);
  74. if (encryptedFields) {
  75. // Create the required index for queryable encryption support.
  76. const createIndexOp = new indexes_1.CreateIndexOperation(db, name, { __safeContent__: 1 }, {});
  77. await createIndexOp.execute(server, session);
  78. }
  79. return coll;
  80. })().then(coll => callback(undefined, coll), err => callback(err));
  81. }
  82. executeWithoutEncryptedFieldsCheck(server, session) {
  83. return new Promise((resolve, reject) => {
  84. const db = this.db;
  85. const name = this.name;
  86. const options = this.options;
  87. const done = err => {
  88. if (err) {
  89. return reject(err);
  90. }
  91. resolve(new collection_1.Collection(db, name, options));
  92. };
  93. const cmd = { create: name };
  94. for (const n in options) {
  95. if (options[n] != null &&
  96. typeof options[n] !== 'function' &&
  97. !ILLEGAL_COMMAND_FIELDS.has(n)) {
  98. cmd[n] = options[n];
  99. }
  100. }
  101. // otherwise just execute the command
  102. super.executeCommandCallback(server, session, cmd, done);
  103. });
  104. }
  105. }
  106. exports.CreateCollectionOperation = CreateCollectionOperation;
  107. (0, operation_1.defineAspects)(CreateCollectionOperation, [operation_1.Aspect.WRITE_OPERATION]);
  108. //# sourceMappingURL=create_collection.js.map