@tanstack/optimistic
Version:
Core optimistic updates library
108 lines (107 loc) • 3.53 kB
JavaScript
import { map } from "@electric-sql/d2ts";
import { extractValueFromNestedRow, evaluateOperandOnNestedRow } from "./extractors.js";
function processSelect(pipeline, query, mainTableAlias, inputs) {
return pipeline.pipe(
map((nestedRow) => {
const result = {};
const isGroupedResult = query.groupBy && Object.keys(nestedRow).some(
(key) => !Object.keys(inputs).includes(key) && typeof nestedRow[key] !== `object`
);
for (const item of query.select) {
if (typeof item === `string`) {
if (item === `@*`) {
if (isGroupedResult) {
Object.assign(result, nestedRow);
} else {
Object.assign(result, extractAllColumnsFromAllTables(nestedRow));
}
continue;
}
if (item.startsWith(`@`) && item.endsWith(`.*`)) {
const tableAlias = item.slice(1, -2);
if (isGroupedResult) {
continue;
} else {
Object.assign(
result,
extractAllColumnsFromTable(nestedRow, tableAlias)
);
}
continue;
}
if (item.startsWith(`@`)) {
const columnRef = item.substring(1);
const alias = columnRef;
if (isGroupedResult && columnRef in nestedRow) {
result[alias] = nestedRow[columnRef];
} else {
result[alias] = extractValueFromNestedRow(
nestedRow,
columnRef,
mainTableAlias,
void 0
);
}
if (alias.includes(`.`)) {
const columnName = alias.split(`.`)[1];
result[columnName] = result[alias];
delete result[alias];
}
}
} else {
for (const [alias, expr] of Object.entries(item)) {
if (typeof expr === `string` && expr.startsWith(`@`)) {
const columnRef = expr.substring(1);
if (isGroupedResult && columnRef in nestedRow) {
result[alias] = nestedRow[columnRef];
} else {
result[alias] = extractValueFromNestedRow(
nestedRow,
columnRef,
mainTableAlias,
void 0
);
}
} else if (typeof expr === `object`) {
if (isGroupedResult && alias in nestedRow) {
result[alias] = nestedRow[alias];
} else {
result[alias] = evaluateOperandOnNestedRow(
nestedRow,
expr,
mainTableAlias,
void 0
);
}
}
}
}
}
return result;
})
);
}
function extractAllColumnsFromAllTables(nestedRow) {
const result = {};
for (const [tableAlias, tableData] of Object.entries(nestedRow)) {
if (tableData && typeof tableData === `object`) {
Object.assign(result, extractAllColumnsFromTable(nestedRow, tableAlias));
}
}
return result;
}
function extractAllColumnsFromTable(nestedRow, tableAlias) {
const result = {};
const tableData = nestedRow[tableAlias];
if (!tableData || typeof tableData !== `object`) {
return result;
}
for (const [columnName, value] of Object.entries(tableData)) {
result[columnName] = value;
}
return result;
}
export {
processSelect
};
//# sourceMappingURL=select.js.map