create_collection.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CreateCollectionOperation = void 0;
  4. const collection_1 = require("../collection");
  5. const command_1 = require("./command");
  6. const indexes_1 = require("./indexes");
  7. const operation_1 = require("./operation");
  8. const ILLEGAL_COMMAND_FIELDS = new Set([
  9. 'w',
  10. 'wtimeout',
  11. 'j',
  12. 'fsync',
  13. 'autoIndexId',
  14. 'pkFactory',
  15. 'raw',
  16. 'readPreference',
  17. 'session',
  18. 'readConcern',
  19. 'writeConcern',
  20. 'raw',
  21. 'fieldsAsRaw',
  22. 'promoteLongs',
  23. 'promoteValues',
  24. 'promoteBuffers',
  25. 'bsonRegExp',
  26. 'serializeFunctions',
  27. 'ignoreUndefined',
  28. 'enableUtf8Validation'
  29. ]);
  30. /** @internal */
  31. class CreateCollectionOperation extends command_1.CommandOperation {
  32. constructor(db, name, options = {}) {
  33. super(db, options);
  34. this.options = options;
  35. this.db = db;
  36. this.name = name;
  37. }
  38. execute(server, session, callback) {
  39. (async () => {
  40. var _a, _b, _c, _d, _e, _f;
  41. const db = this.db;
  42. const name = this.name;
  43. const options = this.options;
  44. const encryptedFields = (_a = options.encryptedFields) !== null && _a !== void 0 ? _a : (_c = (_b = db.s.client.options.autoEncryption) === null || _b === void 0 ? void 0 : _b.encryptedFieldsMap) === null || _c === void 0 ? void 0 : _c[`${db.databaseName}.${name}`];
  45. if (encryptedFields) {
  46. // Create auxilliary collections for queryable encryption support.
  47. const escCollection = (_d = encryptedFields.escCollection) !== null && _d !== void 0 ? _d : `enxcol_.${name}.esc`;
  48. const eccCollection = (_e = encryptedFields.eccCollection) !== null && _e !== void 0 ? _e : `enxcol_.${name}.ecc`;
  49. const ecocCollection = (_f = encryptedFields.ecocCollection) !== null && _f !== void 0 ? _f : `enxcol_.${name}.ecoc`;
  50. for (const collectionName of [escCollection, eccCollection, ecocCollection]) {
  51. const createOp = new CreateCollectionOperation(db, collectionName, {
  52. clusteredIndex: {
  53. key: { _id: 1 },
  54. unique: true
  55. }
  56. });
  57. await createOp.executeWithoutEncryptedFieldsCheck(server, session);
  58. }
  59. if (!options.encryptedFields) {
  60. this.options = { ...this.options, encryptedFields };
  61. }
  62. }
  63. const coll = await this.executeWithoutEncryptedFieldsCheck(server, session);
  64. if (encryptedFields) {
  65. // Create the required index for queryable encryption support.
  66. const createIndexOp = new indexes_1.CreateIndexOperation(db, name, { __safeContent__: 1 }, {});
  67. await new Promise((resolve, reject) => {
  68. createIndexOp.execute(server, session, err => (err ? reject(err) : resolve()));
  69. });
  70. }
  71. return coll;
  72. })().then(coll => callback(undefined, coll), err => callback(err));
  73. }
  74. executeWithoutEncryptedFieldsCheck(server, session) {
  75. return new Promise((resolve, reject) => {
  76. const db = this.db;
  77. const name = this.name;
  78. const options = this.options;
  79. const done = err => {
  80. if (err) {
  81. return reject(err);
  82. }
  83. resolve(new collection_1.Collection(db, name, options));
  84. };
  85. const cmd = { create: name };
  86. for (const n in options) {
  87. if (options[n] != null &&
  88. typeof options[n] !== 'function' &&
  89. !ILLEGAL_COMMAND_FIELDS.has(n)) {
  90. cmd[n] = options[n];
  91. }
  92. }
  93. // otherwise just execute the command
  94. super.executeCommand(server, session, cmd, done);
  95. });
  96. }
  97. }
  98. exports.CreateCollectionOperation = CreateCollectionOperation;
  99. (0, operation_1.defineAspects)(CreateCollectionOperation, [operation_1.Aspect.WRITE_OPERATION]);
  100. //# sourceMappingURL=create_collection.js.map