-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (35 loc) · 1.13 KB
/
index.js
File metadata and controls
42 lines (35 loc) · 1.13 KB
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
const mongoose = require('mongoose');
const csv = require('fast-csv');
// import environmental variables from our variables.env file
require('dotenv').config({ path: 'variables.env' });
const fs = require('fs');
const stream = fs.createReadStream(process.env.CSVFILEPATH);
// Connect to our Database and handle any bad connections
mongoose.connect(process.env.DATABASE);
mongoose.Promise = global.Promise; // Tell Mongoose to use ES6 promises
mongoose.connection.on('error', (err) => {
console.error(`🙅 🚫 🙅 🚫 🙅 🚫 🙅 🚫 → ${err.message}`);
});
// Member Model
const Member = require('./models/member');
const utils = require('./utils');
const addMember = (data) => {
const member = new Member(data);
member.save((err) => {
if (err) console.log(err);
});
};
csv.fromStream(stream, { headers: true })
.on('data', (data) => {
if (data['email'] !== '') {
data['type'] = utils.convertType(data['type']);
data['account'] = data['email'];
// Default password
data['password'] = 'x';
// console.log(data);
addMember(data);
}
})
.on('end', () => {
console.log('---');
});