@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,153 @@
|
|
|
1
|
+
import type { Document } from '../bson';
|
|
2
|
+
import { CursorResponse, ExplainedCursorResponse } from '../cmap/wire_protocol/responses';
|
|
3
|
+
import { type CursorTimeoutMode } from '../cursor/abstract_cursor';
|
|
4
|
+
import { MongoInvalidArgumentError } from '../error';
|
|
5
|
+
import { type ExplainOptions } from '../explain';
|
|
6
|
+
import { type MongoDBNamespace } from '../utils';
|
|
7
|
+
import { WriteConcern } from '../write_concern';
|
|
8
|
+
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
|
|
9
|
+
import { Aspect, defineAspects, type Hint } from './operation';
|
|
10
|
+
|
|
11
|
+
/** @internal */
|
|
12
|
+
export const DB_AGGREGATE_COLLECTION = 1 as const;
|
|
13
|
+
|
|
14
|
+
/** @public */
|
|
15
|
+
export interface AggregateOptions extends Omit<CommandOperationOptions, 'explain'> {
|
|
16
|
+
/** allowDiskUse lets the server know if it can use disk to store temporary results for the aggregation (requires mongodb 2.6 \>). */
|
|
17
|
+
allowDiskUse?: boolean;
|
|
18
|
+
/** The number of documents to return per batch. See [aggregation documentation](https://www.mongodb.com/docs/manual/reference/command/aggregate). */
|
|
19
|
+
batchSize?: number;
|
|
20
|
+
/** Allow driver to bypass schema validation. */
|
|
21
|
+
bypassDocumentValidation?: boolean;
|
|
22
|
+
/** Return the query as cursor, on 2.6 \> it returns as a real cursor on pre 2.6 it returns as an emulated cursor. */
|
|
23
|
+
cursor?: Document;
|
|
24
|
+
/**
|
|
25
|
+
* Specifies a cumulative time limit in milliseconds for processing operations on the cursor. MongoDB interrupts the operation at the earliest following interrupt point.
|
|
26
|
+
*/
|
|
27
|
+
maxTimeMS?: number;
|
|
28
|
+
/** The maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query. */
|
|
29
|
+
maxAwaitTimeMS?: number;
|
|
30
|
+
/** Specify collation. */
|
|
31
|
+
collation?: CollationOptions;
|
|
32
|
+
/** Add an index selection hint to an aggregation command */
|
|
33
|
+
hint?: Hint;
|
|
34
|
+
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
|
|
35
|
+
let?: Document;
|
|
36
|
+
|
|
37
|
+
out?: string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Specifies the verbosity mode for the explain output.
|
|
41
|
+
* @deprecated This API is deprecated in favor of `collection.aggregate().explain()`
|
|
42
|
+
* or `db.aggregate().explain()`.
|
|
43
|
+
*/
|
|
44
|
+
explain?: ExplainOptions['explain'];
|
|
45
|
+
/** @internal */
|
|
46
|
+
timeoutMode?: CursorTimeoutMode;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** @internal */
|
|
50
|
+
export class AggregateOperation extends CommandOperation<CursorResponse> {
|
|
51
|
+
override SERVER_COMMAND_RESPONSE_TYPE = CursorResponse;
|
|
52
|
+
override options: AggregateOptions;
|
|
53
|
+
target: string | typeof DB_AGGREGATE_COLLECTION;
|
|
54
|
+
pipeline: Document[];
|
|
55
|
+
hasWriteStage: boolean;
|
|
56
|
+
|
|
57
|
+
constructor(ns: MongoDBNamespace, pipeline: Document[], options?: AggregateOptions) {
|
|
58
|
+
super(undefined, { ...options, dbName: ns.db });
|
|
59
|
+
|
|
60
|
+
this.options = { ...options };
|
|
61
|
+
|
|
62
|
+
// Covers when ns.collection is null, undefined or the empty string, use DB_AGGREGATE_COLLECTION
|
|
63
|
+
this.target = ns.collection || DB_AGGREGATE_COLLECTION;
|
|
64
|
+
|
|
65
|
+
this.pipeline = pipeline;
|
|
66
|
+
|
|
67
|
+
// determine if we have a write stage, override read preference if so
|
|
68
|
+
this.hasWriteStage = false;
|
|
69
|
+
if (typeof options?.out === 'string') {
|
|
70
|
+
this.pipeline = this.pipeline.concat({ $out: options.out });
|
|
71
|
+
this.hasWriteStage = true;
|
|
72
|
+
} else if (pipeline.length > 0) {
|
|
73
|
+
const finalStage = pipeline[pipeline.length - 1];
|
|
74
|
+
if (finalStage.$out || finalStage.$merge) {
|
|
75
|
+
this.hasWriteStage = true;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (!this.hasWriteStage) {
|
|
80
|
+
delete this.options.writeConcern;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (options?.cursor != null && typeof options.cursor !== 'object') {
|
|
84
|
+
throw new MongoInvalidArgumentError('Cursor options must be an object');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
this.SERVER_COMMAND_RESPONSE_TYPE = this.explain ? ExplainedCursorResponse : CursorResponse;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
override get commandName() {
|
|
91
|
+
return 'aggregate' as const;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
override get canRetryRead(): boolean {
|
|
95
|
+
return !this.hasWriteStage;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
addToPipeline(stage: Document): void {
|
|
99
|
+
this.pipeline.push(stage);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
override buildCommandDocument(): Document {
|
|
103
|
+
const options = this.options;
|
|
104
|
+
const command: Document = { aggregate: this.target, pipeline: this.pipeline };
|
|
105
|
+
|
|
106
|
+
if (this.hasWriteStage && this.writeConcern) {
|
|
107
|
+
WriteConcern.apply(command, this.writeConcern);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (options.bypassDocumentValidation === true) {
|
|
111
|
+
command.bypassDocumentValidation = options.bypassDocumentValidation;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (typeof options.allowDiskUse === 'boolean') {
|
|
115
|
+
command.allowDiskUse = options.allowDiskUse;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (options.hint) {
|
|
119
|
+
command.hint = options.hint;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (options.let) {
|
|
123
|
+
command.let = options.let;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// we check for undefined specifically here to allow falsy values
|
|
127
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
128
|
+
if (options.comment !== undefined) {
|
|
129
|
+
command.comment = options.comment;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
command.cursor = options.cursor || {};
|
|
133
|
+
if (options.batchSize && !this.hasWriteStage) {
|
|
134
|
+
command.cursor.batchSize = options.batchSize;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return command;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
override handleOk(
|
|
141
|
+
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
|
|
142
|
+
): CursorResponse {
|
|
143
|
+
return response;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
defineAspects(AggregateOperation, [
|
|
148
|
+
Aspect.READ_OPERATION,
|
|
149
|
+
Aspect.RETRYABLE,
|
|
150
|
+
Aspect.EXPLAINABLE,
|
|
151
|
+
Aspect.CURSOR_CREATING,
|
|
152
|
+
Aspect.SUPPORTS_RAW_DATA
|
|
153
|
+
]);
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { type Connection } from '../../cmap/connection';
|
|
2
|
+
import { ClientBulkWriteCursorResponse } from '../../cmap/wire_protocol/responses';
|
|
3
|
+
import type { ClientSession } from '../../sessions';
|
|
4
|
+
import { MongoDBNamespace } from '../../utils';
|
|
5
|
+
import { CommandOperation } from '../command';
|
|
6
|
+
import { Aspect, defineAspects } from '../operation';
|
|
7
|
+
import { type ClientBulkWriteCommand, type ClientBulkWriteCommandBuilder } from './command_builder';
|
|
8
|
+
import { type ClientBulkWriteOptions } from './common';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Executes a single client bulk write operation within a potential batch.
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export class ClientBulkWriteOperation extends CommandOperation<ClientBulkWriteCursorResponse> {
|
|
15
|
+
override SERVER_COMMAND_RESPONSE_TYPE = ClientBulkWriteCursorResponse;
|
|
16
|
+
|
|
17
|
+
commandBuilder: ClientBulkWriteCommandBuilder;
|
|
18
|
+
override options: ClientBulkWriteOptions;
|
|
19
|
+
|
|
20
|
+
override get commandName() {
|
|
21
|
+
return 'bulkWrite' as const;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
constructor(commandBuilder: ClientBulkWriteCommandBuilder, options: ClientBulkWriteOptions) {
|
|
25
|
+
super(undefined, options);
|
|
26
|
+
this.commandBuilder = commandBuilder;
|
|
27
|
+
this.options = options;
|
|
28
|
+
this.ns = new MongoDBNamespace('admin', '$cmd');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
override resetBatch(): boolean {
|
|
32
|
+
return this.commandBuilder.resetBatch();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
override get canRetryWrite(): boolean {
|
|
36
|
+
return this.commandBuilder.isBatchRetryable;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
override handleOk(
|
|
40
|
+
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
|
|
41
|
+
): ClientBulkWriteCursorResponse {
|
|
42
|
+
return response;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
override buildCommandDocument(
|
|
46
|
+
connection: Connection,
|
|
47
|
+
_session?: ClientSession
|
|
48
|
+
): ClientBulkWriteCommand {
|
|
49
|
+
const command = this.commandBuilder.buildBatch(
|
|
50
|
+
connection.description.maxMessageSizeBytes,
|
|
51
|
+
connection.description.maxWriteBatchSize,
|
|
52
|
+
connection.description.maxBsonObjectSize
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
// Check _after_ the batch is built if we cannot retry it and override the option.
|
|
56
|
+
if (!this.canRetryWrite) {
|
|
57
|
+
this.options.willRetryWrite = false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return command;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Skipping the collation as it goes on the individual ops.
|
|
65
|
+
defineAspects(ClientBulkWriteOperation, [
|
|
66
|
+
Aspect.WRITE_OPERATION,
|
|
67
|
+
Aspect.SKIP_COLLATION,
|
|
68
|
+
Aspect.CURSOR_CREATING,
|
|
69
|
+
Aspect.RETRYABLE,
|
|
70
|
+
Aspect.COMMAND_BATCHING,
|
|
71
|
+
Aspect.SUPPORTS_RAW_DATA
|
|
72
|
+
]);
|
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import { BSON, type BSONSerializeOptions, type Document } from '../../bson';
|
|
2
|
+
import { DocumentSequence } from '../../cmap/commands';
|
|
3
|
+
import { MongoAPIError, MongoInvalidArgumentError } from '../../error';
|
|
4
|
+
import { type PkFactory } from '../../mongo_client';
|
|
5
|
+
import type { Filter, OptionalId, UpdateFilter, WithoutId } from '../../mongo_types';
|
|
6
|
+
import { formatSort, type SortForCmd } from '../../sort';
|
|
7
|
+
import { DEFAULT_PK_FACTORY, hasAtomicOperators } from '../../utils';
|
|
8
|
+
import { type CollationOptions } from '../command';
|
|
9
|
+
import { type Hint } from '../operation';
|
|
10
|
+
import type {
|
|
11
|
+
AnyClientBulkWriteModel,
|
|
12
|
+
ClientBulkWriteOptions,
|
|
13
|
+
ClientDeleteManyModel,
|
|
14
|
+
ClientDeleteOneModel,
|
|
15
|
+
ClientInsertOneModel,
|
|
16
|
+
ClientReplaceOneModel,
|
|
17
|
+
ClientUpdateManyModel,
|
|
18
|
+
ClientUpdateOneModel
|
|
19
|
+
} from './common';
|
|
20
|
+
|
|
21
|
+
/** @internal */
|
|
22
|
+
export interface ClientBulkWriteCommand {
|
|
23
|
+
bulkWrite: 1;
|
|
24
|
+
errorsOnly: boolean;
|
|
25
|
+
ordered: boolean;
|
|
26
|
+
ops: DocumentSequence;
|
|
27
|
+
nsInfo: DocumentSequence;
|
|
28
|
+
bypassDocumentValidation?: boolean;
|
|
29
|
+
let?: Document;
|
|
30
|
+
comment?: any;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The bytes overhead for the extra fields added post command generation.
|
|
35
|
+
*/
|
|
36
|
+
const MESSAGE_OVERHEAD_BYTES = 1000;
|
|
37
|
+
|
|
38
|
+
/** @internal */
|
|
39
|
+
export class ClientBulkWriteCommandBuilder {
|
|
40
|
+
models: ReadonlyArray<AnyClientBulkWriteModel<Document>>;
|
|
41
|
+
options: ClientBulkWriteOptions;
|
|
42
|
+
pkFactory: PkFactory;
|
|
43
|
+
/** The current index in the models array that is being processed. */
|
|
44
|
+
currentModelIndex: number;
|
|
45
|
+
/** The model index that the builder was on when it finished the previous batch. Used for resets when retrying. */
|
|
46
|
+
previousModelIndex: number;
|
|
47
|
+
/** The last array of operations that were created. Used by the results merger for indexing results. */
|
|
48
|
+
lastOperations: Document[];
|
|
49
|
+
/** Returns true if the current batch being created has no multi-updates. */
|
|
50
|
+
isBatchRetryable: boolean;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Create the command builder.
|
|
54
|
+
* @param models - The client write models.
|
|
55
|
+
*/
|
|
56
|
+
constructor(
|
|
57
|
+
models: ReadonlyArray<AnyClientBulkWriteModel<Document>>,
|
|
58
|
+
options: ClientBulkWriteOptions,
|
|
59
|
+
pkFactory?: PkFactory
|
|
60
|
+
) {
|
|
61
|
+
this.models = models;
|
|
62
|
+
this.options = options;
|
|
63
|
+
this.pkFactory = pkFactory ?? DEFAULT_PK_FACTORY;
|
|
64
|
+
this.currentModelIndex = 0;
|
|
65
|
+
this.previousModelIndex = 0;
|
|
66
|
+
this.lastOperations = [];
|
|
67
|
+
this.isBatchRetryable = true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Gets the errorsOnly value for the command, which is the inverse of the
|
|
72
|
+
* user provided verboseResults option. Defaults to true.
|
|
73
|
+
*/
|
|
74
|
+
get errorsOnly(): boolean {
|
|
75
|
+
if ('verboseResults' in this.options) {
|
|
76
|
+
return !this.options.verboseResults;
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Determines if there is another batch to process.
|
|
83
|
+
* @returns True if not all batches have been built.
|
|
84
|
+
*/
|
|
85
|
+
hasNextBatch(): boolean {
|
|
86
|
+
return this.currentModelIndex < this.models.length;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* When we need to retry a command we need to set the current
|
|
91
|
+
* model index back to its previous value.
|
|
92
|
+
*/
|
|
93
|
+
resetBatch(): boolean {
|
|
94
|
+
this.currentModelIndex = this.previousModelIndex;
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Build a single batch of a client bulk write command.
|
|
100
|
+
* @param maxMessageSizeBytes - The max message size in bytes.
|
|
101
|
+
* @param maxWriteBatchSize - The max write batch size.
|
|
102
|
+
* @returns The client bulk write command.
|
|
103
|
+
*/
|
|
104
|
+
buildBatch(
|
|
105
|
+
maxMessageSizeBytes: number,
|
|
106
|
+
maxWriteBatchSize: number,
|
|
107
|
+
maxBsonObjectSize: number
|
|
108
|
+
): ClientBulkWriteCommand {
|
|
109
|
+
// We start by assuming the batch has no multi-updates, so it is retryable
|
|
110
|
+
// until we find them.
|
|
111
|
+
this.isBatchRetryable = true;
|
|
112
|
+
let commandLength = 0;
|
|
113
|
+
let currentNamespaceIndex = 0;
|
|
114
|
+
const command: ClientBulkWriteCommand = this.baseCommand();
|
|
115
|
+
const namespaces = new Map<string, number>();
|
|
116
|
+
// In the case of retries we need to mark where we started this batch.
|
|
117
|
+
this.previousModelIndex = this.currentModelIndex;
|
|
118
|
+
|
|
119
|
+
while (this.currentModelIndex < this.models.length) {
|
|
120
|
+
const model = this.models[this.currentModelIndex];
|
|
121
|
+
const ns = model.namespace;
|
|
122
|
+
const nsIndex = namespaces.get(ns);
|
|
123
|
+
|
|
124
|
+
// Multi updates are not retryable.
|
|
125
|
+
if (model.name === 'deleteMany' || model.name === 'updateMany') {
|
|
126
|
+
this.isBatchRetryable = false;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (nsIndex != null) {
|
|
130
|
+
// Build the operation and serialize it to get the bytes buffer.
|
|
131
|
+
const operation = buildOperation(model, nsIndex, this.pkFactory, this.options);
|
|
132
|
+
let operationBuffer;
|
|
133
|
+
try {
|
|
134
|
+
operationBuffer = BSON.serialize(operation);
|
|
135
|
+
} catch (cause) {
|
|
136
|
+
throw new MongoInvalidArgumentError(`Could not serialize operation to BSON`, { cause });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
validateBufferSize('ops', operationBuffer, maxBsonObjectSize);
|
|
140
|
+
|
|
141
|
+
// Check if the operation buffer can fit in the command. If it can,
|
|
142
|
+
// then add the operation to the document sequence and increment the
|
|
143
|
+
// current length as long as the ops don't exceed the maxWriteBatchSize.
|
|
144
|
+
if (
|
|
145
|
+
commandLength + operationBuffer.length < maxMessageSizeBytes &&
|
|
146
|
+
command.ops.documents.length < maxWriteBatchSize
|
|
147
|
+
) {
|
|
148
|
+
// Pushing to the ops document sequence returns the total byte length of the document sequence.
|
|
149
|
+
commandLength = MESSAGE_OVERHEAD_BYTES + command.ops.push(operation, operationBuffer);
|
|
150
|
+
// Increment the builder's current model index.
|
|
151
|
+
this.currentModelIndex++;
|
|
152
|
+
} else {
|
|
153
|
+
// The operation cannot fit in the current command and will need to
|
|
154
|
+
// go in the next batch. Exit the loop.
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
// The namespace is not already in the nsInfo so we will set it in the map, and
|
|
159
|
+
// construct our nsInfo and ops documents and buffers.
|
|
160
|
+
namespaces.set(ns, currentNamespaceIndex);
|
|
161
|
+
const nsInfo = { ns: ns };
|
|
162
|
+
const operation = buildOperation(
|
|
163
|
+
model,
|
|
164
|
+
currentNamespaceIndex,
|
|
165
|
+
this.pkFactory,
|
|
166
|
+
this.options
|
|
167
|
+
);
|
|
168
|
+
let nsInfoBuffer;
|
|
169
|
+
let operationBuffer;
|
|
170
|
+
try {
|
|
171
|
+
nsInfoBuffer = BSON.serialize(nsInfo);
|
|
172
|
+
operationBuffer = BSON.serialize(operation);
|
|
173
|
+
} catch (cause) {
|
|
174
|
+
throw new MongoInvalidArgumentError(`Could not serialize ns info to BSON`, { cause });
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
validateBufferSize('nsInfo', nsInfoBuffer, maxBsonObjectSize);
|
|
178
|
+
validateBufferSize('ops', operationBuffer, maxBsonObjectSize);
|
|
179
|
+
|
|
180
|
+
// Check if the operation and nsInfo buffers can fit in the command. If they
|
|
181
|
+
// can, then add the operation and nsInfo to their respective document
|
|
182
|
+
// sequences and increment the current length as long as the ops don't exceed
|
|
183
|
+
// the maxWriteBatchSize.
|
|
184
|
+
if (
|
|
185
|
+
commandLength + nsInfoBuffer.length + operationBuffer.length < maxMessageSizeBytes &&
|
|
186
|
+
command.ops.documents.length < maxWriteBatchSize
|
|
187
|
+
) {
|
|
188
|
+
// Pushing to the ops document sequence returns the total byte length of the document sequence.
|
|
189
|
+
commandLength =
|
|
190
|
+
MESSAGE_OVERHEAD_BYTES +
|
|
191
|
+
command.nsInfo.push(nsInfo, nsInfoBuffer) +
|
|
192
|
+
command.ops.push(operation, operationBuffer);
|
|
193
|
+
// We've added a new namespace, increment the namespace index.
|
|
194
|
+
currentNamespaceIndex++;
|
|
195
|
+
// Increment the builder's current model index.
|
|
196
|
+
this.currentModelIndex++;
|
|
197
|
+
} else {
|
|
198
|
+
// The operation cannot fit in the current command and will need to
|
|
199
|
+
// go in the next batch. Exit the loop.
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// Set the last operations and return the command.
|
|
205
|
+
this.lastOperations = command.ops.documents;
|
|
206
|
+
return command;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private baseCommand(): ClientBulkWriteCommand {
|
|
210
|
+
const command: ClientBulkWriteCommand = {
|
|
211
|
+
bulkWrite: 1,
|
|
212
|
+
errorsOnly: this.errorsOnly,
|
|
213
|
+
ordered: this.options.ordered ?? true,
|
|
214
|
+
ops: new DocumentSequence('ops'),
|
|
215
|
+
nsInfo: new DocumentSequence('nsInfo')
|
|
216
|
+
};
|
|
217
|
+
// Add bypassDocumentValidation if it was present in the options.
|
|
218
|
+
if (this.options.bypassDocumentValidation != null) {
|
|
219
|
+
command.bypassDocumentValidation = this.options.bypassDocumentValidation;
|
|
220
|
+
}
|
|
221
|
+
// Add let if it was present in the options.
|
|
222
|
+
if (this.options.let) {
|
|
223
|
+
command.let = this.options.let;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// we check for undefined specifically here to allow falsy values
|
|
227
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
228
|
+
if (this.options.comment !== undefined) {
|
|
229
|
+
command.comment = this.options.comment;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return command;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function validateBufferSize(name: string, buffer: Uint8Array, maxBsonObjectSize: number) {
|
|
237
|
+
if (buffer.length > maxBsonObjectSize) {
|
|
238
|
+
throw new MongoInvalidArgumentError(
|
|
239
|
+
`Client bulk write operation ${name} of length ${buffer.length} exceeds the max bson object size of ${maxBsonObjectSize}`
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** @internal */
|
|
245
|
+
interface ClientInsertOperation {
|
|
246
|
+
insert: number;
|
|
247
|
+
document: OptionalId<Document>;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Build the insert one operation.
|
|
252
|
+
* @param model - The insert one model.
|
|
253
|
+
* @param index - The namespace index.
|
|
254
|
+
* @returns the operation.
|
|
255
|
+
*/
|
|
256
|
+
export const buildInsertOneOperation = (
|
|
257
|
+
model: ClientInsertOneModel<Document>,
|
|
258
|
+
index: number,
|
|
259
|
+
pkFactory: PkFactory
|
|
260
|
+
): ClientInsertOperation => {
|
|
261
|
+
const document: ClientInsertOperation = {
|
|
262
|
+
insert: index,
|
|
263
|
+
document: model.document
|
|
264
|
+
};
|
|
265
|
+
document.document._id = model.document._id ?? pkFactory.createPk();
|
|
266
|
+
return document;
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
/** @internal */
|
|
270
|
+
export interface ClientDeleteOperation {
|
|
271
|
+
delete: number;
|
|
272
|
+
multi: boolean;
|
|
273
|
+
filter: Filter<Document>;
|
|
274
|
+
hint?: Hint;
|
|
275
|
+
collation?: CollationOptions;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Build the delete one operation.
|
|
280
|
+
* @param model - The insert many model.
|
|
281
|
+
* @param index - The namespace index.
|
|
282
|
+
* @returns the operation.
|
|
283
|
+
*/
|
|
284
|
+
export const buildDeleteOneOperation = (
|
|
285
|
+
model: ClientDeleteOneModel<Document>,
|
|
286
|
+
index: number
|
|
287
|
+
): Document => {
|
|
288
|
+
return createDeleteOperation(model, index, false);
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Build the delete many operation.
|
|
293
|
+
* @param model - The delete many model.
|
|
294
|
+
* @param index - The namespace index.
|
|
295
|
+
* @returns the operation.
|
|
296
|
+
*/
|
|
297
|
+
export const buildDeleteManyOperation = (
|
|
298
|
+
model: ClientDeleteManyModel<Document>,
|
|
299
|
+
index: number
|
|
300
|
+
): Document => {
|
|
301
|
+
return createDeleteOperation(model, index, true);
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Creates a delete operation based on the parameters.
|
|
306
|
+
*/
|
|
307
|
+
function createDeleteOperation(
|
|
308
|
+
model: ClientDeleteOneModel<Document> | ClientDeleteManyModel<Document>,
|
|
309
|
+
index: number,
|
|
310
|
+
multi: boolean
|
|
311
|
+
): ClientDeleteOperation {
|
|
312
|
+
const document: ClientDeleteOperation = {
|
|
313
|
+
delete: index,
|
|
314
|
+
multi: multi,
|
|
315
|
+
filter: model.filter
|
|
316
|
+
};
|
|
317
|
+
if (model.hint) {
|
|
318
|
+
document.hint = model.hint;
|
|
319
|
+
}
|
|
320
|
+
if (model.collation) {
|
|
321
|
+
document.collation = model.collation;
|
|
322
|
+
}
|
|
323
|
+
return document;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** @internal */
|
|
327
|
+
export interface ClientUpdateOperation {
|
|
328
|
+
update: number;
|
|
329
|
+
multi: boolean;
|
|
330
|
+
filter: Filter<Document>;
|
|
331
|
+
updateMods: UpdateFilter<Document> | Document[];
|
|
332
|
+
hint?: Hint;
|
|
333
|
+
upsert?: boolean;
|
|
334
|
+
arrayFilters?: Document[];
|
|
335
|
+
collation?: CollationOptions;
|
|
336
|
+
sort?: SortForCmd;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Build the update one operation.
|
|
341
|
+
* @param model - The update one model.
|
|
342
|
+
* @param index - The namespace index.
|
|
343
|
+
* @returns the operation.
|
|
344
|
+
*/
|
|
345
|
+
export const buildUpdateOneOperation = (
|
|
346
|
+
model: ClientUpdateOneModel<Document>,
|
|
347
|
+
index: number,
|
|
348
|
+
options: BSONSerializeOptions
|
|
349
|
+
): ClientUpdateOperation => {
|
|
350
|
+
return createUpdateOperation(model, index, false, options);
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Build the update many operation.
|
|
355
|
+
* @param model - The update many model.
|
|
356
|
+
* @param index - The namespace index.
|
|
357
|
+
* @returns the operation.
|
|
358
|
+
*/
|
|
359
|
+
export const buildUpdateManyOperation = (
|
|
360
|
+
model: ClientUpdateManyModel<Document>,
|
|
361
|
+
index: number,
|
|
362
|
+
options: BSONSerializeOptions
|
|
363
|
+
): ClientUpdateOperation => {
|
|
364
|
+
return createUpdateOperation(model, index, true, options);
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Validate the update document.
|
|
369
|
+
* @param update - The update document.
|
|
370
|
+
*/
|
|
371
|
+
function validateUpdate(update: Document, options: BSONSerializeOptions) {
|
|
372
|
+
if (!hasAtomicOperators(update, options)) {
|
|
373
|
+
throw new MongoAPIError(
|
|
374
|
+
'Client bulk write update models must only contain atomic modifiers (start with $) and must not be empty.'
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Creates a delete operation based on the parameters.
|
|
381
|
+
*/
|
|
382
|
+
function createUpdateOperation(
|
|
383
|
+
model: ClientUpdateOneModel<Document> | ClientUpdateManyModel<Document>,
|
|
384
|
+
index: number,
|
|
385
|
+
multi: boolean,
|
|
386
|
+
options: BSONSerializeOptions
|
|
387
|
+
): ClientUpdateOperation {
|
|
388
|
+
// Update documents provided in UpdateOne and UpdateMany write models are
|
|
389
|
+
// required only to contain atomic modifiers (i.e. keys that start with "$").
|
|
390
|
+
// Drivers MUST throw an error if an update document is empty or if the
|
|
391
|
+
// document's first key does not start with "$".
|
|
392
|
+
validateUpdate(model.update, options);
|
|
393
|
+
const document: ClientUpdateOperation = {
|
|
394
|
+
update: index,
|
|
395
|
+
multi: multi,
|
|
396
|
+
filter: model.filter,
|
|
397
|
+
updateMods: model.update
|
|
398
|
+
};
|
|
399
|
+
if (model.hint) {
|
|
400
|
+
document.hint = model.hint;
|
|
401
|
+
}
|
|
402
|
+
if (model.upsert) {
|
|
403
|
+
document.upsert = model.upsert;
|
|
404
|
+
}
|
|
405
|
+
if (model.arrayFilters) {
|
|
406
|
+
document.arrayFilters = model.arrayFilters;
|
|
407
|
+
}
|
|
408
|
+
if (model.collation) {
|
|
409
|
+
document.collation = model.collation;
|
|
410
|
+
}
|
|
411
|
+
if (!multi && 'sort' in model && model.sort != null) {
|
|
412
|
+
document.sort = formatSort(model.sort);
|
|
413
|
+
}
|
|
414
|
+
return document;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** @internal */
|
|
418
|
+
export interface ClientReplaceOneOperation {
|
|
419
|
+
update: number;
|
|
420
|
+
multi: boolean;
|
|
421
|
+
filter: Filter<Document>;
|
|
422
|
+
updateMods: WithoutId<Document>;
|
|
423
|
+
hint?: Hint;
|
|
424
|
+
upsert?: boolean;
|
|
425
|
+
collation?: CollationOptions;
|
|
426
|
+
sort?: SortForCmd;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Build the replace one operation.
|
|
431
|
+
* @param model - The replace one model.
|
|
432
|
+
* @param index - The namespace index.
|
|
433
|
+
* @returns the operation.
|
|
434
|
+
*/
|
|
435
|
+
export const buildReplaceOneOperation = (
|
|
436
|
+
model: ClientReplaceOneModel<Document>,
|
|
437
|
+
index: number
|
|
438
|
+
): ClientReplaceOneOperation => {
|
|
439
|
+
if (hasAtomicOperators(model.replacement)) {
|
|
440
|
+
throw new MongoAPIError(
|
|
441
|
+
'Client bulk write replace models must not contain atomic modifiers (start with $) and must not be empty.'
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const document: ClientReplaceOneOperation = {
|
|
446
|
+
update: index,
|
|
447
|
+
multi: false,
|
|
448
|
+
filter: model.filter,
|
|
449
|
+
updateMods: model.replacement
|
|
450
|
+
};
|
|
451
|
+
if (model.hint) {
|
|
452
|
+
document.hint = model.hint;
|
|
453
|
+
}
|
|
454
|
+
if (model.upsert) {
|
|
455
|
+
document.upsert = model.upsert;
|
|
456
|
+
}
|
|
457
|
+
if (model.collation) {
|
|
458
|
+
document.collation = model.collation;
|
|
459
|
+
}
|
|
460
|
+
if (model.sort != null) {
|
|
461
|
+
document.sort = formatSort(model.sort);
|
|
462
|
+
}
|
|
463
|
+
return document;
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
/** @internal */
|
|
467
|
+
export function buildOperation(
|
|
468
|
+
model: AnyClientBulkWriteModel<Document>,
|
|
469
|
+
index: number,
|
|
470
|
+
pkFactory: PkFactory,
|
|
471
|
+
options: BSONSerializeOptions
|
|
472
|
+
): Document {
|
|
473
|
+
switch (model.name) {
|
|
474
|
+
case 'insertOne':
|
|
475
|
+
return buildInsertOneOperation(model, index, pkFactory);
|
|
476
|
+
case 'deleteOne':
|
|
477
|
+
return buildDeleteOneOperation(model, index);
|
|
478
|
+
case 'deleteMany':
|
|
479
|
+
return buildDeleteManyOperation(model, index);
|
|
480
|
+
case 'updateOne':
|
|
481
|
+
return buildUpdateOneOperation(model, index, options);
|
|
482
|
+
case 'updateMany':
|
|
483
|
+
return buildUpdateManyOperation(model, index, options);
|
|
484
|
+
case 'replaceOne':
|
|
485
|
+
return buildReplaceOneOperation(model, index);
|
|
486
|
+
}
|
|
487
|
+
}
|