-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopulatedb.js
88 lines (73 loc) · 2.2 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
#! /usr/bin/env node
//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 jSon = require('./app/models/jsonModel')
var mongoose = require('mongoose');
var mongoDB = userArgs[0];
mongoose.connect(mongoDB, {
useMongoClient: true
});
mongoose.Promise = global.Promise;
var db = mongoose.connection;
mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection error:'));
var jsons = []
function randomString() {
var result = '';
chars = ['1','2','3','4','5','a','b','c','d','e'];
for (var i = 4; i > 0; --i){
result += chars[Math.floor(Math.random() * chars.length)];
}
return result;
}
function jsonCreate(id, data, cb) {
var json = new jSon({ jsonid: id, data: data});
json.save(function (err) {
if (err) {
cb(err, null);
return;
}
console.log('New json: ' + json);
jsons.push(json)
cb(null, json);
} );
}
function createjsons(cb) {
async.parallel([
function(callback) {
jsonCreate(randomString(), [{"food":"rice", "name":"bola"},{"food":"beans", "name":"tade"}] , callback);
},
function(callback) {
jsonCreate(randomString(), [{"food":"beans", "name":"tada"},{"food":"frop", "name":"tfee"}] , callback);
},
function(callback) {
jsonCreate(randomString(), [{"food":"rice", "name":"bola"},{"food":"beans", "name":"tade"}] , callback);
},
function(callback) {
jsonCreate(randomString(), [{"food":"rice", "name":"bola"},{"food":"beans", "name":"tade"}] , callback);
},
function(callback) {
jsonCreate(randomString(), [{"food":"rice", "name":"bola"},{"food":"beans", "name":"tade"}] , callback);
}
],
// optional callback
cb);
}
async.series([
createjsons
],
// optional callback
function(err, results) {
if (err) {
console.log('FINAL ERR: '+err);
}
else {
console.log('jsons: '+jsons);
}
//All done, disconnect from database
mongoose.connection.close();
});