-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulatedb.js
90 lines (81 loc) · 2.1 KB
/
populatedb.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
89
90
#! /usr/bin/env node
console.log(
'This script populates some test issues to the database. Specified database as argument - e.g.: >sudo node populatedb mongodb+srv://<username>:<password>@cluster0-mbdj7.mongodb.net/<db-name>?retryWrites=true'
);
// Get arguments passed on command line
var userArgs = process.argv.slice(2);
/*
if (!userArgs[0].startsWith('mongodb')) {
console.log('ERROR: You need to specify a valid mongodb URL as the first argument');
return
}
*/
var async = require('async');
var Issue = require('./models/issue');
var mongoose = require('mongoose');
var mongoDB = userArgs[0];
mongoose.connect(mongoDB, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
var issues = [];
function issueCreate(title, description, cb) {
issuedetail = {
title: title,
description: description,
};
var issue = new Issue(issuedetail);
issue.save(function(err) {
if (err) {
cb(err, null);
return;
}
console.log('New Issue: ' + issue);
issues.push(issue);
cb(null, issue);
});
}
function createIssues(cb) {
async.series(
[
function(callback) {
issueCreate('Issue10', 'bug in machine', callback);
},
function(callback) {
issueCreate('Issue2', 'bug in coffee', callback);
},
function(callback) {
issueCreate('AC out', 'ac has stopped working', callback);
},
function(callback) {
issueCreate(
'process improvement',
'update change management procedures',
callback
);
},
function(callback) {
issueCreate('Issue1', 'racoon in garage', callback);
},
],
// optional callback
cb
);
}
async.series(
[createIssues],
// Optional callback
function(err, results) {
if (err) {
console.log('FINAL ERR: ' + err);
} else {
console.log('Issues: ' + issues);
}
// All done, disconnect from database
mongoose.connection.close();
}
);