Skip to content

Add and and or logical operators to WHERE clause operations #565

@anatolzak

Description

@anatolzak

Summary

The WHERE clause callback currently provides comparison operations (eq, ne, gt, exists, etc.) but no logical operators. Users who need AND/OR must manually compose template literal strings:

.where(({ from, to }, { lte, gte, notExists }) => {
  return `${lte(from, epoch)} AND (${gte(to, epoch)} OR ${notExists(to)})`;
})

This proposal adds and and or as first-class operations:

.where(({ from, to }, { lte, gte, notExists, and, or }) => {
  return and(lte(from, epoch), or(gte(to, epoch), notExists(to)));
})

Motivation

  • Consistency — comparison operators are functions, but logical composition requires string interpolation
  • Composability — and/or can be nested arbitrarily: or(and(...), and(...))
  • Conditional expressions — accepts undefined values (filtered out), enabling Drizzle ORM-style patterns:
.where(({ animal, dangerous }, { eq, exists, or }) =>
  or(
    eq(animal, "Cow"),
    shouldFilterDangerous ? exists(dangerous) : undefined,
  )
)

Behavior

  • and(...expressions) / or(...expressions) — joins with AND / OR, wrapped in parentheses
  • Single argument — returns unwrapped
  • No arguments or all undefined — returns empty string
  • Accepts (string | undefined)[]
  • Fully backward compatible — existing template literal usage continues to work

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions