Uecko_ERP/packages/rdx-criteria/src/utils.ts
2026-06-15 17:16:07 +02:00

34 lines
1.1 KiB
TypeScript

import type { FindOptions, Includeable, OrderItem } from "sequelize";
export function normalizeOrder(order: FindOptions["order"]): OrderItem[] {
if (!order) return [];
return Array.isArray(order) ? (order as OrderItem[]) : [order as OrderItem];
}
export function normalizeInclude(include: FindOptions["include"]): Includeable[] {
if (!include) return [];
return Array.isArray(include) ? include : [include];
}
export function prependOrder(options: FindOptions, orderItem: OrderItem): void {
options.order = [orderItem, ...normalizeOrder(options.order)];
}
export function appendOrder(options: FindOptions, orderItem: OrderItem): void {
options.order = [...normalizeOrder(options.order), orderItem];
}
/**
* Devuelve los includes aplicables al COUNT.
*
* Los includes con `separate: true` no forman parte del SELECT principal,
* por lo que no deben arrastrarse al conteo.
*/
export function getCountIncludes(include: Includeable[]): Includeable[] {
return include.filter((item) => {
if (!item || typeof item !== "object") return true;
return !("separate" in item && item.separate === true);
});
}