【问题标题】:Google Firestore retention policyGoogle Firestore 保留政策
【发布时间】:2020-11-13 09:05:31
【问题描述】:

我正在使用 Google firestore 作为我的 nodejs api 的 noSql 数据库。我想知道是否有办法为数据配置保留策略。我想自动删除旧文档,例如删除 30 天前的旧文档。

谢谢

【问题讨论】:

  • 你必须自己做。您可以使用 Firebase 函数或等效函数来执行此操作。

标签: node.js google-cloud-platform google-cloud-firestore


【解决方案1】:

对此没有配置或设置。您将不得不安排一些其他过程来查询和删除符合删除条件的文档。这可以通过scheduled function 来实现,例如,您为自己编写。或者你可以use Cloud Tasks to schedule individual document deletes

【讨论】:

  • 感谢您的回答。我希望有一个简单的配置可以像在 Azure 上那样做。我会为此编写一个预定函数
【解决方案2】:

这是一个可以从以下开始的 sn-p:

export default FirebaseFunctions.pubsub.schedule('every 1 hours').onRun(async (context) => {
  const now = Date.now()
  const DAYS = 1000 * 60 * 60 * 24
  const batchSize = 500

  // Delete events older than 30 DAYS
  const events = await FirebaseFirestore.collection('events')
    .where('created_at', '<', now - 30 * DAYS)
    .limit(batchSize)
    .get()

  console.log(`Found ${events.size} events`)

  if (events.size > 0) {
    await deleteBatch(events.docs)
    console.log(`Deleted ${events.size} events`)
    return // stop execution here
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    相关资源
    最近更新 更多