@depup/mongodb 7.1.0-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +201 -0
- package/README.md +33 -0
- package/etc/prepare.js +12 -0
- package/lib/admin.js +136 -0
- package/lib/admin.js.map +1 -0
- package/lib/bson.js +85 -0
- package/lib/bson.js.map +1 -0
- package/lib/bulk/common.js +835 -0
- package/lib/bulk/common.js.map +1 -0
- package/lib/bulk/ordered.js +67 -0
- package/lib/bulk/ordered.js.map +1 -0
- package/lib/bulk/unordered.js +92 -0
- package/lib/bulk/unordered.js.map +1 -0
- package/lib/change_stream.js +513 -0
- package/lib/change_stream.js.map +1 -0
- package/lib/client-side-encryption/auto_encrypter.js +272 -0
- package/lib/client-side-encryption/auto_encrypter.js.map +1 -0
- package/lib/client-side-encryption/client_encryption.js +609 -0
- package/lib/client-side-encryption/client_encryption.js.map +1 -0
- package/lib/client-side-encryption/errors.js +138 -0
- package/lib/client-side-encryption/errors.js.map +1 -0
- package/lib/client-side-encryption/mongocryptd_manager.js +85 -0
- package/lib/client-side-encryption/mongocryptd_manager.js.map +1 -0
- package/lib/client-side-encryption/providers/aws.js +23 -0
- package/lib/client-side-encryption/providers/aws.js.map +1 -0
- package/lib/client-side-encryption/providers/azure.js +132 -0
- package/lib/client-side-encryption/providers/azure.js.map +1 -0
- package/lib/client-side-encryption/providers/gcp.js +16 -0
- package/lib/client-side-encryption/providers/gcp.js.map +1 -0
- package/lib/client-side-encryption/providers/index.js +43 -0
- package/lib/client-side-encryption/providers/index.js.map +1 -0
- package/lib/client-side-encryption/state_machine.js +427 -0
- package/lib/client-side-encryption/state_machine.js.map +1 -0
- package/lib/cmap/auth/auth_provider.js +51 -0
- package/lib/cmap/auth/auth_provider.js.map +1 -0
- package/lib/cmap/auth/aws4.js +161 -0
- package/lib/cmap/auth/aws4.js.map +1 -0
- package/lib/cmap/auth/aws_temporary_credentials.js +103 -0
- package/lib/cmap/auth/aws_temporary_credentials.js.map +1 -0
- package/lib/cmap/auth/gssapi.js +155 -0
- package/lib/cmap/auth/gssapi.js.map +1 -0
- package/lib/cmap/auth/mongo_credentials.js +169 -0
- package/lib/cmap/auth/mongo_credentials.js.map +1 -0
- package/lib/cmap/auth/mongodb_aws.js +128 -0
- package/lib/cmap/auth/mongodb_aws.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/automated_callback_workflow.js +84 -0
- package/lib/cmap/auth/mongodb_oidc/automated_callback_workflow.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/azure_machine_workflow.js +62 -0
- package/lib/cmap/auth/mongodb_oidc/azure_machine_workflow.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/callback_workflow.js +141 -0
- package/lib/cmap/auth/mongodb_oidc/callback_workflow.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/command_builders.js +44 -0
- package/lib/cmap/auth/mongodb_oidc/command_builders.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/gcp_machine_workflow.js +39 -0
- package/lib/cmap/auth/mongodb_oidc/gcp_machine_workflow.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/human_callback_workflow.js +122 -0
- package/lib/cmap/auth/mongodb_oidc/human_callback_workflow.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/k8s_machine_workflow.js +32 -0
- package/lib/cmap/auth/mongodb_oidc/k8s_machine_workflow.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/token_cache.js +52 -0
- package/lib/cmap/auth/mongodb_oidc/token_cache.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/token_machine_workflow.js +23 -0
- package/lib/cmap/auth/mongodb_oidc/token_machine_workflow.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc.js +73 -0
- package/lib/cmap/auth/mongodb_oidc.js.map +1 -0
- package/lib/cmap/auth/plain.js +26 -0
- package/lib/cmap/auth/plain.js.map +1 -0
- package/lib/cmap/auth/providers.js +22 -0
- package/lib/cmap/auth/providers.js.map +1 -0
- package/lib/cmap/auth/scram.js +254 -0
- package/lib/cmap/auth/scram.js.map +1 -0
- package/lib/cmap/auth/x509.js +36 -0
- package/lib/cmap/auth/x509.js.map +1 -0
- package/lib/cmap/command_monitoring_events.js +223 -0
- package/lib/cmap/command_monitoring_events.js.map +1 -0
- package/lib/cmap/commands.js +535 -0
- package/lib/cmap/commands.js.map +1 -0
- package/lib/cmap/connect.js +381 -0
- package/lib/cmap/connect.js.map +1 -0
- package/lib/cmap/connection.js +571 -0
- package/lib/cmap/connection.js.map +1 -0
- package/lib/cmap/connection_pool.js +558 -0
- package/lib/cmap/connection_pool.js.map +1 -0
- package/lib/cmap/connection_pool_events.js +190 -0
- package/lib/cmap/connection_pool_events.js.map +1 -0
- package/lib/cmap/errors.js +108 -0
- package/lib/cmap/errors.js.map +1 -0
- package/lib/cmap/handshake/client_metadata.js +242 -0
- package/lib/cmap/handshake/client_metadata.js.map +1 -0
- package/lib/cmap/metrics.js +62 -0
- package/lib/cmap/metrics.js.map +1 -0
- package/lib/cmap/stream_description.js +70 -0
- package/lib/cmap/stream_description.js.map +1 -0
- package/lib/cmap/wire_protocol/compression.js +178 -0
- package/lib/cmap/wire_protocol/compression.js.map +1 -0
- package/lib/cmap/wire_protocol/constants.js +21 -0
- package/lib/cmap/wire_protocol/constants.js.map +1 -0
- package/lib/cmap/wire_protocol/on_data.js +111 -0
- package/lib/cmap/wire_protocol/on_data.js.map +1 -0
- package/lib/cmap/wire_protocol/on_demand/document.js +222 -0
- package/lib/cmap/wire_protocol/on_demand/document.js.map +1 -0
- package/lib/cmap/wire_protocol/responses.js +315 -0
- package/lib/cmap/wire_protocol/responses.js.map +1 -0
- package/lib/cmap/wire_protocol/shared.js +35 -0
- package/lib/cmap/wire_protocol/shared.js.map +1 -0
- package/lib/collection.js +751 -0
- package/lib/collection.js.map +1 -0
- package/lib/connection_string.js +1090 -0
- package/lib/connection_string.js.map +1 -0
- package/lib/constants.js +170 -0
- package/lib/constants.js.map +1 -0
- package/lib/cursor/abstract_cursor.js +924 -0
- package/lib/cursor/abstract_cursor.js.map +1 -0
- package/lib/cursor/aggregation_cursor.js +164 -0
- package/lib/cursor/aggregation_cursor.js.map +1 -0
- package/lib/cursor/change_stream_cursor.js +104 -0
- package/lib/cursor/change_stream_cursor.js.map +1 -0
- package/lib/cursor/client_bulk_write_cursor.js +52 -0
- package/lib/cursor/client_bulk_write_cursor.js.map +1 -0
- package/lib/cursor/explainable_cursor.js +36 -0
- package/lib/cursor/explainable_cursor.js.map +1 -0
- package/lib/cursor/find_cursor.js +399 -0
- package/lib/cursor/find_cursor.js.map +1 -0
- package/lib/cursor/list_collections_cursor.js +34 -0
- package/lib/cursor/list_collections_cursor.js.map +1 -0
- package/lib/cursor/list_indexes_cursor.js +32 -0
- package/lib/cursor/list_indexes_cursor.js.map +1 -0
- package/lib/cursor/list_search_indexes_cursor.js +14 -0
- package/lib/cursor/list_search_indexes_cursor.js.map +1 -0
- package/lib/cursor/run_command_cursor.js +94 -0
- package/lib/cursor/run_command_cursor.js.map +1 -0
- package/lib/db.js +419 -0
- package/lib/db.js.map +1 -0
- package/lib/deps.js +112 -0
- package/lib/deps.js.map +1 -0
- package/lib/encrypter.js +106 -0
- package/lib/encrypter.js.map +1 -0
- package/lib/error.js +1380 -0
- package/lib/error.js.map +1 -0
- package/lib/explain.js +59 -0
- package/lib/explain.js.map +1 -0
- package/lib/gridfs/download.js +305 -0
- package/lib/gridfs/download.js.map +1 -0
- package/lib/gridfs/index.js +164 -0
- package/lib/gridfs/index.js.map +1 -0
- package/lib/gridfs/upload.js +358 -0
- package/lib/gridfs/upload.js.map +1 -0
- package/lib/index.js +193 -0
- package/lib/index.js.map +1 -0
- package/lib/mongo_client.js +557 -0
- package/lib/mongo_client.js.map +1 -0
- package/lib/mongo_client_auth_providers.js +80 -0
- package/lib/mongo_client_auth_providers.js.map +1 -0
- package/lib/mongo_logger.js +655 -0
- package/lib/mongo_logger.js.map +1 -0
- package/lib/mongo_types.js +56 -0
- package/lib/mongo_types.js.map +1 -0
- package/lib/operations/aggregate.js +90 -0
- package/lib/operations/aggregate.js.map +1 -0
- package/lib/operations/client_bulk_write/client_bulk_write.js +51 -0
- package/lib/operations/client_bulk_write/client_bulk_write.js.map +1 -0
- package/lib/operations/client_bulk_write/command_builder.js +340 -0
- package/lib/operations/client_bulk_write/command_builder.js.map +1 -0
- package/lib/operations/client_bulk_write/common.js +3 -0
- package/lib/operations/client_bulk_write/common.js.map +1 -0
- package/lib/operations/client_bulk_write/executor.js +120 -0
- package/lib/operations/client_bulk_write/executor.js.map +1 -0
- package/lib/operations/client_bulk_write/results_merger.js +204 -0
- package/lib/operations/client_bulk_write/results_merger.js.map +1 -0
- package/lib/operations/command.js +83 -0
- package/lib/operations/command.js.map +1 -0
- package/lib/operations/count.js +45 -0
- package/lib/operations/count.js.map +1 -0
- package/lib/operations/create_collection.js +109 -0
- package/lib/operations/create_collection.js.map +1 -0
- package/lib/operations/delete.js +125 -0
- package/lib/operations/delete.js.map +1 -0
- package/lib/operations/distinct.js +61 -0
- package/lib/operations/distinct.js.map +1 -0
- package/lib/operations/drop.js +93 -0
- package/lib/operations/drop.js.map +1 -0
- package/lib/operations/end_sessions.js +34 -0
- package/lib/operations/end_sessions.js.map +1 -0
- package/lib/operations/estimated_document_count.js +41 -0
- package/lib/operations/estimated_document_count.js.map +1 -0
- package/lib/operations/execute_operation.js +239 -0
- package/lib/operations/execute_operation.js.map +1 -0
- package/lib/operations/find.js +148 -0
- package/lib/operations/find.js.map +1 -0
- package/lib/operations/find_and_modify.js +158 -0
- package/lib/operations/find_and_modify.js.map +1 -0
- package/lib/operations/get_more.js +62 -0
- package/lib/operations/get_more.js.map +1 -0
- package/lib/operations/indexes.js +186 -0
- package/lib/operations/indexes.js.map +1 -0
- package/lib/operations/insert.js +70 -0
- package/lib/operations/insert.js.map +1 -0
- package/lib/operations/kill_cursors.js +43 -0
- package/lib/operations/kill_cursors.js.map +1 -0
- package/lib/operations/list_collections.js +53 -0
- package/lib/operations/list_collections.js.map +1 -0
- package/lib/operations/list_databases.js +40 -0
- package/lib/operations/list_databases.js.map +1 -0
- package/lib/operations/operation.js +102 -0
- package/lib/operations/operation.js.map +1 -0
- package/lib/operations/profiling_level.js +43 -0
- package/lib/operations/profiling_level.js.map +1 -0
- package/lib/operations/remove_user.js +27 -0
- package/lib/operations/remove_user.js.map +1 -0
- package/lib/operations/rename.js +38 -0
- package/lib/operations/rename.js.map +1 -0
- package/lib/operations/run_command.js +47 -0
- package/lib/operations/run_command.js.map +1 -0
- package/lib/operations/search_indexes/create.js +33 -0
- package/lib/operations/search_indexes/create.js.map +1 -0
- package/lib/operations/search_indexes/drop.js +43 -0
- package/lib/operations/search_indexes/drop.js.map +1 -0
- package/lib/operations/search_indexes/update.js +35 -0
- package/lib/operations/search_indexes/update.js.map +1 -0
- package/lib/operations/set_profiling_level.js +53 -0
- package/lib/operations/set_profiling_level.js.map +1 -0
- package/lib/operations/stats.js +27 -0
- package/lib/operations/stats.js.map +1 -0
- package/lib/operations/update.js +188 -0
- package/lib/operations/update.js.map +1 -0
- package/lib/operations/validate_collection.js +37 -0
- package/lib/operations/validate_collection.js.map +1 -0
- package/lib/read_concern.js +73 -0
- package/lib/read_concern.js.map +1 -0
- package/lib/read_preference.js +191 -0
- package/lib/read_preference.js.map +1 -0
- package/lib/sdam/common.js +49 -0
- package/lib/sdam/common.js.map +1 -0
- package/lib/sdam/events.js +146 -0
- package/lib/sdam/events.js.map +1 -0
- package/lib/sdam/monitor.js +544 -0
- package/lib/sdam/monitor.js.map +1 -0
- package/lib/sdam/server.js +404 -0
- package/lib/sdam/server.js.map +1 -0
- package/lib/sdam/server_description.js +204 -0
- package/lib/sdam/server_description.js.map +1 -0
- package/lib/sdam/server_selection.js +294 -0
- package/lib/sdam/server_selection.js.map +1 -0
- package/lib/sdam/server_selection_events.js +85 -0
- package/lib/sdam/server_selection_events.js.map +1 -0
- package/lib/sdam/srv_polling.js +108 -0
- package/lib/sdam/srv_polling.js.map +1 -0
- package/lib/sdam/topology.js +652 -0
- package/lib/sdam/topology.js.map +1 -0
- package/lib/sdam/topology_description.js +383 -0
- package/lib/sdam/topology_description.js.map +1 -0
- package/lib/sessions.js +892 -0
- package/lib/sessions.js.map +1 -0
- package/lib/sort.js +103 -0
- package/lib/sort.js.map +1 -0
- package/lib/timeout.js +296 -0
- package/lib/timeout.js.map +1 -0
- package/lib/transactions.js +135 -0
- package/lib/transactions.js.map +1 -0
- package/lib/utils.js +1163 -0
- package/lib/utils.js.map +1 -0
- package/lib/write_concern.js +100 -0
- package/lib/write_concern.js.map +1 -0
- package/mongodb.d.ts +9018 -0
- package/package.json +197 -0
- package/src/admin.ts +173 -0
- package/src/bson.ts +149 -0
- package/src/bulk/common.ts +1251 -0
- package/src/bulk/ordered.ts +83 -0
- package/src/bulk/unordered.ts +115 -0
- package/src/change_stream.ts +1160 -0
- package/src/client-side-encryption/auto_encrypter.ts +469 -0
- package/src/client-side-encryption/client_encryption.ts +1150 -0
- package/src/client-side-encryption/errors.ts +144 -0
- package/src/client-side-encryption/mongocryptd_manager.ts +100 -0
- package/src/client-side-encryption/providers/aws.ts +33 -0
- package/src/client-side-encryption/providers/azure.ts +181 -0
- package/src/client-side-encryption/providers/gcp.ts +16 -0
- package/src/client-side-encryption/providers/index.ts +207 -0
- package/src/client-side-encryption/state_machine.ts +651 -0
- package/src/cmap/auth/auth_provider.ts +77 -0
- package/src/cmap/auth/aws4.ts +207 -0
- package/src/cmap/auth/aws_temporary_credentials.ts +129 -0
- package/src/cmap/auth/gssapi.ts +203 -0
- package/src/cmap/auth/mongo_credentials.ts +292 -0
- package/src/cmap/auth/mongodb_aws.ts +179 -0
- package/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts +88 -0
- package/src/cmap/auth/mongodb_oidc/azure_machine_workflow.ts +73 -0
- package/src/cmap/auth/mongodb_oidc/callback_workflow.ts +188 -0
- package/src/cmap/auth/mongodb_oidc/command_builders.ts +53 -0
- package/src/cmap/auth/mongodb_oidc/gcp_machine_workflow.ts +46 -0
- package/src/cmap/auth/mongodb_oidc/human_callback_workflow.ts +141 -0
- package/src/cmap/auth/mongodb_oidc/k8s_machine_workflow.ts +31 -0
- package/src/cmap/auth/mongodb_oidc/token_cache.ts +62 -0
- package/src/cmap/auth/mongodb_oidc/token_machine_workflow.ts +22 -0
- package/src/cmap/auth/mongodb_oidc.ts +185 -0
- package/src/cmap/auth/plain.ts +25 -0
- package/src/cmap/auth/providers.ts +22 -0
- package/src/cmap/auth/scram.ts +344 -0
- package/src/cmap/auth/x509.ts +43 -0
- package/src/cmap/command_monitoring_events.ts +316 -0
- package/src/cmap/commands.ts +788 -0
- package/src/cmap/connect.ts +518 -0
- package/src/cmap/connection.ts +931 -0
- package/src/cmap/connection_pool.ts +831 -0
- package/src/cmap/connection_pool_events.ts +300 -0
- package/src/cmap/errors.ts +119 -0
- package/src/cmap/handshake/client_metadata.ts +353 -0
- package/src/cmap/metrics.ts +58 -0
- package/src/cmap/stream_description.ts +96 -0
- package/src/cmap/wire_protocol/compression.ts +211 -0
- package/src/cmap/wire_protocol/constants.ts +17 -0
- package/src/cmap/wire_protocol/on_data.ts +139 -0
- package/src/cmap/wire_protocol/on_demand/document.ts +358 -0
- package/src/cmap/wire_protocol/responses.ts +393 -0
- package/src/cmap/wire_protocol/shared.ts +48 -0
- package/src/collection.ts +1314 -0
- package/src/connection_string.ts +1297 -0
- package/src/constants.ts +176 -0
- package/src/cursor/abstract_cursor.ts +1247 -0
- package/src/cursor/aggregation_cursor.ts +245 -0
- package/src/cursor/change_stream_cursor.ts +171 -0
- package/src/cursor/client_bulk_write_cursor.ts +83 -0
- package/src/cursor/explainable_cursor.ts +51 -0
- package/src/cursor/find_cursor.ts +497 -0
- package/src/cursor/list_collections_cursor.ts +50 -0
- package/src/cursor/list_indexes_cursor.ts +37 -0
- package/src/cursor/list_search_indexes_cursor.ts +20 -0
- package/src/cursor/run_command_cursor.ts +178 -0
- package/src/db.ts +621 -0
- package/src/deps.ts +227 -0
- package/src/encrypter.ts +127 -0
- package/src/error.ts +1567 -0
- package/src/explain.ts +124 -0
- package/src/gridfs/download.ts +483 -0
- package/src/gridfs/index.ts +264 -0
- package/src/gridfs/upload.ts +537 -0
- package/src/index.ts +633 -0
- package/src/mongo_client.ts +1155 -0
- package/src/mongo_client_auth_providers.ts +87 -0
- package/src/mongo_logger.ts +1081 -0
- package/src/mongo_types.ts +678 -0
- package/src/operations/aggregate.ts +153 -0
- package/src/operations/client_bulk_write/client_bulk_write.ts +72 -0
- package/src/operations/client_bulk_write/command_builder.ts +487 -0
- package/src/operations/client_bulk_write/common.ts +276 -0
- package/src/operations/client_bulk_write/executor.ts +149 -0
- package/src/operations/client_bulk_write/results_merger.ts +260 -0
- package/src/operations/command.ts +171 -0
- package/src/operations/count.ts +74 -0
- package/src/operations/create_collection.ts +218 -0
- package/src/operations/delete.ts +183 -0
- package/src/operations/distinct.ts +92 -0
- package/src/operations/drop.ts +130 -0
- package/src/operations/end_sessions.ts +44 -0
- package/src/operations/estimated_document_count.ts +61 -0
- package/src/operations/execute_operation.ts +319 -0
- package/src/operations/find.ts +254 -0
- package/src/operations/find_and_modify.ts +319 -0
- package/src/operations/get_more.ts +108 -0
- package/src/operations/indexes.ts +418 -0
- package/src/operations/insert.ts +108 -0
- package/src/operations/kill_cursors.ts +64 -0
- package/src/operations/list_collections.ts +108 -0
- package/src/operations/list_databases.ts +68 -0
- package/src/operations/operation.ts +181 -0
- package/src/operations/profiling_level.ts +46 -0
- package/src/operations/remove_user.ts +36 -0
- package/src/operations/rename.ts +60 -0
- package/src/operations/run_command.ts +79 -0
- package/src/operations/search_indexes/create.ts +56 -0
- package/src/operations/search_indexes/drop.ts +58 -0
- package/src/operations/search_indexes/update.ts +45 -0
- package/src/operations/set_profiling_level.ts +74 -0
- package/src/operations/stats.ts +37 -0
- package/src/operations/update.ts +318 -0
- package/src/operations/validate_collection.ts +50 -0
- package/src/read_concern.ts +88 -0
- package/src/read_preference.ts +256 -0
- package/src/sdam/common.ts +74 -0
- package/src/sdam/events.ts +219 -0
- package/src/sdam/monitor.ts +771 -0
- package/src/sdam/server.ts +595 -0
- package/src/sdam/server_description.ts +297 -0
- package/src/sdam/server_selection.ts +415 -0
- package/src/sdam/server_selection_events.ts +142 -0
- package/src/sdam/srv_polling.ts +146 -0
- package/src/sdam/topology.ts +1100 -0
- package/src/sdam/topology_description.ts +548 -0
- package/src/sessions.ts +1244 -0
- package/src/sort.ts +141 -0
- package/src/timeout.ts +405 -0
- package/src/transactions.ts +181 -0
- package/src/utils.ts +1434 -0
- package/src/write_concern.ts +183 -0
- package/tsconfig.json +46 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { type Document } from '../../bson';
|
|
2
|
+
import type { Filter, OptionalId, UpdateFilter, WithoutId } from '../../mongo_types';
|
|
3
|
+
import type { CollationOptions, CommandOperationOptions } from '../../operations/command';
|
|
4
|
+
import type { Hint } from '../../operations/operation';
|
|
5
|
+
import { type Sort } from '../../sort';
|
|
6
|
+
|
|
7
|
+
/** @public */
|
|
8
|
+
export interface ClientBulkWriteOptions extends CommandOperationOptions {
|
|
9
|
+
/**
|
|
10
|
+
* If true, when an insert fails, don't execute the remaining writes.
|
|
11
|
+
* If false, continue with remaining inserts when one fails.
|
|
12
|
+
* @defaultValue `true` - inserts are ordered by default
|
|
13
|
+
*/
|
|
14
|
+
ordered?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Allow driver to bypass schema validation.
|
|
17
|
+
* @defaultValue `false` - documents will be validated by default
|
|
18
|
+
**/
|
|
19
|
+
bypassDocumentValidation?: boolean;
|
|
20
|
+
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
|
|
21
|
+
let?: Document;
|
|
22
|
+
/**
|
|
23
|
+
* Whether detailed results for each successful operation should be included in the returned
|
|
24
|
+
* BulkWriteResult.
|
|
25
|
+
*/
|
|
26
|
+
verboseResults?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** @public */
|
|
30
|
+
export interface ClientWriteModel {
|
|
31
|
+
/**
|
|
32
|
+
* The namespace for the write.
|
|
33
|
+
*
|
|
34
|
+
* A namespace is a combination of the database name and the name of the collection: `<database-name>.<collection>`.
|
|
35
|
+
* All documents belong to a namespace.
|
|
36
|
+
*
|
|
37
|
+
* @see https://www.mongodb.com/docs/manual/reference/limits/#std-label-faq-dev-namespace
|
|
38
|
+
*/
|
|
39
|
+
namespace: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** @public */
|
|
43
|
+
export interface ClientInsertOneModel<TSchema> extends ClientWriteModel {
|
|
44
|
+
name: 'insertOne';
|
|
45
|
+
/** The document to insert. */
|
|
46
|
+
document: OptionalId<TSchema>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** @public */
|
|
50
|
+
export interface ClientDeleteOneModel<TSchema> extends ClientWriteModel {
|
|
51
|
+
name: 'deleteOne';
|
|
52
|
+
/**
|
|
53
|
+
* The filter used to determine if a document should be deleted.
|
|
54
|
+
* For a deleteOne operation, the first match is removed.
|
|
55
|
+
*/
|
|
56
|
+
filter: Filter<TSchema>;
|
|
57
|
+
/** Specifies a collation. */
|
|
58
|
+
collation?: CollationOptions;
|
|
59
|
+
/** The index to use. If specified, then the query system will only consider plans using the hinted index. */
|
|
60
|
+
hint?: Hint;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** @public */
|
|
64
|
+
export interface ClientDeleteManyModel<TSchema> extends ClientWriteModel {
|
|
65
|
+
name: 'deleteMany';
|
|
66
|
+
/**
|
|
67
|
+
* The filter used to determine if a document should be deleted.
|
|
68
|
+
* For a deleteMany operation, all matches are removed.
|
|
69
|
+
*/
|
|
70
|
+
filter: Filter<TSchema>;
|
|
71
|
+
/** Specifies a collation. */
|
|
72
|
+
collation?: CollationOptions;
|
|
73
|
+
/** The index to use. If specified, then the query system will only consider plans using the hinted index. */
|
|
74
|
+
hint?: Hint;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** @public */
|
|
78
|
+
export interface ClientReplaceOneModel<TSchema> extends ClientWriteModel {
|
|
79
|
+
name: 'replaceOne';
|
|
80
|
+
/**
|
|
81
|
+
* The filter used to determine if a document should be replaced.
|
|
82
|
+
* For a replaceOne operation, the first match is replaced.
|
|
83
|
+
*/
|
|
84
|
+
filter: Filter<TSchema>;
|
|
85
|
+
/** The document with which to replace the matched document. */
|
|
86
|
+
replacement: WithoutId<TSchema>;
|
|
87
|
+
/** Specifies a collation. */
|
|
88
|
+
collation?: CollationOptions;
|
|
89
|
+
/** The index to use. If specified, then the query system will only consider plans using the hinted index. */
|
|
90
|
+
hint?: Hint;
|
|
91
|
+
/** When true, creates a new document if no document matches the query. */
|
|
92
|
+
upsert?: boolean;
|
|
93
|
+
/** Specifies the sort order for the documents matched by the filter. */
|
|
94
|
+
sort?: Sort;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** @public */
|
|
98
|
+
export interface ClientUpdateOneModel<TSchema> extends ClientWriteModel {
|
|
99
|
+
name: 'updateOne';
|
|
100
|
+
/**
|
|
101
|
+
* The filter used to determine if a document should be updated.
|
|
102
|
+
* For an updateOne operation, the first match is updated.
|
|
103
|
+
*/
|
|
104
|
+
filter: Filter<TSchema>;
|
|
105
|
+
/**
|
|
106
|
+
* The modifications to apply. The value can be either:
|
|
107
|
+
* UpdateFilter<Document> - A document that contains update operator expressions,
|
|
108
|
+
* Document[] - an aggregation pipeline.
|
|
109
|
+
*/
|
|
110
|
+
update: UpdateFilter<TSchema> | Document[];
|
|
111
|
+
/** A set of filters specifying to which array elements an update should apply. */
|
|
112
|
+
arrayFilters?: Document[];
|
|
113
|
+
/** Specifies a collation. */
|
|
114
|
+
collation?: CollationOptions;
|
|
115
|
+
/** The index to use. If specified, then the query system will only consider plans using the hinted index. */
|
|
116
|
+
hint?: Hint;
|
|
117
|
+
/** When true, creates a new document if no document matches the query. */
|
|
118
|
+
upsert?: boolean;
|
|
119
|
+
/** Specifies the sort order for the documents matched by the filter. */
|
|
120
|
+
sort?: Sort;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** @public */
|
|
124
|
+
export interface ClientUpdateManyModel<TSchema> extends ClientWriteModel {
|
|
125
|
+
name: 'updateMany';
|
|
126
|
+
/**
|
|
127
|
+
* The filter used to determine if a document should be updated.
|
|
128
|
+
* For an updateMany operation, all matches are updated.
|
|
129
|
+
*/
|
|
130
|
+
filter: Filter<TSchema>;
|
|
131
|
+
/**
|
|
132
|
+
* The modifications to apply. The value can be either:
|
|
133
|
+
* UpdateFilter<Document> - A document that contains update operator expressions,
|
|
134
|
+
* Document[] - an aggregation pipeline.
|
|
135
|
+
*/
|
|
136
|
+
update: UpdateFilter<TSchema> | Document[];
|
|
137
|
+
/** A set of filters specifying to which array elements an update should apply. */
|
|
138
|
+
arrayFilters?: Document[];
|
|
139
|
+
/** Specifies a collation. */
|
|
140
|
+
collation?: CollationOptions;
|
|
141
|
+
/** The index to use. If specified, then the query system will only consider plans using the hinted index. */
|
|
142
|
+
hint?: Hint;
|
|
143
|
+
/** When true, creates a new document if no document matches the query. */
|
|
144
|
+
upsert?: boolean;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Used to represent any of the client bulk write models that can be passed as an array
|
|
149
|
+
* to MongoClient#bulkWrite.
|
|
150
|
+
* @public
|
|
151
|
+
*/
|
|
152
|
+
export type AnyClientBulkWriteModel<TSchema extends Document> =
|
|
153
|
+
| ClientInsertOneModel<TSchema>
|
|
154
|
+
| ClientReplaceOneModel<TSchema>
|
|
155
|
+
| ClientUpdateOneModel<TSchema>
|
|
156
|
+
| ClientUpdateManyModel<TSchema>
|
|
157
|
+
| ClientDeleteOneModel<TSchema>
|
|
158
|
+
| ClientDeleteManyModel<TSchema>;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* A mapping of namespace strings to collections schemas.
|
|
162
|
+
* @public
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* type MongoDBSchemas = {
|
|
167
|
+
* 'db.books': Book;
|
|
168
|
+
* 'db.authors': Author;
|
|
169
|
+
* }
|
|
170
|
+
*
|
|
171
|
+
* const model: ClientBulkWriteModel<MongoDBSchemas> = {
|
|
172
|
+
* namespace: 'db.books'
|
|
173
|
+
* name: 'insertOne',
|
|
174
|
+
* document: { title: 'Practical MongoDB Aggregations', authorName: 3 } // error `authorName` cannot be number
|
|
175
|
+
* };
|
|
176
|
+
* ```
|
|
177
|
+
*
|
|
178
|
+
* The type of the `namespace` field narrows other parts of the BulkWriteModel to use the correct schema for type assertions.
|
|
179
|
+
*
|
|
180
|
+
*/
|
|
181
|
+
export type ClientBulkWriteModel<
|
|
182
|
+
SchemaMap extends Record<string, Document> = Record<string, Document>
|
|
183
|
+
> = {
|
|
184
|
+
[Namespace in keyof SchemaMap]: AnyClientBulkWriteModel<SchemaMap[Namespace]> & {
|
|
185
|
+
namespace: Namespace;
|
|
186
|
+
};
|
|
187
|
+
}[keyof SchemaMap];
|
|
188
|
+
|
|
189
|
+
/** @public */
|
|
190
|
+
export interface ClientBulkWriteResult {
|
|
191
|
+
/**
|
|
192
|
+
* Whether the bulk write was acknowledged.
|
|
193
|
+
*/
|
|
194
|
+
readonly acknowledged: boolean;
|
|
195
|
+
/**
|
|
196
|
+
* The total number of documents inserted across all insert operations.
|
|
197
|
+
*/
|
|
198
|
+
readonly insertedCount: number;
|
|
199
|
+
/**
|
|
200
|
+
* The total number of documents upserted across all update operations.
|
|
201
|
+
*/
|
|
202
|
+
readonly upsertedCount: number;
|
|
203
|
+
/**
|
|
204
|
+
* The total number of documents matched across all update operations.
|
|
205
|
+
*/
|
|
206
|
+
readonly matchedCount: number;
|
|
207
|
+
/**
|
|
208
|
+
* The total number of documents modified across all update operations.
|
|
209
|
+
*/
|
|
210
|
+
readonly modifiedCount: number;
|
|
211
|
+
/**
|
|
212
|
+
* The total number of documents deleted across all delete operations.
|
|
213
|
+
*/
|
|
214
|
+
readonly deletedCount: number;
|
|
215
|
+
/**
|
|
216
|
+
* The results of each individual insert operation that was successfully performed.
|
|
217
|
+
*/
|
|
218
|
+
readonly insertResults?: ReadonlyMap<number, ClientInsertOneResult>;
|
|
219
|
+
/**
|
|
220
|
+
* The results of each individual update operation that was successfully performed.
|
|
221
|
+
*/
|
|
222
|
+
readonly updateResults?: ReadonlyMap<number, ClientUpdateResult>;
|
|
223
|
+
/**
|
|
224
|
+
* The results of each individual delete operation that was successfully performed.
|
|
225
|
+
*/
|
|
226
|
+
readonly deleteResults?: ReadonlyMap<number, ClientDeleteResult>;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** @public */
|
|
230
|
+
export interface ClientBulkWriteError {
|
|
231
|
+
code: number;
|
|
232
|
+
message: string;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** @public */
|
|
236
|
+
export interface ClientInsertOneResult {
|
|
237
|
+
/**
|
|
238
|
+
* The _id of the inserted document.
|
|
239
|
+
*/
|
|
240
|
+
insertedId: any;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** @public */
|
|
244
|
+
export interface ClientUpdateResult {
|
|
245
|
+
/**
|
|
246
|
+
* The number of documents that matched the filter.
|
|
247
|
+
*/
|
|
248
|
+
matchedCount: number;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* The number of documents that were modified.
|
|
252
|
+
*/
|
|
253
|
+
modifiedCount: number;
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* The _id field of the upserted document if an upsert occurred.
|
|
257
|
+
*
|
|
258
|
+
* It MUST be possible to discern between a BSON Null upserted ID value and this field being
|
|
259
|
+
* unset. If necessary, drivers MAY add a didUpsert boolean field to differentiate between
|
|
260
|
+
* these two cases.
|
|
261
|
+
*/
|
|
262
|
+
upsertedId?: any;
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Determines if the upsert did include an _id, which includes the case of the _id being null.
|
|
266
|
+
*/
|
|
267
|
+
didUpsert: boolean;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** @public */
|
|
271
|
+
export interface ClientDeleteResult {
|
|
272
|
+
/**
|
|
273
|
+
* The number of documents that were deleted.
|
|
274
|
+
*/
|
|
275
|
+
deletedCount: number;
|
|
276
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { type Document } from '../../bson';
|
|
2
|
+
import { CursorTimeoutContext, CursorTimeoutMode } from '../../cursor/abstract_cursor';
|
|
3
|
+
import { ClientBulkWriteCursor } from '../../cursor/client_bulk_write_cursor';
|
|
4
|
+
import {
|
|
5
|
+
MongoClientBulkWriteError,
|
|
6
|
+
MongoClientBulkWriteExecutionError,
|
|
7
|
+
MongoInvalidArgumentError,
|
|
8
|
+
MongoServerError
|
|
9
|
+
} from '../../error';
|
|
10
|
+
import { type MongoClient } from '../../mongo_client';
|
|
11
|
+
import { TimeoutContext } from '../../timeout';
|
|
12
|
+
import { resolveTimeoutOptions } from '../../utils';
|
|
13
|
+
import { WriteConcern } from '../../write_concern';
|
|
14
|
+
import { executeOperation } from '../execute_operation';
|
|
15
|
+
import { ClientBulkWriteOperation } from './client_bulk_write';
|
|
16
|
+
import { ClientBulkWriteCommandBuilder } from './command_builder';
|
|
17
|
+
import {
|
|
18
|
+
type AnyClientBulkWriteModel,
|
|
19
|
+
type ClientBulkWriteOptions,
|
|
20
|
+
type ClientBulkWriteResult
|
|
21
|
+
} from './common';
|
|
22
|
+
import { ClientBulkWriteResultsMerger } from './results_merger';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Responsible for executing a client bulk write.
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
export class ClientBulkWriteExecutor {
|
|
29
|
+
private readonly client: MongoClient;
|
|
30
|
+
private readonly options: ClientBulkWriteOptions;
|
|
31
|
+
private readonly operations: ReadonlyArray<AnyClientBulkWriteModel<Document>>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Instantiate the executor.
|
|
35
|
+
* @param client - The mongo client.
|
|
36
|
+
* @param operations - The user supplied bulk write models.
|
|
37
|
+
* @param options - The bulk write options.
|
|
38
|
+
*/
|
|
39
|
+
constructor(
|
|
40
|
+
client: MongoClient,
|
|
41
|
+
operations: ReadonlyArray<AnyClientBulkWriteModel<Document>>,
|
|
42
|
+
options?: ClientBulkWriteOptions
|
|
43
|
+
) {
|
|
44
|
+
if (operations.length === 0) {
|
|
45
|
+
throw new MongoClientBulkWriteExecutionError('No client bulk write models were provided.');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
this.client = client;
|
|
49
|
+
this.operations = operations;
|
|
50
|
+
this.options = {
|
|
51
|
+
ordered: true,
|
|
52
|
+
bypassDocumentValidation: false,
|
|
53
|
+
verboseResults: false,
|
|
54
|
+
...options
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// If no write concern was provided, we inherit one from the client.
|
|
58
|
+
if (!this.options.writeConcern) {
|
|
59
|
+
this.options.writeConcern = WriteConcern.fromOptions(this.client.s.options);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (this.options.writeConcern?.w === 0) {
|
|
63
|
+
if (this.options.verboseResults) {
|
|
64
|
+
throw new MongoInvalidArgumentError(
|
|
65
|
+
'Cannot request unacknowledged write concern and verbose results'
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (this.options.ordered) {
|
|
70
|
+
throw new MongoInvalidArgumentError(
|
|
71
|
+
'Cannot request unacknowledged write concern and ordered writes'
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Execute the client bulk write. Will split commands into batches and exhaust the cursors
|
|
79
|
+
* for each, then merge the results into one.
|
|
80
|
+
* @returns The result.
|
|
81
|
+
*/
|
|
82
|
+
async execute(): Promise<ClientBulkWriteResult> {
|
|
83
|
+
// The command builder will take the user provided models and potential split the batch
|
|
84
|
+
// into multiple commands due to size.
|
|
85
|
+
const pkFactory = this.client.s.options.pkFactory;
|
|
86
|
+
const commandBuilder = new ClientBulkWriteCommandBuilder(
|
|
87
|
+
this.operations,
|
|
88
|
+
this.options,
|
|
89
|
+
pkFactory
|
|
90
|
+
);
|
|
91
|
+
// Unacknowledged writes need to execute all batches and return { ok: 1}
|
|
92
|
+
const resolvedOptions = resolveTimeoutOptions(this.client, this.options);
|
|
93
|
+
const context = TimeoutContext.create(resolvedOptions);
|
|
94
|
+
|
|
95
|
+
if (this.options.writeConcern?.w === 0) {
|
|
96
|
+
while (commandBuilder.hasNextBatch()) {
|
|
97
|
+
const operation = new ClientBulkWriteOperation(commandBuilder, this.options);
|
|
98
|
+
await executeOperation(this.client, operation, context);
|
|
99
|
+
}
|
|
100
|
+
return ClientBulkWriteResultsMerger.unacknowledged();
|
|
101
|
+
} else {
|
|
102
|
+
const resultsMerger = new ClientBulkWriteResultsMerger(this.options);
|
|
103
|
+
// For each command will will create and exhaust a cursor for the results.
|
|
104
|
+
while (commandBuilder.hasNextBatch()) {
|
|
105
|
+
const cursorContext = new CursorTimeoutContext(context, Symbol());
|
|
106
|
+
const options = {
|
|
107
|
+
...this.options,
|
|
108
|
+
timeoutContext: cursorContext,
|
|
109
|
+
...(resolvedOptions.timeoutMS != null && { timeoutMode: CursorTimeoutMode.LIFETIME })
|
|
110
|
+
};
|
|
111
|
+
const cursor = new ClientBulkWriteCursor(this.client, commandBuilder, options);
|
|
112
|
+
try {
|
|
113
|
+
await resultsMerger.merge(cursor);
|
|
114
|
+
} catch (error) {
|
|
115
|
+
// Write concern errors are recorded in the writeConcernErrors field on MongoClientBulkWriteError.
|
|
116
|
+
// When a write concern error is encountered, it should not terminate execution of the bulk write
|
|
117
|
+
// for either ordered or unordered bulk writes. However, drivers MUST throw an exception at the end
|
|
118
|
+
// of execution if any write concern errors were observed.
|
|
119
|
+
if (error instanceof MongoServerError && !(error instanceof MongoClientBulkWriteError)) {
|
|
120
|
+
// Server side errors need to be wrapped inside a MongoClientBulkWriteError, where the root
|
|
121
|
+
// cause is the error property and a partial result is to be included.
|
|
122
|
+
const bulkWriteError = new MongoClientBulkWriteError({
|
|
123
|
+
message: 'Mongo client bulk write encountered an error during execution'
|
|
124
|
+
});
|
|
125
|
+
bulkWriteError.cause = error;
|
|
126
|
+
bulkWriteError.partialResult = resultsMerger.bulkWriteResult;
|
|
127
|
+
throw bulkWriteError;
|
|
128
|
+
} else {
|
|
129
|
+
// Client side errors are just thrown.
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// If we have write concern errors or unordered write errors at the end we throw.
|
|
136
|
+
if (resultsMerger.writeConcernErrors.length > 0 || resultsMerger.writeErrors.size > 0) {
|
|
137
|
+
const error = new MongoClientBulkWriteError({
|
|
138
|
+
message: 'Mongo client bulk write encountered errors during execution.'
|
|
139
|
+
});
|
|
140
|
+
error.writeConcernErrors = resultsMerger.writeConcernErrors;
|
|
141
|
+
error.writeErrors = resultsMerger.writeErrors;
|
|
142
|
+
error.partialResult = resultsMerger.bulkWriteResult;
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return resultsMerger.bulkWriteResult;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { MongoWriteConcernError } from '../..';
|
|
2
|
+
import { type Document } from '../../bson';
|
|
3
|
+
import { type ClientBulkWriteCursor } from '../../cursor/client_bulk_write_cursor';
|
|
4
|
+
import { MongoClientBulkWriteError } from '../../error';
|
|
5
|
+
import {
|
|
6
|
+
type ClientBulkWriteError,
|
|
7
|
+
type ClientBulkWriteOptions,
|
|
8
|
+
type ClientBulkWriteResult,
|
|
9
|
+
type ClientDeleteResult,
|
|
10
|
+
type ClientInsertOneResult,
|
|
11
|
+
type ClientUpdateResult
|
|
12
|
+
} from './common';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Unacknowledged bulk writes are always the same.
|
|
16
|
+
*/
|
|
17
|
+
const UNACKNOWLEDGED = {
|
|
18
|
+
acknowledged: false,
|
|
19
|
+
insertedCount: 0,
|
|
20
|
+
upsertedCount: 0,
|
|
21
|
+
matchedCount: 0,
|
|
22
|
+
modifiedCount: 0,
|
|
23
|
+
deletedCount: 0,
|
|
24
|
+
insertResults: undefined,
|
|
25
|
+
updateResults: undefined,
|
|
26
|
+
deleteResults: undefined
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
interface ClientBulkWriteResultAccumulation {
|
|
30
|
+
/**
|
|
31
|
+
* Whether the bulk write was acknowledged.
|
|
32
|
+
*/
|
|
33
|
+
acknowledged: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* The total number of documents inserted across all insert operations.
|
|
36
|
+
*/
|
|
37
|
+
insertedCount: number;
|
|
38
|
+
/**
|
|
39
|
+
* The total number of documents upserted across all update operations.
|
|
40
|
+
*/
|
|
41
|
+
upsertedCount: number;
|
|
42
|
+
/**
|
|
43
|
+
* The total number of documents matched across all update operations.
|
|
44
|
+
*/
|
|
45
|
+
matchedCount: number;
|
|
46
|
+
/**
|
|
47
|
+
* The total number of documents modified across all update operations.
|
|
48
|
+
*/
|
|
49
|
+
modifiedCount: number;
|
|
50
|
+
/**
|
|
51
|
+
* The total number of documents deleted across all delete operations.
|
|
52
|
+
*/
|
|
53
|
+
deletedCount: number;
|
|
54
|
+
/**
|
|
55
|
+
* The results of each individual insert operation that was successfully performed.
|
|
56
|
+
*/
|
|
57
|
+
insertResults?: Map<number, ClientInsertOneResult>;
|
|
58
|
+
/**
|
|
59
|
+
* The results of each individual update operation that was successfully performed.
|
|
60
|
+
*/
|
|
61
|
+
updateResults?: Map<number, ClientUpdateResult>;
|
|
62
|
+
/**
|
|
63
|
+
* The results of each individual delete operation that was successfully performed.
|
|
64
|
+
*/
|
|
65
|
+
deleteResults?: Map<number, ClientDeleteResult>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Merges client bulk write cursor responses together into a single result.
|
|
70
|
+
* @internal
|
|
71
|
+
*/
|
|
72
|
+
export class ClientBulkWriteResultsMerger {
|
|
73
|
+
private result: ClientBulkWriteResultAccumulation;
|
|
74
|
+
private options: ClientBulkWriteOptions;
|
|
75
|
+
private currentBatchOffset: number;
|
|
76
|
+
writeConcernErrors: Document[];
|
|
77
|
+
writeErrors: Map<number, ClientBulkWriteError>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @returns The standard unacknowledged bulk write result.
|
|
81
|
+
*/
|
|
82
|
+
static unacknowledged(): ClientBulkWriteResult {
|
|
83
|
+
return UNACKNOWLEDGED;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Instantiate the merger.
|
|
88
|
+
* @param options - The options.
|
|
89
|
+
*/
|
|
90
|
+
constructor(options: ClientBulkWriteOptions) {
|
|
91
|
+
this.options = options;
|
|
92
|
+
this.currentBatchOffset = 0;
|
|
93
|
+
this.writeConcernErrors = [];
|
|
94
|
+
this.writeErrors = new Map();
|
|
95
|
+
this.result = {
|
|
96
|
+
acknowledged: true,
|
|
97
|
+
insertedCount: 0,
|
|
98
|
+
upsertedCount: 0,
|
|
99
|
+
matchedCount: 0,
|
|
100
|
+
modifiedCount: 0,
|
|
101
|
+
deletedCount: 0,
|
|
102
|
+
insertResults: undefined,
|
|
103
|
+
updateResults: undefined,
|
|
104
|
+
deleteResults: undefined
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
if (options.verboseResults) {
|
|
108
|
+
this.result.insertResults = new Map<number, ClientInsertOneResult>();
|
|
109
|
+
this.result.updateResults = new Map<number, ClientUpdateResult>();
|
|
110
|
+
this.result.deleteResults = new Map<number, ClientDeleteResult>();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Get the bulk write result object.
|
|
116
|
+
*/
|
|
117
|
+
get bulkWriteResult(): ClientBulkWriteResult {
|
|
118
|
+
return {
|
|
119
|
+
acknowledged: this.result.acknowledged,
|
|
120
|
+
insertedCount: this.result.insertedCount,
|
|
121
|
+
upsertedCount: this.result.upsertedCount,
|
|
122
|
+
matchedCount: this.result.matchedCount,
|
|
123
|
+
modifiedCount: this.result.modifiedCount,
|
|
124
|
+
deletedCount: this.result.deletedCount,
|
|
125
|
+
insertResults: this.result.insertResults,
|
|
126
|
+
updateResults: this.result.updateResults,
|
|
127
|
+
deleteResults: this.result.deleteResults
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Merge the results in the cursor to the existing result.
|
|
133
|
+
* @param currentBatchOffset - The offset index to the original models.
|
|
134
|
+
* @param response - The cursor response.
|
|
135
|
+
* @param documents - The documents in the cursor.
|
|
136
|
+
* @returns The current result.
|
|
137
|
+
*/
|
|
138
|
+
async merge(cursor: ClientBulkWriteCursor): Promise<ClientBulkWriteResult> {
|
|
139
|
+
let writeConcernErrorResult;
|
|
140
|
+
try {
|
|
141
|
+
for await (const document of cursor) {
|
|
142
|
+
// Only add to maps if ok: 1
|
|
143
|
+
if (document.ok === 1) {
|
|
144
|
+
if (this.options.verboseResults) {
|
|
145
|
+
this.processDocument(cursor, document);
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
// If an individual write error is encountered during an ordered bulk write, drivers MUST
|
|
149
|
+
// record the error in writeErrors and immediately throw the exception. Otherwise, drivers
|
|
150
|
+
// MUST continue to iterate the results cursor and execute any further bulkWrite batches.
|
|
151
|
+
if (this.options.ordered) {
|
|
152
|
+
const error = new MongoClientBulkWriteError({
|
|
153
|
+
message: 'Mongo client ordered bulk write encountered a write error.'
|
|
154
|
+
});
|
|
155
|
+
error.writeErrors.set(document.idx + this.currentBatchOffset, {
|
|
156
|
+
code: document.code,
|
|
157
|
+
message: document.errmsg
|
|
158
|
+
});
|
|
159
|
+
error.partialResult = this.result;
|
|
160
|
+
throw error;
|
|
161
|
+
} else {
|
|
162
|
+
this.writeErrors.set(document.idx + this.currentBatchOffset, {
|
|
163
|
+
code: document.code,
|
|
164
|
+
message: document.errmsg
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
} catch (error) {
|
|
170
|
+
if (error instanceof MongoWriteConcernError) {
|
|
171
|
+
const result = error.result;
|
|
172
|
+
writeConcernErrorResult = {
|
|
173
|
+
insertedCount: result.nInserted,
|
|
174
|
+
upsertedCount: result.nUpserted,
|
|
175
|
+
matchedCount: result.nMatched,
|
|
176
|
+
modifiedCount: result.nModified,
|
|
177
|
+
deletedCount: result.nDeleted,
|
|
178
|
+
writeConcernError: result.writeConcernError
|
|
179
|
+
};
|
|
180
|
+
if (this.options.verboseResults && result.cursor.firstBatch) {
|
|
181
|
+
for (const document of result.cursor.firstBatch) {
|
|
182
|
+
if (document.ok === 1) {
|
|
183
|
+
this.processDocument(cursor, document);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
} else {
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
} finally {
|
|
191
|
+
// Update the counts from the cursor response.
|
|
192
|
+
if (cursor.response) {
|
|
193
|
+
const response = cursor.response;
|
|
194
|
+
this.incrementCounts(response);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Increment the batch offset.
|
|
198
|
+
this.currentBatchOffset += cursor.operations.length;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// If we have write concern errors ensure they are added.
|
|
202
|
+
if (writeConcernErrorResult) {
|
|
203
|
+
const writeConcernError = writeConcernErrorResult.writeConcernError as Document;
|
|
204
|
+
this.incrementCounts(writeConcernErrorResult);
|
|
205
|
+
this.writeConcernErrors.push({
|
|
206
|
+
code: writeConcernError.code,
|
|
207
|
+
message: writeConcernError.errmsg
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return this.result;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Process an individual document in the results.
|
|
216
|
+
* @param cursor - The cursor.
|
|
217
|
+
* @param document - The document to process.
|
|
218
|
+
*/
|
|
219
|
+
private processDocument(cursor: ClientBulkWriteCursor, document: Document) {
|
|
220
|
+
// Get the corresponding operation from the command.
|
|
221
|
+
const operation = cursor.operations[document.idx];
|
|
222
|
+
// Handle insert results.
|
|
223
|
+
if ('insert' in operation) {
|
|
224
|
+
this.result.insertResults?.set(document.idx + this.currentBatchOffset, {
|
|
225
|
+
insertedId: operation.document._id
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
// Handle update results.
|
|
229
|
+
if ('update' in operation) {
|
|
230
|
+
const result: ClientUpdateResult = {
|
|
231
|
+
matchedCount: document.n,
|
|
232
|
+
modifiedCount: document.nModified ?? 0,
|
|
233
|
+
// Check if the bulk did actually upsert.
|
|
234
|
+
didUpsert: document.upserted != null
|
|
235
|
+
};
|
|
236
|
+
if (document.upserted) {
|
|
237
|
+
result.upsertedId = document.upserted._id;
|
|
238
|
+
}
|
|
239
|
+
this.result.updateResults?.set(document.idx + this.currentBatchOffset, result);
|
|
240
|
+
}
|
|
241
|
+
// Handle delete results.
|
|
242
|
+
if ('delete' in operation) {
|
|
243
|
+
this.result.deleteResults?.set(document.idx + this.currentBatchOffset, {
|
|
244
|
+
deletedCount: document.n
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Increment the result counts.
|
|
251
|
+
* @param document - The document with the results.
|
|
252
|
+
*/
|
|
253
|
+
private incrementCounts(document: Document) {
|
|
254
|
+
this.result.insertedCount += document.insertedCount;
|
|
255
|
+
this.result.upsertedCount += document.upsertedCount;
|
|
256
|
+
this.result.matchedCount += document.matchedCount;
|
|
257
|
+
this.result.modifiedCount += document.modifiedCount;
|
|
258
|
+
this.result.deletedCount += document.deletedCount;
|
|
259
|
+
}
|
|
260
|
+
}
|