From a0fdd183eaae32c642c29fcc477528327a7270f4 Mon Sep 17 00:00:00 2001 From: Nathan Smith Date: Mon, 26 Feb 2024 09:22:37 -0500 Subject: [PATCH] Add array comparisons Add array contains, is contained, and overlaps comparisons. --- src/db/conditions.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/db/conditions.ts b/src/db/conditions.ts index 5d0a57d..417cc4e 100644 --- a/src/db/conditions.ts +++ b/src/db/conditions.ts @@ -62,6 +62,11 @@ export const or = (...conditions: (SQLFragment | Whereable)[]) => sql export const and = (...conditions: (SQLFragment | Whereable)[]) => sql`(${mapWithSeparator(conditions, sql` AND `, c => c)})`; export const not = (condition: SQLFragment | Whereable) => sql`(NOT ${condition})`; +// array conditions +export const arrayContains = (a: readonly T[]) => a.length > 0 ? sql`${self} @> ARRAY[${vals(a)}]` : sql`true`; +export const arrayIsContained = (a: readonly T[]) => sql`${self} <@ ARRAY[${vals(a)}]`; +export const arrayOverlaps = (a: readonly T[]) => a.length > 0 ? sql`${self} && ARRAY[${vals(a)}]` : sql`false`; + // things that aren't genuinely conditions type PluralisingIntervalUnit = 'microsecond' | 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year' | 'decade'; type IntervalUnit = PluralisingIntervalUnit | `${PluralisingIntervalUnit}s` | 'century' | 'centuries' | 'millennium' | 'millennia';