NestJS and DrizzleOrm question
Please tell me, I have a very complicated idea.
I'm making an event app, events can be created by clubs, and a club can have people with the roles of President and Staff
and only they can create events.
I have a roles table.
as well as the user_roles many to many table, a user can be the president of many clubs, or be on the staff of other clubs
The problem now is that I'm trying to implement a method to get information about which clubs the user has access to create events.

8 Replies

I guess I need to join the tables, because now I need to compare by id whether the user has the id of the role through which he has access, but in the code I need to achieve a comparison with the enum like current user roles = [President, Staff]

sorry if it's confusing, I hope I explained the problem clearly, I don't know how to approach the solution.
so far, this is the answer, although HobbyHorsing should not be because the role corresponds to the role of a Member.



please tell me how to write this with drizzle
so far, I only see a solution to take and filter the backend response in the map, where it explicitly checks the role ids (since they will not change)
but I would like to compare it with the name of the role, and do it through the database, not the map.
@👾Rnbsov are you using laravel?
If you're using laravel, mysql
you can do like this
First define
- User.php
class User extends Authenticatable
{
public function roles()
{
return $this->hasMany(UserRole::class);
}
public function authorizedClubs()
{
return $this->belongsToMany(Club::class, 'user_roles')
->whereIn('user_roles.role_id', function ($query) {
$query->select('id')
->from('roles')
->whereIn('name', ['President', 'Staff']);
});
}
}
- Club.php
class Club extends Model
{
public function users()
{
return $this->belongsToMany(User::class, 'user_roles');
}
}
On your controller, you can get clubs like this easliy:
$user = Auth::user();
$clubs = $user->authorizedClubs()->get();
no, but thanks!
I made a sql query out of your code
thanks!
@👾Rnbsov awesome, sql query is more scalable and fast method than using pre-built in mvc