Get Pinned/Favourite User Items - Xano

Hi all,

I am trying to display a list of items that a user has selected as favourite.

The User Table is linked to the Items table.

When getting the collection, I used a GET_USER/{user_id} and I used an ADDON for the Item details.

My problem is that I’m not sure how to filter the collection based on the User.id. So that way I only get the items from the loggedin user.

Any ideas?

Hi,
How is the favourite field? Is this a boolean in the Item table?

You need to apply this filter on the frontend?
If on frontend, you can just bring the field “Favourite” in the addon and then filter in Weweb:

If you can filter in the backend and not load all items, only items that are “favourite”, you can always apply this condition directly in the addon:

When you create the addon in the endpoint, this addon is created in the XANO library, there you can edit the addon and apply the query filter to only return items that are favourite.

I have the relationship on the User Table. So the User would have multiple favourite items. Should the relationship be the other way around? Wouldn’t that be more difficult? for an item to have multiple users that have added that item as favourite.

What I thought was that I can use an addon on the xano api and get the items by getting the user. But I’m not sure how to filter to the logged-in user.

Hi @emdiemeric ,

I’m also using Xano backend but I’m trying to avoid such a relationships in a DB. I would put pinned_responses to a separate table and build queries via JOINS or Addons (which I prefer).
Then you only have to write a constrain user.id == auth.id on the Xano side an you are ready to go!

I’m using relationships as a list of objects (array) if there is only limited and predictable number of items.

ok, so you are recommending of having a simple table, such as user, item, which would be relationship fields.

For each item added by the user, we would create a new entry in the table. correct?

Is this best practice for these types of data relationships?

Yes. db.user → db.responses. So each user can have many responses. It will by much faster and easier to manage.

You are correct: For each item added by the user, we would create a new entry in the table. correct?

So in terms of the filter, what is the best way to apply that? On the collection? I don’t really get the user.id

You should filter this in Xano. Otherwise everybody will be able to see all users in the API response.

When you create the Endpoint in the items, QUERY ALL records where all items are as favourite, where the QUERY all records conditions is user.id = items.user.id.

In the items table you can create a user reference field to enable the condition “user.id = items.user.id.”

@Dominic you don’t need to use “Get User Record” if you use AUTH during login, just add Id = Auth.id

And has @Dominic mentioned, it’s not good practices to pull data into the user collection unless specific user data itself.

Not home now, but later can printscreen the process.