【发布时间】:2022-12-10 04:06:28
【问题描述】:
我想添加默认情况下在数据库中保留 1 天然后被删除的数据。除非我设置不同的时间(超过一天)。
【问题讨论】:
-
这个问题的细节很少,但我会说你应该看看使用 Firebase 云函数来实现它。没有执行此操作的 RTDB 机制。
-
您可以使用 firebase 云功能来完成该任务。
标签: flutter firebase-realtime-database
我想添加默认情况下在数据库中保留 1 天然后被删除的数据。除非我设置不同的时间(超过一天)。
【问题讨论】:
标签: flutter firebase-realtime-database
您可以创建类似以下方法的内容,您需要将其上传到 Firebase Cloud Function
const CUT_OFF_TIME = 24 * 60 * 60 * 1000; // 2 小时,以毫秒为单位。
exports.deleteOldMessages = functions.database.ref('[path]').onWrite(async (change) => {
const ref = change.after.ref.parent; // reference to the parent
const now = Date.now();
const cutoff = (now - CUT_OFF_TIME) / 1000; // convert to seconds
const oldItemsQuery = ref.orderByChild('seconds').endAt(cutoff);
const snapshot = await oldItemsQuery.once('value');
// create a map with all children that need to be removed
const updates = {};
snapshot.forEach(child => {
updates[child.key] = null;
});
// execute all updates in one go and return the result to end the function
return ref.update(updates);
});
您也可以查看以下链接以供参考。
【讨论】: