-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenerate.js
More file actions
61 lines (51 loc) · 1.6 KB
/
generate.js
File metadata and controls
61 lines (51 loc) · 1.6 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
const mongoose = require('mongoose');
const faker = require('faker');
const path = require('path');
const db = require('./models/');
const IMG_URL = 'https://s3-us-west-1.amazonaws.com/airfec2018/photos/file-';
const MAX_ID_RANGE = 100;
const MAX_IMG_RANGE = 10;
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive
}
// drop cellection if exists
console.log('cleared db for re-seed...\n');
db.Photo.remove({}).exec(function(err, results) {
if (err) {
console.error(err);
return process.exit(0);
}
console.log('db cleared!\n');
console.log('starting re-seeding....\n');
var inProgressDataBaseEntrys = [];
for (let id = 1; id <= MAX_ID_RANGE; id++) {
for (let imgs = 1; imgs <= MAX_IMG_RANGE; imgs++) {
var photo = new db.Photo({
room_id: id,
photo_url: IMG_URL + getRandomIntInclusive(1, 75) + '.jpg',
verified: !Math.floor(Math.random() * 2),
description: faker.lorem.sentence()
});
inProgressDataBaseEntrys.push(
photo.save().then(item => {
console.log('photo #' + item.id + ' was created');
return Promise.resolve(item);
})
);
}
}
Promise.all(inProgressDataBaseEntrys)
.then(function(results) {
console.log(results.length + ' entrys saved in DataBase');
})
.catch(function(err) {
console.error(err);
})
.then(function() {
mongoose.connection.close(function() {
process.exit(0);
});
});
});