export type Query = Record; export type QuerySpec = { where: Query; orderBy?: Record[]; offset?: number; limit?: number; }; export function orderBy(order: string[][]): Record[] { return order.map(([field, direction]) => { if (!/^(asc|desc)$/i.test(direction)) throw new Error('Invalid sort direction'); return { [field]: direction.toLowerCase() }; }); } export function textSearch(fields: string[], text: string): Query { return { $or: fields.flatMap((field) => [...new Set([text, encodeURI(text)])].map((value) => ({ [field]: { $like: `%${value}%` }, })), ), }; }