简体   繁体   中英

Creating expiration functionality with firebase realtime database and firebase functions?

Short general question: Is there a listener (either with firebase realtime, functions, or generally) I can set on a date stored in a firebase realtime object, that deletes the object when the date expires?

Long specific question: I'm working with firebase realtime database and firebase functions to build a market place app where user's can bid on products, but those bids will expire on a date stored in the app. The user can also search/filter products by highest or lowest bid, I'm using algolia for full text search and type filtering. In order to minimize cost I structured my db like this:

bids {expirationDate, productID, ... etc.} products {highestBidID, highestBidCost, lowestBidID, lowestBidCost, ... etc.} (algolia models have similar structure for all searchable/filterable variables)

The issue I'm running into is how do I delete expired bids and then update the product model and aloglia model if the bid contains the stored value.

The only way (given the database I have set up) I can think of is to use some kind of listener on the bid items and if the bid expires and the bid was the highest or lowest bid then call another function that updates the product model and the algolia model to handle with the next highest/lowest bid respectively.

I am strongly against just updating the highest/lowest bid every time I pull a product as it will cause a lot of extra calls, run up the charge and slow down everything down. I will also have to remove the highest/lowest bids from the algolia models and do that filtering only once a list is returned from algolia again drastically increasing the number of calls.

Does this kind of listener exist? Is there a better way to do this?

Is there a listener (either with firebase realtime, functions, or generally) I can set on a date stored in a firebase realtime object, that deletes the object when the date expires?

No, there is not a simple way to do this.

You will have to write code to arrange for deletion of the node using some other mechanism or product. I've written a blog that describes how to expire (TTL) Firestore documents using Cloud Tasks as a mechanism to schedule the deletion . The same could be applied to nodes in Realtime Database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM