问题
So I was using Parse.com AfterSave cloud code function to keep a list of the most recent Posts relating to a specific location. The reason for this is to get the most recent post for a location without having to search and order the entire list of posts for all locations.
This was nicely done in Parse AfterSave because I didnt have to worry about clients exectuing this command, it made sense for it to run in the cloud. Now that parse is shutting down, I've decided to migrate over to Firebase. Is there an equivilant functionality in Firebase or will my clients need to be maintaining the list of most recent posts?
Thanks
Original question re Parse Get latest record per field in Parse.com JS query
回答1:
There is currently no way to run your code on Firebase's servers. So you will have to come up with a less-direct mapping of Cloud Code to a new solution. Running the functionality on each client is one option, running it on a cheap "bot"/server is another one.
I'll find a few questions where this has been covered before and link them here:
- Firebase and backend logic (recent question that covers using a nodejs server)
- How would I run server-side code in Firebase?
- How do I use Firebase to handle automatic server-side calculations?
- Firebase and indexing/search
- Server Side Calculation using Firebase (which leads to four more questions about this)
One thing to keep in mind is that running a server like this is not the type of "writing a server" that you're immediately thinking of. Most of these act more like a "bot" that interacts with Firebase in the same way as your client-side code does. The only difference in that case is that it runs on an environment you control, so that it can run with elevated credentials.
回答2:
A couple of thoughts; one of which you already mentioned as 'no' but wanted to include it for completeness:
all_messages
message_0
message: blah blah
timestamp: the timestamp
message_1
message: blah blah
timestamp: the timestamp
message_2
message: blah blah
timestamp: the timestamp
If all clients are observing the all_messages node, any time a new message is added, the clients will be notified and can query for the last three messages by timestamp. Pretty simple solution.
Another thought is to have a node that keeps a reference to just the last three messages, reducing overhead and sorting.
all_messages
message_45
message: blah blah
message_50
message: blah blah
message_51
message: blah blah
last_three_messages
last_message_0: message_45
last_message_1: message_50
last_message_2: message_51
When a client writes a message to the all_messages node, write a reference to it in the last_three_messages node and shuffle the older messages down.
With this, clients can just observe the last_three_messages node.
It will take a little client logic to handle pushing the most current message onto the last_message_0 slot and then shuffling the other down but it would only take a few lines of code with minimal overhead.
来源:https://stackoverflow.com/questions/35373221/firebase-aftersave-function-like-parse-com