Skip to main content

SerializeContext

The object threaded through every step of serialization — created once per .generate() call and passed to every node's _serialize(ctx) method. It carries the resolved GenerateOptions, and is the thing a SerializerExtension actually receives and inspects. See The Dialect Plugin System for how this fits into the bigger picture.

You don't normally construct one yourself — Query.generate() creates it internally — but its shape matters when writing a dialect extension or a per-query 'serialize' hook. Every internal query node's _serialize(ctx) method (an SqlElement, the shape every Field, Join, and operator implements) receives it as its argument.

Constructor​

constructor(rootQuery: Query, options?: GenerateOptions)

Copies every field of options (dialect, dialectVersion, prettyPrint, params, strictParams) directly onto the instance via Object.assign — SerializeContext itself implements GenerateOptions.

Properties​

KeyTypeReadonlyDescription
rootQueryQueryYesThe statement .generate() was called on.
dialectstring | undefinedNoCopied from GenerateOptions.dialect. What extensions filter on.
dialectVersionstring | undefinedNoCopied from GenerateOptions.dialectVersion.
prettyPrintboolean | undefinedNoCopied from GenerateOptions.prettyPrint.
strictParamsboolean | undefinedNoCopied from GenerateOptions.strictParams.
paramsRecord<string, any> | undefinedNoThe input parameter values passed to generate().
orgParamsRecord<string, any> | undefinedNoA snapshot of params taken before serialization starts.
preparedParamsanyNoBind-parameter values actually referenced during serialization, built up as nodes serialize — becomes GenerateResult.params.
paramOptionsRecord<string, ParamOptions> | ParamOptions[] | undefinedNoPer-parameter type metadata collected during serialization.
returningFields{ field: string; alias?: string }[] | undefinedNoPopulated when a RETURNING clause was serialized.
serializeHooksFunction[] | undefinedNoThe root query's 'serialize' event listeners, consulted before the SerializerRegistry.
reservedWordsSet<string>YesA small built-in set of ANSI-ish reserved words (select, from, where, order, group, join, ...), checked before any dialect extension.

Methods​

serialize()​

serialize(type: SerializationType | string, obj: any, defaultFn: DefaultSerializeFunction): string

The core dispatch point — every query node's _serialize() method calls this for itself and for each of its parts. Resolution order:

  1. Any serializeHooks (per-query 'serialize' event listeners) — the first one that returns a non-null value wins.
  2. Every SerializerRegistry extension whose dialect matches this.dialect, tried in registration order — the first serialize() call that returns a non-null value wins.
  3. defaultFn(this, obj) — the builder's own dialect-neutral rendering for that node.
import { SerializationType } from '@sqb/builder';

ctx.serialize(SerializationType.SELECT_QUERY, o, (ctx, o) => 'select ...');

isReservedWord()​

isReservedWord(word: string | undefined | null): boolean

true if word is in the built-in reservedWords set, or if any registered extension for the current dialect implements isReservedWord() and returns true for it. Used before rendering an identifier, to decide whether it needs quoting.

escapeReserved()​

escapeReserved(word: string): string

Returns word wrapped in double quotes if isReservedWord(word) is true, otherwise returns it unchanged.

See also​