insert.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.InsertManyOperation = exports.InsertOneOperation = exports.InsertOperation = void 0;
  4. const error_1 = require("../error");
  5. const write_concern_1 = require("../write_concern");
  6. const bulk_write_1 = require("./bulk_write");
  7. const command_1 = require("./command");
  8. const common_functions_1 = require("./common_functions");
  9. const operation_1 = require("./operation");
  10. /** @internal */
  11. class InsertOperation extends command_1.CommandOperation {
  12. constructor(ns, documents, options) {
  13. super(undefined, options);
  14. this.options = { ...options, checkKeys: options.checkKeys ?? false };
  15. this.ns = ns;
  16. this.documents = documents;
  17. }
  18. async execute(server, session) {
  19. const options = this.options ?? {};
  20. const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
  21. const command = {
  22. insert: this.ns.collection,
  23. documents: this.documents,
  24. ordered
  25. };
  26. if (typeof options.bypassDocumentValidation === 'boolean') {
  27. command.bypassDocumentValidation = options.bypassDocumentValidation;
  28. }
  29. // we check for undefined specifically here to allow falsy values
  30. // eslint-disable-next-line no-restricted-syntax
  31. if (options.comment !== undefined) {
  32. command.comment = options.comment;
  33. }
  34. return super.executeCommand(server, session, command);
  35. }
  36. }
  37. exports.InsertOperation = InsertOperation;
  38. class InsertOneOperation extends InsertOperation {
  39. constructor(collection, doc, options) {
  40. super(collection.s.namespace, (0, common_functions_1.prepareDocs)(collection, [doc], options), options);
  41. }
  42. async execute(server, session) {
  43. const res = await super.execute(server, session);
  44. if (res.code)
  45. throw new error_1.MongoServerError(res);
  46. if (res.writeErrors) {
  47. // This should be a WriteError but we can't change it now because of error hierarchy
  48. throw new error_1.MongoServerError(res.writeErrors[0]);
  49. }
  50. return {
  51. acknowledged: this.writeConcern?.w !== 0,
  52. insertedId: this.documents[0]._id
  53. };
  54. }
  55. }
  56. exports.InsertOneOperation = InsertOneOperation;
  57. /** @internal */
  58. class InsertManyOperation extends operation_1.AbstractOperation {
  59. constructor(collection, docs, options) {
  60. super(options);
  61. if (!Array.isArray(docs)) {
  62. throw new error_1.MongoInvalidArgumentError('Argument "docs" must be an array of documents');
  63. }
  64. this.options = options;
  65. this.collection = collection;
  66. this.docs = docs;
  67. }
  68. async execute(server, session) {
  69. const coll = this.collection;
  70. const options = { ...this.options, ...this.bsonOptions, readPreference: this.readPreference };
  71. const writeConcern = write_concern_1.WriteConcern.fromOptions(options);
  72. const bulkWriteOperation = new bulk_write_1.BulkWriteOperation(coll, (0, common_functions_1.prepareDocs)(coll, this.docs, options).map(document => ({ insertOne: { document } })), options);
  73. try {
  74. const res = await bulkWriteOperation.execute(server, session);
  75. return {
  76. acknowledged: writeConcern?.w !== 0,
  77. insertedCount: res.insertedCount,
  78. insertedIds: res.insertedIds
  79. };
  80. }
  81. catch (err) {
  82. if (err && err.message === 'Operation must be an object with an operation key') {
  83. throw new error_1.MongoInvalidArgumentError('Collection.insertMany() cannot be called with an array that has null/undefined values');
  84. }
  85. throw err;
  86. }
  87. }
  88. }
  89. exports.InsertManyOperation = InsertManyOperation;
  90. (0, operation_1.defineAspects)(InsertOperation, [operation_1.Aspect.RETRYABLE, operation_1.Aspect.WRITE_OPERATION]);
  91. (0, operation_1.defineAspects)(InsertOneOperation, [operation_1.Aspect.RETRYABLE, operation_1.Aspect.WRITE_OPERATION]);
  92. (0, operation_1.defineAspects)(InsertManyOperation, [operation_1.Aspect.WRITE_OPERATION]);
  93. //# sourceMappingURL=insert.js.map