-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.js
104 lines (95 loc) · 2.92 KB
/
seed.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const mongoose = require("mongoose");
const faker = require("faker");
const { mongo } = require("./src/config/vars");
const User = require("./src/models/user.model");
const Message = require("./src/models/message.model");
const random = require("lodash/random");
const bcrypt = require("bcryptjs");
const sexes = ["male", "female", "transgender", "neutral", "non-binary"];
console.log(
`********************************
******** seeding begins ********
********************************`
);
let messagesSeedData = [];
let usersSeedData = [];
function generateMessageSeedData() {
// const seedData = [];
for (let i = 0; i < 20; i += 1) {
const message_id = mongoose.Types.ObjectId();
messagesSeedData.push({
_id: message_id,
location: {
type: "Point",
geocoded: `${faker.helpers.contextualCard().address.street}, ${
faker.helpers.contextualCard().address.city
}`,
// lon/lat generated return non-realistic human addresses
coordinates: [
parseFloat(faker.address.longitude()),
parseFloat(faker.address.latitude()),
],
},
seenDate: faker.date.past(),
title: faker.lorem.words(5),
body: faker.lorem.words(10),
});
}
console.log(`Created ${messagesSeedData.length} messages`);
return messagesSeedData;
}
async function generateUsersSeedData() {
// const seedData = [];
for (let i = 0; i < 20; i += 1) {
const user_id = mongoose.Types.ObjectId();
usersSeedData.push({
_id: user_id,
email: faker.internet.email(),
username: faker.internet.userName(),
password: faker.internet.password(),
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
active: true,
gender: sexes[random(sexes.length - 1)], // @TODO can faker do this?
messages_ids: [messagesSeedData[i]._id],
});
messagesSeedData[i].user = usersSeedData[i]._id;
}
const hash = await bcrypt.hash("1234", 10);
usersSeedData.push({
_id: mongoose.Types.ObjectId(),
email: "[email protected]",
username: "gkatsanos",
password: hash,
firstName: "George",
lastName: "Katsanos",
active: true,
gender: "male",
messages_ids: [messagesSeedData[1]._id],
});
console.log(`Created ${usersSeedData.length} users`);
return usersSeedData;
}
generateMessageSeedData();
generateUsersSeedData();
mongoose.set("useCreateIndex", true);
mongoose.connect(mongo.uri, {
poolSize: 10,
bufferMaxEntries: 0,
useNewUrlParser: true,
useUnifiedTopology: true,
});
User.deleteMany({})
.then(() => User.insertMany(usersSeedData))
.then(() => {
console.log("seeding users done");
mongoose.connection.close();
console.log(
`********************************
******** seeding ended ********
********************************`
);
});
Message.deleteMany({})
.then(() => Message.insertMany(messagesSeedData))
.then(() => console.log("seeding messages done"));