mongodb-data-service
Version:
MongoDB Data Service
32 lines • 1.47 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.runCommand = void 0;
const mongodb_1 = require("mongodb");
/**
* Runs command against provided database using db.command. Provides a better
* return type based on provided command spec
*
* @param db database to run command against
* @param spec command name in the format { <command name>: 1 }
* @param options command options
* @returns command result
*/
const runCommand = (db, spec, options) => {
/**
* NB: Driver spec says that drivers command method should always run commands
* with primary readPreference disregarding whatever is the readPreference
* of the client/database. We don't want that, and instead want all the
* commands to run with whatever readPreference user has provided during the
* connection
*
* @see https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst#use-of-read-preferences-with-commands
*/
const readPreference = db.readPreference ?? mongodb_1.ReadPreference.PRIMARY_PREFERRED;
return db.command({ ...spec }, { readPreference, ...options }
// It's pretty hard to convince TypeScript that we are doing the right thing
// here due to how vague the driver types are hence the `any` assertion
// eslint-disable-next-line @typescript-eslint/no-explicit-any
);
};
exports.runCommand = runCommand;
//# sourceMappingURL=run-command.js.map
;