-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.js
117 lines (110 loc) · 2.74 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
105
106
107
108
109
110
111
112
113
114
115
116
117
const db = require('./server/db');
const Author = require('./server/db/models/author');
const Message = require('./server/db/models/message');
const Channel = require('./server/db/models/channel');
const channels = [
{ name: 'really_random' },
{ name: 'generally_speaking' },
{ name: 'dogs_of_fullstack' },
{ name: 'lunch_planning' }
];
const authors = [{
name: 'Cody',
image: '/images/cody.jpg'
}, {
name: 'Ben',
image: '/images/ben.jpg'
}, {
name: 'Star',
image: '/images/star.jpg'
}, {
name: 'Batman',
image: '/images/batman.jpg'
}, {
name: 'Elliott',
image: '/images/elliott.jpg'
}, {
name: 'Fira',
image: '/images/fira.jpg'
}, {
name: 'Henry',
image: '/images/henry.jpg'
}, {
name: 'Marcy',
image: '/images/marcy.jpg'
}, {
name: 'Milton',
image: '/images/milton.jpg'
}, {
name: 'Murphy',
image: '/images/murphy.jpg'
}, {
name: 'Raffi',
image: '/images/raffi.jpg'
}, {
name: 'Tulsi',
image: '/images/tulsi.jpg'
}, {
name: 'Pork Chop',
image: '/images/pork_chop.jpg'
}, {
name: 'Ribs',
image: '/images/ribs.jpg'
}, {
name: 'Stacey',
image: '/images/stacey.jpg'
}, {
name: 'JD',
image: '/images/jd.jpg'
}, {
name: 'BenBen',
image: '/images/benben.png'
}, {
name: 'Odie',
image: '/images/odie.jpg'
}];
const id = () => Math.round(Math.random() * (authors.length - 1)) + 1;
const messages = [
{ authorId: id(), content: 'I like React!', channelId: 1 },
{ authorId: id(), content: 'I like Redux!', channelId: 1 },
{ authorId: id(), content: 'I like React-Redux!', channelId: 1 },
{ authorId: id(), content: 'I like writing web apps!', channelId: 2 },
{ authorId: id(), content: 'You should learn JavaScript!', channelId: 2 },
{ authorId: id(), content: 'JavaScript is pretty great!', channelId: 2 },
{ authorId: id(), content: 'Dogs are great!', channelId: 3 },
{ authorId: id(), content: 'Cats are also great!', channelId: 3 },
{ authorId: id(), content: 'Why must we fight so?', channelId: 3 },
{ authorId: id(), content: 'I want to get tacos!', channelId: 4 },
{ authorId: id(), content: 'I want to get salad!', channelId: 4 },
{ authorId: id(), content: 'I want a taco salad!', channelId: 4 }
];
const seed = () =>
Promise.all(authors.map(author =>
Author.create(author))
)
.then(() =>
Promise.all(channels.map(channel =>
Channel.create(channel))
))
.then(() =>
Promise.all(messages.map(message =>
Message.create(message))
)
);
const main = () => {
console.log('Syncing db...');
db.sync({ force: true })
.then(() => {
console.log('Seeding databse...');
return seed();
})
.catch(err => {
console.log('Error while seeding');
console.log(err.stack);
})
.then(() => {
db.close();
return null;
});
};
main();