Skip to main content

Match

A comparison operator intended purely as an extension point for dialect-specific full-text search (MATCH() AGAINST() in MySQL, @@ in Postgres, CONTAINS() in SQL Server, ...). Extends CompOperator. Shorthand key: match.

Constructor​

Match(left: string | SqlElement, right?: string | SqlElement, customArgs?: any): Match
new Match(left: string | SqlElement, right?: string | SqlElement, customArgs?: any): Match

Dual-callable. left/right follow the same rules as Like; customArgs is an arbitrary value stashed on the instance for a SerializerExtension to read back during serialization.

import { Match, Select } from '@sqb/builder';

Select().from('articles').where(Match('body', 'search terms'));

By itself Match serializes exactly like Eq (field = 'value') — @sqb/builder ships no built-in full-text syntax. A SerializerExtension registered for SerializationType.COMPARISON_EXPRESSION can inspect _operatorType === OperatorType.match (or the customArgs passed as the third constructor argument) and render the dialect-specific syntax instead. See Generating SQL per dialect.

Dialect support​

DialectRendering
sqlite (@sqb/sqlite-dialect >= 6.1.0)FTS5 column MATCH pattern; with customArgs as a table name or { table, key }, a rowid IN (SELECT rowid FROM <fts table> WHERE ... MATCH ...) sub-select. See @sqb/sqlite-dialect.
every other dialectFalls back to =.
import { Match, Param, Select } from '@sqb/builder';
import { toFtsQuery } from '@sqb/sqlite-dialect';

Select('id')
.from('logs')
.where(Match('message', Param('text'), 'logs_fts'))
.generate({ dialect: 'sqlite', params: { text: toFtsQuery('connection refused') } });
// select id from logs where rowid IN (select rowid from logs_fts where message MATCH :text)

Properties​

Inherits every property from CompOperator (_type, _left, _right, _symbol, _isArray); _symbol defaults to '='. Adds:

KeyTypeReadonlyDescription
customArgsanyNoArbitrary dialect-specific data, set from the constructor's third argument.

Methods​

Match overrides __serialize() to coerce a non-string right-hand expression to a string, skip serialization when it's empty, and forward customArgs to the serializer; it adds no chainable methods of its own.

Object-literal shorthand​

Select().from('articles').where({ 'body match': 'search terms' });

See also​