Commit f72cbca
authored
feat: Add ilike and trigram similarity search to pagination (#431)
# Why
This PR adds search capabilities to the existing cursor-based pagination system added in #422 which only supported orderBy ordering.
This adds:
1. Case-insensitive pattern matching (ILIKE) - Useful for basic text search across entity fields
2. Trigram similarity search - Provides fuzzy matching capabilities for more advanced search use cases like handling typos or finding similar text
This enables building user-facing search features while maintaining the benefits of cursor-based pagination (stable results, efficient queries).
# How
The implementation introduces a unified PaginationSpecification that supports three strategies:
1. Standard pagination - The existing orderBy-based pagination
2. ILIKE search - Pattern matching with automatic wildcard escaping
3. Trigram search - PostgreSQL trigram similarity with configurable threshold
Key implementation details:
- Search terms are properly parameterized to prevent SQL injection
- ILIKE special characters (%, _, \) are escaped to prevent pattern injection
- Results maintain stable cursor-based pagination with proper ordering:
- ILIKE: Ordered by search fields, then ID
- Trigram: Ordered by exact match priority, similarity score, optional extra fields, then ID
# Test Plan
The PR includes comprehensive test coverage:
- Unit tests for both AuthorizationResultBasedKnexEntityLoader and
EnforcingKnexEntityLoader
- Integration tests covering:
- Forward/backward pagination with search
- Multi-field search
- Case-insensitive matching
- Trigram similarity with various thresholds
- Cursor stability across pages
- Edge cases (empty results, special characters)
- Combined with WHERE clauses
- Security validation ensuring proper escaping and parameterization
All existing tests pass, confirming backward compatibility for standard pagination when using the new API.1 parent fb72061 commit f72cbca
10 files changed
Lines changed: 2461 additions & 510 deletions
File tree
- packages/entity-database-adapter-knex/src
- __integration-tests__
- __testfixtures__
- __tests__
- internal
Lines changed: 105 additions & 23 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
76 | 77 | | |
77 | 78 | | |
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 | + | |
79 | 153 | | |
80 | | - | |
| 154 | + | |
81 | 155 | | |
82 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
83 | 168 | | |
84 | 169 | | |
85 | 170 | | |
| |||
89 | 174 | | |
90 | 175 | | |
91 | 176 | | |
92 | | - | |
| 177 | + | |
93 | 178 | | |
94 | | - | |
| 179 | + | |
95 | 180 | | |
96 | 181 | | |
97 | 182 | | |
98 | | - | |
| 183 | + | |
99 | 184 | | |
100 | | - | |
| 185 | + | |
101 | 186 | | |
102 | 187 | | |
103 | | - | |
| 188 | + | |
104 | 189 | | |
105 | | - | |
| 190 | + | |
106 | 191 | | |
107 | 192 | | |
108 | 193 | | |
109 | 194 | | |
110 | | - | |
| 195 | + | |
111 | 196 | | |
112 | 197 | | |
113 | 198 | | |
114 | 199 | | |
115 | 200 | | |
116 | | - | |
| 201 | + | |
117 | 202 | | |
118 | | - | |
| 203 | + | |
119 | 204 | | |
120 | 205 | | |
121 | | - | |
| 206 | + | |
122 | 207 | | |
123 | | - | |
| 208 | + | |
124 | 209 | | |
125 | 210 | | |
126 | 211 | | |
127 | 212 | | |
128 | | - | |
| 213 | + | |
129 | 214 | | |
130 | 215 | | |
131 | 216 | | |
132 | 217 | | |
133 | 218 | | |
134 | | - | |
| 219 | + | |
135 | 220 | | |
136 | 221 | | |
137 | 222 | | |
138 | 223 | | |
139 | 224 | | |
140 | | - | |
141 | | - | |
| 225 | + | |
| 226 | + | |
142 | 227 | | |
143 | 228 | | |
144 | 229 | | |
| |||
267 | 352 | | |
268 | 353 | | |
269 | 354 | | |
270 | | - | |
| 355 | + | |
271 | 356 | | |
272 | 357 | | |
273 | 358 | | |
274 | 359 | | |
275 | | - | |
| 360 | + | |
276 | 361 | | |
277 | 362 | | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
| 363 | + | |
282 | 364 | | |
283 | 365 | | |
284 | 366 | | |
| |||
Lines changed: 4 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
| 194 | + | |
| 195 | + | |
215 | 196 | | |
216 | 197 | | |
217 | | - | |
| 198 | + | |
218 | 199 | | |
219 | 200 | | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
| 201 | + | |
225 | 202 | | |
226 | 203 | | |
227 | 204 | | |
| |||
Lines changed: 32 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
Lines changed: 21 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
113 | | - | |
114 | | - | |
115 | 113 | | |
| 114 | + | |
| 115 | + | |
116 | 116 | | |
117 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
118 | 125 | | |
119 | | - | |
120 | | - | |
121 | 126 | | |
122 | 127 | | |
123 | 128 | | |
| |||
216 | 221 | | |
217 | 222 | | |
218 | 223 | | |
219 | | - | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
220 | 227 | | |
221 | 228 | | |
222 | | - | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
223 | 237 | | |
224 | 238 | | |
225 | 239 | | |
| |||
0 commit comments