简体   繁体   中英

Firebase Database Architecture - Snapchat like

I want to create an app with Firebase.

I have two main targets for this app:

  1. Get Friend relationship
  2. Send image to friend

In my Firestore database, I have "users" collection with user ID as document and id & email in field. (users > id > id, email). When a user registers, I send informations in database.

        db.collection("users").document(currentUser.uid).setData([
            "id": currentUser.uid,
            "email": currentUser.email ?? ""
        ]) { err in
            if let err = err {
                print("Error writing document: \(err)")
            } else {
                print("Document successfully written!")
            }
        }

I think if I want to take care of friend relationship, I have to add collection inside document user (users > id > id, email (fields)... friendsRelationship (collection) > id (users added as field)).

And to add this in the other user document id user (targetedUser) --> bidirectional.

When some one user delete the other user, we have to delete the id in friendsRelationship collection from currentUser and targetedUser.

Is there any other solution / better database architecture to use for friend relationship in Firebase?

When friend relationship is completed, we have to send picture to targetedUser.

I'd rather do users > id > images (collection) > url, date (fields)... or images > id (generated) > fromUser (id), toUserId (collection) > id?

How to query all images that current User has received? Like db.collection("users").document(currentUser.uid)...

Here is a screenshot of my Cloud Firestore database: https://imgur.com/4bGJOoP

I don't know how to manage friend relationship with this, and post like snapshat, when firstUser sends a snap to other user.

Thanks for your advice!

I think if I want to take care of friend relationship, I have to add collection inside document user (users > id > id, email (fields)... friendsRelationship (collection) > id (users added as field)).

The way you are storing user data looks good to me.

Is there any other solution / better database architecture to use for friend relationship in Firebase?

Regarding the firends relationship, there is another possible solution which doesn't require the creation of a subcollection, you can simply add every friend id in an array in user document. The new schema might look like this:

Firestore-root
   |
   --- users
        |
        --- uid
             |
             --- email: "name@email.com"
             |
             --- id: "userId"
             |
             --- friendIds: ["friendIdOne", "friendIdTwo", "friendIdThree"]

This will work perfectly fine only if the size of the document it will not reach the limit. So there are some limits when it comes to how much data you can put into a document. According to the official documentation regarding usage and limits :

Maximum size for a document: 1 MiB (1,048,576 bytes)

As you can see, you are limited to 1 MiB total of data in a single document. When we are talking about storing text, you can store pretty much.

If you think that the size of the document might get over that limit, then your solution of storing firends as documents in a separate collection will solve your problem.

And to add this in the other user document id user (targetedUser) --> bidirectional.

To have a bidirectional relationship, in the solution above, you should simply copy the friendId in user's friendIds array and userId in friend's friendIds array.

When some one user delete the other user, we have to delete the id in friendsRelationship collection from currentUser and targetedUser.

In the solution above, you should delete the userId from friendIds array and the friendId from user's friendIds array.

I'd rather do users > id > images (collection) > url, date (fields)... or images > id (generated) > fromUser (id), toUserId (collection) > id?

You can also use in this case an array for storing image references. Again, if you think that the size of the document might get over that limit, the solution of storing images as documents in a separate collection is the solution to go ahead with.

How to query all images that current User has received? Like db.collection("users").document(currentUser.uid)...

Yes, by getting a reference to user document:

db.collection("users").document(currentUser.uid)

And then reading the images array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM