Querying a Many to Many Relationship with Sequelize

preview_player
Показать описание
Learn how to query teams you are a member of by querying the many to many relationship we setup with Sequelize.

Code:

Links from video:

----
Video Suggestions:

----
Follow Me Online Here:

----
#benawad

----
#benawad
Рекомендации по теме
Комментарии
Автор

Hi Ben, great video series!

Regarding of the cancel button warning, instead of handling it in the function, we can just assign roles to the buttons

<Button type="button" disabled={isSubmitting} fluid
<Button type="submit" disabled={isSubmitting} fluid>Create Channel</Button>

That way, the form will only expect to be submitted by one of the buttons, and will ignore the actions of the cancel button. I think that can make the handler functions cleaner

luanlaco
Автор

BTW - loving these videos Ben. Great job and keep them coming!

Also, perhaps the reason the owner didn't work in the gql query is that you needed to specify the id like so:
{
allTeams {
id
name
owner {
id
}
channels {
id
name
}
}
}

chadmichael
Автор

I wish i could just spend all day going through the series, but alas, life :P

bantinggamer
Автор

Hi Ben. Loving this series - thanks.

Regarding the single join request, I think that it can be done; instead of fetching teams as a Query type, you /could/ create them as joined elements on the User type, e.g. ownedTeams: [Team!]! and memberTeams: [Team!]!

You would still need to do the same work in the type resolvers, or in join-monster metadata, but you would then be able to fetch a larger tree of results from the user query in one request.

MarkFairhurst
Автор

The way yo merge the inviteTeams and allTeams arrays throws an error:
Invalid attempt to spread non-iterable instance

If either is empty. Solution I used the lodash concat method

adnanjam
Автор

Loved it buddy, just wow. Keep up the good work

EQuimper
Автор

There is a way to join those two. Here is how. Notice that its important not to include raw: true, since it flattened the object!!!
const teams = await models.Team.findAll({
{
[

user.id,


user.id,



[{ model: models.User }],
});

sannge
Автор

Hi Ben,

What could be a potential reason for getting this following error?

TypeError: Cannot convert undefined or null to object

_toConsumableArray
ViewTeam
src/routes/ViewTeam.js:24
21 | }
22 | console.log(inviteTeams);
23 |
> 24 | const teams = [...allTeams, ...inviteTeams];
25 |
26 | if (!teams.length) {
27 | return <Redirect to="/create-team" />;


I tried both the way you showed in this video and in the next video (Raw) and got the same error...

ronifintech
Автор

Hi Ben, I have a question on this query code:

inviteTeams: (parent, args, { models, user }) =>
models.Team.findAll(
{
include: [
{
model: models.User,
where: { id: user.id },
},
],
},
{ raw: true },
)),

I was wondering how does Sequelize know that 'member' is our table that associates user and teams together? Because no where in the code did we specify 'member' was our table that links our 'user' and 'team' table together.

MoonRed
Автор

just add the owner property to the member object ... or to be more future proof .. add a role property to the member model, and have 'owner', 'admin', 'moderator' then just check the users membership for the team to see what user level they have

CodesmithEvanion
Автор

One way to avoid two sequelize findAlls is to embed the sql in the resolver like so:

allTeams: (parent, args, { models, user }) =>
models.sequelize.query(
`
SELECT "id", "name", "created_at", "updated_at", "owner"
FROM "teams"
WHERE "owner" = ${user.id}
UNION
SELECT "t"."id", "t"."name", "t"."created_at", "t"."updated_at", "t"."owner"
FROM "members" "m"
INNER JOIN "teams" "t" ON "t"."id" = "m"."team_id"
WHERE "m"."user_id" = ${user.id}
`,
{ model: models.Team, raw: true },
)),

I'm not sure I like this but it's good to know that you can take control of the query if needed. Surely there is a better, more native way in sequelize to do it but I don't know it well enough. Still, I think this is better handled on the server than on the client.

chadmichael