@tanstack/db
Version:
A reactive client store for building super fast apps on sync
123 lines (122 loc) • 3.74 kB
JavaScript
import { isFunctionCall, evaluateFunction } from "./functions.js";
function extractValueFromNamespacedRow(namespacedRow, columnRef, mainTableAlias, joinedTableAlias) {
if (columnRef.includes(`.`)) {
const [tableAlias, colName] = columnRef.split(`.`);
const tableData = namespacedRow[tableAlias];
if (!tableData) {
return null;
}
const value = tableData[colName];
return value;
} else {
if (mainTableAlias && namespacedRow[mainTableAlias]) {
const mainTableData = namespacedRow[mainTableAlias];
if (typeof mainTableData === `object` && columnRef in mainTableData) {
return mainTableData[columnRef];
}
}
if (joinedTableAlias && namespacedRow[joinedTableAlias]) {
const joinedTableData = namespacedRow[joinedTableAlias];
if (typeof joinedTableData === `object` && columnRef in joinedTableData) {
return joinedTableData[columnRef];
}
}
for (const [_tableAlias, tableData] of Object.entries(namespacedRow)) {
if (tableData && typeof tableData === `object` && columnRef in tableData) {
return tableData[columnRef];
}
}
return void 0;
}
}
function evaluateOperandOnNamespacedRow(namespacedRow, operand, mainTableAlias, joinedTableAlias) {
if (typeof operand === `string` && operand.startsWith(`@`)) {
const columnRef = operand.substring(1);
return extractValueFromNamespacedRow(
namespacedRow,
columnRef,
mainTableAlias,
joinedTableAlias
);
}
if (operand && typeof operand === `object` && `col` in operand) {
const colRef = operand.col;
if (typeof colRef === `string`) {
const nestedValue = extractValueFromNamespacedRow(
namespacedRow,
colRef,
mainTableAlias,
joinedTableAlias
);
if (nestedValue === void 0 && colRef in namespacedRow) {
return namespacedRow[colRef];
}
return nestedValue;
}
return void 0;
}
if (operand && typeof operand === `object` && isFunctionCall(operand)) {
const functionName = Object.keys(operand)[0];
const args = operand[functionName];
const evaluatedArgs = Array.isArray(args) ? args.map(
(arg) => evaluateOperandOnNamespacedRow(
namespacedRow,
arg,
mainTableAlias,
joinedTableAlias
)
) : evaluateOperandOnNamespacedRow(
namespacedRow,
args,
mainTableAlias,
joinedTableAlias
);
return evaluateFunction(
functionName,
evaluatedArgs
);
}
if (operand && typeof operand === `object` && `value` in operand) {
return operand.value;
}
return operand;
}
function extractJoinKey(row, operand, defaultTableAlias) {
let keyValue;
if (typeof operand === `string` && operand.startsWith(`@`)) {
const columnRef = operand.substring(1);
if (columnRef.includes(`.`)) {
const [tableAlias, colName] = columnRef.split(`.`);
if (tableAlias === defaultTableAlias) {
keyValue = row[colName];
} else {
keyValue = void 0;
}
} else {
keyValue = row[columnRef];
}
} else if (operand && typeof operand === `object` && `col` in operand) {
const colRef = operand.col;
if (typeof colRef === `string`) {
if (colRef.includes(`.`)) {
const [tableAlias, colName] = colRef.split(`.`);
if (tableAlias === defaultTableAlias) {
keyValue = row[colName];
} else {
keyValue = void 0;
}
} else {
keyValue = row[colRef];
}
}
} else {
keyValue = operand;
}
return keyValue;
}
export {
evaluateOperandOnNamespacedRow,
extractJoinKey,
extractValueFromNamespacedRow
};
//# sourceMappingURL=extractors.js.map