-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeder.js
88 lines (70 loc) · 3.07 KB
/
seeder.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { getModel } from './models/db/db'
import { seeds } from './models/db/seeds'
import logger from './logger'
const hasForceSeedParam = () => process.argv.includes('--forceseed')
const hasForceDropParam = () => process.argv.includes('--forcedrop')
const hasDocuments = async (model) => await model.countDocuments()
const dropCollection = async (model, collectionName) => {
logger.info(`Dropping ${collectionName} collection.`)
await model.drop()
}
// eslint-disable-next-line func-style
async function saveChildren (document) {
const savedChildren = await Promise
.all(Object
.keys(document)
.filter(key => typeof document[key] === 'object')
.filter(key => document[key].hasOwnProperty('entity'))
.map(async (key) => ({ key, value: await workOnCollection(document[key])})))
if (!savedChildren.length) return document
const documentCopy = {...document}
savedChildren.forEach((prop) => {
documentCopy[prop.key] = prop.value
})
return documentCopy
}
// eslint-disable-next-line func-style
async function insertCollection (model, collectionName, documents) {
logger.info(`Adding ${documents.length} to ${collectionName} collection.`)
try {
const documentWithChildrenId = await Promise.all(documents.map(saveChildren))
const insertedDocs = await model.insertMany(documentWithChildrenId)
logger.info(`A total of ${insertedDocs.length} had been added to ${collectionName} collection.`)
return Promise.resolve(insertedDocs.map(item => item._doc))
} catch (e) {
return Promise.reject(e)
}
}
// eslint-disable-next-line func-style
async function workOnCollection (collection) {
logger.info(`Seeding ${collection.entity.model} collection...`)
try {
const model = await getModel(collection.entity)
if (await hasDocuments(model)) {
if (!hasForceSeedParam()) {
logger.info(`Not seeding ${collection.entity.model} collection. It already contains documents. (If you want it so, run 'npm run [task] -- --forceseed')`)
return Promise.resolve()
}
if (hasForceDropParam()) {
await dropCollection(model, collection.entity.model)
} else {
logger.info(`Not dropping ${collection.entity.model} collection. (If you want it so, run 'npm run [task] -- --forcedrop')`)
}
}
const result = await insertCollection(model, collection.entity.model, collection.data)
const newCount = await hasDocuments(model)
logger.info(`The ${collection.entity.model} collection now has ${newCount} documents.\n`)
return Promise.resolve(result._docs ? result._docs.map(({_id}) => _id) : result)
} catch (e) {
logger.error(e)
return Promise.reject(e)
}
}
module.exports = () => {
logger.info('Seeder started\n')
Promise
.all(seeds.map(workOnCollection))
.then(() => logger.info('Seed completed'))
.catch((err) => logger.error(err))
.finally(() => process.exit())
}