validate-data-of-model.mjs 732 B

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