Skip to main content

RawBuilder

An instance of RawBuilder can be used to create raw SQL snippets or queries. You shouldn’t need to create RawBuilder instances directly. Instead you should use the sql template tag.

Type Parameters

O
any
required
The output type of the raw SQL expression

Properties

isRawBuilder

Identifier property that’s always true for RawBuilder instances.

Methods

as

Returns an aliased version of the SQL expression.
alias
string | Expression
required
The alias name for the expression
In addition to slapping as "the_alias" to the end of the SQL, this method also provides strict typing. Example:
Raw SQL alias example: You can also pass in a raw SQL snippet but in that case you must provide the alias as the only type argument:

$castTo

Change the output type of the raw expression.
C
type parameter
required
The new output type
This method call doesn’t change the SQL in any way. This methods simply returns a copy of this RawBuilder with a new output type.

$notNull

Omit null from the expression’s type.
This function can be useful in cases where you know an expression can’t be null, but Kysely is unable to infer it. This method call doesn’t change the SQL in any way. This methods simply returns a copy of this with a new output type.

withPlugin

Adds a plugin for this SQL snippet.
plugin
KyselyPlugin
required
The plugin to add

compile

Compiles the builder to a CompiledQuery.
executorProvider
QueryExecutorProvider
required
The query executor provider (typically the Kysely instance)
Example:

execute

Executes the raw query.
executorProvider
QueryExecutorProvider
required
The query executor provider (typically the Kysely instance)
Example:

toOperationNode

Converts the builder to an operation node for internal use.

AliasedRawBuilder

RawBuilder with an alias. The result of calling RawBuilder.as.
O
any
The output type of the expression
A
string
The alias name

Properties

rawBuilder Returns the underlying RawBuilder instance.

Usage with the sql template tag

The sql template tag is the primary way to create RawBuilder instances: