validate-data-of-model.mjs 687 B

1234567891011121314151617181920212223242526272829
  1. import BatchedUpdateModule from './helpers/batchedUpdate.mjs'
  2. const { batchedUpdateWithResultHandling } = BatchedUpdateModule
  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. Model.collection.name,
  19. {},
  20. async nextBatch => {
  21. await processBatch(nextBatch)
  22. },
  23. {}
  24. )