-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (159 loc) · 4.06 KB
/
index.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const Sequelize = require('sequelize');
const config = require('./config.json');
const db = require('./models')(Sequelize, config);
setTimeout(()=>{
//1 - Bulk create - films
db.films.bulkCreate([
{ title: 'Собачья жизнь', rating: '8.100', year: 2010, budget: 63000560, gross: 105000100 },
{ title: '8-жизней', rating: '9.100', year: 2013, budget: 23000560, gross: 201000100 },
{ title: 'Проклятие', rating: '4.014', year: 2000, budget: 10000, gross: 23000 },
{ title: 'Миссия невыполнима', rating: '6.014', year: 2005, budget: 100000, gross: 2300000 }
]).then(() => {
return db.films.findAll({raw:true});
}).then(films => {
console.log(films);
})
//Bulk create - actors
db.actors.bulkCreate([
{ name: 'Peter Griffin', birth: '1960-10-11', liked: 14689},
{ name: 'Lois Griffin', birth: '1965-02-24', liked: 12152},
{ name: 'Chris Griffin', birth: '1990-01-01', liked: 5600},
{ name: 'Meg Griffin', birth: '1989-12-30', liked: 1},
]).then(() => {
return db.actors.findAll({raw:true});
}).then(actors => {
console.log(actors);
})
//Create projects
//1
db.films.findByPk(1)
.then(film=>{
if(!film) return;
db.actors.findByPk(1)
.then(actor=>{
if(!actor) return;
film.addActor(actor);
});
db.actors.findByPk(2)
.then(actor=>{
if(!actor) return;
film.addActor(actor);
});
db.actors.findByPk(3)
.then(actor=>{
if(!actor) return;
film.addActor(actor);
});
db.actors.findByPk(4)
.then(actor=>{
if(!actor) return;
film.addActor(actor);
});
});
//2
db.films.findByPk(2)
.then(film=>{
if(!film) return;
db.actors.findByPk(1)
.then(actor=>{
if(!actor) return;
film.addActor(actor);
});
db.actors.findByPk(3)
.then(actor=>{
if(!actor) return;
film.addActor(actor);
});
db.actors.findByPk(4)
.then(actor=>{
if(!actor) return;
film.addActor(actor);
});
});
//3
db.films.findByPk(3)
.then(film=>{
if(!film) return;
db.actors.findByPk(1)
.then(actor=>{
if(!actor) return;
film.addActor(actor);
});
db.actors.findByPk(4)
.then(actor=>{
if(!actor) return;
film.addActor(actor);
});
});
//3
db.films.findByPk(4)
.then(film=>{
if(!film) return;
db.actors.findByPk(2)
.then(actor=>{
if(!actor) return;
film.addActor(actor);
});
});
}, 3000);
setTimeout(() =>{
//2 - Bulk update
db.sequelize.query("SELECT actorId, count(*) FROM `projects` group by actorId having count(*) = 3",
{ type: db.sequelize.QueryTypes.SELECT})
.then(actors => {
actors.forEach((item, i) => {
db.actors.update(
{ liked: 0 },
{ where: { id: item.actorId }}
).then(() =>{
console.log('Actor was updated!');
}).catch((err) =>{
console.error("Actor wasn't updated");
})
});
})
.then(() =>{
db.actors.findAll({raw:true})
.then(actors =>{
console.log(actors);
});
});
// 4 - Use include
db.films.findAll({
include: [{
model: db.actors
}]
, raw: true}).then(res =>{
console.log('Films and Actors:');
console.log(res);
});
// 5 - Scopes
const lastFilms = db.films.scope('lastFilms');
lastFilms.findAll({raw:true}).then(films =>{
console.log('SCOPE');
console.log(films);
});
// 6 - Transactions
db.sequelize.transaction().then((t) =>{
return db.actors.update({ liked: 0 }, {where: {liked: {[Sequelize.Op.gt]: 0}}} , {transaction: t})
.then(() =>{
t.rollback();
})
.catch((err) =>{
console.error('Rollback tran');
});
});
}, 4000);
setTimeout(() =>{
//3 - Bulk delete
db.actors.destroy({
where:{
liked: 0
}
}).then(() =>{
db.actors.findAll({raw:true})
.then(actors =>{
console.log(actors);
});
});
}, 5000);