/
where.ts
298 lines (272 loc) · 8.69 KB
/
where.ts
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
import { expectTypeOf } from "expect-type";
import { AndOperator, fn, Model, Op, OrOperator, Sequelize, WhereOperators, WhereOptions, literal, where as whereFn } from 'sequelize';
import Transaction from 'sequelize/lib/transaction';
class MyModel extends Model {
public hi!: number;
}
// Simple options
expectTypeOf({
string: 'foo',
strings: ['foo'],
number: 1,
numbers: [1],
boolean: true,
buffer: Buffer.alloc(0),
buffers: [Buffer.alloc(0)],
null: null,
date: new Date()
}).toMatchTypeOf<WhereOptions>();
// Optional values
expectTypeOf<{ needed: number; optional?: number }>().toMatchTypeOf<WhereOptions>();
// Misusing optional values (typings allow this, sequelize will throw an error during runtime)
// This might be solved by updates to typescript itself (https://github.com/microsoft/TypeScript/issues/13195)
// expectTypeOf({ needed: 2, optional: undefined }).not.toMatchTypeOf<WhereOptions>();
// Operators
expectTypeOf({
[Op.and]: { a: 5 }, // AND (a = 5)
}).toMatchTypeOf<AndOperator>();
expectTypeOf({
[Op.and]: { a: 5 }, // AND (a = 5)
}).toMatchTypeOf<AndOperator<{ a: number }>>();
expectTypeOf({
[Op.or]: [{ a: 5 }, { a: 6 }], // (a = 5 OR a = 6)
}).toMatchTypeOf<OrOperator>();
expectTypeOf({
[Op.or]: [{ a: 5 }, { a: 6 }], // (a = 5 OR a = 6)
}).toMatchTypeOf<OrOperator<{ a: number }>>();
expectTypeOf({
[Op.eq]: 6, // = 6
[Op.eq]: Sequelize.col('SOME_COL'), // = <column>
[Op.gt]: 6, // > 6
[Op.gt]: Sequelize.col('SOME_COL'), // > <column>
[Op.gte]: 6, // >= 6
[Op.gte]: Sequelize.col('SOME_COL'), // >= <column>
[Op.lt]: 10, // < 10
[Op.lt]: Sequelize.col('SOME_COL'), // < <column>
[Op.lte]: 10, // <= 10
[Op.lte]: Sequelize.col('SOME_COL'), // <= <column>
[Op.ne]: 20, // != 20
[Op.not]: true, // IS NOT TRUE
[Op.is]: null, // IS NULL
[Op.between]: [6, 10], // BETWEEN 6 AND 10
[Op.notBetween]: [11, 15], // NOT BETWEEN 11 AND 15
[Op.in]: [1, 2], // IN [1, 2]
[Op.notIn]: [1, 2], // NOT IN [1, 2]
[Op.like]: '%hat', // LIKE '%hat'
[Op.notLike]: '%hat', // NOT LIKE '%hat'
[Op.iLike]: '%hat', // ILIKE '%hat' (case insensitive) (PG only)
[Op.notILike]: '%hat', // NOT ILIKE '%hat' (PG only)
[Op.startsWith]: 'hat',
[Op.endsWith]: 'hat',
[Op.substring]: 'hat',
[Op.overlap]: [1, 2], // && [1, 2] (PG array overlap operator)
[Op.contains]: [1, 2], // @> [1, 2] (PG array contains operator)
[Op.contained]: [1, 2], // <@ [1, 2] (PG array contained by operator)
[Op.any]: [2, 3], // ANY ARRAY[2, 3]::INTEGER (PG only)
[Op.regexp]: '^[h|a|t]', // REGEXP/~ '^[h|a|t]' (MySQL/PG only)
[Op.notRegexp]: '^[h|a|t]', // NOT REGEXP/!~ '^[h|a|t]' (MySQL/PG only)
[Op.iRegexp]: '^[h|a|t]', // ~* '^[h|a|t]' (PG only)
[Op.notIRegexp]: '^[h|a|t]' // !~* '^[h|a|t]' (PG only)
} as const).toMatchTypeOf<WhereOperators>();
expectTypeOf({
[Op.like]: { [Op.any]: ['cat', 'hat'] }, // LIKE ANY ARRAY['cat', 'hat']
[Op.iLike]: { [Op.any]: ['cat', 'hat'] }, // LIKE ANY ARRAY['cat', 'hat']
[Op.notLike]: { [Op.any]: ['cat', 'hat'] }, // LIKE ANY ARRAY['cat', 'hat']
[Op.notILike]: { [Op.any]: ['cat', 'hat'] }, // LIKE ANY ARRAY['cat', 'hat']
}).toMatchTypeOf<WhereOperators>();
// Complex where options via combinations
expectTypeOf([
{ [Op.or]: [{ a: 5 }, { a: 6 }] },
Sequelize.and(),
Sequelize.or(),
{ [Op.and]: [] },
{ rank: Sequelize.and({ [Op.lt]: 1000 }, { [Op.eq]: null }) },
{ rank: Sequelize.or({ [Op.lt]: 1000 }, { [Op.eq]: null }) },
{ rank: { [Op.or]: { [Op.lt]: 1000, [Op.eq]: null } } },
{
createdAt: {
[Op.lt]: new Date(),
[Op.gt]: new Date(Date.now() - 24 * 60 * 60 * 1000),
}
},
{
[Op.or]: [
{ title: { [Op.like]: 'Boat%' } },
{ description: { [Op.like]: '%boat%' } }
]
},
{
meta: {
[Op.contains]: {
site: {
url: 'https://sequelize.org/'
}
}
},
meta2: {
[Op.contains]: ['stringValue1', 'stringValue2', 'stringValue3']
},
meta3: {
[Op.contains]: [1, 2, 3, 4]
},
},
{
name: 'a project',
[Op.or]: [{ id: [1, 2, 3] }, { id: { [Op.gt]: 10 } }]
},
{
id: {
[Op.or]: [[1, 2, 3], { [Op.gt]: 10 }]
},
name: 'a project'
},
{
id: {
[Op.or]: [[1, 2, 3], { [Op.gt]: 10 }]
},
name: 'a project'
},
{
name: 'a project',
type: {
[Op.and]: [['a', 'b'], { [Op.notLike]: '%z' }],
},
},
{
name: 'a project',
[Op.not]: [{ id: [1, 2, 3] }, { array: { [Op.contains]: [3, 4, 5] } }],
},
{
meta: {
video: {
url: {
[Op.ne]: null,
},
},
},
},
{
'meta.audio.length': {
[Op.gt]: 20,
},
},
{
[Op.and]: [{ id: [1, 2, 3] }, { array: { [Op.contains]: [3, 4, 5] } }],
},
{
[Op.gt]: fn('NOW'),
},
whereFn('test', { [Op.gt]: new Date() }),
literal('true'),
fn('LOWER', 'asd'),
{ [Op.lt]: Sequelize.literal('SOME_STRING') }
]).toMatchTypeOf<WhereOptions[]>();
// Relations / Associations
// Find all projects with a least one task where task.state === project.task
MyModel.findAll({
include: [
{
model: MyModel,
where: { state: Sequelize.col('project.state') },
},
],
});
{
const where: WhereOptions = 0 as any;
MyModel.findOne({
include: [
{
include: [{ model: MyModel, where }],
model: MyModel,
where,
},
],
where,
});
MyModel.destroy({ where });
MyModel.update({ hi: 1 }, { where });
// Where as having option
MyModel.findAll({ having: where });
}
// From https://sequelize.org/master/en/v4/docs/models-usage/
async function test() {
// find multiple entries
let projects: MyModel[] = await MyModel.findAll();
// search for specific attributes - hash usage
projects = await MyModel.findAll({ where: { name: 'A MyModel', enabled: true } })
// search within a specific range
projects = await MyModel.findAll({ where: { id: [1, 2, 3] } });
// locks
projects = await MyModel.findAll({ lock: Transaction.LOCK.KEY_SHARE });
// locks on model
projects = await MyModel.findAll({ lock: { level: Transaction.LOCK.KEY_SHARE, of: MyModel} });
}
MyModel.findAll({
where: {
id: {
// casting here to check a missing operator is not accepted as field name
[Op.eq]: 6, // id = 6
[Op.eq]: Sequelize.col('SOME_COL'), // id = <column>
[Op.and]: { a: 5 }, // AND (a = 5)
[Op.or]: [{ a: 5 }, { a: 6 }], // (a = 5 OR a = 6)
[Op.gt]: 6, // id > 6
[Op.gt]: Sequelize.col('SOME_COL'), // id > <column>
[Op.gte]: 6, // id >= 6
[Op.gte]: Sequelize.col('SOME_COL'), // id >= <column>
[Op.lt]: 10, // id < 10
[Op.lt]: Sequelize.col('SOME_COL'), // id < <column>
[Op.lte]: 10, // id <= 10
[Op.lte]: Sequelize.col('SOME_COL'), // id <= <column>
[Op.ne]: 20, // id != 20
[Op.between]: [6, 10] || [new Date(), new Date()] || ["2020-01-01", "2020-12-31"], // BETWEEN 6 AND 10
[Op.notBetween]: [11, 15], // NOT BETWEEN 11 AND 15
[Op.in]: [1, 2], // IN [1, 2]
[Op.notIn]: [1, 2], // NOT IN [1, 2]
[Op.like]: '%hat', // LIKE '%hat'
[Op.notLike]: '%hat', // NOT LIKE '%hat'
[Op.iLike]: '%hat', // ILIKE '%hat' (case insensitive) (PG only)
[Op.notILike]: '%hat', // NOT ILIKE '%hat' (PG only)
[Op.overlap]: [1, 2], // && [1, 2] (PG array overlap operator)
[Op.contains]: [1, 2], // @> [1, 2] (PG array contains operator)
[Op.contained]: [1, 2], // <@ [1, 2] (PG array contained by operator)
[Op.any]: [2, 3], // ANY ARRAY[2, 3]::INTEGER (PG only)
[Op.adjacent]: [1, 2],
[Op.strictLeft]: [1, 2],
[Op.strictRight]: [1, 2],
[Op.noExtendLeft]: [1, 2],
[Op.noExtendRight]: [1, 2],
[Op.values]: [1, 2],
} as WhereOperators,
status: {
[Op.not]: false, // status NOT FALSE
},
},
});
Sequelize.where(
Sequelize.cast(Sequelize.col('SOME_COL'), 'INTEGER'), {
[Op.lt]: Sequelize.literal('LIT'),
[Op.any]: Sequelize.literal('LIT'),
[Op.gte]: Sequelize.literal('LIT'),
[Op.lt]: Sequelize.literal('LIT'),
[Op.lte]: Sequelize.literal('LIT'),
[Op.ne]: Sequelize.literal('LIT'),
[Op.not]: Sequelize.literal('LIT'),
[Op.in]: Sequelize.literal('LIT'),
[Op.notIn]: Sequelize.literal('LIT'),
[Op.like]: Sequelize.literal('LIT'),
[Op.notLike]: Sequelize.literal('LIT'),
[Op.iLike]: Sequelize.literal('LIT'),
[Op.overlap]: Sequelize.literal('LIT'),
[Op.contains]: Sequelize.literal('LIT'),
[Op.contained]: Sequelize.literal('LIT'),
[Op.gt]: Sequelize.literal('LIT'),
[Op.notILike]: Sequelize.literal('LIT'),
}
)
Sequelize.where(Sequelize.col("ABS"), Op.is, null);
Sequelize.where(
Sequelize.fn("ABS", Sequelize.col("age")),
Op.like,
Sequelize.fn("ABS", Sequelize.col("age"))
);
Sequelize.where(Sequelize.col("ABS"), null);