You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: "A guide to using event filters to create more precise and tailored job triggers."
3
+
description: "They are declarative pattern-matching rules, modeled after [AWS EventBridge patterns](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html)."
4
4
---
5
5
6
-
## What are Event Filters?
6
+
Given the following custom event payload:
7
7
8
-
Event filters provide a powerful way to define specific conditions that determine when a job should be triggered based on incoming events. Filters allow you to narrow down event processing to only those events that match certain criteria, ensuring that your jobs are executed in a targeted manner.
8
+
```json
9
+
{
10
+
"uid": "jexAgaeJFJsrGfans1pxqm",
11
+
"type": "15 Min Meeting",
12
+
"price": 0,
13
+
"title": "15 Min Meeting between Eric Allam and John Doe",
14
+
"length": 15,
15
+
"status": "ACCEPTED",
16
+
"endTime": "2023-01-25T16:00:00Z",
17
+
"bookingId": 198052,
18
+
"organizer": {
19
+
"id": 32794,
20
+
"name": "Eric Allam",
21
+
"email": "eric@trigger.dev",
22
+
"language": { "locale": "en" },
23
+
"timeZone": "Europe/London"
24
+
}
25
+
}
26
+
```
9
27
10
-
we will cover the various types of event filters and how they can be used to create more precise triggers.
28
+
The following event filter would match the event:
11
29
12
-
### Event Filter Types
30
+
```json
31
+
{
32
+
"type": ["15 Min Meeting"],
33
+
"status": ["ACCEPTED", "REJECTED"],
34
+
"organizer": {
35
+
"name": ["Eric Allam"]
36
+
}
37
+
}
38
+
```
13
39
14
-
#### 1. String Filters
40
+
For an event pattern to match an event, the event must contain all the field names listed in the event pattern. The field names must also appear in the event with the same nesting structure.
41
+
42
+
The value of each field name in the event pattern must be an array of strings, numbers, or booleans. The event pattern matches the event if the value of the field name in the event is equal to any of the values in the array.
43
+
44
+
Effectively, each array is an OR condition, and the entire event pattern is an AND condition.
45
+
46
+
So the above event filter will match because `status == "ACCEPTED"`, and it would also match if `status == "REJECTED"`.
47
+
48
+
### String Filters
15
49
16
50
String filters are used to match string values within the event payload. You can filter events based on exact string matches, case-insensitive matches, starts-with, ends-with, and more.
17
51
18
-
**Examples of string filters:**
52
+
**Examples:**
19
53
20
54
-`name: ["John"]`: Triggers the job if the name field in the event payload is exactly "John".
21
55
-`name: [{ $startsWith: "Jo" }]`: Triggers the job if the name field starts with "Jo".
@@ -64,11 +98,11 @@ client.defineJob({
64
98
});
65
99
```
66
100
67
-
### 2. Boolean Filters
101
+
### Boolean Filters
68
102
69
103
Boolean filters are used to filter events based on boolean values within the event payload.
70
104
71
-
**_Examples of boolean filters:_**
105
+
**Examples:**
72
106
73
107
-`paidPlan: [true]`: Triggers the job if the `PaidPlan` field in the event payload is `true`.
74
108
-`isAdmin: [false]`: Triggers the job if the `isAdmin` field in the event payload is `false`.
@@ -116,17 +150,19 @@ client.defineJob({
116
150
});
117
151
```
118
152
119
-
### 3. Number Filters
153
+
### Number Filters
120
154
121
-
Number filters are used to filter events based on numeric values within the event payload.
155
+
Number filters are used to filter events based on numeric values within the event payload. It can also be used to perform numeric comparisons on values within the event payload.
122
156
123
-
**_Examples of number filters:_**
157
+
**Examples:**
124
158
125
159
-`age [18]`: Triggers the job if the `age` field in the event payload is equal to `18`.
126
160
-`score: [{ $gt: 90 }]` : Triggers the job if the score field is greater than `90`.
127
161
-`score: [{ $gte: 90 }]` : Triggers the job if the score field is greater than or equal to `90`.
128
162
-`score: [{ $lt: 90 }]` : Triggers the job if the score field is less than `90`.
129
163
-`score: [{ $lte: 90 }]` : Triggers the job if the score field is less than or equal to `90`.
164
+
-`age: [{ $gt: 20 }, { $lt: 40 }]`: Triggers the job if the `age` field is greater than 20 and less than 40.
165
+
-`score: [{ $between: [90, 110] }]`: Triggers the job if the `score` field is between 90 and 110.
130
166
131
167
#### Examples of number filters used in a Trigger.
132
168
@@ -168,9 +204,47 @@ client.defineJob({
168
204
});
169
205
```
170
206
171
-
### 4. Content Filters
207
+
```ts
208
+
client.defineJob({
209
+
id: "send-discount-code",
210
+
name: "Send Discount Code",
211
+
version: "1.0.0",
212
+
trigger: eventTrigger({
213
+
name: "user.purchase",
214
+
schema: z.object({
215
+
userId: z.string(),
216
+
age: z.number(),
217
+
}),
218
+
filter: {
219
+
age: [{ $gt: 18 }, { $lt: 30 }], // Only trigger for users aged between 18 and 30
220
+
},
221
+
}),
222
+
run: async (payload, io, ctx) => {},
223
+
});
224
+
```
225
+
226
+
```ts
227
+
client.defineJob({
228
+
id: "update-user-level",
229
+
name: "Update User Level",
230
+
version: "1.0.0",
231
+
trigger: eventTrigger({
232
+
name: "user.scoreUpdated",
233
+
schema: z.object({
234
+
userId: z.string(),
235
+
score: z.number(),
236
+
}),
237
+
filter: {
238
+
score: [{ $between: [50, 100] }], // Only trigger for scores between 50 and 100 (inclusive)
239
+
},
240
+
}),
241
+
run: async (payload, io, ctx) => {},
242
+
});
243
+
```
172
244
173
-
Content filters are used to filter events based on the content of arrays within the event payload.
245
+
### Array Filters
246
+
247
+
Array filters are used to filter events based on the content of arrays within the event payload.
174
248
175
249
**Examples:**
176
250
@@ -199,7 +273,7 @@ client.defineJob({
199
273
});
200
274
```
201
275
202
-
### 5. Existence Filters
276
+
### Existence Filters
203
277
204
278
Existence filters are used to filter events based on the presence or absence of certain keys within the event payload.
205
279
@@ -249,58 +323,9 @@ client.defineJob({
249
323
});
250
324
```
251
325
252
-
### 6. Comparison Filters
253
-
254
-
Comparison filters are used to perform numeric comparisons on values within the event payload.
255
-
256
-
**Examples:**
257
-
258
-
-`age: [{ $gt: 20 }, { $lt: 40 }]`: Triggers the job if the `age` field is greater than 20 and less than 40.
259
-
-`score: [{ $between: [90, 110] }]`: Triggers the job if the `score` field is between 90 and 110.
260
-
261
-
#### Examples of comparison filters being used in a Trigger.
262
-
263
-
```ts
264
-
client.defineJob({
265
-
id: "send-discount-code",
266
-
name: "Send Discount Code",
267
-
version: "1.0.0",
268
-
trigger: eventTrigger({
269
-
name: "user.purchase",
270
-
schema: z.object({
271
-
userId: z.string(),
272
-
age: z.number(),
273
-
}),
274
-
filter: {
275
-
age: [{ $gt: 18 }, { $lt: 30 }], // Only trigger for users aged between 18 and 30
276
-
},
277
-
}),
278
-
run: async (payload, io, ctx) => {},
279
-
});
280
-
```
281
-
282
-
```ts
283
-
client.defineJob({
284
-
id: "update-user-level",
285
-
name: "Update User Level",
286
-
version: "1.0.0",
287
-
trigger: eventTrigger({
288
-
name: "user.scoreUpdated",
289
-
schema: z.object({
290
-
userId: z.string(),
291
-
score: z.number(),
292
-
}),
293
-
filter: {
294
-
score: [{ $between: [50, 100] }], // Only trigger for scores between 50 and 100 (inclusive)
295
-
},
296
-
}),
297
-
run: async (payload, io, ctx) => {},
298
-
});
299
-
```
300
-
301
-
### 7. Combining Filters
326
+
### Combining Filters
302
327
303
-
Filters can be combined to create more complex conditions for triggering jobs. For example:
328
+
Filters can be combined to create more complex conditions for triggering jobs.
304
329
305
330
**Examples:**
306
331
@@ -355,55 +380,9 @@ client.defineJob({
355
380
});
356
381
```
357
382
358
-
### Other examples
359
-
360
-
Given the following custom event payload:
361
-
362
-
```json
363
-
{
364
-
"uid": "jexAgaeJFJsrGfans1pxqm",
365
-
"type": "15 Min Meeting",
366
-
"price": 0,
367
-
"title": "15 Min Meeting between Eric Allam and John Doe",
368
-
"length": 15,
369
-
"status": "ACCEPTED",
370
-
"endTime": "2023-01-25T16:00:00Z",
371
-
"bookingId": 198052,
372
-
"organizer": {
373
-
"id": 32794,
374
-
"name": "Eric Allam",
375
-
"email": "eric@trigger.dev",
376
-
"language": { "locale": "en" },
377
-
"timeZone": "Europe/London"
378
-
}
379
-
}
380
-
```
381
-
382
-
The following event filter would match the event:
383
+
### Stripe Trigger with Event Filter
383
384
384
-
```json
385
-
{
386
-
"type": ["15 Min Meeting"],
387
-
"status": ["ACCEPTED", "REJECTED"],
388
-
"organizer": {
389
-
"name": ["Eric Allam"]
390
-
}
391
-
}
392
-
```
393
-
394
-
For an event pattern to match an event, the event must contain all the field names listed in the event pattern. The field names must also appear in the event with the same nesting structure.
395
-
396
-
The value of each field name in the event pattern must be an array of strings, numbers, or booleans. The event pattern matches the event if the value of the field name in the event is equal to any of the values in the array.
397
-
398
-
Effectively, each array is an OR condition, and the entire event pattern is an AND condition.
399
-
400
-
So the above event filter will match because status == "ACCEPTED", and it would also match if status == "REJECTED".
401
-
402
-
## Other types of triggers that can also support event filters.
403
-
404
-
#### 1. Stripe Trigger with Event Filter
405
-
406
-
**Here is an example of a Stripe Trigger with an Event Filter:**
385
+
Here is an example of a Stripe Trigger with an Event Filter:
407
386
408
387
```ts
409
388
client.defineJob({
@@ -425,9 +404,9 @@ The job is triggered by the `onCustomerSubscriptionCreated` event from Stripe. I
425
404
426
405
- Currency: Only subscriptions with the currency "USD" will trigger this job.
427
406
428
-
#### 2. Supabase trigger with Event Filter
407
+
### Supabase trigger with Event Filter
429
408
430
-
**Here is an example of a Supabase Trigger with an Event Filter:**
409
+
Here is an example of a Supabase Trigger with an Event Filter:
0 commit comments