insert.js 4.2 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. 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. if (options.comment != null) {
  32. command.comment = options.comment;
  33. }
  34. super.executeCommand(server, session, command, callback);
  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. execute(server, session, callback) {
  43. super.execute(server, session, (err, res) => {
  44. var _a, _b;
  45. if (err || res == null)
  46. return callback(err);
  47. if (res.code)
  48. return callback(new error_1.MongoServerError(res));
  49. if (res.writeErrors) {
  50. // This should be a WriteError but we can't change it now because of error hierarchy
  51. return callback(new error_1.MongoServerError(res.writeErrors[0]));
  52. }
  53. callback(undefined, {
  54. acknowledged: (_b = ((_a = this.writeConcern) === null || _a === void 0 ? void 0 : _a.w) !== 0) !== null && _b !== void 0 ? _b : true,
  55. insertedId: this.documents[0]._id
  56. });
  57. });
  58. }
  59. }
  60. exports.InsertOneOperation = InsertOneOperation;
  61. /** @internal */
  62. class InsertManyOperation extends operation_1.AbstractOperation {
  63. constructor(collection, docs, options) {
  64. super(options);
  65. if (!Array.isArray(docs)) {
  66. throw new error_1.MongoInvalidArgumentError('Argument "docs" must be an array of documents');
  67. }
  68. this.options = options;
  69. this.collection = collection;
  70. this.docs = docs;
  71. }
  72. execute(server, session, callback) {
  73. const coll = this.collection;
  74. const options = { ...this.options, ...this.bsonOptions, readPreference: this.readPreference };
  75. const writeConcern = write_concern_1.WriteConcern.fromOptions(options);
  76. const bulkWriteOperation = new bulk_write_1.BulkWriteOperation(coll, (0, common_functions_1.prepareDocs)(coll, this.docs, options).map(document => ({ insertOne: { document } })), options);
  77. bulkWriteOperation.execute(server, session, (err, res) => {
  78. var _a;
  79. if (err || res == null)
  80. return callback(err);
  81. callback(undefined, {
  82. acknowledged: (_a = (writeConcern === null || writeConcern === void 0 ? void 0 : writeConcern.w) !== 0) !== null && _a !== void 0 ? _a : true,
  83. insertedCount: res.insertedCount,
  84. insertedIds: res.insertedIds
  85. });
  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