forked from jtarasova/sequelize_intro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appMany.js
59 lines (57 loc) · 1.45 KB
/
appMany.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
const db = require('./db/models');
const {
Sequelize: { Op },
sequelize,
Cat,
Dog,
Dogscats,
} = db;
const manyMain = async () => {
// await Cat.create({ name: 'Lol', age: 9 });
// await Cat.create({ name: 'Lil', age: 2 });
// await Cat.create({ name: 'Lal', age: 4 });
// await Dog.create({ name: 'Bob', age: 3 });
// await Dog.create({ name: 'Beb', age: 12 });
// await Dog.create({ name: 'Bib', age: 8 });
// const bob = await Dog.findOne({
// where: { name: 'Bob' },
// // attributes: ['id'],
// // raw: true,
// });
// const lol = await Cat.findOne({
// where: { name: 'Lol' },
// // attributes: ['id'],
// raw: true,
// });
// await Dogscats.create({ dog_id: bob.id, cat_id: lol.id });
// await Dogscats.create({ dog_id: 3, cat_id: 2 });
// await Dogscats.create({ dog_id: 2, cat_id: 2 });
const catsWithFriends = await Cat.findAll({
include: {
model: Dog,
attributes: ['name', 'age'],
through: {
model: Dogscats,
attributes: [],
},
},
order: [['age', 'DESC']],
raw: true,
});
catsWithFriends.map((el) => console.log(el));
// const catsWithFriends = await Cat.findAll({
// order: [[Dog, 'age', 'ASC']],
// include: [Dog],
// raw: true,
// });
// console.log(catsWithFriends);
// const a = await Cat.findAll({
// where: {
// age: {
// [Op.gte]: 4,
// },
// },
// });
// console.log(a);
};
manyMain();