How would I order by the role?
Hello, I'm trying to set up an API, where I return some users. I've got it working, where it returns the right users, with their roles.
But I would like to order them so the role "ADMIN" comes first. I know there is
orderBy
, but every time I use it, it just sends a bunch of errors. Maybe I'm on a completely wrong track?
What I'm currently doing to get the user:
30 Replies
can you show the error?
Well i tried this:
Error:
But I don't think I can use asc, as it's just a string I'm trying to check for?
try this
Giving me this error:
Can it be ordered by ascending? Because what is it ascending to?
can you show the whole query?
Do you mean this?
yeah
But again what is it ordering by?
Is there a way to make some custom orderBy?
i have this setup for orderBy
and worked fine
What comes first then? Staff or Partner?
partner
is role a separate relationship?
or is a user prop?
if is the first, being a separate relationship
This is my schema
try this
Nothing changed. Won't that only filter in the role array?
i made a mistake
orderBy should be inside the role thing
the first role? inside the where
Is that not the same thing as before or...?
no
before i did the orderBy outside the role
and edited the code after
Nothing really changed
Idk if im blind, but it still looks like it's inside the role
Slightly off topic but why are you storing roles as a string and not a number / bitfield? @Børge
Prisma
Data model (Reference)
Learn about the concepts for building your data model with Prisma: Models, scalar types, enums, attributes, functions, IDs, default values and more.
Just thought that would be easy. Would it be better storing it in a number or enum?
From an efficiency perspective, you'd want to store it as a number, instead of having Roles[], you'd just have a number, and each bit of that number represents a different role:
i.e:
bits:
0 - User
1 - Admin
2 - Manager
For evaluating that, you then just set an individual bit i.e
5 decimal becomes 101 in binary, that then is a user with the roles User and Admin
1 in decimal becomes 001 in binary which becomes the roles just User
Implementation wise, enum array would be easier but if you want the bitfield approach, here's how ive got it implemented in my codebase https://github.com/AnswerOverflow/AnswerOverflow/blob/main/packages/db/src/channel.ts
GitHub
AnswerOverflow/channel.ts at main · AnswerOverflow/AnswerOverflow
Indexing Discord Help Channel Questions into Google - AnswerOverflow/channel.ts at main · AnswerOverflow/AnswerOverflow
I don't fully understand. Could you give an example?
If I wanted to set the user just as a Manager
What number would that be?
its not so much what number would it be as it is what bit it would be, you need to set the third bit of a number to 1 to make the user a manager
CodeProject
An Introduction to Bitwise Operators
This article gives a brief overview of C style bitwise operators
https://discordapi.com/permissions.html Discord also uses bitfields for their permissions, check out this website how the number changes as you select more permissions
Discord Permissions Calculator
A small calculator that generates Discord OAuth invite links