@tanstack/optimistic
Version:
Core optimistic updates library
123 lines (122 loc) • 3.82 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const functions = require("./functions.cjs");
function extractValueFromNestedRow(nestedRow, columnRef, mainTableAlias, joinedTableAlias) {
if (columnRef.includes(`.`)) {
const [tableAlias, colName] = columnRef.split(`.`);
const tableData = nestedRow[tableAlias];
if (!tableData) {
return null;
}
const value = tableData[colName];
return value;
} else {
if (mainTableAlias && nestedRow[mainTableAlias]) {
const mainTableData = nestedRow[mainTableAlias];
if (typeof mainTableData === `object` && columnRef in mainTableData) {
return mainTableData[columnRef];
}
}
if (joinedTableAlias && nestedRow[joinedTableAlias]) {
const joinedTableData = nestedRow[joinedTableAlias];
if (typeof joinedTableData === `object` && columnRef in joinedTableData) {
return joinedTableData[columnRef];
}
}
for (const [_tableAlias, tableData] of Object.entries(nestedRow)) {
if (tableData && typeof tableData === `object` && columnRef in tableData) {
return tableData[columnRef];
}
}
return void 0;
}
}
function evaluateOperandOnNestedRow(nestedRow, operand, mainTableAlias, joinedTableAlias) {
if (typeof operand === `string` && operand.startsWith(`@`)) {
const columnRef = operand.substring(1);
return extractValueFromNestedRow(
nestedRow,
columnRef,
mainTableAlias,
joinedTableAlias
);
}
if (operand && typeof operand === `object` && `col` in operand) {
const colRef = operand.col;
if (typeof colRef === `string`) {
const nestedValue = extractValueFromNestedRow(
nestedRow,
colRef,
mainTableAlias,
joinedTableAlias
);
if (nestedValue === void 0 && colRef in nestedRow) {
return nestedRow[colRef];
}
return nestedValue;
}
return void 0;
}
if (operand && typeof operand === `object` && functions.isFunctionCall(operand)) {
const functionName = Object.keys(operand)[0];
const args = operand[functionName];
const evaluatedArgs = Array.isArray(args) ? args.map(
(arg) => evaluateOperandOnNestedRow(
nestedRow,
arg,
mainTableAlias,
joinedTableAlias
)
) : evaluateOperandOnNestedRow(
nestedRow,
args,
mainTableAlias,
joinedTableAlias
);
return functions.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;
}
exports.evaluateOperandOnNestedRow = evaluateOperandOnNestedRow;
exports.extractJoinKey = extractJoinKey;
exports.extractValueFromNestedRow = extractValueFromNestedRow;
//# sourceMappingURL=extractors.cjs.map