UserSelect across functions

Does anyone know how I can type this function such that the caller can optionally provide a selection which is then set on thefindUnique and then the return type matches what was selected. If the argument is not set it should return all the fields.
export async function getUser<T extends Omit<UserFindUniqueArgs, "where">>(
userId: number,
options: T,
): Promise</** WHAT TO PUT HERE? */> {
const user = await database.user.findUnique({
where: { id: userId },
...options,
});

if (!user) {
throw data(
{ error: `User with ID "${userId}" does not exist` },
{ status: 400 },
);
}

return user;
}
export async function getUser<T extends Omit<UserFindUniqueArgs, "where">>(
userId: number,
options: T,
): Promise</** WHAT TO PUT HERE? */> {
const user = await database.user.findUnique({
where: { id: userId },
...options,
});

if (!user) {
throw data(
{ error: `User with ID "${userId}" does not exist` },
{ status: 400 },
);
}

return user;
}
I don't know how useful this utility function is but it saves me from typng all this every time. Downside is that I'd also need to implement omit here and supposedly include according to the docs but in my IDE it says that field does not exist for findUnique
3 Replies
Prisma AI Help
Prisma AI Help7mo ago
You decided to hold for human wisdom. We'll chime in soon! Meanwhile, #ask-ai is there if you need a quick second opinion.
roguehusky3
roguehusky3OP7mo ago
THis allows me to do
const user = await getUser(session.userId, { name: false });
const user = await getUser(session.userId, { name: false });
in React Router return type still doesn't match
Nurul
Nurul6mo ago
GitHub
Specifying the "select" param in a query using a variable with a de...
Bug description After upgrading Prisma from 2.2 to 2.5, I&#39;m now running into an issue where, when I try to provide the select parameter using a typed variable rather than inline code in a query...

Did you find this page helpful?