All files / src/expressions trigram-helpers.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 3/3
100% Lines 6/6

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                                132x 62x                           132x 62x                             132x 123x  
import { Fn } from './call-expressions.ts';
import { Where } from './condition-expressions.ts';
 
/**
 * `similarity(a, b)` of `pg_trgm`: trigram similarity from 0 to 1. PostgreSQL extension; needs
 * `CREATE EXTENSION pg_trgm`.
 *
 * @postgrelize
 * @param left - Text expression, e.g. `col('name')`.
 * @param right - Text expression or value (bound as a parameter).
 * @returns Function call expression.
 * @example
 * ```ts
 * await User.findAll({ order: [[trigramSimilarity(col('name'), 'jon'), 'DESC']], limit: 5 });
 * ```
 */
export const trigramSimilarity = (left: unknown, right: unknown): Fn =>
  new Fn('similarity', [left, right]);
 
/**
 * `word_similarity(a, b)` of `pg_trgm`: similarity of `a` to the best matching part of `b`. PostgreSQL extension.
 *
 * @postgrelize
 * @param left - Text expression or value, the searched word.
 * @param right - Text expression, e.g. `col('body')`.
 * @returns Function call expression.
 * @example
 * ```ts
 * await Post.findAll({ attributes: [[trigramWordSimilarity('postgre', col('title')), 'score']] });
 * ```
 */
export const trigramWordSimilarity = (left: unknown, right: unknown): Fn =>
  new Fn('word_similarity', [left, right]);
 
/**
 * `left <-> right` of `pg_trgm`: trigram distance (`1 - similarity`), served by GiST trigram
 * indexes for nearest-neighbour `order`. PostgreSQL extension.
 *
 * @postgrelize
 * @param left - Text expression, e.g. `col('name')`.
 * @param right - Text expression or value (bound as a parameter).
 * @returns Where expression usable in `order` and `attributes`.
 * @example
 * ```ts
 * await User.findAll({ order: [[trigramDistance(col('name'), 'jon'), 'ASC']], limit: 5 });
 * ```
 */
export const trigramDistance = (left: unknown, right: unknown): Where =>
  new Where(left, '<->', right);