list_collections.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import type { Binary, Document } from '../bson';
  2. import type { Db } from '../db';
  3. import type { Server } from '../sdam/server';
  4. import type { ClientSession } from '../sessions';
  5. import { type Callback, maxWireVersion } from '../utils';
  6. import { CommandCallbackOperation, type CommandOperationOptions } from './command';
  7. import { Aspect, defineAspects } from './operation';
  8. /** @public */
  9. export interface ListCollectionsOptions extends Omit<CommandOperationOptions, 'writeConcern'> {
  10. /** Since 4.0: If true, will only return the collection name in the response, and will omit additional info */
  11. nameOnly?: boolean;
  12. /** Since 4.0: If true and nameOnly is true, allows a user without the required privilege (i.e. listCollections action on the database) to run the command when access control is enforced. */
  13. authorizedCollections?: boolean;
  14. /** The batchSize for the returned command cursor or if pre 2.8 the systems batch collection */
  15. batchSize?: number;
  16. }
  17. /** @internal */
  18. export class ListCollectionsOperation extends CommandCallbackOperation<string[]> {
  19. /**
  20. * @remarks WriteConcern can still be present on the options because
  21. * we inherit options from the client/db/collection. The
  22. * key must be present on the options in order to delete it.
  23. * This allows typescript to delete the key but will
  24. * not allow a writeConcern to be assigned as a property on options.
  25. */
  26. override options: ListCollectionsOptions & { writeConcern?: never };
  27. db: Db;
  28. filter: Document;
  29. nameOnly: boolean;
  30. authorizedCollections: boolean;
  31. batchSize?: number;
  32. constructor(db: Db, filter: Document, options?: ListCollectionsOptions) {
  33. super(db, options);
  34. this.options = { ...options };
  35. delete this.options.writeConcern;
  36. this.db = db;
  37. this.filter = filter;
  38. this.nameOnly = !!this.options.nameOnly;
  39. this.authorizedCollections = !!this.options.authorizedCollections;
  40. if (typeof this.options.batchSize === 'number') {
  41. this.batchSize = this.options.batchSize;
  42. }
  43. }
  44. override executeCallback(
  45. server: Server,
  46. session: ClientSession | undefined,
  47. callback: Callback<string[]>
  48. ): void {
  49. return super.executeCommandCallback(
  50. server,
  51. session,
  52. this.generateCommand(maxWireVersion(server)),
  53. callback
  54. );
  55. }
  56. /* This is here for the purpose of unit testing the final command that gets sent. */
  57. generateCommand(wireVersion: number): Document {
  58. const command: Document = {
  59. listCollections: 1,
  60. filter: this.filter,
  61. cursor: this.batchSize ? { batchSize: this.batchSize } : {},
  62. nameOnly: this.nameOnly,
  63. authorizedCollections: this.authorizedCollections
  64. };
  65. // we check for undefined specifically here to allow falsy values
  66. // eslint-disable-next-line no-restricted-syntax
  67. if (wireVersion >= 9 && this.options.comment !== undefined) {
  68. command.comment = this.options.comment;
  69. }
  70. return command;
  71. }
  72. }
  73. /** @public */
  74. export interface CollectionInfo extends Document {
  75. name: string;
  76. type?: string;
  77. options?: Document;
  78. info?: {
  79. readOnly?: false;
  80. uuid?: Binary;
  81. };
  82. idIndex?: Document;
  83. }
  84. defineAspects(ListCollectionsOperation, [
  85. Aspect.READ_OPERATION,
  86. Aspect.RETRYABLE,
  87. Aspect.CURSOR_CREATING
  88. ]);