All files / src where.ts

90.8% Statements 79/87
80% Branches 24/30
100% Functions 8/8
90.58% Lines 77/85

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 19422x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x           22x           22x                                           22x                 103x 27x     76x 80x 74x 74x   6x   70x       53x 6x   47x 47x 17x 17x     2x     2x     2x       2x     2x     2x   13x                 30x       170x 114x   56x 56x 49x 49x 8x     8x   41x 2x     46x 53x 46x                     139x                       22x 79x 79x 6x   73x                   22x 9x 9x 4x   5x     22x 2x         42x  
import { isDefined } from './is-defined.js'
import { isObject } from './is-object.js'
import auto from './auto.js'
import between from './between.js'
import distinct from './distinct.js'
import eq from './eq.js'
import gt from './gt.js'
import gte from './gte.js'
import in_ from './in.js'
import interpolate1 from './interpolate1.js'
import like from './like.js'
import logicalFalse from './logical-false.js'
import logicalTrue from './logical-true.js'
import lt from './lt.js'
import lte from './lte.js'
import ne from './ne.js'
import ng from './ng.js'
import nl from './nl.js'
import notBetween from './not-between.js'
import notDistinct from './not-distinct.js'
import notIn from './not-in.js'
import raw from './raw.js'
import S from './sanitised.js'
import template from './template.js'
import unique from './unique.js'
 
export type Where = S | Record<string, unknown>
 
export type t = Where
 
const unary = {
  $not: not
} as const
 
type Unary = typeof unary
 
const binary = {
  $between: between,
  $distinct: distinct,
  $eq: eq,
  $gt: gt,
  $gte: gte,
  $in: in_,
  $like: like,
  $lt: lt,
  $lte: lte,
  $ne: ne,
  $ng: ng,
  $nl: nl,
  $notBetween: notBetween,
  $notDistinct: notDistinct,
  $ndistinct: notDistinct,
  $notIn: notIn,
  $nin: notIn
} as const
 
type Binary = typeof binary
 
const logical = {
  $and: and,
  $or: or
} as const
 
type Logical = typeof logical
 
/** @returns single key in object iff object has single key, `undefined` otherwise. */
function single(value: unknown): undefined | string {
  if (!isObject(value)) {
    return undefined
  }
  let result: undefined | string
  for (const key in value) {
    if (result === undefined) {
      result = key
      continue
    }
    return undefined
  }
  return result
}
 
function visitEntry(key: string, value: unknown): undefined | S {
  if (typeof value === 'function') {
    return value(key)
  }
  const key_ = single(value)
  if (key_ && key_ in binary) {
    const value_ = (value as Record<string, unknown>)[key_]
    switch (key_) {
      case '$between':
      case '$notBetween':
        Iif (value_ == null) {
          return binary[key_](key, value_)
        }
        Iif (!Array.isArray(value_) || value_.length > 2) {
          throw new TypeError(`Invalid ${key_} parameters.`)
        }
        return binary[key_](key, [ value_[0], value_[1] ])
      case '$in':
      case '$nin':
      case '$notIn':
        Iif (value_ == null) {
          return binary[key_](key, value_)
        }
        Iif (!Array.isArray(value_)) {
          throw new TypeError(`Invalid ${key_} parameters.`)
        }
        return binary[key_](key, value_)
      default:
        return binary[key_ as Exclude<keyof Binary,
          | '$between'
          | '$notBetween'
          | '$in'
          | '$nin'
          | '$notIn'
        >](key, value_)
    }
  }
  return eq(key, value)
}
 
export function visit(value: unknown): S {
  if (value instanceof S) {
    return value
  }
  const key = single(value)
  if (key) {
    const value_ = (value as Record<string, unknown>)[key]
    if ((key in logical) && Array.isArray(value_)) {
      Iif (typeof logical[key as keyof Logical] !== 'function') {
        console.log('ERROR', key, Object.keys(logical), logical[key as keyof Logical], logical)
      }
      return logical[key as keyof Logical](...value_.map(visit))
    }
    if ((key in unary)) {
      return unary[key as keyof Unary](visit(value_)) as S
    }
  }
  if (isObject(value)) {
    const entries = Object.entries(value).map(_ => visitEntry(..._))
    return and(...entries)
  }
  return auto(value)
}
 
/**
 * @returns WHERE clause.
 *
 * Supports MondoDB-like query.
 */
export function where(value: Where): S {
  return visit(value)
}
 
/**
 * @returns terms joined with AND operator.
 *
 * `undefined` terms are filtered out.
 *
 * An empty list of terms returns logical true (1=1).
 *
 * Terms are deduplicated.
 */
export function and(...xs: (undefined | Where)[]): S {
  const xs_ = xs.filter(isDefined)
  if (xs_.length === 0) {
    return logicalTrue
  }
  return template`(${raw(interpolate1(unique(xs_.map(where)), raw(' and ')).join(''))})`
}
 
/**
 * @returns terms joined with OR operator.
 *
 * `undefined` terms are filtered out.
 *
 * An empty list of terms returns logical false (0=1).
 */
export function or(...xs: (undefined | Where)[]): S {
  const xs_ = xs.filter(isDefined)
  if (xs_.length === 0) {
    return logicalFalse
  }
  return template`(${raw(interpolate1(unique(xs_.map(where)), raw(' or ')).join(''))})`
}
 
export function not(rhs: undefined | Where): undefined | S {
  return isDefined(rhs) ?
    template`not (${where(rhs)})` :
    undefined
}
 
export default where