All files / core/blong-gogo/src/adapter/schema/knex order.ts

100% Statements 82/82
96.77% Branches 30/31
100% Functions 4/4
100% Lines 82/82

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 831x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 1x 18x 18x 4x 4x 4x 4x 18x 13x 18x 18x 18x 18x 1x 1x 1x 1x 1x 1x 1x 1x 1x 91x 91x 91x 91x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 81x 81x 81x 81x 81x 81x 81x 81x 6x 6x 2x 6x 81x 81x  
/**
 * The `order` / `orderBy` clause a caller sends, folded into what knex reads.
 *
 * A portal sends `[{field: 'coralName', dir: 'ASC'}]` (`useTableList` and the
 * Explorer both do), while knex reads `{column, order}` — and anything else it
 * cannot see is formatted as the literal `undefined`, producing
 * `order by \`undefined\`` and a MySQL `Unknown column 'undefined' in 'order
 * clause'` on every column the reader sorts by. So the shape is normalised here,
 * once, and both spellings work.
 *
 * A column name is passed to knex as an identifier and knex backticks it, so a
 * reserved word (`order` on `marine.family`) is safe in this clause — the reason
 * a sort never had to be told to avoid one @@D-742.
 */
const DIRECTIONS = new Set(['asc', 'desc']);
 
/** One order term, already reduced to a column and a direction. */
export interface IOrderTerm {
    column: string;
    order: 'asc' | 'desc';
}
 
function direction(value: unknown): 'asc' | 'desc' {
    const text = typeof value === 'string' ? value.toLowerCase() : '';
    return DIRECTIONS.has(text) ? (text as 'asc' | 'desc') : 'asc';
}
 
function termOf(entry: unknown): IOrderTerm | undefined {
    if (typeof entry === 'string') {
        // `'name desc'` is knex's own shorthand; keep it usable.
        const [column, dir] = entry.trim().split(/\s+/);
        return column ? {column, order: direction(dir)} : undefined;
    }
    if (!entry || typeof entry !== 'object') return undefined;
    const record = entry as Record<string, unknown>;
    const column = record['column'] ?? record['field'];
    if (typeof column !== 'string' || !column) return undefined;
    return {column, order: direction(record['order'] ?? record['dir'])};
}
 
/**
 * Every order term a caller asked for, in order.
 *
 * An unrecognised entry is dropped rather than turned into `undefined`: a clause
 * nobody can read is worse than a row order nobody chose. Accepts one term or a
 * list of them.
 */
export function normalizeOrderBy(value: unknown): IOrderTerm[] {
    if (value == null) return [];
    const entries = Array.isArray(value) ? value : [value];
    return entries.map(termOf).filter((term): term is IOrderTerm => term !== undefined);
}
 
/**
 * The terms a read can actually order by, and the ones it cannot.
 *
 * A caller may name something the table does not have: a browser model's display
 * field, which the read joins in rather than stores (`targetName` on an ACL rule
 * is built from the target's resource name), or a column the schema has moved.
 * MySQL fails the whole statement for it — `Unknown column 'targetName' in
 * 'order clause'` — so the caller drops the term and reports it, and the page
 * shows its rows unordered instead of an error. An order is presentation; it is
 * never worth losing the data over.
 *
 * A dotted name is kept: it says which table it belongs to, so this function has
 * no way to judge it. A label alias is kept because the read joins it in.
 */
export function orderableTerms(
    terms: IOrderTerm[],
    columns: Iterable<string>,
    aliases: Record<string, string> = {},
): {terms: IOrderTerm[]; dropped: string[]} {
    const known = new Set(columns);
    const orderable: IOrderTerm[] = [];
    const dropped: string[] = [];
    for (const term of terms) {
        if (term.column.includes('.') || known.has(term.column) || aliases[term.column])
            orderable.push(term);
        else dropped.push(term.column);
    }
    return {terms: orderable, dropped};
}