-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.js
41 lines (36 loc) · 970 Bytes
/
clean.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require('dotenv-safe').config()
const initMongo = require('./config/mongo')
const fs = require('fs')
const modelsPath = `./app/models`
const { removeExtensionFromFile } = require('./app/middleware/utils')
initMongo()
// Loop models path and loads every file as a model except index file
const models = fs.readdirSync(modelsPath).filter((file) => {
return removeExtensionFromFile(file) !== 'index'
})
const deleteModelFromDB = (model) => {
return new Promise((resolve, reject) => {
model = require(`./app/models/${model}`)
model.deleteMany({}, (err, row) => {
if (err) {
reject(err)
} else {
resolve(row)
}
})
})
}
const clean = async () => {
try {
const promiseArray = models.map(
async (model) => await deleteModelFromDB(model)
)
await Promise.all(promiseArray)
console.log('Cleanup complete!')
process.exit(0)
} catch (err) {
console.log(err)
process.exit(0)
}
}
clean()