Optimising Firebase database by automatically overwriting data -


in firebase app have following structure:

  • posts
    • latest

all users can write posts > latest. want limit entries 20 posts save space in database. , posts beyond 20 unnecessary never shown on homepage.

how can limit posts 20, when user writes posts > latest posts @ end drop off (be deleted) automatically?

this sample should you.

you need :

const max_log_count = 20;  exports.removeold = functions.database.ref('/posts/latest/{postid}').oncreate(event => {     const parentref = event.data.ref.parent;      return parentref.once('value').then(snapshot => {         if (snapshot.numchildren() >= max_log_count) {             let childcount = 0;              const updates = {};              snapshot.foreach(function(child) {                 if (++childcount <= snapshot.numchildren() - max_log_count) {                     updates[child.key] = null;                 }             });              // update parent. removes children.             return parentref.update(updates);         }     }); }); 

you can find cloud functions firebase samples here.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -