generated from AgathePons/Base-Express-EJS-Session-pg-SASS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
55 lines (49 loc) · 1.28 KB
/
test.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
const dotenv = require('dotenv');
dotenv.config();
const {
Author,
Book,
Category,
Comment,
Edition,
User
} = require('./app/models');
const allBooksWithAuthor = async () => {
const allBooks = await Book.findAll({
include: 'author'
});
const books = [];
for(const book of allBooks) {
books.push(book.title + ' de '+ book.author.firstname + ' ' + book.author.lastname);
}
console.log('all books:');
console.table(books);
};
const bookWithCategories = async (id) => {
const book = await Book.findByPk(id, {
include: 'categories'
});
console.log('------------------------');
console.log(`${book.title} has ${book.categories.length} categories:`);
for(const category of book.categories) {
console.log(category.name);
}
};
const userHasWritten = async (id) => {
const user = await User.findByPk(id, {
include: [
{ association: 'comments', include: 'book'}
]
});
console.log('------------------------')
console.log(user.fullname + ' has written '+ user.comments.length + ' comments:');
for(const comment of user.comments) {
console.log(`"${comment.text}" on the book ${comment.book.title}`);
}
};
const test = async () => {
await allBooksWithAuthor();
await bookWithCategories(1);
await userHasWritten(1);
};
test();