2022-01-10 06:36:18 -05:00
|
|
|
const {
|
|
|
|
db,
|
|
|
|
getCollectionNames,
|
|
|
|
getCollectionInternal,
|
|
|
|
waitForDb,
|
|
|
|
} = require('../../app/src/infrastructure/mongodb')
|
2021-07-21 06:34:09 -04:00
|
|
|
|
|
|
|
async function addIndexesToCollection(collection, indexes) {
|
|
|
|
return Promise.all(
|
|
|
|
indexes.map(index => {
|
|
|
|
index.background = true
|
|
|
|
return collection.createIndex(index.key, index)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function dropIndexesFromCollection(collection, indexes) {
|
|
|
|
return Promise.all(indexes.map(index => collection.dropIndex(index.name)))
|
|
|
|
}
|
|
|
|
|
2022-01-10 06:36:18 -05:00
|
|
|
async function dropCollection(collectionName) {
|
|
|
|
await waitForDb()
|
|
|
|
if (db[collectionName]) {
|
|
|
|
throw new Error(`blocking drop of an active collection: ${collectionName}`)
|
|
|
|
}
|
|
|
|
|
2021-07-21 06:34:09 -04:00
|
|
|
const allCollections = await getCollectionNames()
|
|
|
|
if (!allCollections.includes(collectionName)) return
|
2022-01-10 06:36:18 -05:00
|
|
|
const collection = await getCollectionInternal(collectionName)
|
|
|
|
await collection.drop()
|
2021-07-21 06:34:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
addIndexesToCollection,
|
|
|
|
dropIndexesFromCollection,
|
|
|
|
dropCollection,
|
|
|
|
}
|