-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulateDB.js
More file actions
202 lines (186 loc) · 4.4 KB
/
populateDB.js
File metadata and controls
202 lines (186 loc) · 4.4 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#! /usr/bin/env node
console.log(
"This script populates some test authors, posts, comments to your database. Specified database as argument - e.g.: populatedb mongodb+srv://cooluser:coolpassword@cluster0.a9azn.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 { faker } = require("@faker-js/faker");
var Author = require("./models/Author");
var Post = require("./models/Post");
var Comment = require("./models/Comment");
var async = require("async");
var mongoose = require("mongoose");
var mongoDB = userArgs[0];
mongoose.connect(mongoDB, {
useNewUrlParser: true,
useUnifiedTopology: true,
dbName: "working",
});
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));
var authors = [];
var posts = [];
var comments = [];
function createAuthors(cb) {
var first_name = faker.name.firstName();
var last_name = faker.name.lastName();
var username = faker.internet.userName(first_name, last_name);
let authorDetails = {
first_name,
last_name,
username,
password: "password",
};
var author = new Author(authorDetails);
author.save(function (err) {
if (err) {
cb(err, null);
return;
}
authors.push(author);
cb(null, author);
});
}
function createPost(author, cb) {
var title = faker.hacker.phrase();
var blog = faker.lorem.paragraphs(4);
let writter = author._id;
var timestamp = faker.date.past();
var postDetails = {
title,
blog,
timestamp,
author: writter,
public: true,
};
var newpost = new Post(postDetails);
newpost.save(function (err) {
if (err) {
cb(err, null);
return;
}
posts.push(newpost);
cb(null, newpost);
});
}
function createComment(post, cb) {
var commentDetails = {
name: faker.name.fullName(),
message: faker.lorem.sentence(),
post: post._id,
timestamp: faker.date.past(),
};
var comment = new Comment(commentDetails);
comment.save(function (err) {
if (err) {
cb(err, null);
return;
}
cb(null, comment);
});
}
function authorCreate(cb) {
async.parallel(
[
function (callback) {
createAuthors(callback);
},
function (callback) {
createAuthors(callback);
},
function (callback) {
createAuthors(callback);
},
],
cb
);
}
function postCreate(cb) {
async.parallel(
[
function (callback) {
createPost(authors[0], callback);
},
function (callback) {
createPost(authors[0], callback);
},
function (callback) {
createPost(authors[1], callback);
},
function (callback) {
createPost(authors[1], callback);
},
function (callback) {
createPost(authors[2], callback);
},
function (callback) {
createPost(authors[2], callback);
},
function (callback) {
createPost(authors[2], callback);
},
],
cb
);
}
function commentCreate(cb) {
async.parallel(
[
function (callback) {
createComment(posts[0], callback);
},
function (callback) {
createComment(posts[0], callback);
},
function (callback) {
createComment(posts[1], callback);
},
function (callback) {
createComment(posts[2], callback);
},
function (callback) {
createComment(posts[3], callback);
},
function (callback) {
createComment(posts[4], callback);
},
function (callback) {
createComment(posts[4], callback);
},
function (callback) {
createComment(posts[5], callback);
},
function (callback) {
createComment(posts[5], callback);
},
function (callback) {
createComment(posts[6], callback);
},
function (callback) {
createComment(posts[0], callback);
},
function (callback) {
createComment(posts[1], callback);
},
],
cb
);
}
async.series(
[authorCreate, postCreate, commentCreate],
function (err, results) {
if (err) {
console.log("FINAL ERROR:", err);
} else {
console.log(results);
}
mongoose.connection.close();
}
);