insert.js 4.7 KB

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