-
Notifications
You must be signed in to change notification settings - Fork 0
/
one_to_many2.js
55 lines (43 loc) · 1.02 KB
/
one_to_many2.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 Sequelize = require('sequelize');
const path = 'mysql://user:password@localhost:3306/db';
const sequelize = new Sequelize(path, {
operatorsAliases: false,
logging: false
});
let User = sequelize.define('user', {
name: Sequelize.STRING
});
let Task = sequelize.define('task', {
description: Sequelize.STRING,
});
User.hasMany(Task);
let mytasks1 = [{
description: 'write memo'
}, {
description: 'check accounts'
}];
let mytasks2 = [{
description: 'make two phone calls'
},
{
description: 'read new emails'
},
{
description: 'arrange meeting'
}
];
async function addUsersTasks() {
let user1 = await User.create({
name: 'John Doe'
});
let tasks1 = await Task.bulkCreate(mytasks1);
await user1.setTasks(tasks1);
let user2 = await User.create({
name: 'Debbie Griffin'
});
let tasks2 = await Task.bulkCreate(mytasks2);
await user2.setTasks(tasks2);
console.log('done');
sequelize.close();
}
addUsersTasks();