How can I store a complete user data in attribute of a collection in appwrite?

325 views Asked by At

I am creating a social media app using react and appwrite for backend. In appwrite I have a collection in database named "users". I want to store "followings" and "followers" in array as a attribute in user collection.

I am confused how to store user details in followings or followers of all user I am following. There are two ways in which I can store details:

  1. I can store every users id.
  2. Somehow If I can create a relation between every user in users collection.

If i store ids of all users then to show all my followers on screen I have to get all users by id which requires so much call to database, which I don't think a good way.

I can not create relations between data of same collection in appwrite.

So, I am stuck how to store other users data in my followes and followings array.

One other way can we to store complete user in array. But we can store the whole data as string, so now I can convert it to string, like JSON.stringify and when I wan data i will JSON.parse every data. But I am also not sure if it is a good way or can create any other problems.

in attribute i also can not store data directly that why I am thinking to convert it to a string.

enter image description here

1

There are 1 answers

1
Hans Murangaza DRCongo On BEST ANSWER

I have been working on an app, a social media like app. But in Firebase. I think the best approache is to create a link between users. It involves creating another table (or db) where you can store connections data like: DB1: Users DB2: Connections In the connections DB, write objects that link users

an object like:

{follower_user_id: "SHJSN45Q",
following_user_id: "HJ85SUI",
following_date: 12-23-2015,
privileges: ["can_comment", "can_like", "can_text", "can_tag"]
}

So every time a userA will click on a follow button in a userB profile; it will create this object in Connections DB. the userA will be the follower_user_id and the userB will be the following_user_id. This will help, if userB want to fetch his followers, you can write a logic to fetch all objects where following_user_id === userB_ID.

Initially you can think that it will make a lot of fetchs and cost money and data, but this is the recommanded way of doing because it avoid duplicates and the fetched data will usually be up-to-date.

Storing followers ids in an array can seem to be simple but, when your users will get each millions (I wish) followers, you will get an array of million IDs