clear_feedback_collection.mjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* Clear feedback collection before a cutoff date
  2. *
  3. * Usage
  4. * node scripts/clear_feedback_collection.js 2022-11-01 # dry run mode
  5. * DRY_RUN=false node scripts/clear_feedback_collection.js 2022-11-01 # deletion mode
  6. */
  7. import { db, ObjectId, waitForDb } from '../app/src/infrastructure/mongodb.js'
  8. import { fileURLToPath } from 'url'
  9. const runScript = async (timestamp, dryRun) => {
  10. await waitForDb()
  11. const t = new Date(timestamp)
  12. if (isNaN(t)) {
  13. throw new Error('invalid date ' + timestamp)
  14. }
  15. const cutoffId = ObjectId.createFromTime(t / 1000)
  16. console.log('deleting all feedback entries before', t, '=>', cutoffId)
  17. const cursor = db.feedbacks.find({ _id: { $lt: cutoffId } })
  18. for await (const entry of cursor) {
  19. console.log('deleting', entry._id)
  20. if (dryRun) {
  21. console.log('skipping in dry run mode')
  22. continue
  23. }
  24. await db.feedbacks.deleteOne({ _id: entry._id })
  25. }
  26. }
  27. if (fileURLToPath(import.meta.url) === process.argv[1]) {
  28. // we are in the root module, which means that we're running as a script
  29. const timestamp = process.env.CUTOFF_TIMESTAMP || process.argv[2]
  30. const dryRun = process.env.DRY_RUN !== 'false'
  31. runScript(timestamp, dryRun)
  32. .then(() => process.exit())
  33. .catch(err => {
  34. console.error(err)
  35. process.exit(1)
  36. })
  37. }
  38. export default runScript