0

I'm currently working with a Supabase database that comprises several tables, including 'users' (for user metadata), 'network' (for user relationships like followers, following, and premium followers), 'image_posts,' 'video_posts,' and 'audio_posts' for different types of user-generated content.

The 'network' table has the following structure:

Table network {
  id int PK
  user_id uuid FK
  follower_id uuid FK
  connection_type Text // e.g., follower, premium_follower, blocked
  is_accepted bool
  sent_on TIMESTAMP
}

The all 'post' tables has the following structure:

Table image_posts {
  id int PK
  user_id uuid FK
  post_type image
  content image_url
  caption TEXT
  reach TEXT // is this post for followers or premium_followers
  created_on TIMESTAMP
}

My goal is to fetch posts from the 'image_posts,' 'video_posts,' and 'audio_posts' tables based on whether a specific user ('USER A') is following another user. Additionally, I need to consider the 'reach' condition in the post table and the 'connection_type' in the 'network' table.

For example, if a user is a premium follower and not blocked, they should be able to retrieve all the premium posts from all the post tables of the users they are following. This scenario is akin to a social media platform.

I attempted to retrieve the posts using the supabase.from('image_posts', 'audio_posts', 'video_posts').select method, but I'm uncertain if this is the most efficient approach.

Here are my questions:

  1. Should I contemplate merging all the post-related tables into a single 'posts' table to simplify queries and data management?
  2. Alternatively, is it better to maintain separate tables for each post type, considering my specific use case and migration from Firebase?

Currently Using this:

  Future<void> fetchAllPost() async {
    try {
      await supabase
          .from('network')
          .select('user_id')
          .eq(
            'follower_id',
            currentUuid,
          )
          .eq(
            'is_accepted',
            true,
          )
          .then(
        (value) async {
          for (var user in value) {
            final postData = await supabase
                .from('users')
                .select(
                  'first_name,last_name, image_posts(*), audio_posts(*)',
                )
                .eq(
                  'user_id',
                  user['user_id'],
                );

            print(postData);
          }
        },
      );

      //

      update();
    } catch (e) {
      if (kDebugMode) {
        print(e);
      }
    }
  }

I greatly appreciate your guidance, especially as I transition from Firebase to Supabase. Thank You!

1
  • You stated "Flutter Supabase" in the title. Is Supabase really part of Flutter? It seems to me that by this you are misleading those who will answer your question. Commented Oct 6, 2023 at 20:13

1 Answer 1

0

Merging Tables:

Yes, if you prioritize simpler queries and unified data management, especially if attributes and interactions with all post types are similar.

Maintaining Separate Tables:

Yes, if different post types have numerous unique attributes or if you expect to query and manage them independently most of the time, ensuring optimized performance and cleaner organization per type. This might be particularly relevant if migrating from a non-relational system like Firebase to maintain a similar data segregation.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey @Mike, first off, sorry if my English sounds a bit formal, Spanish is my first language. I've got some questions about your answer: You mentioned merging tables could simplify queries. Do you think it would also improve performance? If Raj A keeps the tables separate, how would you suggest optimizing the queries, especially when considering the 'reach' and 'connection_type' conditions? Any tips for Raj A as he transitions from Firebase to Supabase? Anything he should watch out for?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.