-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequalize.js
336 lines (304 loc) · 8.15 KB
/
sequalize.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// const { Sequalize, Op, QueryTypes } = require("sequelize");
/*
// include-exclude columns
let data = await User.findAll({
attributes: {
exclude: ["created_at", "updated_at"],
include: [
// this will concat word Singh after the name
[Sequelize.fn("CONCAT", Sequelize.col("name"), "Singh"), "fullName"]
]
}
})
*/
/*
// select attributes, change column name
let data = await User.findAll({
attributes: [
"name",
["email", "emailId"], // this will change column name from "email" to "emailId"
"gender",
[Sequelize.fn("Count", Sequelize.col("email")), "emailCount"], // this will return count of email
]
})
*/
/*
// find query with condition, order, group, pagination
let data = await User.findAll({
where: {
// id: 2,
id: {
[Op.gt] : 2,
},
email: {
[Op.like]: "%@gmail.com"
},
},
order: [
["name", "DESC"],
["email", "DESC"]
],
group: ["email", "name"],
limit: 2,
offset: 1
})
*/
/*
// findByPk
let data = await User.findByPk(5);
*/
/*
// findOne
let data = await User.findOne({});
*/
/*
// findAndCountAll (this will return row and count)
let data = await findAndCountAll({
where: {
email: "hello@gmail.com"
}
});
*/
/*
// findOrCreate (this will return "data" and "created". If data already exist then it will return data with created: false flag, otherwise it will create new data and return with created: true flag)
let [data, created] = await User.findOrCreate({
where: {
name: "dummy",
},
defaults: {
email: "dummy@gmail.com",
gender: "male"
}
});
*/
/*
// setters and getters and model instances
module.exports = (sequelize, DataTypes) => {
const Users = sequelize.define("users", {
name: {
type: DataTypes.STRING,
// when create new data then this will set singh at the end of the name
set(value) {
this.setDataValue("name", value + " singh");
},
// when we get data then this will add xyz at the end of the name
get() {
this.getDataValue("name") + "xyz"
}
},
email: {
type: DataTypes.STRING,
// defaultValue: "test@gmail.com",
// allowNull: false,
// unique: true,
set(value) {
this.setDataValue("email", value+ "@gmail.com")
}
},
gender: {
type: DataTypes.STRING,
// setting validations
validate: {
equals: {
args: "male",
msg: "Please enter valid input"
},
// isIn: [["male", "female"]]
}
}
}, {
tableName: "user",
timestamps: false,
// createdAt: false,
// updatedAt: false,
createdAt: "created_at",
updatedAt: "updated_at",
engine: "MYISAM"
})
}
*/
/*
// row/native query
// let users = await db.sequelize.query("SELECT * from users WHERE gender IN(:gender)", {
// let users = await db.sequelize.query("SELECT * from users WHERE email LIKE :searchEmail ", {
type: QueryTypes.SELECT,
// model: Users,
// mapToModel: true,
// raw: true,
// replacements: { gender: 'male' }, // gender = :gender"
// replacements: ["male"], // gender = ?
// replacements: { gender: ["male", "female"]}, // gender IN(:gender)
// replacements: { searchEmail: "%@gmail.com"} // email LIKE :searchEmail
})
*/
// one-to-one association
/*
db.users.hasOne(db.posts, {foreignKey: "user_id"}); // default foreignKey is userId
db.posts.belongsTo(db.users, {foreighKey: "user_id"})
// orm query on User table
let data = await Users.findAll({
attributes: ["name", "email"],
include: [{
model: Posts,
attributes: ["title", ["name", "PostName"]]
}],
where: {
id: 1
}
});
// orm query on Post table
let data = await Posts.findAll({
attributes: ["content", "title"],
include: [{
model: Users,
attributes: ["name", ["email", "userEmail"]]
}],
});
*/
// one-to-many association
/*
db.users.hasMany(db.posts, { foreignKey: user_id }); // default foreignKey is userId
db.posts.belongsTo(db.users, { foreignKey: user_id });
// orm query on user table
let data = await Users.findAll({
attributes: ["name", "email"],
include: [{
model: Posts,
attributes: ["title", ["name", "PostName"]]
}],
where: {
id: 8
}
})
*/
// many-to-many association
/*
db.posts.belongsToMany(db.tags, { through: "post_tag", foreignKey: "post_id" }); // through: "post_tag", here post_tag is third table which contains post_id and tag_id too
db.tags.belongsToMany(db.posts, { through: "post_tag", foreignKey: "tag_id"});
// Post--to--Tag
let data = await Posts.findAll({
attributes: ["title", "content"],
include: [{
model: Tags,
attributes: ["name"]
}].
});
// Tag--to--Post
let data = await Tags.findAll({
attributes: ["name"],
include: [{
model: Posts,
attributes: ["title", "content"]
}]
})
*/
// Polymorphic one-to-many association
/*
db.image.hasMany(db.comments, {
foreignKey: "commentableId",
constraints: false,
scope: {
commentableType: 'image', // commentableType is column name of comment table
}
});
db.video.hasMany(db.comments, {
foreignKey: "commentableId",
constraints: false,
scope: {
commentableType: 'video', // commentableType is column name of comment table
}
});
db.comment.belongsTo(db.image, { foreignKey: "commentableId", constraints: false });
db.comment.belongsTo(db.video, { foreignKey: "commentableId", constraints: false });
// Image--to--comment
let data = await Image.findAll({
include: [{
model: Comment,
}]
});
// Video--to--comment
let data = await Video.findAll({
include: [{
model: Comment,
}]
});
// Comment--to--image/video
let data = await Comment.findAll({
include: [
{
model: Image,
},
{
model: Video,
}
]
})
*/
// Polymorphic Many-to-Many association
/*
// table details
image - (id, created_at, updated_at, title, url)
video - (id, created_at, updated_at, title, text)
tag - (id, created_at, updated_at, name)
tag_taggables - (id, created_at, updated_at, tagId, taggableId(image/video id), taggableType(image/video))
// Image--to--Tag
db.image.belongsToMany(db.tags, {
through: {
model: db.tag_taggable,
unique: true,
scope: {
taggableType: 'image'
}
},
foreignKey: 'taggableId,
constraints: false,
});
// Tag--to--Image
db.tags.belongsToMany(db.image, {
through: {
model: db.tag_taggable,
unique: true,
scope: {
taggableType: 'image'
}
},
foreignKey: 'tagId',
constraints: false,
});
// Video--to--Tag
db.video.belongsToMany(db.tags, {
through: {
model: db.tag_taggable,
unique: true,
scope: {
taggableType: 'video',
}
},
foreignKey: 'taggableId',
constraints: false,
});
// Tag--to--Video
db.tags.belongsToMany(db.video, {
through: {
model: db.tag_taggable,
unique: true,
scope: {
taggableType: 'video',
}
},
foreignKey: 'tagId',
constraints: false,
});
// Image-to-tag
let data = await Image.findAll({
include: [Tags]
});
// Video-to-tag
let data = await Video.findAll({
include: [Tags]
});
// Tag-to-image/video
let data = await Tag.findAll({
include: [Video, Image],
});
*/