helpers.js 955 B

12345678910111213141516171819202122232425262728293031323334353637
  1. const {
  2. db,
  3. getCollectionNames,
  4. getCollectionInternal,
  5. waitForDb,
  6. } = require('../../app/src/infrastructure/mongodb')
  7. async function addIndexesToCollection(collection, indexes) {
  8. return Promise.all(
  9. indexes.map(index => {
  10. index.background = true
  11. return collection.createIndex(index.key, index)
  12. })
  13. )
  14. }
  15. async function dropIndexesFromCollection(collection, indexes) {
  16. return Promise.all(indexes.map(index => collection.dropIndex(index.name)))
  17. }
  18. async function dropCollection(collectionName) {
  19. await waitForDb()
  20. if (db[collectionName]) {
  21. throw new Error(`blocking drop of an active collection: ${collectionName}`)
  22. }
  23. const allCollections = await getCollectionNames()
  24. if (!allCollections.includes(collectionName)) return
  25. const collection = await getCollectionInternal(collectionName)
  26. await collection.drop()
  27. }
  28. module.exports = {
  29. addIndexesToCollection,
  30. dropIndexesFromCollection,
  31. dropCollection,
  32. }