Files
optima/dalpuri/old-src/helper/collectorQuery.ts

55 lines
1.2 KiB
TypeScript

export type CollectorQueryOptions<
SelectShape extends object,
IncludeShape extends object,
> = {
select?: (keyof SelectShape)[];
include?: (keyof IncludeShape)[];
};
const normalizeCollectorQuery = <
SelectShape extends object,
IncludeShape extends object,
>(
opts?: CollectorQueryOptions<SelectShape, IncludeShape>,
): CollectorQueryOptions<SelectShape, IncludeShape> => {
if (!opts) {
return {};
}
return opts;
};
export const buildCollectorFindManyArgs = <
SelectShape extends object,
IncludeShape extends object,
>(
opts?: CollectorQueryOptions<SelectShape, IncludeShape>,
) => {
const { select: selectedKeys, include: includedKeys } =
normalizeCollectorQuery(opts);
if (selectedKeys?.length && includedKeys?.length) {
throw new Error("Use either opts.select or opts.include, not both.");
}
if (selectedKeys?.length) {
const select: Partial<Record<keyof SelectShape, boolean>> = {};
selectedKeys.forEach((key) => {
select[key] = true;
});
return { select };
}
if (includedKeys?.length) {
const include: Partial<Record<keyof IncludeShape, boolean>> = {};
includedKeys.forEach((key) => {
include[key] = true;
});
return { include };
}
return {};
};