-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
seeder.js
143 lines (125 loc) · 3.38 KB
/
seeder.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
/* eslint-disable */
import dotenv from 'dotenv';
import mongoose from 'mongoose';
import Education from './models/Education.js';
import Experience from './models/Experience.js';
import Extras from './models/Extras.js';
import Personal from './models/Personal.js';
import Resume from './models/Resume.js';
import { education, experience, extras, personal } from './shared/utils/demoData.js';
dotenv.config({
path: './.env',
});
/*
To seed
Package.json set "type":"module",
Modify acc. to you needs
RUN!
*/
const importData = async () => {
try {
await mongoose.connect(process.env.NEXT_PUBLIC_MONOGO_URI, {
useFindAndModify: false,
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});
await Education.deleteMany();
await Experience.deleteMany();
await Extras.deleteMany();
await Personal.deleteMany();
await Resume.deleteMany();
const resumeTrical = await Resume.create({
userId: 'template_user',
template: true,
title: 'Trical',
templateName: 'Trical',
});
const resumeJakePaul = await Resume.create({
userId: 'template_user',
template: true,
title: 'Jake',
templateName: 'Jake',
});
const expsTrical = await Experience.insertMany(
experience.map(exp => ({
...exp,
resumeId: resumeTrical._id,
})),
);
const edusTrical = await Education.insertMany(
education.map(edu => ({
...edu,
resumeId: resumeTrical._id,
})),
);
const extsTrical = await Extras.insertMany(
extras.map(ext => ({
...ext,
resumeId: resumeTrical._id,
})),
);
const personalDataTrical = await Personal.create({ ...personal, resumeId: resumeTrical._id });
await Resume.findOneAndUpdate(
{ _id: resumeTrical.id },
{
experience: expsTrical.map(exp => exp._id),
extras: extsTrical.map(ext => ext._id),
education: edusTrical.map(edu => edu._id),
personal: personalDataTrical,
},
);
const expsJakePaul = await Experience.insertMany(
experience.map(exp => ({
...exp,
resumeId: resumeJakePaul._id,
})),
);
const edusJakePaul = await Education.insertMany(
education.map(edu => ({
...edu,
resumeId: resumeJakePaul._id,
})),
);
const extsJakePaul = await Extras.insertMany(
extras.map(ext => ({
...ext,
resumeId: resumeJakePaul._id,
})),
);
const personalDataJakePaul = await Personal.create({ ...personal, resumeId: resumeJakePaul._id });
await Resume.findOneAndUpdate(
{ _id: resumeJakePaul.id },
{
experience: expsJakePaul.map(exp => exp._id),
extras: extsJakePaul.map(ext => ext._id),
education: edusJakePaul.map(edu => edu._id),
personal: personalDataJakePaul,
},
);
console.log('Data Imported!');
process.exit();
} catch (error) {
console.log(error);
process.exit(1);
}
};
const destroyData = async () => {
try {
await Education.deleteMany();
await Experience.deleteMany();
await Extras.deleteMany();
await Personal.deleteMany();
await Resume.deleteMany();
console.log('Data Destroyed');
process.exit();
} catch (error) {
console.log(error);
process.exit(1);
}
};
if (process.argv[2] === '-d') {
destroyData();
} else {
importData();
}