options_operation.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import type { Document } from '../bson';
  2. import type { Collection } from '../collection';
  3. import { MongoAPIError } from '../error';
  4. import type { Server } from '../sdam/server';
  5. import type { ClientSession } from '../sessions';
  6. import type { Callback } from '../utils';
  7. import { AbstractCallbackOperation, type OperationOptions } from './operation';
  8. /** @internal */
  9. export class OptionsOperation extends AbstractCallbackOperation<Document> {
  10. override options: OperationOptions;
  11. collection: Collection;
  12. constructor(collection: Collection, options: OperationOptions) {
  13. super(options);
  14. this.options = options;
  15. this.collection = collection;
  16. }
  17. override executeCallback(
  18. server: Server,
  19. session: ClientSession | undefined,
  20. callback: Callback<Document>
  21. ): void {
  22. const coll = this.collection;
  23. coll.s.db
  24. .listCollections(
  25. { name: coll.collectionName },
  26. { ...this.options, nameOnly: false, readPreference: this.readPreference, session }
  27. )
  28. .toArray()
  29. .then(
  30. collections => {
  31. if (collections.length === 0) {
  32. // TODO(NODE-3485)
  33. return callback(new MongoAPIError(`collection ${coll.namespace} not found`));
  34. }
  35. callback(undefined, collections[0].options);
  36. },
  37. error => callback(error)
  38. );
  39. }
  40. }