-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
129 lines (115 loc) · 4.4 KB
/
app.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
'use strict';
const {sequelize, models} = require('./db');
// Get references to our models.
const {Person, Movie} = models;
// Define variables for the people and movies. NOTE: We'll use these variables to assist with the creation of our related data after we've defined the relationships (or associations) between our models.
let bradBird;
let vinDiesel;
let eliMarienthal;
let craigTNelson;
let hollyHunter;
let theIronGiant;
let theIncredibles;
console.log('Testing the connection to the database...');
(async () => {
try {
// Test the connection to the database
console.log('Connection to the database successful!');
await sequelize.authenticate();
// Sync the models
console.log('Synchronizing the models with the database...');
await sequelize.sync({force: true});
// Seed the database with some people
console.log('Adding people to the database...');
// Store the people array in a variable. We await the resolution of all of the calls to Person.create().
const peopleInstances = await Promise.all([
// The create method will create a single record
Person.create({
firstName: 'Brad',
lastName: 'Bird',
}),
Person.create({
firstName: 'Vin',
lastName: 'Diesel',
}),
Person.create({
firstName: 'Eli',
lastName: 'Marienthal',
}),
Person.create({
firstName: 'Craig T.',
lastName: 'Nelson',
}),
Person.create({
firstName: 'Holly',
lastName: 'Hunter',
}),
]);
// Log the resolved peopleInstances array
console.log(JSON.stringify(peopleInstances, null, 2));
// Update the global variables for the people instances.
[bradBird, vinDiesel, eliMarienthal, craigTNelson, hollyHunter] = peopleInstances;
// Seed the database with some movies
console.log('Adding movies to the database...');
// The process is similar to the one used for peopleInstances above, but when creating a movie, we're also setting our foreign key value - directorPersonId. (We are able to access bradBird.id b/c it's in the global scope.) This foreign key was not one of the properties we defined when we were creating the Movie model. Sequelize adds this property b/c of the relationship we defined.
const movieInstances = await Promise.all([
Movie.create({
title: 'The Iron Giant',
releaseYear: 1999,
directorPersonId: bradBird.id
}),
Movie.create({
title: 'The Incredibles',
releaseYear: 2004,
directorPersonId: bradBird.id
}),
]);
// Log the resolved movieInstances array
console.log(JSON.stringify(movieInstances, null, 2));
// Retrieve all of our movies with the findAll method
const movies = await Movie.findAll({
include: [
{
model: Person,
as: 'director',
}
]
});
console.log(movies.map(movie => movie.get({plain: true})));
// Retrieve all of our people with the findAll method
const people = await Person.findAll({
include: [
{
model: Movie,
as: 'director',
}
]
});
console.log(people.map(person => person.get({plain: true})));
// Tell Node to immediately terminate the process.
process.exit();
} catch (error) {
if (error.name === 'SequelizeValidationError') {
const errors = error.errors.map(err => err.message);
console.error('Validation errors: ', errors);
} else {
throw error;
}
}
})();
// running npm start should produce an array of objects like this one:
// {
// id: 2,
// title: 'The Incredibles',
// releaseYear: 2004,
// createdAt: 2020-03-04T19:43:21.040Z,
// updatedAt: 2020-03-04T19:43:21.040Z,
// directorPersonId: 1,
// director: {
// id: 1,
// firstName: 'Brad',
// lastName: 'Bird',
// createdAt: 2020-03-04T19:43:20.960Z,
// updatedAt: 2020-03-04T19:43:20.960Z
// }
// }