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
| Key | Type | Readonly | Description |
|---|---|---|---|
rootQuery | Query | Yes | The statement .generate() was called on. |
dialect | string | undefined | No | Copied from GenerateOptions.dialect. What extensions filter on. |
dialectVersion | string | undefined | No | Copied from GenerateOptions.dialectVersion. |
prettyPrint | boolean | undefined | No | Copied from GenerateOptions.prettyPrint. |
strictParams | boolean | undefined | No | Copied from GenerateOptions.strictParams. |
params | Record<string, any> | undefined | No | The input parameter values passed to generate(). |
orgParams | Record<string, any> | undefined | No | A snapshot of params taken before serialization starts. |
preparedParams | any | No | Bind-parameter values actually referenced during serialization, built up as nodes serialize — becomes GenerateResult.params. |
paramOptions | Record<string, ParamOptions> | ParamOptions[] | undefined | No | Per-parameter type metadata collected during serialization. |
returningFields | { field: string; alias?: string }[] | undefined | No | Populated when a RETURNING clause was serialized. |
serializeHooks | Function[] | undefined | No | The root query's 'serialize' event listeners, consulted before the SerializerRegistry. |
reservedWords | Set<string> | Yes | A 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:
- Any
serializeHooks(per-query'serialize'event listeners) — the first one that returns a non-nullvalue wins. - Every
SerializerRegistryextension whosedialectmatchesthis.dialect, tried in registration order — the firstserialize()call that returns a non-nullvalue wins. 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.