helpers.js 723 B

1234567891011121314151617181920212223242526
  1. const { getCollectionNames } = require('../../app/src/infrastructure/mongodb')
  2. async function addIndexesToCollection(collection, indexes) {
  3. return Promise.all(
  4. indexes.map(index => {
  5. index.background = true
  6. return collection.createIndex(index.key, index)
  7. })
  8. )
  9. }
  10. async function dropIndexesFromCollection(collection, indexes) {
  11. return Promise.all(indexes.map(index => collection.dropIndex(index.name)))
  12. }
  13. async function dropCollection(db, collectionName) {
  14. const allCollections = await getCollectionNames()
  15. if (!allCollections.includes(collectionName)) return
  16. return db[collectionName].drop()
  17. }
  18. module.exports = {
  19. addIndexesToCollection,
  20. dropIndexesFromCollection,
  21. dropCollection,
  22. }