validate-data-of-model.mjs 664 B

123456789101112131415161718192021222324252627
  1. import { batchedUpdateWithResultHandling } from '@overleaf/mongo-utils/batchedUpdate.js'
  2. import { db } from '../app/src/infrastructure/mongodb.mjs'
  3. const MODEL_NAME = process.argv.pop()
  4. const { [MODEL_NAME]: Model } = await import(
  5. `../app/src/models/${MODEL_NAME}.mjs`
  6. )
  7. function processBatch(batch) {
  8. for (const doc of batch) {
  9. const error = new Model(doc).validateSync()
  10. if (error) {
  11. const { errors } = error
  12. console.log(JSON.stringify({ _id: doc._id, errors }))
  13. }
  14. }
  15. }
  16. batchedUpdateWithResultHandling(
  17. db[Model.collection.name],
  18. {},
  19. async nextBatch => {
  20. processBatch(nextBatch)
  21. },
  22. {} // fetch the entire record
  23. )