#Join (non inner) with Select

3 messages · Page 1 of 1 (latest)

gusty lodge
#

I am trying to select cards with the assigned user. I am joining the cards collection with the users collection with user_id. I am then using select and want the user to be under user on the card. However, because I am using a leftJoin the returned type just makes every field of user optional rather than the user itself as optional.

  const { data: cards = [] } = useLiveQuery(
    (q) =>
      q
        .from({ card: cardCollection })
        .leftJoin({ user: userCollection }, ({ card, user }) =>
          eq(card.user_id, user.id),
        )
        .select(({ card, user, }) => ({
          ...card,
          user: user,
        })),
    [],
  );

Is this a bug or is there a solution for this?

#

I think I figured this out this works:

 const { data: cards = [] } = useLiveQuery(
    (q) =>
      q
        .from({ card: cardCollection })
        .leftJoin({ user: userCollection }, ({ card, user }) =>
          eq(card.user_id, user.id),
        )
        .select(({ card, user, }) => ({
          ...card,
          user: user ? user : undefined,
        })),
    [],
  );

This does not:

 const { data: cards = [] } = useLiveQuery(
    (q) =>
      q
        .from({ card: cardCollection })
        .leftJoin({ user: userCollection }, ({ card, user }) =>
          eq(card.user_id, user.id),
        )
        .select(({ card, user, }) => ({
          ...card,
          user: user ?? undefined,
        })),
    [],
  );
bleak turret
#

Faced a similar thing, is this the recommended approach to fix this?