All files / src/expressions methods.ts

100% Statements 27/27
100% Branches 7/7
100% Functions 13/13
100% Lines 19/19

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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229                                  812x               687x 687x 687x         681x                           685x                           252x                       1841x                         163x                             147x     3x                                                       147x       752x                                 248x                         147x                         147x                                                                 147x 9x                             147x  
import { Cast, Col, Fn } from './call-expressions.ts';
import { Excluded, Json, Where } from './condition-expressions.ts';
import { SqlExpression, type SqlExpressionVisitor } from './expression.ts';
import { Exists, type Subquery } from './query-expressions.ts';
import { Over, Tuple } from './row-expressions.ts';
 
export { Cast, Col, Fn } from './call-expressions.ts';
export { Excluded, Json, Where } from './condition-expressions.ts';
export { SqlExpression, type SqlExpressionVisitor } from './expression.ts';
export { Exists, Subquery, WriteStatement, type SetOperationOptions } from './query-expressions.ts';
export { Over, Tuple } from './row-expressions.ts';
 
// Kept in the module of the factories: a second copy of this module (another package build) then
// has its own class, which the cross-copy instance check tests rely on.
/** Raw SQL fragment inserted into a query as-is. Created by {@link literal}. */
export class Literal extends SqlExpression {
  /** @internal */
  static override readonly kind = 'Literal';
  /** SQL text. */
  readonly val: string;
 
  /**
   * @param val - SQL text.
   */
  constructor(val: string) {
    super();
    this.val = val;
    Object.freeze(this);
  }
 
  /** @internal */
  accept<R>(visitor: SqlExpressionVisitor<R>): R {
    return visitor.visitLiteral(this);
  }
}
 
/**
 * Creates a raw SQL fragment. The text is not escaped — never pass user input.
 *
 * @param val - SQL text.
 * @returns Literal expression.
 * @example
 * ```ts
 * await Post.findAll({ order: [literal('"createdAt" DESC NULLS LAST')] });
 * ```
 */
export const literal = (val: string): Literal => new Literal(val);
 
/**
 * Creates an SQL function call. String arguments are written into the SQL as escaped literals, like
 * v6 (`COUNT('*')`, `concat_ws(' ', ...)`); other values are bound as parameters; expressions are rendered as is.
 *
 * @param name - Function name, e.g. `'lower'`.
 * @param args - Function arguments.
 * @returns Function call expression.
 * @example
 * ```ts
 * await User.findAll({ where: where(fn('lower', col('email')), 'a@b.c') });
 * ```
 */
export const fn = (name: string, ...args: readonly unknown[]): Fn => new Fn(name, args, true);
 
/**
 * Creates a column reference. Dots separate table and column: `'user.id'`.
 *
 * @param name - Column name, optionally prefixed with a table alias.
 * @returns Column expression.
 * @example
 * ```ts
 * await Post.findAll({ attributes: [[fn('count', col('Post.id')), 'total']], group: [col('Post.userId')] });
 * ```
 */
export const col = (name: string): Col => new Col(name);
 
/**
 * Creates a type cast `CAST(value AS type)`.
 *
 * @param val - Value or expression to cast.
 * @param type - PostgreSQL type name, e.g. `'integer'`.
 * @returns Cast expression.
 * @example
 * ```ts
 * await Product.findAll({ attributes: [[cast(col('price'), 'integer'), 'rounded']] });
 * ```
 */
export const cast = (val: unknown, type: string): Cast => new Cast(val, type);
 
/**
 * Accesses a JSON(B) value by path (`json('meta.a.b')`) or builds JSON conditions
 * (`json({ meta: { a: 1 } })`).
 *
 * @param conditionsOrPath - Dot-separated path, starting with the attribute name, or a conditions object.
 * @param value - Value to compare with when a path is given.
 * @returns JSON expression.
 * @example
 * ```ts
 * await User.findAll({ where: json('meta.address.city', 'Berlin') });
 * await User.findAll({ where: json({ meta: { active: true } }) });
 * ```
 */
export const json = (
  conditionsOrPath: string | object,
  value?: string | number | boolean | null,
): Json => new Json(conditionsOrPath, value);
 
/** Call signatures of {@link where}. */
export type WhereFactory = {
  /**
   * @param attribute - Left-hand expression.
   * @param logic - Right-hand value or condition object, compared with `=`.
   */
  (attribute: unknown, logic: unknown): Where;
  /**
   * @param attribute - Left-hand expression.
   * @param comparator - `Op.*` symbol or SQL operator string such as `'>='`.
   * @param logic - Right-hand value.
   */
  (attribute: unknown, comparator: string | symbol, logic: unknown): Where;
};
 
/**
 * Compares an expression with a value: `where(fn('lower', col('name')), 'bob')` or
 * `where(col('age'), Op.gt, 18)`.
 *
 * @returns Where expression.
 * @example
 * ```ts
 * await User.findAll({ where: where(fn('lower', col('name')), 'bob') });
 * await User.findAll({ where: where(col('age'), Op.gt, 18) });
 * ```
 */
export const where: WhereFactory = (
  attribute: unknown,
  ...rest: readonly [unknown] | readonly [string | symbol, unknown]
): Where =>
  rest.length === 1 ? new Where(attribute, '=', rest[0]) : new Where(attribute, rest[0], rest[1]);
 
/**
 * References the value proposed for insertion (`EXCLUDED."column"`) inside an `onConflict`
 * update. PostgreSQL extension.
 *
 * @postgrelize
 * @param attribute - Model attribute name.
 * @returns Excluded column expression.
 * @example
 * ```ts
 * const rows = [{ key: 'home', hits: 1 }];
 * await Product.bulkCreate(rows, {
 *   onConflict: { target: ['key'], action: { update: { hits: excluded('hits') } } },
 * });
 * ```
 */
export const excluded = (attribute: string): Excluded => new Excluded(attribute);
 
/**
 * `EXISTS (subquery)`. PostgreSQL extension.
 *
 * @postgrelize
 * @param subquery - Subquery from `Model.subquery()`.
 * @returns Condition usable as `where`.
 * @example
 * ```ts
 * await User.findAll({ where: exists(Post.subquery({ where: { userId: col('User.id') } })) });
 * ```
 */
export const exists = (subquery: Subquery): Exists => new Exists(subquery, false);
 
/**
 * `NOT EXISTS (subquery)`. PostgreSQL extension.
 *
 * @postgrelize
 * @param subquery - Subquery from `Model.subquery()`.
 * @returns Condition usable as `where`.
 * @example
 * ```ts
 * await User.findAll({ where: notExists(Post.subquery({ where: { userId: col('User.id') } })) });
 * ```
 */
export const notExists = (subquery: Subquery): Exists => new Exists(subquery, true);
 
/**
 * Window definition of {@link over}. PostgreSQL extension.
 *
 * @postgrelize
 * @example
 * ```ts
 * const window: WindowOptions = { partitionBy: ['userId'], orderBy: [['createdAt', 'DESC']] };
 * ```
 */
export type WindowOptions = Readonly<{
  /** `PARTITION BY` attributes or expressions. */
  partitionBy?: readonly unknown[];
  /** `ORDER BY` items: attribute, `[attribute, direction]` or expression. */
  orderBy?: readonly unknown[];
}>;
 
/**
 * Window function call: `over(fn('row_number'), { partitionBy: ['userId'], orderBy: [['createdAt', 'DESC']] })`.
 * PostgreSQL extension.
 *
 * @postgrelize
 * @param windowFunction - Function call, e.g. `fn('rank')`.
 * @param window - Partition and order.
 * @returns Expression usable in `attributes` and `order`.
 * @example
 * ```ts
 * await Post.findAll({
 *   attributes: ['id', [over(fn('row_number'), { partitionBy: ['userId'], orderBy: [['createdAt', 'DESC']] }), 'rank']],
 * });
 * ```
 */
export const over = (windowFunction: Fn | Literal, window: WindowOptions = {}): Over =>
  new Over(windowFunction, window.partitionBy ?? [], window.orderBy ?? []);
 
/**
 * Row constructor for row comparisons: `where(tuple(col('createdAt'), col('id')), Op.lt, tuple(date, id))`.
 * PostgreSQL extension.
 *
 * @postgrelize
 * @param items - Row items (values or expressions).
 * @returns Row expression.
 * @example
 * ```ts
 * const cursor = new Date();
 * await Post.findAll({ where: where(tuple(col('createdAt'), col('id')), Op.lt, tuple(cursor, 100)) });
 * ```
 */
export const tuple = (...items: readonly unknown[]): Tuple => new Tuple(items);