/
12-populate-members-table-from-subscribers.js
50 lines (43 loc) · 1.44 KB
/
12-populate-members-table-from-subscribers.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
const ObjectId = require('bson-objectid');
const _ = require('lodash');
const common = require('../../../../lib/common');
module.exports.config = {
transaction: true,
irreversible: true
};
module.exports.up = (options) => {
const localOptions = _.merge({
context: {internal: true},
migrating: true
}, options);
const memberAttrs = [
'name',
'email',
'created_at',
'created_by',
'updated_at',
'updated_by'
];
return localOptions.transacting('subscribers').select()
.then((subscribers) => {
if (subscribers && subscribers.length > 0) {
common.logging.info(`Adding ${subscribers.length} entries to members`);
let members = _.map(subscribers, (subscriber) => {
let member = memberAttrs.reduce(function (obj, prop) {
return Object.assign(obj, {
[prop]: subscriber[prop]
});
}, {});
member.id = ObjectId.generate();
return member;
});
return localOptions.transacting('members').insert(members);
} else {
common.logging.info('Skipping populating members table: found 0 subscribers');
return Promise.resolve();
}
});
};
module.exports.down = () => {
return Promise.reject();
};