-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.js
43 lines (38 loc) · 1.01 KB
/
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
42
43
require('dotenv-safe').config()
process.env.NODE_ENV = 'test'
const initMongo = require('./src/config/database')
const fs = require('fs')
const modelsPath = `./src/models`
const { removeExtensionFromFile } = require('./src/middlewares/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(`./src/models/${model}`)
//Deelete all Data in DB
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()