-
Notifications
You must be signed in to change notification settings - Fork 0
/
---Sequelize.js
148 lines (100 loc) · 3.94 KB
/
---Sequelize.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
/*
---------------------------------------------------------------------------
Sequelize notes
//---------------------------------------------------------------------------
Relationships: many employees to 1 manager
can setManager for an employee
can addEmployee to a manager
*/
//---------------------------------------------------------------------------
// general sequelizestuff:
const User = sequelize.define('user', {
username: Sequelize.STRING,
birthday: Sequelize.DATE,
article: Sequelize.INTEGER,
summary: {
type: Sequelize.STRING,
allowNull: false
validate: { notEmpty: true }
}
},{
getterMethods: {
snippet: function() {
return `${this.summary.slice(0,23)}...`
}
},
hooks: {
beforeUpdate: function() {
article++
}
}
}
)
//---------------------------------------------------------------------------
// deleting a record:
book.destroy({ where: id: req })
//or
book.findById() //you will put a where inside the ()
.then(book => book.destroy())
//---------------------------------------------------------------------------
//on-delete cascade:
var Sequelize = require('sequelize')
, sequelize = new Sequelize('advo_test', 'advo', 'preveza', {dialect: 'postgres'});
var User = sequelize.define('User', {
username: Sequelize.STRING,
birthday: Sequelize.DATE
}, {paranoid: true}); //Without paranoid the code works fine
var Email = sequelize.define('Email', {
email: Sequelize.STRING,
primary: Sequelize.BOOLEAN
}, {paranoid: true}); //Without paranoid the code works fine
User.hasMany(Email, {onDelete: 'cascade'});
Email.belongsTo(User, {onDelete: 'cascade'});
sequelize.sync({force:true}).success(function () {
/*
---------------------------------------------------------------------------
updating the user:
Project.find({ where: { title: 'aProject' } })
.on('success', function (project) {
// Check if record exists in db
if (project) {
project.updateAttributes({
title: 'a very different title now'
})
.success(function () {})
}
})
or
Project.update(
{ title: 'a very different title now' },
{ where: { _id: 1 } }
)
.success(result =>
...
)
.error(err =>
...
)
This is what we want:
*/
var Foo = db.define('foo', {
bar: Sequelize.STRING
});
app.put('/foos/:id', function(req, res, next){
console.log(req.body);
Foo.findById(req.params.id)
.then(function(foo){
if(!foo){
return res.sendStatus(404);
}
foo.bar = req.body.bar;
return foo.save();
})
.then(function(foo){
if(foo){
res.send(foo);
}
});
});
/*
*/