validate-data-of-model.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const { getNextBatch } = require('./helpers/batchedUpdate')
  2. const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
  3. const MODEL_NAME = process.argv.pop()
  4. const Model = require(`../app/src/models/${MODEL_NAME}`)[MODEL_NAME]
  5. function processBatch(batch) {
  6. for (const doc of batch) {
  7. const error = new Model(doc).validateSync()
  8. if (error) {
  9. const { errors } = error
  10. console.log(JSON.stringify({ _id: doc._id, errors }))
  11. }
  12. }
  13. }
  14. async function main() {
  15. await waitForDb()
  16. const collection = db[Model.collection.name]
  17. const query = {}
  18. const projection = {}
  19. let nextBatch
  20. let processed = 0
  21. let maxId
  22. while (
  23. (nextBatch = await getNextBatch(collection, query, maxId, projection))
  24. .length
  25. ) {
  26. processBatch(nextBatch)
  27. maxId = nextBatch[nextBatch.length - 1]._id
  28. processed += nextBatch.length
  29. console.error(maxId, processed)
  30. }
  31. console.error('done')
  32. }
  33. main()
  34. .then(() => {
  35. process.exit(0)
  36. })
  37. .catch(error => {
  38. console.error({ error })
  39. process.exit(1)
  40. })