-
Notifications
You must be signed in to change notification settings - Fork 5
/
seed.js
115 lines (102 loc) · 4.58 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
"use strict";
const {
db,
models: { User, Board, List, TaskCard, UserTaskCard, UserBoard, Comment },
} = require("./server/db");
/**
* seed - this function clears the database, updates tables to
* match the models, and populates the database.
*/
async function seed() {
console.log(`\nHere\n\nis\nthe\nbeforeSync`);
await db.sync({ force: true }); // clears db and matches models to tables
console.log("db synced!");
console.log(`\nHere\n\nis\nthe\n AFTER \n SYNC\n`);
// Creating Users
const users = await Promise.all([
User.create({ firstName: 'Cody', lastName: 'Code', email: "cody@gmail.com", password: "123" }),
User.create({ firstName: 'Murphy', lastName: 'Murph', email: "murphy@gmail.com", password: "123" }),
]);
// Creating Board
const boards = await Promise.all([
Board.create({ boardName: "This Board", creatorId: 1 }),
Board.create({ boardName: "That Board", creatorId: 2 }),
]);
// Creating List
const list = await Promise.all([
List.create({ listName: "To Do", position: 0, boardId: 1 }),
List.create({ listName: "In Progress", position: 1, boardId: 1 }),
List.create({ listName: "Code Review", position: 2, boardId: 1 }),
List.create({ listName: "Complete", position: 3, boardId: 1 }),
List.create({ listName: "To Do", position: 0, boardId: 2 }),
List.create({ listName: "In Progress", position: 1, boardId: 2 }),
List.create({ listName: "Code Review", position: 2, boardId: 2 }),
List.create({ listName: "Complete", position: 3, boardId: 2 }),
]);
// Creating TaskCard
const taskcard = await Promise.all([
TaskCard.create({start: new Date('2023-01-29'), end:new Date('2023-01-30T03:24:00') , title: "Navbar", position: 0, listId: 2, boardId: 1 }),
TaskCard.create({start: new Date('2023-01-29'), end:new Date('2023-01-30T03:24:00') , title: "Express Routes", position: 0, listId: 3, boardId: 1 }),
TaskCard.create({start: new Date('2023-01-29'), end:new Date('2023-01-30T03:24:00') , title: "Board Feature", position: 1, listId: 2, boardId: 1 }),
TaskCard.create({start: new Date('2023-01-29') , title: "List Feature", position: 2, listId: 2, boardId: 1 }),
TaskCard.create({start: new Date('2023-01-29') , title: "TaskCard Feature", position: 0, listId: 1, boardId: 1 }),
TaskCard.create({start: new Date('2023-01-29'), title: "Task Feature", position: 1, listId: 1, boardId: 1 }),
]);
// Creating UserTaskCard
const userTaskCard = await Promise.all([
UserTaskCard.create({ userId: 1, taskcardId: 1 }),
UserTaskCard.create({ userId: 1, taskcardId: 2 }),
UserTaskCard.create({ userId: 2, taskcardId: 3 }),
UserTaskCard.create({ userId: 2, taskcardId: 4 }),
]);
// Creating UserBoard
// privilege on UserBoard future proofs to add functionality, because user can be added to the board to only add privilege of certain types of things.
const userBoard = await Promise.all([
UserBoard.create({ userId: 1, boardId: 1, privilege: "ADMIN" }),
UserBoard.create({ userId: 2, boardId: 2, privilege: "ADMIN" }),
UserBoard.create({ userId: 2, boardId: 1, privilege: "USER" }),
UserBoard.create({ userId: 1, boardId: 2, privilege: "USER" }),
]);
// Creating Comment
const comment = await Promise.all([
Comment.create({ content: 'nice job!', taskcardId: 1, userId: 1, createdAt: '2023-02-08 12:09:05.836-05' }),
Comment.create({ content: "what's the estimated timeline for completion?", taskcardId: 1, userId: 2, createdAt: '2023-02-08 12:09:06.836-05' }),
Comment.create({ content: "ping us when it's ready for code review", taskcardId: 1, userId: 1, createdAt: '2023-02-08 12:09:07.836-05' }),
])
console.log(`seeded ${users.length} users`);
console.log(`seeded successfully`);
return {
users: {
cody: users[0],
murphy: users[1],
},
};
}
/*
We've separated the `seed` function from the `runSeed` function.
This way we can isolate the error handling and exit trapping.
The `seed` function is concerned only with modifying the database.
*/
async function runSeed() {
console.log("seeding...");
try {
await seed();
} catch (err) {
console.error(err);
process.exitCode = 1;
} finally {
console.log("closing db connection");
await db.close();
console.log("db connection closed");
}
}
/*
Execute the `seed` function, IF we ran this module directly (`node seed`).
`Async` functions always return a promise, so we can use `catch` to handle
any errors that might occur inside of `seed`.
*/
if (module === require.main) {
runSeed();
}
// we export the seed function for testing purposes (see `./seed.spec.js`)
module.exports = seed;