Objective : Limiting signedIn users to be able to write only 'N' times per day in firebase realtime database.
Schema : Tree of the database:
Condition Value : Object dipt contains now
key and document-name is the date & time when a user wrote in database via the application.
dipt:
{
ip: "",
now: "Tuesday, September 29th, 2020, 11:48:00 PM",
reg: ""
}
Problem Statement : Figure out a logic that isn't very resource intensive. I mean, I can get all search-data > Objs of a signedIn user and use foreach to make comparisons from now()
and count to the value of 'N' but you & I, we both know that isn't going to be just very resource intensive but also a real pain in rear.
So, there is gotta be a simpler way of doing this. Any help, or any idea of doing this differently, or may be any past experience of dealing with such a scenario is highly appreciated. I look forward to hear from stack overflow geniuses and Firebase-Angular gurus.
After gaining the insight of developing a counter from Puf, I wrote a cloud function to limit writing signedIn users into firebase realtime database for N time per day.
Export this function with your function name and initialize following:
This tree will look something like this:
After setting up appropriate read/write rules for your realtime database, everytime a signedIn user who writes on a same date (remember we are taking date from server and not from user) will going to write under today's date and with a
now()
timestamp and updates the counter value.It's important to note here that I am using
.update()
even for the first time where there is no counter created. Please read the difference between.set()
&.update()
here.I am no pro, but that is how I have written my counter. Feel free to let me know if there is more efficient way to achieve similar results.