Immutable chainable query with where, select, sort, and limit
An analytics page keeps a few thousand rows in memory and lets each widget slice them differently: filter, pick and rename columns, compute derived columns, sort, and paginate. Instead of hand-writing filter/map/sort chains in every widget, you are asked to build a small fluent query builder in the style of SQL query builders.
Implement queryTable(rows). It returns a query object with chainable methods.
queryTable(rows)
.where(predicate) // keep source rows where predicate(row) is truthy
.select(fields) // choose and rename output fields
.orderBy(field, direction?) // 'asc' (default) or 'desc'
.offset(n) // skip the first n result rows
.limit(n) // keep at most n result rows
.all() // => array of result rows
.first() // => the first result row, or null
where, select, orderBy, offset and limit each return a new query. The query they were called on is unchanged and can be reused.where(predicate) receives the original source row (not the projected one). Several where calls must all pass.select(fields): fields maps each output key to either:
'name' or 'address.city'. If any step along the path is null or undefined, the value is undefined.(row) => value that receives the source row.Output rows have exactly the keys of fields, in that order. A later select replaces an earlier one. Without select, each output row is a shallow copy of its source row.
orderBy(field, direction) sorts by a key of the output row, so it can use aliases and computed fields. The first orderBy call is the primary sort and later calls break ties.
< and > (values in one column share a type).null and undefined always sort last, in both directions.offset(n) and limit(n) take non-negative integers. Calling either again replaces the previous value.all() returns new row objects every time. first() returns the first row all() would return, or null if there is none.rows or any row in it.const tickets = [
{ id: 't1', title: 'Login fails', points: 5, owner: { name: 'Ivy' }, status: 'open' },
{ id: 't2', title: 'Dark mode', points: 3, owner: null, status: 'open' },
{ id: 't3', title: 'Export CSV', points: 8, owner: { name: 'Raj' }, status: 'done' },
{ id: 't4', title: 'Slow search', points: 5, owner: { name: 'Ana' }, status: 'open' },
];
const open = queryTable(tickets).where((t) => t.status === 'open');
open
.select({ ticket: 'id', assignee: 'owner.name', hours: (t) => t.points * 4 })
.orderBy('hours', 'desc')
.orderBy('assignee')
.all();
// => [
// { ticket: 't4', assignee: 'Ana', hours: 20 },
// { ticket: 't1', assignee: 'Ivy', hours: 20 },
// { ticket: 't2', assignee: undefined, hours: 12 },
// ]
open.orderBy('title').offset(1).limit(1).all();
// => [{ id: 't1', title: 'Login fails', points: 5, owner: { name: 'Ivy' }, status: 'open' }]
open.select({ assignee: 'owner.name' }).orderBy('assignee', 'desc').all();
// => [{ assignee: 'Ivy' }, { assignee: 'Ana' }, { assignee: undefined }] (missing values last)
open.where((t) => t.points > 100).first();
// => null
open.all().length;
// => 3 (open was never changed by the chains above)