Added support for deep selecting

This commit is contained in:
Alex van Andel committed 2021-07-06 18:20:25 +00:00
1 parent 47d7634638
commit e08815ce78
2 files changed
+117 -7

No files matched your search

+16 -7
View File
@@ -12,16 +12,25 @@ if (process.env.NODE_ENV === "production") {
prisma = globalAny.prisma;
}
const pluck = (select: Record<string, boolean>, attr: string) => {
const parts = attr.split(".");
const alwaysAttr = parts[0];
const pluckedValue =
parts.length > 1
? {
select: pluck(select[alwaysAttr] ? select[alwaysAttr].select : {}, parts.slice(1).join(".")),
}
: true;
return {
...select,
[alwaysAttr]: pluckedValue,
};
};
const whereAndSelect = (modelQuery, criteria: Record<string, unknown>, pluckedAttributes: string[]) =>
modelQuery({
where: criteria,
select: pluckedAttributes.reduce(
(select: { [string]: boolean }, attr: string) => ({
...select,
[attr]: true,
}),
{}
),
select: pluckedAttributes.reduce(pluck, {}),
});
export { whereAndSelect };