Skip to content

Commit

Permalink
Lower complexity of query info formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ecooper committed Dec 18, 2024
1 parent 60fde64 commit ec4f267
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/lib/fauna-client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,29 @@ export const formatQuerySummary = (summary) => {
}
};

/**
* Selects a subset of query info fields from a v10 query response.
* @param {import("fauna").QueryInfo} response - The query response
* @param {string[]} include - The query info fields to include
* @returns {object} An object with the selected query info fields
*/
const pickAndCamelCaseQueryInfo = (response, include) => {
const queryInfo = {};

if (include.includes("txnTs") && response.txn_ts)
queryInfo.txnTs = response.txn_ts;
if (include.includes("schemaVersion") && response.schema_version)
queryInfo.schemaVersion = response.schema_version.toString();
if (include.includes("summary") && response.summary)
queryInfo.summary = response.summary;
if (include.includes("queryTags") && response.query_tags)
queryInfo.queryTags = response.query_tags;
if (include.includes("stats") && response.stats)
queryInfo.stats = response.stats;
const getQueryInfoValue = (response, field) => {
switch (field) {
case "txnTs":
return response.txn_ts;
case "schemaVersion":
return response.schema_version?.toString();
case "summary":
return response.summary;
case "queryTags":
return response.query_tags;
case "stats":
return response.stats;
default:
return undefined;
}
};

const getIncludedQueryInfo = (response, include) => {
const queryInfo = {};
include.forEach((field) => {
const value = getQueryInfoValue(response, field);
if (value) queryInfo[field] = value;
});
return queryInfo;
};

Expand All @@ -224,7 +227,7 @@ export const formatQueryInfo = (response, { apiVersion, color, include }) => {

return `${colorized}\n`;
} else if (apiVersion === "10") {
const queryInfoToDisplay = pickAndCamelCaseQueryInfo(response, include);
const queryInfoToDisplay = getIncludedQueryInfo(response, include);

if (Object.keys(queryInfoToDisplay).length === 0) return "";

Expand Down

0 comments on commit ec4f267

Please sign in to comment.