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 | 132x 132x 132x 132x 792x 221x 571x 132x 539x 132x 12x 2x 2x 132x 185x 2x 183x 132x 138x 132x 62x 132x 138x 132x 62x 132x 139x 132x 371x 371x 371x 132x 331x 132x 77x 75x 2x | import { Cast, Fn } from './call-expressions.ts';
import { Literal } from './methods.ts';
const CONFIG_NAME = /^[A-Za-z_][\w$]*(?:\.[A-Za-z_][\w$]*)?$/;
const WEIGHTS: ReadonlySet<string> = new Set(['A', 'B', 'C', 'D']);
const MAX_NORMALIZATION = 63;
/**
* Options of {@link tsRank}. PostgreSQL extension.
*
* @postgrelize
* @example
* ```ts
* const options: TsRankOptions = { cd: true, weights: [0.1, 0.2, 0.4, 1], normalization: 1 };
* ```
*/
export type TsRankOptions = Readonly<{
/** Uses `ts_rank_cd` (cover density) instead of `ts_rank`. */
cd?: boolean;
/** Weights of the labels `D`, `C`, `B`, `A`, in this order. */
weights?: readonly [number, number, number, number];
/** Normalization bit mask, an integer from 0 to 63. */
normalization?: number;
}>;
/**
* Options of {@link tsHeadline}. PostgreSQL extension.
*
* @postgrelize
* @example
* ```ts
* const options: TsHeadlineOptions = { config: 'english', options: 'StartSel=<b>, StopSel=</b>' };
* ```
*/
export type TsHeadlineOptions = Readonly<{
/** Text search configuration, e.g. `'english'`. */
config?: string;
/** `ts_headline` options string, e.g. `'StartSel=<b>, StopSel=</b>'`. Bound as a parameter. */
options?: string;
}>;
const regconfig = (config: string): Cast => {
if (typeof config !== 'string' || !CONFIG_NAME.test(config)) {
throw new Error(`Invalid text search configuration: ${String(config)}`);
}
return new Cast(config, 'regconfig');
};
const withConfig = (name: string, text: unknown, config: string | undefined): Fn =>
new Fn(name, config === undefined ? [text] : [regconfig(config), text]);
const weightsOf = (weights: readonly number[]): Cast => {
if (weights.length !== 4 || !weights.every((weight) => Number.isFinite(weight))) {
throw new Error('weights must be four finite numbers');
}
return new Cast([...weights], 'real[]');
};
const normalizationOf = (value: number): Cast => {
if (!Number.isInteger(value) || value < 0 || value > MAX_NORMALIZATION) {
throw new Error('normalization must be an integer from 0 to 63');
}
return new Cast(value, 'integer');
};
/**
* `to_tsvector([config,] document)`: parses a document into a `tsvector`. PostgreSQL extension.
*
* @postgrelize
* @param document - Text value (bound as a parameter) or an expression such as `col('body')`.
* @param config - Text search configuration; the server default when omitted.
* @returns Function call expression.
* @throws Error when `config` is not a plain or schema-qualified identifier.
* @example
* ```ts
* await Post.findAll({ where: where(toTsVector(col('body'), 'english'), Op.match, websearchToTsQuery('cat -dog', 'english')) });
* ```
*/
export const toTsVector = (document: unknown, config?: string): Fn =>
withConfig('to_tsvector', document, config);
/**
* `to_tsquery([config,] query)`: parses `tsquery` syntax (`&`, `|`, `!`, `<->`). PostgreSQL extension.
*
* @postgrelize
* @param query - Query text in `tsquery` syntax, bound as a parameter. Invalid syntax is a database error.
* @param config - Text search configuration; the server default when omitted.
* @returns Function call expression.
* @throws Error when `config` is not a plain or schema-qualified identifier.
* @example
* ```ts
* await Post.findAll({ where: { search: { [Op.match]: toTsQuery('cat & !dog', 'english') } } });
* ```
*/
export const toTsQuery = (query: unknown, config?: string): Fn =>
withConfig('to_tsquery', query, config);
/**
* `plainto_tsquery([config,] text)`: ANDs all words of plain text. PostgreSQL extension.
*
* @postgrelize
* @param text - Plain text, bound as a parameter.
* @param config - Text search configuration; the server default when omitted.
* @returns Function call expression.
* @throws Error when `config` is not a plain or schema-qualified identifier.
* @example
* ```ts
* await Post.findAll({ where: { search: { [Op.match]: plainToTsQuery('fat cats', 'english') } } });
* ```
*/
export const plainToTsQuery = (text: unknown, config?: string): Fn =>
withConfig('plainto_tsquery', text, config);
/**
* `phraseto_tsquery([config,] text)`: words of plain text that must follow each other. PostgreSQL extension.
*
* @postgrelize
* @param text - Plain text, bound as a parameter.
* @param config - Text search configuration; the server default when omitted.
* @returns Function call expression.
* @throws Error when `config` is not a plain or schema-qualified identifier.
* @example
* ```ts
* await Post.findAll({ where: { search: { [Op.match]: phraseToTsQuery('fat cats', 'english') } } });
* ```
*/
export const phraseToTsQuery = (text: unknown, config?: string): Fn =>
withConfig('phraseto_tsquery', text, config);
/**
* `websearch_to_tsquery([config,] text)`: web-search syntax (quotes, `or`, `-`) that never fails to parse. PostgreSQL extension.
*
* @postgrelize
* @param text - User search text, bound as a parameter.
* @param config - Text search configuration; the server default when omitted.
* @returns Function call expression.
* @throws Error when `config` is not a plain or schema-qualified identifier.
* @example
* ```ts
* await Post.findAll({ where: { search: { [Op.match]: websearchToTsQuery('"fat cat" -dog', 'english') } } });
* ```
*/
export const websearchToTsQuery = (text: unknown, config?: string): Fn =>
withConfig('websearch_to_tsquery', text, config);
/**
* `ts_rank` / `ts_rank_cd`: relevance of a `tsvector` for a `tsquery`, for `attributes` and `order`. PostgreSQL extension.
*
* @postgrelize
* @param vector - `tsvector` expression, e.g. `col('search')`.
* @param query - `tsquery` expression, e.g. {@link websearchToTsQuery}.
* @param options - `cd`, `weights` and `normalization`.
* @returns Function call expression.
* @throws Error when `weights` is not four finite numbers or `normalization` is not an integer from 0 to 63.
* @example
* ```ts
* const query = websearchToTsQuery('cat', 'english');
* await Post.findAll({ where: { search: { [Op.match]: query } }, order: [[tsRank(col('search'), query, { normalization: 1 }), 'DESC']] });
* ```
*/
export const tsRank = (vector: unknown, query: unknown, options: TsRankOptions = {}): Fn => {
const weights = options.weights === undefined ? [] : [weightsOf(options.weights)];
const normalization =
options.normalization === undefined ? [] : [normalizationOf(options.normalization)];
return new Fn(options.cd === true ? 'ts_rank_cd' : 'ts_rank', [
...weights,
vector,
query,
...normalization,
]);
};
/**
* `ts_headline`: a document excerpt with the query terms highlighted. PostgreSQL extension.
*
* @postgrelize
* @param document - Text expression or value, bound as a parameter.
* @param query - `tsquery` expression.
* @param options - Configuration and `ts_headline` options string.
* @returns Function call expression.
* @throws Error when `config` is not a plain or schema-qualified identifier.
* @example
* ```ts
* await Post.findAll({ attributes: [[tsHeadline(col('body'), websearchToTsQuery('cat'), { options: 'StartSel=<b>, StopSel=</b>' }), 'excerpt']] });
* ```
*/
export const tsHeadline = (
document: unknown,
query: unknown,
options: TsHeadlineOptions = {},
): Fn =>
new Fn('ts_headline', [
...(options.config === undefined ? [] : [regconfig(options.config)]),
document,
query,
...(options.options === undefined ? [] : [new Cast(options.options, 'text')]),
]);
/**
* `setweight(vector, weight)`: labels every lexeme of a `tsvector` `A`, `B`, `C` or `D`. PostgreSQL extension.
*
* @postgrelize
* @param vector - `tsvector` expression, e.g. {@link toTsVector}.
* @param weight - Label `'A'`, `'B'`, `'C'` or `'D'`.
* @returns Function call expression.
* @throws Error when `weight` is not one of the four labels.
* @example
* ```ts
* await Post.findAll({ attributes: [[setWeight(toTsVector(col('title'), 'english'), 'A'), 'titleVector']] });
* ```
*/
export const setWeight = (vector: unknown, weight: 'A' | 'B' | 'C' | 'D'): Fn => {
if (!WEIGHTS.has(weight)) {
throw new Error(`Invalid text search weight: ${String(weight)}`);
}
return new Fn('setweight', [vector, new Literal(`'${weight}'`)]);
};
|