Files
mongo/jstests/libs/cmd_object_utils.js
Catalin Sumanaru 224f399581 SERVER-89828 Explicitly pass the db object to query_settings_index_hi… (#21493)
GitOrigin-RevId: 881d023c82f71cb6487c0088e3e28d2c3af9238c
2024-05-02 09:26:32 +00:00

68 lines
2.0 KiB
JavaScript

/**
* Resolves the command name for the given 'cmdObj'.
*/
export function getCommandName(cmdObj) {
return Object.keys(cmdObj)[0];
}
/**
* Returns the inner command if 'cmdObj' represents an explain command, or simply 'cmdObj'
* otherwise.
*/
export function getInnerCommand(cmdObj) {
const isExplain = "explain" in cmdObj;
if (!isExplain) {
return cmdObj;
}
if (typeof cmdObj.explain === "object") {
return cmdObj.explain;
}
const {explain, ...cmdWithoutExplain} = cmdObj;
return cmdWithoutExplain;
}
/**
* Returns the explain command object for the given 'cmdObj'.
*/
export function getExplainCommand(cmdObj) {
const isAggregateCmd = getCommandName(cmdObj) === "aggregate";
return isAggregateCmd ? {explain: {...cmdObj, cursor: {}}} : {explain: cmdObj};
}
/**
* Resolves the collection name for the given 'cmdObj'. If the command targets a view, then this
* will return the underlying collection's name. Returns 'undefined' if the collection does not
* exist.
*/
export function getCollectionName(db, cmdObj) {
try {
const name = cmdObj[getCommandName(cmdObj)];
const collInfos = db.getCollectionInfos({name});
if (!collInfos || collInfos.length === 0) {
return undefined;
}
return collInfos[0].options.viewOn || name;
} catch (ex) {
switch (ex.code) {
case ErrorCodes.InvalidViewDefinition: {
// The 'DB.prototype.getCollectionInfos()' implementation may throw an exception
// when faced with a malformed view definition. This is analogous to a missing
// collection for the purpose of passthrough suites.
return undefined;
}
default:
throw ex;
}
}
}
export function isSystemCollectionName(collectionName) {
return collectionName.startsWith("system.");
}
export function isInternalDbName(dbName) {
return ["admin", "local", "config"].includes(dbName);
}