-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.js
49 lines (42 loc) · 1.22 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
const { green, red } = require('chalk');
const { db } = require('./server/db');
const Campus = require('./server/db/campus');
const Student = require('./server/db/student');
const campuses = require('./bin/MOCK_DATA_campuses.json');
const students = require('./bin/MOCK_DATA_students.json');
const seed = async () => {
try {
await db.sync({ force: true });
// seed your database here!
await Promise.all(
campuses.map((campus) => {
return Campus.create(campus);
})
);
await Promise.all(
students.map((student) => {
return Student.create(student);
})
);
console.log(green('Seeding success!'));
// db.close();
} catch (err) {
console.log(red(err));
}
};
module.exports = seed;
// If this module is being required from another module, then we just export the
// function, to be used as necessary. But it will run right away if the module
// is executed directly (e.g. `node seed.js` or `npm run seed`)
if (require.main === module) {
seed()
.then(() => {
console.log(green('Seeding success!'));
db.close();
})
.catch((err) => {
console.error(red('Oh noes! Something went wrong!'));
console.error(err);
db.close();
});
}