0

My database structure looks like this

{
    "user_notifications": {
      "user1": {
       "location1": {
       "name": "Nice location",
       "postCounter": "6",
       "newPosts": "true"
       },  

       "location2": {
       "name": "Another location",
       "postCounter": "0",
       "newPosts": "false"
       }
      }
    },

    "location_users": {
      "location1": {
         "user1": "true",
         "user2": "true"            
        },

         "location2": {
         "user1": "true"
        }
    }
}

I think that new notification for any new action in location for every user will be bad idea because it will be like 20 megabytes of data for a location with 100,000 subscribers. So I want to store just one instance of location notification for one user that will be containing location name, new posts counter and boolean value that will be informing user about new posts when he enter his notifications screen.

The question is: How to update every user notification about certain location when new post will be added to this location. I mean raise the post counter, set newPosts value to true.

I found Frank van Puffelen's post (https://stackoverflow.com/a/30699277/8437130) about Transactional updates, modified it and I've got something like this:

function updateNotifications(ref, locationId) {
  var updates = {};
  var query = ref.child('location_users'+'/'+locationId);
  query.once('value', function(snapshot) {
    snapshot.forEach(function(userSnapshot) {
      updates['user_notifications/'+userSnapshot.(don't know how to get user id from there)+'/'+locationId+'/postCounter'] = (don't know how to raise the counter by 1);
      updates['user_notifications/'+userSnapshot.(don't know how to get user id from there)+'/'+locationId+'/newPosts'] = true;
    })
    ref.update(updates);
  });
}

Still I don't know if that is the right way and don't know how to make it work.

dojei
  • 13
  • 3
  • Aside from that: it seems you're looking for `userSnapshot.key`, but you say in your code that's certainly not it. Why not? – Frank van Puffelen Aug 16 '17 at 14:10
  • Link added. I don't have working code for now. I'm thinking about my database structure first. userSnapshot.key will just return true or I'm wrong? Could you help me with increasing counter number? Is it possible? – dojei Aug 16 '17 at 14:42
  • Oh, it may be .key but I just messed up. Still question about increasing counter remains – dojei Aug 16 '17 at 14:59
  • There is no built-in counter mechanism. You can use a transaction, but that would pull the transaction up to the root of your database, probably reducing scalability to zero. Alternatively you could do some elaborate security rules validation of the delta. See my answer here for more: https://stackoverflow.com/questions/37954217/is-the-way-the-firebase-database-quickstart-handles-counts-secure/37956590#37956590 – Frank van Puffelen Aug 16 '17 at 17:15
  • Ok, I will look for solution. Thank you – dojei Aug 16 '17 at 17:53

0 Answers0