Is there a way I can the type can be inferred as O...
# orm-help
h
Is there a way I can the type can be inferred as OR while accessing them? I am selecting data based on roles. if role is admin then defaultAdminSelect and if role is student then defaultStudentSelect. It is typed as defaultAdminSelector, when though I am trying to access data as role student
Copy code
if (role === Role.admin)
    user = await prisma.user.findUnique({
      where: {
        id,
      },
      select: defaultAdminSelect,
    });
  if (role === Role.student)
    user = await prisma.user.findUnique({
      where: {
        id,
      },
      select: defaultStudentSelect,
    });
👀 1
l
Is the role property on the User that you are querying? As in, youre doing a nested query? Otherwise you can just combine the login for the select:
Copy code
user = await prisma.user.findUnique({
      where: {
        id,
      },
      select: role === Role.admin ? defaultAdminSelect : defaultStudentSelect,
    });
or make some a specific
getSelectByRole()
if you have more then two roles
n
Hey Hussam 👋 We have this guide on how to get access to return types when operating against partial structures of model types. Can you have a look at this? You should be able to use
PromiseReturnType
as mentioned in the guide to get correct return types for both the cases (admin and student)
h
@Lloyd Richards
Is the role property on the User that you are querying?
yes.
As in, you're doing a nested query?
Yes, this is my schema. If user is admin , the details field will be null, if student the selector will have nested data( from details which has 1-1 relation with another table)
Copy code
model user {
  id            String    @id @default(cuid())
  role          Role      @default(student)
  //...
  details student?
}
Meanwhile I'm checking at the link provided by Nurul.
I am missing anything ? @Nurul // The types inferred as shown in image
Copy code
const defaultAdminSelect = Prisma.validator<Prisma.userSelect>()({
  id: true,
  role: true,
  email: true,
});
l
Im not sure if this reduces the SQL in the backend, but one solution would be be to condition the
where
with the roles for admin and then on fail default to the student:
Copy code
const getUserOrAdmin = () => prisma.user.user.findUnique({
      where: {
        id,
        role: Role.admin
      },
      select: defaultAdminSelect,
    }).then((foundAdmin)=>foundAdmin ||       
      prisma.user.user.findUnique({
        where: {
          id,
        },
        select: defaultStudentSelect,
    })
  });
This could be done cleaner using fp_ts
h
So I tried this long enough , only to get find it is equivalent to first method. To confirm I added this inside then block 1st case
if (foundAdmin) return null; else return return .....selectStudent....
Copy code
const getUserOrAdmin: (session: Maybe<Session>) => Promise<{
    role: Role;
    id: string;
    email: string;
    details: student | null;  // works if foundAdmin is not returned
} | null>
and
if (foundAdmin) return foundAdmin;
Copy code
const getUserOrAdmin: (session: Maybe<Session>) => Promise<{
    role: Role;
    id: string;
    email: string; 
// does not work as there can be a case in which foundAdmin can be returned , so that will be the only inferred types from prisma
} | null>
l
This is where fp-ts (with ADTs) could really help, but you could also make some kind of sum type that is
type User = Admin | Student
and then just check the role and get the correct type definition for the object. https://dev.to/anthonyjoeseph/fp-ts-and-even-more-beautiful-api-calls-w-sum-types-53j0#unions-of-complex-types---sum-types
Copy code
type Admin = {
  role: Role.admin;
  id: string;
  email: string;
}

type Student= {
  role: Role.student;
  id: string;
  email: string;
  details: StudentDetails
}

const getUserOrAdmin: (session: Maybe<Session>) => Promise<Admin | Student | null>