Skip to content

Commit 2f4df87

Browse files
authored
chore: Mark array filter values readonly (#443)
1 parent c95035a commit 2f4df87

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

src/PostgrestFilterBuilder.ts

+39-24
Original file line numberDiff line numberDiff line change
@@ -124,28 +124,34 @@ export default class PostgrestFilterBuilder<
124124
return this
125125
}
126126

127-
likeAllOf<ColumnName extends string & keyof Row>(column: ColumnName, patterns: string[]): this
128-
likeAllOf(column: string, patterns: string[]): this
127+
likeAllOf<ColumnName extends string & keyof Row>(
128+
column: ColumnName,
129+
patterns: readonly string[]
130+
): this
131+
likeAllOf(column: string, patterns: readonly string[]): this
129132
/**
130133
* Match only rows where `column` matches all of `patterns` case-sensitively.
131134
*
132135
* @param column - The column to filter on
133136
* @param patterns - The patterns to match with
134137
*/
135-
likeAllOf(column: string, patterns: string[]): this {
138+
likeAllOf(column: string, patterns: readonly string[]): this {
136139
this.url.searchParams.append(column, `like(all).{${patterns.join(',')}}`)
137140
return this
138141
}
139142

140-
likeAnyOf<ColumnName extends string & keyof Row>(column: ColumnName, patterns: string[]): this
141-
likeAnyOf(column: string, patterns: string[]): this
143+
likeAnyOf<ColumnName extends string & keyof Row>(
144+
column: ColumnName,
145+
patterns: readonly string[]
146+
): this
147+
likeAnyOf(column: string, patterns: readonly string[]): this
142148
/**
143149
* Match only rows where `column` matches any of `patterns` case-sensitively.
144150
*
145151
* @param column - The column to filter on
146152
* @param patterns - The patterns to match with
147153
*/
148-
likeAnyOf(column: string, patterns: string[]): this {
154+
likeAnyOf(column: string, patterns: readonly string[]): this {
149155
this.url.searchParams.append(column, `like(any).{${patterns.join(',')}}`)
150156
return this
151157
}
@@ -163,28 +169,34 @@ export default class PostgrestFilterBuilder<
163169
return this
164170
}
165171

166-
ilikeAllOf<ColumnName extends string & keyof Row>(column: ColumnName, patterns: string[]): this
167-
ilikeAllOf(column: string, patterns: string[]): this
172+
ilikeAllOf<ColumnName extends string & keyof Row>(
173+
column: ColumnName,
174+
patterns: readonly string[]
175+
): this
176+
ilikeAllOf(column: string, patterns: readonly string[]): this
168177
/**
169178
* Match only rows where `column` matches all of `patterns` case-insensitively.
170179
*
171180
* @param column - The column to filter on
172181
* @param patterns - The patterns to match with
173182
*/
174-
ilikeAllOf(column: string, patterns: string[]): this {
183+
ilikeAllOf(column: string, patterns: readonly string[]): this {
175184
this.url.searchParams.append(column, `ilike(all).{${patterns.join(',')}}`)
176185
return this
177186
}
178187

179-
ilikeAnyOf<ColumnName extends string & keyof Row>(column: ColumnName, patterns: string[]): this
180-
ilikeAnyOf(column: string, patterns: string[]): this
188+
ilikeAnyOf<ColumnName extends string & keyof Row>(
189+
column: ColumnName,
190+
patterns: readonly string[]
191+
): this
192+
ilikeAnyOf(column: string, patterns: readonly string[]): this
181193
/**
182194
* Match only rows where `column` matches any of `patterns` case-insensitively.
183195
*
184196
* @param column - The column to filter on
185197
* @param patterns - The patterns to match with
186198
*/
187-
ilikeAnyOf(column: string, patterns: string[]): this {
199+
ilikeAnyOf(column: string, patterns: readonly string[]): this {
188200
this.url.searchParams.append(column, `ilike(any).{${patterns.join(',')}}`)
189201
return this
190202
}
@@ -211,15 +223,18 @@ export default class PostgrestFilterBuilder<
211223
return this
212224
}
213225

214-
in<ColumnName extends string & keyof Row>(column: ColumnName, values: Row[ColumnName][]): this
215-
in(column: string, values: unknown[]): this
226+
in<ColumnName extends string & keyof Row>(
227+
column: ColumnName,
228+
values: ReadonlyArray<Row[ColumnName]>
229+
): this
230+
in(column: string, values: readonly unknown[]): this
216231
/**
217232
* Match only rows where `column` is included in the `values` array.
218233
*
219234
* @param column - The column to filter on
220235
* @param values - The values array to filter with
221236
*/
222-
in(column: string, values: unknown[]): this {
237+
in(column: string, values: readonly unknown[]): this {
223238
const cleanedValues = values
224239
.map((s) => {
225240
// handle postgrest reserved characters
@@ -234,17 +249,17 @@ export default class PostgrestFilterBuilder<
234249

235250
contains<ColumnName extends string & keyof Row>(
236251
column: ColumnName,
237-
value: string | Row[ColumnName][] | Record<string, unknown>
252+
value: string | ReadonlyArray<Row[ColumnName]> | Record<string, unknown>
238253
): this
239-
contains(column: string, value: string | unknown[] | Record<string, unknown>): this
254+
contains(column: string, value: string | readonly unknown[] | Record<string, unknown>): this
240255
/**
241256
* Only relevant for jsonb, array, and range columns. Match only rows where
242257
* `column` contains every element appearing in `value`.
243258
*
244259
* @param column - The jsonb, array, or range column to filter on
245260
* @param value - The jsonb, array, or range value to filter with
246261
*/
247-
contains(column: string, value: string | unknown[] | Record<string, unknown>): this {
262+
contains(column: string, value: string | readonly unknown[] | Record<string, unknown>): this {
248263
if (typeof value === 'string') {
249264
// range types can be inclusive '[', ']' or exclusive '(', ')' so just
250265
// keep it simple and accept a string
@@ -261,17 +276,17 @@ export default class PostgrestFilterBuilder<
261276

262277
containedBy<ColumnName extends string & keyof Row>(
263278
column: ColumnName,
264-
value: string | Row[ColumnName][] | Record<string, unknown>
279+
value: string | ReadonlyArray<Row[ColumnName]> | Record<string, unknown>
265280
): this
266-
containedBy(column: string, value: string | unknown[] | Record<string, unknown>): this
281+
containedBy(column: string, value: string | readonly unknown[] | Record<string, unknown>): this
267282
/**
268283
* Only relevant for jsonb, array, and range columns. Match only rows where
269284
* every element appearing in `column` is contained by `value`.
270285
*
271286
* @param column - The jsonb, array, or range column to filter on
272287
* @param value - The jsonb, array, or range value to filter with
273288
*/
274-
containedBy(column: string, value: string | unknown[] | Record<string, unknown>): this {
289+
containedBy(column: string, value: string | readonly unknown[] | Record<string, unknown>): this {
275290
if (typeof value === 'string') {
276291
// range
277292
this.url.searchParams.append(column, `cd.${value}`)
@@ -360,17 +375,17 @@ export default class PostgrestFilterBuilder<
360375

361376
overlaps<ColumnName extends string & keyof Row>(
362377
column: ColumnName,
363-
value: string | Row[ColumnName][]
378+
value: string | ReadonlyArray<Row[ColumnName]>
364379
): this
365-
overlaps(column: string, value: string | unknown[]): this
380+
overlaps(column: string, value: string | readonly unknown[]): this
366381
/**
367382
* Only relevant for array and range columns. Match only rows where
368383
* `column` and `value` have an element in common.
369384
*
370385
* @param column - The array or range column to filter on
371386
* @param value - The array or range value to filter with
372387
*/
373-
overlaps(column: string, value: string | unknown[]): this {
388+
overlaps(column: string, value: string | readonly unknown[]): this {
374389
if (typeof value === 'string') {
375390
// range
376391
this.url.searchParams.append(column, `ov.${value}`)

test/filters.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ test('is', async () => {
312312
})
313313

314314
test('in', async () => {
315-
const res = await postgrest.from('users').select('status').in('status', ['ONLINE', 'OFFLINE'])
315+
const statuses = ['ONLINE', 'OFFLINE'] as const
316+
const res = await postgrest.from('users').select('status').in('status', statuses)
316317
expect(res).toMatchInlineSnapshot(`
317318
Object {
318319
"count": null,

0 commit comments

Comments
 (0)