@tanstack/optimistic
Version:
Core optimistic updates library
217 lines (216 loc) • 6.45 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const d2ts = require("@electric-sql/d2ts");
const extractors = require("./extractors.cjs");
const utils = require("./utils.cjs");
function processOrderBy(resultPipeline, query, mainTableAlias) {
let hasOrderIndexColumn = false;
let orderIndexType = `numeric`;
let orderIndexAlias = ``;
for (const item of query.select) {
if (typeof item === `object`) {
for (const [alias, expr] of Object.entries(item)) {
if (typeof expr === `object` && utils.isOrderIndexFunctionCall(expr)) {
hasOrderIndexColumn = true;
orderIndexAlias = alias;
orderIndexType = getOrderIndexType(expr);
break;
}
}
}
if (hasOrderIndexColumn) break;
}
const orderByItems = [];
if (typeof query.orderBy === `string`) {
orderByItems.push({
operand: query.orderBy,
direction: `asc`
});
} else if (Array.isArray(query.orderBy)) {
for (const item of query.orderBy) {
if (typeof item === `string`) {
orderByItems.push({
operand: item,
direction: `asc`
});
} else if (typeof item === `object`) {
for (const [column, direction] of Object.entries(item)) {
orderByItems.push({
operand: column,
direction
});
}
}
}
} else if (typeof query.orderBy === `object`) {
for (const [column, direction] of Object.entries(query.orderBy)) {
orderByItems.push({
operand: column,
direction
});
}
}
const valueExtractor = (value) => {
const row = value;
const nestedRow = { [mainTableAlias]: row };
if (orderByItems.length > 1) {
return orderByItems.map((item) => {
const val = extractors.evaluateOperandOnNestedRow(
nestedRow,
item.operand,
mainTableAlias
);
return item.direction === `desc` && typeof val === `number` ? -val : item.direction === `desc` && typeof val === `string` ? String.fromCharCode(
...[...val].map((c) => 65535 - c.charCodeAt(0))
) : val;
});
} else if (orderByItems.length === 1) {
const item = orderByItems[0];
const val = extractors.evaluateOperandOnNestedRow(
nestedRow,
item.operand,
mainTableAlias
);
return item.direction === `desc` && typeof val === `number` ? -val : item.direction === `desc` && typeof val === `string` ? String.fromCharCode(
...[...val].map((c) => 65535 - c.charCodeAt(0))
) : val;
}
return null;
};
const comparator = (a, b) => {
if (typeof a === `number` && typeof b === `number`) {
return a - b;
}
if (typeof a === `string` && typeof b === `string`) {
return a.localeCompare(b);
}
if (typeof a === `boolean` && typeof b === `boolean`) {
return a ? 1 : -1;
}
if (a instanceof Date && b instanceof Date) {
return a.getTime() - b.getTime();
}
if (a === null && b === null) {
return 0;
}
if (Array.isArray(a) && Array.isArray(b)) {
for (let i = 0; i < a.length; i++) {
const result = comparator(a[i], b[i]);
if (result !== 0) return result;
}
return 0;
}
if ((a === null || a === void 0) && (b === null || b === void 0)) {
return 0;
}
return a.toString().localeCompare(b.toString());
};
let topKComparator;
if (!query.keyBy) {
topKComparator = (a, b) => {
const aValue = valueExtractor(a);
const bValue = valueExtractor(b);
return comparator(aValue, bValue);
};
}
if (hasOrderIndexColumn) {
if (orderIndexType === `numeric`) {
if (query.keyBy) {
resultPipeline = resultPipeline.pipe(
d2ts.orderByWithIndex(valueExtractor, {
limit: query.limit,
offset: query.offset,
comparator
}),
d2ts.map(([key, [value, index]]) => {
const result = {
...value,
[orderIndexAlias]: index
};
return [key, result];
})
);
} else {
resultPipeline = resultPipeline.pipe(
d2ts.map((value) => [null, value]),
d2ts.topKWithIndex(topKComparator, {
limit: query.limit,
offset: query.offset
}),
d2ts.map(([_, [value, index]]) => {
return {
...value,
[orderIndexAlias]: index
};
})
);
}
} else {
if (query.keyBy) {
resultPipeline = resultPipeline.pipe(
d2ts.orderByWithFractionalIndex(valueExtractor, {
limit: query.limit,
offset: query.offset,
comparator
}),
d2ts.map(([key, [value, index]]) => {
const result = {
...value,
[orderIndexAlias]: index
};
return [key, result];
})
);
} else {
resultPipeline = resultPipeline.pipe(
d2ts.map((value) => [null, value]),
d2ts.topKWithFractionalIndex(topKComparator, {
limit: query.limit,
offset: query.offset
}),
d2ts.map(([_, [value, index]]) => {
return {
...value,
[orderIndexAlias]: index
};
})
);
}
}
} else {
if (query.keyBy) {
resultPipeline = resultPipeline.pipe(
d2ts.orderBy(valueExtractor, {
limit: query.limit,
offset: query.offset,
comparator
})
);
} else {
resultPipeline = resultPipeline.pipe(
d2ts.map((value) => [null, value]),
d2ts.topK(topKComparator, {
limit: query.limit,
offset: query.offset
}),
d2ts.map(([_, value]) => value)
);
}
}
return resultPipeline;
}
function getOrderIndexType(obj) {
if (!utils.isOrderIndexFunctionCall(obj)) {
throw new Error(`Not an ORDER_INDEX function call`);
}
const arg = obj[`ORDER_INDEX`];
if (arg === `numeric` || arg === true || arg === `default`) {
return `numeric`;
} else if (arg === `fractional`) {
return `fractional`;
} else {
throw new Error(`Invalid ORDER_INDEX type: ` + arg);
}
}
exports.processOrderBy = processOrderBy;
//# sourceMappingURL=order-by.cjs.map