|
| 1 | +# Conditions |
| 2 | + |
| 3 | +Build complex trigger conditions with type safety. |
| 4 | + |
| 5 | +## Basic Comparisons |
| 6 | + |
| 7 | +```typescript |
| 8 | +.when(c => c.NEW('status').eq('active')) // equals |
| 9 | +.when(c => c.NEW('price').gt(100)) // greater than |
| 10 | +.when(c => c.NEW('stock').gte(10)) // greater or equal |
| 11 | +.when(c => c.NEW('discount').lt(50)) // less than |
| 12 | +.when(c => c.NEW('quantity').lte(5)) // less or equal |
| 13 | +.when(c => c.NEW('email').ne('old@mail.com')) // not equal |
| 14 | +``` |
| 15 | + |
| 16 | +## Field Comparisons |
| 17 | + |
| 18 | +```typescript |
| 19 | +// Compare OLD vs NEW |
| 20 | +.when(c => c.OLD('email').ne(c.NEW('email'))) |
| 21 | + |
| 22 | +// Check if changed |
| 23 | +.when(c => c.changed('status')) |
| 24 | +.when(c => c.changed('email', 'phone')) // Any of these changed |
| 25 | +``` |
| 26 | + |
| 27 | +## Null Checks |
| 28 | + |
| 29 | +```typescript |
| 30 | +.when(c => c.NEW('deletedAt').isNull()) |
| 31 | +.when(c => c.NEW('approvedBy').isNotNull()) |
| 32 | +``` |
| 33 | + |
| 34 | +## Boolean Logic |
| 35 | + |
| 36 | +```typescript |
| 37 | +// AND |
| 38 | +.when(c => c.and( |
| 39 | + c.NEW('status').eq('published'), |
| 40 | + c.NEW('visibility').eq('public') |
| 41 | +)) |
| 42 | + |
| 43 | +// OR |
| 44 | +.when(c => c.or( |
| 45 | + c.NEW('priority').eq('high'), |
| 46 | + c.NEW('deadline').lt(new Date()) |
| 47 | +)) |
| 48 | + |
| 49 | +// NOT |
| 50 | +.when(c => c.not(c.NEW('archived').eq(true))) |
| 51 | +``` |
| 52 | + |
| 53 | +## Complex Conditions |
| 54 | + |
| 55 | +```typescript |
| 56 | +// Nested logic |
| 57 | +.when(c => c.and( |
| 58 | + c.changed('status'), |
| 59 | + c.or( |
| 60 | + c.and( |
| 61 | + c.OLD('status').eq('draft'), |
| 62 | + c.NEW('status').eq('published') |
| 63 | + ), |
| 64 | + c.and( |
| 65 | + c.OLD('status').eq('published'), |
| 66 | + c.NEW('status').eq('archived') |
| 67 | + ) |
| 68 | + ) |
| 69 | +)) |
| 70 | +``` |
| 71 | + |
| 72 | +## Pattern Matching |
| 73 | + |
| 74 | +```typescript |
| 75 | +// LIKE |
| 76 | +.when(c => c.NEW('email').like('%@company.com')) |
| 77 | + |
| 78 | +// IN |
| 79 | +.when(c => c.NEW('status').in(['active', 'pending', 'processing'])) |
| 80 | + |
| 81 | +// BETWEEN |
| 82 | +.when(c => c.NEW('age').between(18, 65)) |
| 83 | +``` |
| 84 | + |
| 85 | +## Real-World Examples |
| 86 | + |
| 87 | +### Order Status Workflow |
| 88 | + |
| 89 | +```typescript |
| 90 | +triggers |
| 91 | + .for('order') |
| 92 | + .after() |
| 93 | + .on('UPDATE') |
| 94 | + .when(c => c.and( |
| 95 | + c.changed('status'), |
| 96 | + c.OLD('status').eq('payment_pending'), |
| 97 | + c.NEW('status').eq('payment_confirmed'), |
| 98 | + c.NEW('amount').gt(0) |
| 99 | + )) |
| 100 | + .notify('order_paid') |
| 101 | + .build(); |
| 102 | +``` |
| 103 | + |
| 104 | +### User Account Changes |
| 105 | + |
| 106 | +```typescript |
| 107 | +triggers |
| 108 | + .for('user') |
| 109 | + .after() |
| 110 | + .on('UPDATE') |
| 111 | + .when(c => c.or( |
| 112 | + c.changed('email'), |
| 113 | + c.changed('password'), |
| 114 | + c.changed('twoFactorEnabled') |
| 115 | + )) |
| 116 | + .notify('security_change') |
| 117 | + .build(); |
| 118 | +``` |
| 119 | + |
| 120 | +### Inventory Alerts |
| 121 | + |
| 122 | +```typescript |
| 123 | +triggers |
| 124 | + .for('product') |
| 125 | + .after() |
| 126 | + .on('UPDATE') |
| 127 | + .when(c => c.and( |
| 128 | + c.NEW('stock').lt(10), |
| 129 | + c.OLD('stock').gte(10), |
| 130 | + c.NEW('active').eq(true) |
| 131 | + )) |
| 132 | + .notify('low_stock_alert') |
| 133 | + .build(); |
| 134 | +``` |
| 135 | + |
| 136 | +## Raw SQL Escape Hatch |
| 137 | + |
| 138 | +```typescript |
| 139 | +// For complex conditions not supported by the builder |
| 140 | +.when('NEW.data::jsonb @> \'{"featured": true}\'::jsonb') |
| 141 | +``` |
| 142 | + |
| 143 | +## Next: [Real-time Events](./realtime-events.md) |
0 commit comments