Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array operators as conditions #164

Open
pi-nathan opened this issue Feb 23, 2024 · 1 comment · May be fixed by #165
Open

Array operators as conditions #164

pi-nathan opened this issue Feb 23, 2024 · 1 comment · May be fixed by #165

Comments

@pi-nathan
Copy link

Postgres supports a few array operators that I think would fit as conditions: https://www.postgresql.org/docs/16/functions-array.html

Specifically the @>, <@, and && operators. I'm not entirely certain if it's possible to typecheck that the left-hand column is an array, but the actual implementation should be fairly straightforward? Something similar to this?

export const arrayContains = <T>(a: readonly T[]) => a.length > 0 ? sql<SQL, boolean | null, T>`${self} @> (${vals(a)})` : sql`false`;
export const arrayIsContained = <T>(a: readonly T[]) => a.length > 0 ? sql<SQL, boolean | null, T>`${self} <@ (${vals(a)})` : sql`false`;
export const arrayOverlaps = <T>(a: readonly T[]) => a.length > 0 ? sql<SQL, boolean | null, T>`${self} && (${vals(a)})` : sql`false`;

That syntax is probably slightly off. I'll probably end up making these for myself and can happily put up a PR once I have them functional.

@pi-nathan
Copy link
Author

pi-nathan commented Feb 23, 2024

This seems to work? At least in my very limited prototyping.

export const arrayContains = <T>(a: readonly T[]) =>
    a.length > 0 ? sql<SQL, boolean | null, T[]>`${self} @> ARRAY[${vals(a)}]` : sql`false`;
export const arrayIsContained = <T>(a: readonly T[]) =>
    a.length > 0 ? sql<SQL, boolean | null, T[]>`${self} <@ ARRAY[${vals(a)}]` : sql`false`;
export const arrayOverlaps = <T>(a: readonly T[]) =>
    a.length > 0 ? sql<SQL, boolean | null, T[]>`${self} && ARRAY[${vals(a)}]` : sql`false`;

@pi-nathan pi-nathan linked a pull request Feb 26, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant