forked from xraystyle1980/marketing-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeds.js
171 lines (154 loc) · 4.78 KB
/
seeds.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
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
"use strict";
require("dotenv").config({
path: __dirname + "/.env"
});
const fs = require("fs");
const path = require("path");
const mongoose = require("mongoose");
mongoose.connect(process.env.MONGOURL, {
useNewUrlParser: true,
useCreateIndex: true
});
mongoose.Promise = global.Promise; // Tell Mongoose to use ES6 promises
const Story = require("./models/story");
const User = require("./models/user");
const Menulocation = require("./models/menulocation");
const Contact = require("./models/contact");
const Employee = require("./models/employee");
const Course = require("./models/course");
const Location = require("./models/location");
const Partner = require("./models/partner");
const Page = require("./models/page");
const Event = require("./models/event");
const Language = require("./models/language");
const EventsController = require("./controllers/admin/AdminEventsController");
const IMAGE_UPLOAD_DIR = process.env.IMAGE_UPLOAD_DIR;
const {
menulocations,
stories,
pages,
pagesgerman,
courses,
users,
events,
partners,
adminUser,
contacts,
employees,
languages,
locations
} = require("./seeddata");
const imageUploadDir = IMAGE_UPLOAD_DIR;
async function deleteData() {
const files = await fs.readdirSync(imageUploadDir);
let images = [];
for (const file of files) {
console.log('file', file);
if (file !== ".gitignore") {
images.push(fs.unlinkSync(path.join(imageUploadDir, file)));
}
}
console.log("files", images);
await Promise.all(images);
console.log("😢 Goodbye Data...");
await Story.deleteMany();
await Menulocation.deleteMany();
await Location.deleteMany();
await Partner.deleteMany();
await Employee.deleteMany();
await Course.deleteMany();
await Contact.deleteMany();
await Page.deleteMany();
await User.deleteMany();
await Event.deleteMany();
await Language.deleteMany();
console.log("Data Deleted. To load sample data, run\n\n\t npm run seeds\n\n");
process.exit();
}
async function seedRandomNtoN(arrayOfRecords, relationship, model) {
arrayOfRecords.map((record, index) => {
var randomSetter = Math.floor(
Math.random(relationship.length) * relationship.length + 1
);
record[model.collection.name] = [];
for (let i = 0; i < randomSetter; i++) {
return (record[model.collection.name][i] = relationship[
randomSetter - 1
]._id.toString());
}
});
return arrayOfRecords;
}
async function seedRandomImages() {
if (!fs.existsSync(imageUploadDir)){
fs.mkdirSync(imageUploadDir);
}
var images = [
"./assets/media/bg1.jpg",
"./assets/media/bg2.jpg",
"./assets/media/bg3.jpg"
];
let index = 0;
var promises = [];
for await (let image of images) {
index++;
await fs.copyFileSync(image, `${imageUploadDir}example_image_${index}.jpg`);
}
return promises;
}
async function loadData() {
try {
await Promise.all(await seedRandomImages());
console.log(`Images saved to ${imageUploadDir}`);
await EventsController.fetchevents();
const createdMenulocations = await Menulocation.insertMany(menulocations);
const createdLocations = await Location.insertMany(locations); //Location.find();
await Course.insertMany(courses);
await Employee.insertMany(employees);
await Story.insertMany(stories);
await Partner.insertMany(partners);
const createdLanguages = await Language.insertMany(languages);
await User.create(adminUser);
console.log(`You can now login as: `);
console.log(`Email: ${adminUser.email} Password: password`);
var associatedMenulocations = await seedRandomNtoN(
pages,
createdMenulocations,
Menulocation
);
var associatedLocations = await seedRandomNtoN(
contacts,
createdLocations,
Location
);
var associatedEmployees = await seedRandomNtoN(
employees,
createdLocations,
Location
);
await Page.insertMany(associatedMenulocations);
await Page.insertMany(pagesgerman);
const devugees = await Page.find({title: "Devugees"})
devugees[0].languageVersion = devugees[1]._id;
devugees[0].language = createdLanguages[0]._id;
devugees[1].languageVersion = devugees[0]._id;
devugees[1].language = createdLanguages[1]._id;
await devugees[0].save()
await devugees[1].save()
await Contact.insertMany(associatedLocations);
await Employee.insertMany(associatedEmployees);
console.log("👍 Done!\n\n Successfully loaded sample data");
process.exit();
} catch (e) {
console.log(
"\n👎 Error! The Error info is below but if you are importing sample data make sure to drop the existing database first with.\n\n\t npm run blowitallaway\n\n\n"
);
console.log(e);
process.exit();
}
}
if (process.argv.includes("--delete")) {
deleteData();
} else {
loadData();
}