insert.js 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.CommandCallbackOperation {
  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. executeCallback(server, session, callback) {
  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. super.executeCommandCallback(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. executeCallback(server, session, callback) {
  43. super.executeCallback(server, session, (err, res) => {
  44. if (err || res == null)
  45. return callback(err);
  46. if (res.code)
  47. return callback(new error_1.MongoServerError(res));
  48. if (res.writeErrors) {
  49. // This should be a WriteError but we can't change it now because of error hierarchy
  50. return callback(new error_1.MongoServerError(res.writeErrors[0]));
  51. }
  52. callback(undefined, {
  53. acknowledged: this.writeConcern?.w !== 0 ?? true,
  54. insertedId: this.documents[0]._id
  55. });
  56. });
  57. }
  58. }
  59. exports.InsertOneOperation = InsertOneOperation;
  60. /** @internal */
  61. class InsertManyOperation extends operation_1.AbstractCallbackOperation {
  62. constructor(collection, docs, options) {
  63. super(options);
  64. if (!Array.isArray(docs)) {
  65. throw new error_1.MongoInvalidArgumentError('Argument "docs" must be an array of documents');
  66. }
  67. this.options = options;
  68. this.collection = collection;
  69. this.docs = docs;
  70. }
  71. executeCallback(server, session, callback) {
  72. const coll = this.collection;
  73. const options = { ...this.options, ...this.bsonOptions, readPreference: this.readPreference };
  74. const writeConcern = write_concern_1.WriteConcern.fromOptions(options);
  75. const bulkWriteOperation = new bulk_write_1.BulkWriteOperation(coll, (0, common_functions_1.prepareDocs)(coll, this.docs, options).map(document => ({ insertOne: { document } })), options);
  76. bulkWriteOperation.executeCallback(server, session, (err, res) => {
  77. if (err || res == null) {
  78. if (err && err.message === 'Operation must be an object with an operation key') {
  79. err = new error_1.MongoInvalidArgumentError('Collection.insertMany() cannot be called with an array that has null/undefined values');
  80. }
  81. return callback(err);
  82. }
  83. callback(undefined, {
  84. acknowledged: writeConcern?.w !== 0 ?? true,
  85. insertedCount: res.insertedCount,
  86. insertedIds: res.insertedIds
  87. });
  88. });
  89. }
  90. }
  91. exports.InsertManyOperation = InsertManyOperation;
  92. (0, operation_1.defineAspects)(InsertOperation, [operation_1.Aspect.RETRYABLE, operation_1.Aspect.WRITE_OPERATION]);
  93. (0, operation_1.defineAspects)(InsertOneOperation, [operation_1.Aspect.RETRYABLE, operation_1.Aspect.WRITE_OPERATION]);
  94. (0, operation_1.defineAspects)(InsertManyOperation, [operation_1.Aspect.WRITE_OPERATION]);
  95. //# sourceMappingURL=insert.js.map