/opt/canhelp/node_modules/kysely/dist/cjs/raw-builder
Edit: /opt/canhelp/node_modules/kysely/dist/cjs/raw-builder/raw-builder.d.ts (7167B)
import type { QueryResult } from '../driver/database-connection.js';
import type { RawNode } from '../operation-node/raw-node.js';
import type { CompiledQuery } from '../query-compiler/compiled-query.js';
import type { KyselyPlugin } from '../plugin/kysely-plugin.js';
import type { QueryExecutorProvider } from '../query-executor/query-executor-provider.js';
import type { QueryId } from '../util/query-id.js';
import type { AliasableExpression, AliasedExpression, Expression } from '../expression/expression.js';
/**
* An instance of this class can be used to create raw SQL snippets or queries.
*
* You shouldn't need to create `RawBuilder` instances directly. Instead you should
* use the {@link sql} template tag.
*/
export interface RawBuilder
extends AliasableExpression {
get isRawBuilder(): true;
/**
* Returns an aliased version of the SQL expression.
*
* In addition to slapping `as "the_alias"` to the end of the SQL,
* this method also provides strict typing:
*
* ```ts
* import { sql } from 'kysely'
*
* const result = await db
* .selectFrom('person')
* .select(
* sql`concat(first_name, ' ', last_name)`.as('full_name')
* )
* .executeTakeFirstOrThrow()
*
* // `full_name: string` field exists in the result type.
* console.log(result.full_name)
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select concat(first_name, ' ', last_name) as "full_name"
* from "person"
* ```
*
* You can also pass in a raw SQL snippet but in that case you must
* provide the alias as the only type argument:
*
* ```ts
* import { sql } from 'kysely'
*
* const values = sql<{ a: number, b: string }>`(values (1, 'foo'))`
*
* // The alias is `t(a, b)` which specifies the column names
* // in addition to the table name. We must tell kysely that
* // columns of the table can be referenced through `t`
* // by providing an explicit type argument.
* const aliasedValues = values.as<'t'>(sql`t(a, b)`)
*
* await db
* .insertInto('person')
* .columns(['first_name', 'last_name'])
* .expression(
* db.selectFrom(aliasedValues).select(['t.a', 't.b'])
* )
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* insert into "person" ("first_name", "last_name")
* from (values (1, 'foo')) as t(a, b)
* select "t"."a", "t"."b"
* ```
*/
as(alias: A): AliasedRawBuilder;
/**
* Returns an aliased version of the expression.
*
* ### Examples
*
* In addition to slapping `as "the_alias"` at the end of the expression,
* this method also provides strict typing:
*
* ```ts
* const result = await db
* .selectFrom('person')
* .select((eb) =>
* // `eb.fn` returns an AliasableExpression
* eb.fn('concat', ['first_name', eb.val(' '), 'last_name']).as('full_name')
* )
* .executeTakeFirstOrThrow()
*
* // `full_name: string` field exists in the result type.
* console.log(result.full_name)
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select
* concat("first_name", $1, "last_name") as "full_name"
* from
* "person"
* ```
*
* You can also pass in a raw SQL snippet (or any expression) but in that case you must
* provide the alias as the only type argument:
*
* ```ts
* import { sql } from 'kysely'
*
* const values = sql<{ a: number, b: string }>`(values (1, 'foo'))`
*
* // The alias is `t(a, b)` which specifies the column names
* // in addition to the table name. We must tell kysely that
* // columns of the table can be referenced through `t`
* // by providing an explicit type argument.
* const aliasedValues = values.as<'t'>(sql`t(a, b)`)
*
* await db
* .insertInto('person')
* .columns(['first_name', 'last_name'])
* .expression(
* db.selectFrom(aliasedValues).select(['t.a', 't.b'])
* )
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* insert into "person" ("first_name", "last_name")
* from (values (1, 'foo')) as t(a, b)
* select "t"."a", "t"."b"
* ```
*/
as(alias: Expression): AliasedRawBuilder;
/**
* Change the output type of the raw expression.
*
* 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.
*/
$castTo(): RawBuilder;
/**
* 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.
*/
$notNull(): RawBuilder>;
/**
* Adds a plugin for this SQL snippet.
*/
withPlugin(plugin: KyselyPlugin): RawBuilder;
/**
* Compiles the builder to a `CompiledQuery`.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* const compiledQuery = sql`select * from ${sql.table('person')}`.compile(db)
* console.log(compiledQuery.sql)
* ```
*/
compile(executorProvider: QueryExecutorProvider): CompiledQuery;
/**
* Executes the raw query.
*
* ### Examples
*
* ```ts
* import { sql } from 'kysely'
*
* const result = await sql`select * from ${sql.table('person')}`.execute(db)
* ```
*/
execute(executorProvider: QueryExecutorProvider): Promise>;
/**
* Creates the OperationNode that describes how to compile this expression into SQL.
*
* ### Examples
*
* If you are creating a custom expression, it's often easiest to use the {@link sql}
* template tag to build the node:
*
* ```ts
* import { type Expression, type OperationNode, sql } from 'kysely'
*
* class SomeExpression implements Expression {
* get expressionType(): T | undefined {
* return undefined
* }
*
* toOperationNode(): OperationNode {
* return sql`some sql here`.toOperationNode()
* }
* }
* ```
*/
toOperationNode(): RawNode;
}
export interface RawBuilderProps {
readonly queryId: QueryId;
readonly rawNode: RawNode;
readonly plugins?: ReadonlyArray;
}
export declare function createRawBuilder(props: RawBuilderProps): RawBuilder;
/**
* {@link RawBuilder} with an alias. The result of calling {@link RawBuilder.as}.
*/
export interface AliasedRawBuilder extends AliasedExpression {
get rawBuilder(): RawBuilder;
}