UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

53 lines (52 loc) 2.28 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const functions = require("../query/builder/functions.cjs"); const ir = require("../query/ir.cjs"); function isNullish(expression) { return functions.or(functions.isNull(expression), functions.isUndefined(expression)); } function followsBoundary(clause, value) { const nullish = isNullish(clause.expression); if (value == null) { return clause.compareOptions.nulls === `first` ? functions.not(nullish) : new ir.Value(false); } const operator = clause.compareOptions.direction === `asc` ? functions.gt : functions.lt; const comparison = operator(clause.expression, new ir.Value(value)); return clause.compareOptions.nulls === `last` ? functions.or(comparison, nullish) : comparison; } function buildCursor(orderBy, values) { if (values.length === 0) return void 0; if (orderBy.length !== 1 || values.length !== 1) { throw new Error(`Only single-column cursors are supported`); } return followsBoundary(orderBy[0], values[0]); } function buildCursorCurrent(orderBy, values) { const { expression } = orderBy[0] ?? {}; if (!expression || values.length === 0) return void 0; const value = values[0]; if (value == null) return isNullish(expression); if (value instanceof Date) { if (!Number.isFinite(value.getTime())) return void 0; return functions.and( functions.gte(expression, new ir.Value(value)), functions.lt(expression, new ir.Value(new Date(value.getTime() + 1))) ); } if (typeof value === `object`) return void 0; return functions.eq(expression, new ir.Value(value)); } function canExpressCursorOrder(orderBy, values) { if (orderBy.length !== 1 || values.length !== 1) return false; const value = values[0]; if (value == null) return false; if (value instanceof Date) return Number.isFinite(value.getTime()); if (typeof value === `string`) { return orderBy[0].compareOptions.stringSort === `lexical`; } return typeof value === `number` && Number.isFinite(value) || typeof value === `bigint` || typeof value === `boolean`; } exports.buildCursor = buildCursor; exports.buildCursorCurrent = buildCursorCurrent; exports.canExpressCursorOrder = canExpressCursorOrder; //# sourceMappingURL=cursor.cjs.map