How to structure Firestore database for followers/following - database

I have an app where users can follow each other and I currently have a database structure which allows users to follow, unfollow and see their followers/following users, and see the number of followers/following they have.
The way I have structured my database is by having a users document stored in a usernames collections which contains their followers/following count, and all their info. Like profile image and display name etc.
Then, this users followers/following users are stored in a subcollection as a document. In this document, there is a document reference which points back to the root usernames collection, and to the following/followers users document, where i then pull their info if needed.
I was just wandering if there is a more efficient way to structure my database. This seems like the best option but I have a feeling their is an even better way which i am not thinking of.
Thank you

Related

FaunaDB, make connection between different users, I.e. become friends

I have a use case where I want to connect two different user roles, and if they accept and want to connect, new features will open up. It is very similar to how friend requests work at Facebook or LinkedIn, opening up and showing more content. Let's call them role1 and role2.
All users are stored within a "users" collection with an id. Depending on their provided role within the document attached to the "users" collection, they can store additional data in their respective role-collection, i.e., role1 collection or role2 collection.
What is the best approach and structure to connect the two users, i.e., become "friends"? Should I have the connection stored in a new collection, named perhaps connections-collection, or multiple collections?
I'm using Next.js, NextAuth for user authentication, and FaunaDB as a database. I'm using Fauna's query language, FQL.
Have you perhaps seen fireship's video RE: fauna db? I think it covers what you want to do and how you can proceed.
Edit: There are many ways to implement this. Based on my understanding, perhaps you can have "Friends" and "Requests" arrays stored under a user document. That way you can differentiate between confirmed friends or a just request.
Example: When user1 initiates a friend request with user2, you store user1's ref under "Requests" of user2's document. When user2 confirms, you move user1's ref to the "Friends" array.
This is just a overly simplified idea and you may need to consider your options and the implications. You would need to plan and define the predicate in both roles so you would only see what is necessary.

Saving "likes" database design (Parse)

Im using Parse to save data from my social network. So far I have three class: Users, Posts, and Relationships. I want the user to be able to like a post.
Should I create a new table for Likes. If so, then on my storyboard page I would have to query through Relationships to get the user followers, then Posts to get the posts from the followers, and then Likes to get the likes from those posts?
Is it efficient to have three API requests to parse on one page. I feel like this will slow down performance but I'm not sure how else to save likes.
Another thing is, I would like to display a notifications tableView. So all likes and requested follows. So Im guessing I would save likes in Relationships and just query through it twice on the storyboard to first get followers, then likes. And on the notification page, have one class to query though once to get all recent notifications.
What are your suggestions?
Thanks.
This is sort of a broad question so there is no way that I could say for sure, but I do have a couple suggestion that you can do with parse.com.
Use the local datastore: You can save all of the likes that the user has in the local datastore as well as in the cloud of your application. So, for instance, you create the like when the user likes something, save it to the cloud, and then pin it to the local datastore. That way, you can efficiently query all of the likes that your user has created without using an API request. But in the event the user logs into the app somewhere else, you also have the likes in the cloud to retrieve. So, I would create a new table for likes.
You could use a join table to implement the followers, so, you could also pin the followers of the user to the local datastore like you would for a like. This is like is done in the Parse.com anypic tutorial.
I would also have another table for notifications. In cloud code, you could even update how many notifications the user has with an afterSave method, and then get all the notifications through a query when requested by the user.

Parse.com - How should I structure this data?

I have a music based app I'm building using Parse for the back end. I'm wondering how I should structure my data when it comes to user identities:
A user can be a regular user, and also a musician, and also a venue owner, etc.
So the idea is that a user can have different identities.
I would love your help on how to best structure this data.
You can have a role column in your user table, assign the role based on the user. When the user logs in to the app, you will read his role and based on the role, the ui will be populated. Is this what you are looking for?

How "Follow" button works in Graph DB

I am using Titan Graph DB. I want to implement "follow" button in my page i.e I a page update something, it should be know to all follower. I dont know how does this follow mechanism work.
In social networking when we follow something we keep getting all updates from that page. How does it work? What is the idea behind implementing this in Graph DB.
Suppose I am following a page XYZ and there happend one update on XYZ. then how does it sends its update info to all followers.
Is it something that it will store update info for all individual
user in graph DB
OR Is it like when a user login it will check all page that it follow
for any new update?
OR something else
The process to trigger the notification has nothing todo with the underlying database you use. This is part of your business logic, how you design your application and how you notify all the listeners about the change.
A graph database will give you the opportunity to store the information about your users and theire tweets, or your sites and the follower in a more natural and semantic way.
In a graph database, you can store your Persons, e.g. John and Doe as Vertices and a relation or edge between them, labeled with follows. In your SQL database, you would store them in a separate table plus a table for your joins to store all the followers.
If you now have a new tweet, you have to join your tables in your SQL table to find all followers to notify. In a graph database, you just have to check the incoming edges on the person who wrote the tweet. Also, in the graph database, you could store the tweet as well, where an extra edge from the person who wrote it goes to the tweet, to have all the necessary informations for your notification at one place, instead of lots of lookups in SQL tables.

Using themoviedatabase.org's database to fill my own database, best practices

I'm building a site where I want to allow users to keep wishlists of movies they want to see and movies they already have seen. To do this I want to use data from the movie tmdb, but I'm not sure how to handle this.
What if a user comes on my site and enters the query 'Batman', what is the next step I should take?
Search my own database for 'Batman'
Search API for 'Batman'
Merge results from own database and external and print, but don't save anything to my db
If a user then clicks on a result that's not in my database I would do another request to the API for the more detailed information, also saving images and so on before showing it to the user.
Is this the way I should go about this or is there a better way?
You should browse API for this movie. Data in TMDB Api is often changing, so I suggest you not to store it for a long time in your db.

Resources