-
Notifications
You must be signed in to change notification settings - Fork 0
/
populatedb.js
80 lines (67 loc) · 2.18 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
#! /usr/bin/env node
console.log('This script populates some test books, authors, genres and bookinstances to your database. Specified database as argument - e.g.: populatedb mongodb+srv://cooluser:coolpassword@cluster0-mbdj7.mongodb.net/local_library?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 Task = require('./models/task');
var mongoose = require('mongoose');
var mongoDB = userArgs[0];
mongoose.connect(mongoDB, { useNewUrlParser: true });
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
var tasks = [];
function taskCreate(name, importance, urgency, progress, cb) {
taskdetail = { name: name, importance: importance, urgency: urgency, progress: progress }
var task = new Task(taskdetail);
task.save(function (err) {
if (err) {
cb(err, null)
return
}
console.log('New Task: ' + task);
tasks.push(task)
cb(null, task)
});
}
function createTasks(cb) {
async.series([
function (callback) {
taskCreate('Create', 1, 5, 0, callback);
},
function (callback) {
taskCreate('Read', 2, 4, 20, callback);
},
function (callback) {
taskCreate('Delete', 3, 3, 40, callback);
},
function (callback) {
taskCreate('Update', 4, 2, 60, callback);
},
function (callback) {
taskCreate('Love', 5, 1, 80, callback);
},
],
// optional callback
cb);
}
async.series([
createTasks
],
// Optional callback
function (err, results) {
if (err) {
console.log('FINAL ERR: ' + err);
}
else {
console.log('TASKInstances: ' + tasks);
}
// All done, disconnect from database
mongoose.connection.close();
});