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?
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); } }); });
Comments
Post a Comment