-
Notifications
You must be signed in to change notification settings - Fork 19
/
seed-consortia.js
38 lines (35 loc) · 1.02 KB
/
seed-consortia.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
'use strict';
const Consortium = require('coinstac-common').models.Consortium;
const dbRegistryService = require('./db-registry');
const logger = require('./logger.js');
/**
* @module service/seed-consortia
*/
/**
* conditionally seeds consortia db
* @param {object} config server config
* @returns {Promise} resolves to boolean indicating whether seeding happened or not
*/
/* istanbul ignore next */
module.exports = function seedConsortia(config) {
let seedDocs;
if (config.seed && typeof config.seed === 'string') {
try {
seedDocs = JSON.parse(config.seed);
} catch (error) {
return Promise.reject(error);
}
} else {
seedDocs = config.seed;
}
if (!seedDocs || !Array.isArray(seedDocs) || !seedDocs.length) {
return Promise.resolve(false);
}
const consortiaDb = dbRegistryService.get().get('consortia');
return consortiaDb
.bulkDocs(seedDocs.map(seedDoc => new Consortium(seedDoc).serialize()))
.then(() => {
logger.info('Seeded consortia database');
return true;
});
};