iosfirebasekinveymbaasnosql

firebase google or query support or any alternative solution


We have just started exploring firebase for our new app. The app basically needs chat between users and Group chat.

For the chat application we are creating: MessageThread(id, User:userA, User:userB, UserGroup:group, Array:deletedBy, ...)

The main problem is firebase doesn't support OR queries and IN queries, which I need in my use-case. For example:

Dummy query to access user's conversations list: fetch MessageThread(s) where (currentUser.id==userA.id) OR (currentUser.id==userB.id) OR (currentUser.id IN group.members)

I am missing something to understand Firebase? What is the other alternative way to design our database to solve this problem?


Solution

  • In NoSQL you will have to model the data for the way your app wants to use it. So if you need a list of the conversations that the user is part of, you should keep that list in your database:

    /conversationMessages
        $conversationId
            $messageId
                text: "Hello world"
                sender: "uid of sender"
                name: "name of sender"
    /userConversations
       $uid
         $conversationId1: true
         $conversationId2: true
    

    With this you can easily look up the list of conversations that each user is a part of and then load the messages for one of them. You'll probably also want to keep a separate list of the metadata for each conversation:

    /conversationMetadata
        $conversationId
            lastUpdated: timestamp (set with ServerValue.TIMESTAMP)
            title: "name of our chat room"
    

    That way you can show the list of conversations for a user by loading their conversation ids and then loading the metadata of each conversation:

    var ref = database.ref().child("userConversations").child(auth.uid);
    ref.on('child_added', function(conversationKey) {
        var conversationRef = database.ref().child("conversationMetadata").child(conversationKey.key);
        conversationRef.once('value', function(metadataSnapshot) {
          console.log(metadataSnapshot.val());
        });
    });
    

    Trying to map SQL knowledge onto a NoSQL database leads to a difficult learning experience. I highly recommend reading the relevant section of the Firebase documentation and this article about NoSQL data modeling.