kysely 0.17.1 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/README.md +2 -0
  2. package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +19 -0
  3. package/dist/cjs/driver/default-connection-provider.d.ts +1 -1
  4. package/dist/cjs/driver/default-connection-provider.js +2 -2
  5. package/dist/cjs/driver/dummy-driver.d.ts +8 -0
  6. package/dist/cjs/driver/dummy-driver.js +8 -0
  7. package/dist/cjs/driver/single-connection-provider.d.ts +1 -1
  8. package/dist/cjs/driver/single-connection-provider.js +2 -2
  9. package/dist/cjs/operation-node/alias-node.d.ts +2 -2
  10. package/dist/cjs/operation-node/alias-node.js +1 -2
  11. package/dist/cjs/operation-node/operation-node-transformer.js +1 -0
  12. package/dist/cjs/operation-node/update-query-node.d.ts +3 -0
  13. package/dist/cjs/operation-node/update-query-node.js +11 -2
  14. package/dist/cjs/parser/complex-expression-parser.d.ts +1 -1
  15. package/dist/cjs/parser/reference-parser.js +2 -1
  16. package/dist/cjs/parser/table-parser.js +2 -1
  17. package/dist/cjs/parser/update-set-parser.d.ts +3 -3
  18. package/dist/cjs/parser/with-parser.d.ts +2 -2
  19. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +2 -1
  20. package/dist/cjs/query-builder/delete-query-builder.d.ts +12 -5
  21. package/dist/cjs/query-builder/expression-builder.d.ts +1 -1
  22. package/dist/cjs/query-builder/expression-builder.js +2 -2
  23. package/dist/cjs/query-builder/function-builder.js +2 -2
  24. package/dist/cjs/query-builder/insert-query-builder.d.ts +1 -1
  25. package/dist/cjs/query-builder/on-conflict-builder.d.ts +25 -11
  26. package/dist/cjs/query-builder/select-query-builder.d.ts +15 -6
  27. package/dist/cjs/query-builder/select-query-builder.js +5 -5
  28. package/dist/cjs/query-builder/update-query-builder.d.ts +69 -33
  29. package/dist/cjs/query-builder/update-query-builder.js +7 -0
  30. package/dist/cjs/query-builder/where-interface.d.ts +12 -5
  31. package/dist/cjs/query-compiler/default-query-compiler.js +4 -0
  32. package/dist/cjs/query-creator.d.ts +1 -1
  33. package/dist/cjs/query-executor/default-query-executor.d.ts +8 -5
  34. package/dist/cjs/query-executor/default-query-executor.js +2 -2
  35. package/dist/cjs/query-executor/noop-query-executor.d.ts +7 -4
  36. package/dist/cjs/query-executor/noop-query-executor.js +2 -2
  37. package/dist/cjs/query-executor/query-executor-base.d.ts +62 -0
  38. package/dist/cjs/query-executor/query-executor-base.js +46 -0
  39. package/dist/cjs/query-executor/query-executor.d.ts +16 -17
  40. package/dist/cjs/query-executor/query-executor.js +0 -59
  41. package/dist/cjs/raw-builder/raw-builder.d.ts +32 -2
  42. package/dist/cjs/raw-builder/raw-builder.js +5 -26
  43. package/dist/cjs/raw-builder/sql.d.ts +11 -3
  44. package/dist/cjs/raw-builder/sql.js +10 -2
  45. package/dist/cjs/schema/alter-table-builder.d.ts +24 -2
  46. package/dist/cjs/schema/alter-table-builder.js +20 -0
  47. package/dist/cjs/schema/column-definition-builder.d.ts +4 -2
  48. package/dist/cjs/schema/create-table-builder.d.ts +1 -1
  49. package/dist/cjs/schema/create-table-builder.js +1 -1
  50. package/dist/cjs/util/type-utils.d.ts +1 -1
  51. package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +19 -0
  52. package/dist/esm/driver/default-connection-provider.d.ts +1 -1
  53. package/dist/esm/driver/default-connection-provider.js +2 -2
  54. package/dist/esm/driver/dummy-driver.d.ts +8 -0
  55. package/dist/esm/driver/dummy-driver.js +8 -0
  56. package/dist/esm/driver/single-connection-provider.d.ts +1 -1
  57. package/dist/esm/driver/single-connection-provider.js +2 -2
  58. package/dist/esm/operation-node/alias-node.d.ts +2 -2
  59. package/dist/esm/operation-node/alias-node.js +1 -2
  60. package/dist/esm/operation-node/operation-node-transformer.js +1 -0
  61. package/dist/esm/operation-node/update-query-node.d.ts +3 -0
  62. package/dist/esm/operation-node/update-query-node.js +11 -2
  63. package/dist/esm/parser/complex-expression-parser.d.ts +1 -1
  64. package/dist/esm/parser/reference-parser.js +2 -1
  65. package/dist/esm/parser/table-parser.js +2 -1
  66. package/dist/esm/parser/update-set-parser.d.ts +3 -3
  67. package/dist/esm/parser/with-parser.d.ts +2 -2
  68. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +2 -1
  69. package/dist/esm/query-builder/delete-query-builder.d.ts +12 -5
  70. package/dist/esm/query-builder/expression-builder.d.ts +1 -1
  71. package/dist/esm/query-builder/expression-builder.js +1 -1
  72. package/dist/esm/query-builder/function-builder.js +1 -1
  73. package/dist/esm/query-builder/insert-query-builder.d.ts +1 -1
  74. package/dist/esm/query-builder/on-conflict-builder.d.ts +25 -11
  75. package/dist/esm/query-builder/select-query-builder.d.ts +15 -6
  76. package/dist/esm/query-builder/select-query-builder.js +5 -5
  77. package/dist/esm/query-builder/update-query-builder.d.ts +69 -33
  78. package/dist/esm/query-builder/update-query-builder.js +7 -0
  79. package/dist/esm/query-builder/where-interface.d.ts +12 -5
  80. package/dist/esm/query-compiler/default-query-compiler.js +4 -0
  81. package/dist/esm/query-creator.d.ts +1 -1
  82. package/dist/esm/query-executor/default-query-executor.d.ts +8 -5
  83. package/dist/esm/query-executor/default-query-executor.js +2 -2
  84. package/dist/esm/query-executor/noop-query-executor.d.ts +7 -4
  85. package/dist/esm/query-executor/noop-query-executor.js +2 -2
  86. package/dist/esm/query-executor/query-executor-base.d.ts +62 -0
  87. package/dist/esm/query-executor/query-executor-base.js +43 -0
  88. package/dist/esm/query-executor/query-executor.d.ts +16 -17
  89. package/dist/esm/query-executor/query-executor.js +1 -57
  90. package/dist/esm/raw-builder/raw-builder.d.ts +32 -2
  91. package/dist/esm/raw-builder/raw-builder.js +5 -26
  92. package/dist/esm/raw-builder/sql.d.ts +11 -3
  93. package/dist/esm/raw-builder/sql.js +10 -2
  94. package/dist/esm/schema/alter-table-builder.d.ts +24 -2
  95. package/dist/esm/schema/alter-table-builder.js +20 -0
  96. package/dist/esm/schema/column-definition-builder.d.ts +4 -2
  97. package/dist/esm/schema/create-table-builder.d.ts +1 -1
  98. package/dist/esm/schema/create-table-builder.js +1 -1
  99. package/dist/esm/util/type-utils.d.ts +1 -1
  100. package/package.json +9 -9
@@ -3,11 +3,14 @@ import { DatabaseConnection } from '../driver/database-connection.js';
3
3
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
4
4
  import { RootOperationNode, QueryCompiler } from '../query-compiler/query-compiler.js';
5
5
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
6
- import { QueryExecutor } from './query-executor.js';
7
- import { DialectAdapter } from '../index-nodeless.js';
8
- export declare class DefaultQueryExecutor extends QueryExecutor {
6
+ import { QueryExecutorBase } from './query-executor-base.js';
7
+ import { DialectAdapter } from '../dialect/dialect-adapter.js';
8
+ export declare class DefaultQueryExecutor extends QueryExecutorBase {
9
9
  #private;
10
10
  constructor(compiler: QueryCompiler, adapter: DialectAdapter, connectionProvider: ConnectionProvider, plugins?: KyselyPlugin[]);
11
+ /**
12
+ * Returns the adapter for the current dialect.
13
+ */
11
14
  get adapter(): DialectAdapter;
12
15
  /**
13
16
  * Compiles the transformed query into SQL. You usually want to pass
@@ -38,9 +41,9 @@ export declare class DefaultQueryExecutor extends QueryExecutor {
38
41
  /**
39
42
  * Returns a copy of this executor with a new connection provider.
40
43
  */
41
- withConnectionProvider(connectionProvider: ConnectionProvider): QueryExecutor;
44
+ withConnectionProvider(connectionProvider: ConnectionProvider): DefaultQueryExecutor;
42
45
  /**
43
46
  * Returns a copy of this executor without any plugins.
44
47
  */
45
- withoutPlugins(): QueryExecutor;
48
+ withoutPlugins(): DefaultQueryExecutor;
46
49
  }
@@ -1,6 +1,6 @@
1
1
  /// <reference types="./default-query-executor.d.ts" />
2
- import { QueryExecutor } from './query-executor.js';
3
- export class DefaultQueryExecutor extends QueryExecutor {
2
+ import { QueryExecutorBase } from './query-executor-base.js';
3
+ export class DefaultQueryExecutor extends QueryExecutorBase {
4
4
  #compiler;
5
5
  #adapter;
6
6
  #connectionProvider;
@@ -1,13 +1,16 @@
1
1
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
2
- import { QueryExecutor } from './query-executor.js';
3
2
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
4
3
  import { DialectAdapter } from '../dialect/dialect-adapter.js';
4
+ import { QueryExecutorBase } from './query-executor-base.js';
5
5
  /**
6
6
  * A {@link QueryExecutor} subclass that can be used when you don't
7
7
  * have a {@link QueryCompiler}, {@link ConnectionProvider} or any
8
8
  * other needed things to actually execute queries.
9
9
  */
10
- export declare class NoopQueryExecutor extends QueryExecutor {
10
+ export declare class NoopQueryExecutor extends QueryExecutorBase {
11
+ /**
12
+ * Returns the adapter for the current dialect.
13
+ */
11
14
  get adapter(): DialectAdapter;
12
15
  /**
13
16
  * Compiles the transformed query into SQL. You usually want to pass
@@ -23,7 +26,7 @@ export declare class NoopQueryExecutor extends QueryExecutor {
23
26
  /**
24
27
  * Returns a copy of this executor with a new connection provider.
25
28
  */
26
- withConnectionProvider(): QueryExecutor;
29
+ withConnectionProvider(): NoopQueryExecutor;
27
30
  /**
28
31
  * Returns a copy of this executor with a plugin added as the
29
32
  * last plugin.
@@ -42,6 +45,6 @@ export declare class NoopQueryExecutor extends QueryExecutor {
42
45
  /**
43
46
  * Returns a copy of this executor without any plugins.
44
47
  */
45
- withoutPlugins(): QueryExecutor;
48
+ withoutPlugins(): NoopQueryExecutor;
46
49
  }
47
50
  export declare const NOOP_QUERY_EXECUTOR: NoopQueryExecutor;
@@ -1,11 +1,11 @@
1
1
  /// <reference types="./noop-query-executor.d.ts" />
2
- import { QueryExecutor } from './query-executor.js';
2
+ import { QueryExecutorBase } from './query-executor-base.js';
3
3
  /**
4
4
  * A {@link QueryExecutor} subclass that can be used when you don't
5
5
  * have a {@link QueryCompiler}, {@link ConnectionProvider} or any
6
6
  * other needed things to actually execute queries.
7
7
  */
8
- export class NoopQueryExecutor extends QueryExecutor {
8
+ export class NoopQueryExecutor extends QueryExecutorBase {
9
9
  get adapter() {
10
10
  throw new Error('this query cannot be compiled to SQL');
11
11
  }
@@ -0,0 +1,62 @@
1
+ import { ConnectionProvider } from '../driver/connection-provider.js';
2
+ import { DatabaseConnection, QueryResult } from '../driver/database-connection.js';
3
+ import { CompiledQuery } from '../query-compiler/compiled-query.js';
4
+ import { RootOperationNode } from '../query-compiler/query-compiler.js';
5
+ import { KyselyPlugin } from '../plugin/kysely-plugin.js';
6
+ import { QueryId } from '../util/query-id.js';
7
+ import { DialectAdapter } from '../dialect/dialect-adapter.js';
8
+ import { QueryExecutor } from './query-executor.js';
9
+ export declare abstract class QueryExecutorBase implements QueryExecutor {
10
+ #private;
11
+ constructor(plugins?: ReadonlyArray<KyselyPlugin>);
12
+ abstract get adapter(): DialectAdapter;
13
+ /**
14
+ * Returns all installed plugins.
15
+ */
16
+ get plugins(): ReadonlyArray<KyselyPlugin>;
17
+ /**
18
+ * Given the query the user has built (expressed as an operation node tree)
19
+ * this method runs it through all plugins' `transformQuery` methods and
20
+ * returns the result.
21
+ */
22
+ transformQuery<T extends RootOperationNode>(node: T, queryId: QueryId): T;
23
+ /**
24
+ * Compiles the transformed query into SQL. You usually want to pass
25
+ * the output of {@link transformQuery} into this method but you can
26
+ * compile any query using this method.
27
+ */
28
+ abstract compileQuery(node: RootOperationNode, queryId: QueryId): CompiledQuery;
29
+ /**
30
+ * Provides a connection for the callback and takes care of disposing
31
+ * the connection after the callback has been run.
32
+ */
33
+ abstract provideConnection<T>(consumer: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
34
+ /**
35
+ * Executes a compiled query and runs the result through all plugins'
36
+ * `transformResult` method.
37
+ */
38
+ executeQuery<R>(compiledQuery: CompiledQuery, queryId: QueryId): Promise<QueryResult<R>>;
39
+ /**
40
+ * Returns a copy of this executor with a new connection provider.
41
+ */
42
+ abstract withConnectionProvider(connectionProvider: ConnectionProvider): QueryExecutorBase;
43
+ /**
44
+ * Returns a copy of this executor with a plugin added as the
45
+ * last plugin.
46
+ */
47
+ abstract withPlugin(plugin: KyselyPlugin): QueryExecutorBase;
48
+ /**
49
+ * Returns a copy of this executor with a list of plugins added
50
+ * as the last plugins.
51
+ */
52
+ abstract withPlugins(plugin: ReadonlyArray<KyselyPlugin>): QueryExecutorBase;
53
+ /**
54
+ * Returns a copy of this executor with a plugin added as the
55
+ * first plugin.
56
+ */
57
+ abstract withPluginAtFront(plugin: KyselyPlugin): QueryExecutorBase;
58
+ /**
59
+ * Returns a copy of this executor without any plugins.
60
+ */
61
+ abstract withoutPlugins(): QueryExecutorBase;
62
+ }
@@ -0,0 +1,43 @@
1
+ /// <reference types="./query-executor-base.d.ts" />
2
+ import { freeze } from '../util/object-utils.js';
3
+ const NO_PLUGINS = freeze([]);
4
+ export class QueryExecutorBase {
5
+ #plugins;
6
+ constructor(plugins = NO_PLUGINS) {
7
+ this.#plugins = plugins;
8
+ }
9
+ get plugins() {
10
+ return this.#plugins;
11
+ }
12
+ transformQuery(node, queryId) {
13
+ for (const plugin of this.#plugins) {
14
+ const transformedNode = plugin.transformQuery({ node, queryId });
15
+ // We need to do a runtime check here. There is no good way
16
+ // to write types that enforce this constraint.
17
+ if (transformedNode.kind === node.kind) {
18
+ node = transformedNode;
19
+ }
20
+ else {
21
+ throw new Error([
22
+ `KyselyPlugin.transformQuery must return a node`,
23
+ `of the same kind that was given to it.`,
24
+ `The plugin was given a ${node.kind}`,
25
+ `but it returned a ${transformedNode.kind}`,
26
+ ].join(' '));
27
+ }
28
+ }
29
+ return node;
30
+ }
31
+ async executeQuery(compiledQuery, queryId) {
32
+ return await this.provideConnection(async (connection) => {
33
+ const result = await connection.executeQuery(compiledQuery);
34
+ return this.#transformResult(result, queryId);
35
+ });
36
+ }
37
+ async #transformResult(result, queryId) {
38
+ for (const plugin of this.#plugins) {
39
+ result = await plugin.transformResult({ result, queryId });
40
+ }
41
+ return result;
42
+ }
43
+ }
@@ -1,20 +1,24 @@
1
1
  import { ConnectionProvider } from '../driver/connection-provider.js';
2
- import { DatabaseConnection, QueryResult } from '../driver/database-connection.js';
2
+ import { QueryResult } from '../driver/database-connection.js';
3
3
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
4
4
  import { RootOperationNode } from '../query-compiler/query-compiler.js';
5
5
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
6
6
  import { QueryId } from '../util/query-id.js';
7
7
  import { DialectAdapter } from '../dialect/dialect-adapter.js';
8
8
  /**
9
- * This class abstracts away the details of how to compile a query into SQL
9
+ * This interface abstracts away the details of how to compile a query into SQL
10
10
  * and execute it. Instead of passing around all those details, {@link SelectQueryBuilder}
11
11
  * and other classes that execute queries can just pass around and instance of
12
12
  * `QueryExecutor`.
13
13
  */
14
- export declare abstract class QueryExecutor implements ConnectionProvider {
15
- #private;
16
- constructor(plugins?: KyselyPlugin[]);
17
- abstract get adapter(): DialectAdapter;
14
+ export interface QueryExecutor extends ConnectionProvider {
15
+ /**
16
+ * Returns the adapter for the current dialect.
17
+ */
18
+ get adapter(): DialectAdapter;
19
+ /**
20
+ * Returns all installed plugins.
21
+ */
18
22
  get plugins(): ReadonlyArray<KyselyPlugin>;
19
23
  /**
20
24
  * Given the query the user has built (expressed as an operation node tree)
@@ -27,12 +31,7 @@ export declare abstract class QueryExecutor implements ConnectionProvider {
27
31
  * the output of {@link transformQuery} into this method but you can
28
32
  * compile any query using this method.
29
33
  */
30
- abstract compileQuery(node: RootOperationNode, queryId: QueryId): CompiledQuery;
31
- /**
32
- * Provides a connection for the callback and takes care of disposing
33
- * the connection after the callback has been run.
34
- */
35
- abstract provideConnection<T>(consumer: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
34
+ compileQuery(node: RootOperationNode, queryId: QueryId): CompiledQuery;
36
35
  /**
37
36
  * Executes a compiled query and runs the result through all plugins'
38
37
  * `transformResult` method.
@@ -41,24 +40,24 @@ export declare abstract class QueryExecutor implements ConnectionProvider {
41
40
  /**
42
41
  * Returns a copy of this executor with a new connection provider.
43
42
  */
44
- abstract withConnectionProvider(connectionProvider: ConnectionProvider): QueryExecutor;
43
+ withConnectionProvider(connectionProvider: ConnectionProvider): QueryExecutor;
45
44
  /**
46
45
  * Returns a copy of this executor with a plugin added as the
47
46
  * last plugin.
48
47
  */
49
- abstract withPlugin(plugin: KyselyPlugin): QueryExecutor;
48
+ withPlugin(plugin: KyselyPlugin): QueryExecutor;
50
49
  /**
51
50
  * Returns a copy of this executor with a list of plugins added
52
51
  * as the last plugins.
53
52
  */
54
- abstract withPlugins(plugin: ReadonlyArray<KyselyPlugin>): QueryExecutor;
53
+ withPlugins(plugin: ReadonlyArray<KyselyPlugin>): QueryExecutor;
55
54
  /**
56
55
  * Returns a copy of this executor with a plugin added as the
57
56
  * first plugin.
58
57
  */
59
- abstract withPluginAtFront(plugin: KyselyPlugin): QueryExecutor;
58
+ withPluginAtFront(plugin: KyselyPlugin): QueryExecutor;
60
59
  /**
61
60
  * Returns a copy of this executor without any plugins.
62
61
  */
63
- abstract withoutPlugins(): QueryExecutor;
62
+ withoutPlugins(): QueryExecutor;
64
63
  }
@@ -1,58 +1,2 @@
1
1
  /// <reference types="./query-executor.d.ts" />
2
- import { freeze } from '../util/object-utils.js';
3
- const NO_PLUGINS = freeze([]);
4
- /**
5
- * This class abstracts away the details of how to compile a query into SQL
6
- * and execute it. Instead of passing around all those details, {@link SelectQueryBuilder}
7
- * and other classes that execute queries can just pass around and instance of
8
- * `QueryExecutor`.
9
- */
10
- export class QueryExecutor {
11
- #plugins;
12
- constructor(plugins) {
13
- this.#plugins = plugins ?? NO_PLUGINS;
14
- }
15
- get plugins() {
16
- return this.#plugins;
17
- }
18
- /**
19
- * Given the query the user has built (expressed as an operation node tree)
20
- * this method runs it through all plugins' `transformQuery` methods and
21
- * returns the result.
22
- */
23
- transformQuery(node, queryId) {
24
- for (const plugin of this.#plugins) {
25
- const transformedNode = plugin.transformQuery({ node, queryId });
26
- // We need to do a runtime check here. There is no good way
27
- // to write types that enforce this constraint.
28
- if (transformedNode.kind === node.kind) {
29
- node = transformedNode;
30
- }
31
- else {
32
- throw new Error([
33
- `KyselyPlugin.transformQuery must return a node`,
34
- `of the same kind that was given to it.`,
35
- `The plugin was given a ${node.kind}`,
36
- `but it returned a ${transformedNode.kind}`,
37
- ].join(' '));
38
- }
39
- }
40
- return node;
41
- }
42
- /**
43
- * Executes a compiled query and runs the result through all plugins'
44
- * `transformResult` method.
45
- */
46
- async executeQuery(compiledQuery, queryId) {
47
- return await this.provideConnection(async (connection) => {
48
- const result = await connection.executeQuery(compiledQuery);
49
- return this.#transformResult(result, queryId);
50
- });
51
- }
52
- async #transformResult(result, queryId) {
53
- for (const plugin of this.#plugins) {
54
- result = await plugin.transformResult({ result, queryId });
55
- }
56
- return result;
57
- }
58
- }
2
+ export {};
@@ -5,6 +5,7 @@ import { RawNode } from '../operation-node/raw-node.js';
5
5
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
6
6
  import { QueryExecutorProvider } from '../query-executor/query-executor-provider.js';
7
7
  import { QueryId } from '../util/query-id.js';
8
+ import { AnyRawBuilder } from '../util/type-utils.js';
8
9
  /**
9
10
  * An instance of this class can be used to create raw SQL snippets or queries.
10
11
  *
@@ -38,8 +39,37 @@ export declare class RawBuilder<O = unknown> implements OperationNodeSource {
38
39
  * select concat(first_name, ' ', last_name) as "full_name"
39
40
  * from "person"
40
41
  * ```
42
+ *
43
+ * You can also pass in a raw SQL snippet but in that case you must
44
+ * provide the alias as the only type argument:
45
+ *
46
+ * ```ts
47
+ * const values = sql<{ a: number, b: string }>`(values (1, 'foo'))`
48
+ *
49
+ * // The alias is `t(a, b)` which specifies the column names
50
+ * // in addition to the table name. We must tell kysely that
51
+ * // columns of the table can be referenced through `t`
52
+ * // by providing an explicit type argument.
53
+ * const aliasedValues = values.as<'t'>(sql`t(a, b)`)
54
+ *
55
+ * await db
56
+ * .insertInto('person')
57
+ * .columns(['first_name', 'last_name'])
58
+ * .expression(
59
+ * db.selectFrom(aliasedValues).select(['t.a', 't.b'])
60
+ * )
61
+ * ```
62
+ *
63
+ * The generated SQL (PostgreSQL):
64
+ *
65
+ * ```ts
66
+ * insert into "person" ("first_name", "last_name")
67
+ * from (values (1, 'foo')) as t(a, b)
68
+ * select "t"."a", "t"."b"
69
+ * ```
41
70
  */
42
71
  as<A extends string>(alias: A): AliasedRawBuilder<O, A>;
72
+ as<A extends string = never>(alias: AnyRawBuilder): AliasedRawBuilder<O, A>;
43
73
  /**
44
74
  * Change the output type of the raw expression.
45
75
  *
@@ -67,9 +97,9 @@ export declare class AliasedRawBuilder<O = unknown, A extends string = never> im
67
97
  * which causes this type to be equal to AliasedRawBuilder with any A
68
98
  * as long as O is the same.
69
99
  */
70
- protected get alias(): A;
100
+ protected get alias(): A | AnyRawBuilder;
71
101
  toOperationNode(): AliasNode;
72
- constructor(rawBuilder: RawBuilder<O>, alias: A);
102
+ constructor(rawBuilder: RawBuilder<O>, alias: A | AnyRawBuilder);
73
103
  }
74
104
  export interface RawBuilderProps {
75
105
  readonly queryId: QueryId;
@@ -1,8 +1,10 @@
1
1
  /// <reference types="./raw-builder.d.ts" />
2
2
  import { AliasNode } from '../operation-node/alias-node.js';
3
+ import { isOperationNodeSource, } from '../operation-node/operation-node-source.js';
3
4
  import { preventAwait } from '../util/prevent-await.js';
4
5
  import { freeze } from '../util/object-utils.js';
5
6
  import { NOOP_QUERY_EXECUTOR } from '../query-executor/noop-query-executor.js';
7
+ import { IdentifierNode } from '../operation-node/identifier-node.js';
6
8
  /**
7
9
  * An instance of this class can be used to create raw SQL snippets or queries.
8
10
  *
@@ -14,31 +16,6 @@ export class RawBuilder {
14
16
  constructor(props) {
15
17
  this.#props = freeze(props);
16
18
  }
17
- /**
18
- * Returns an aliased version of the SQL expression.
19
- *
20
- * In addition to slapping `as "the_alias"` to the end of the SQL,
21
- * this method also provides strict typing:
22
- *
23
- * ```ts
24
- * const result = await db
25
- * .selectFrom('person')
26
- * .select(
27
- * sql<string>`concat(first_name, ' ', last_name)`.as('full_name')
28
- * )
29
- * .executeTakeFirstOrThrow()
30
- *
31
- * // `full_name: string` field exists in the result type.
32
- * console.log(result.full_name)
33
- * ```
34
- *
35
- * The generated SQL (PostgreSQL):
36
- *
37
- * ```ts
38
- * select concat(first_name, ' ', last_name) as "full_name"
39
- * from "person"
40
- * ```
41
- */
42
19
  as(alias) {
43
20
  return new AliasedRawBuilder(this, alias);
44
21
  }
@@ -100,7 +77,9 @@ export class AliasedRawBuilder {
100
77
  return this.#alias;
101
78
  }
102
79
  toOperationNode() {
103
- return AliasNode.create(this.#rawBuilder.toOperationNode(), this.#alias);
80
+ return AliasNode.create(this.#rawBuilder.toOperationNode(), isOperationNodeSource(this.#alias)
81
+ ? this.#alias.toOperationNode()
82
+ : IdentifierNode.create(this.#alias));
104
83
  }
105
84
  constructor(rawBuilder, alias) {
106
85
  this.#rawBuilder = rawBuilder;
@@ -221,7 +221,7 @@ export interface Sql {
221
221
  * sql.id('first_name')
222
222
  * ]
223
223
  *
224
- * sql`BEFORE ${sql.array(things, sql`::varchar, `)} AFTER`
224
+ * sql`BEFORE ${sql.join(things, sql`::varchar, `)} AFTER`
225
225
  * ```
226
226
  *
227
227
  * The generated SQL (PostgreSQL):
@@ -281,7 +281,7 @@ export interface Sql {
281
281
  * from "person"
282
282
  * where birthdate between $1 and $2
283
283
  * and "nicknames" @> ARRAY[$3, $4, $5, $6, $7, $8, $9, $10]
284
- * order by concate(first_name, ' ', last_name)
284
+ * order by concat(first_name, ' ', last_name)
285
285
  * ```
286
286
  *
287
287
  * SQL snippets can be executed by calling the `execute` method and passing a `Kysely`
@@ -313,7 +313,15 @@ export interface Sql {
313
313
  * .select([
314
314
  * 'first_name',
315
315
  * 'last_name',
316
- * (eb) => sql<string>`concat(${eb.ref('first_name')}, ' ', ${eb.ref('last_name')})`.as('full_name')
316
+ * (eb) => {
317
+ * // The `eb.ref` method is type-safe and only accepts
318
+ * // column references that are possible.
319
+ * const firstName = eb.ref('first_name')
320
+ * const lastName = eb.ref('last_name')
321
+ *
322
+ * const fullName = sql<string>`concat(${firstName}, ' ', ${lastName})`
323
+ * return fullName.as('full_name')
324
+ * }
317
325
  * ])
318
326
  * ```
319
327
  *
@@ -56,7 +56,7 @@ import { RawBuilder } from './raw-builder.js';
56
56
  * from "person"
57
57
  * where birthdate between $1 and $2
58
58
  * and "nicknames" @> ARRAY[$3, $4, $5, $6, $7, $8, $9, $10]
59
- * order by concate(first_name, ' ', last_name)
59
+ * order by concat(first_name, ' ', last_name)
60
60
  * ```
61
61
  *
62
62
  * SQL snippets can be executed by calling the `execute` method and passing a `Kysely`
@@ -88,7 +88,15 @@ import { RawBuilder } from './raw-builder.js';
88
88
  * .select([
89
89
  * 'first_name',
90
90
  * 'last_name',
91
- * (eb) => sql<string>`concat(${eb.ref('first_name')}, ' ', ${eb.ref('last_name')})`.as('full_name')
91
+ * (eb) => {
92
+ * // The `eb.ref` method is type-safe and only accepts
93
+ * // column references that are possible.
94
+ * const firstName = eb.ref('first_name')
95
+ * const lastName = eb.ref('last_name')
96
+ *
97
+ * const fullName = sql<string>`concat(${firstName}, ' ', ${lastName})`
98
+ * return fullName.as('full_name')
99
+ * }
92
100
  * ])
93
101
  * ```
94
102
  *
@@ -23,6 +23,13 @@ export declare class AlterTableBuilder {
23
23
  alterColumn(column: string): AlterColumnBuilder;
24
24
  dropColumn(column: string): AlterTableExecutor;
25
25
  renameColumn(column: string, newColumn: string): AlterTableExecutor;
26
+ /**
27
+ * See {@link CreateTableBuilder.addColumn}
28
+ *
29
+ * Unlike {@link CreateTableBuilder.addColumn} this method returns the column builder
30
+ * and doesn't take a callback as the last argument. This is because you can only
31
+ * add one column per `ALTER TABLE` query.
32
+ */
26
33
  addColumn(columnName: string, dataType: DataTypeExpression): AlterTableAddColumnBuilder;
27
34
  /**
28
35
  * Creates an `alter table modify column` query. The `modify column` statement
@@ -30,8 +37,21 @@ export declare class AlterTableBuilder {
30
37
  * should use the `alterColumn` method.
31
38
  */
32
39
  modifyColumn(columnName: string, dataType: DataTypeExpression): AlterTableModifyColumnBuilder;
40
+ /**
41
+ * See {@link CreateTableBuilder.addUniqueConstraint}
42
+ */
33
43
  addUniqueConstraint(constraintName: string, columns: string[]): AlterTableExecutor;
44
+ /**
45
+ * See {@link CreateTableBuilder.addCheckConstraint}
46
+ */
34
47
  addCheckConstraint(constraintName: string, checkExpression: AnyRawBuilder): AlterTableExecutor;
48
+ /**
49
+ * See {@link CreateTableBuilder.addForeignKeyConstraint}
50
+ *
51
+ * Unlike {@link CreateTableBuilder.addForeignKeyConstraint} this method returns
52
+ * the constraint builder and doesn't take a callback as the last argument. This
53
+ * is because you can only add one column per `ALTER TABLE` query.
54
+ */
35
55
  addForeignKeyConstraint(constraintName: string, columns: string[], targetTable: string, targetColumns: string[]): AlterTableAddForeignKeyConstraintBuilder;
36
56
  }
37
57
  export interface AlterTableBuilderProps {
@@ -142,7 +162,8 @@ export declare class AlterTableAddColumnBuilder implements ColumnDefinitionBuild
142
162
  * .execute()
143
163
  * ```
144
164
  *
145
- * Raw {@link sql} expressions are also supported:
165
+ * Values passed to `defaultTo` are interpreted as value literals by default. You can define
166
+ * an arbitrary SQL expression using the {@link sql} template tag:
146
167
  *
147
168
  * ```ts
148
169
  * import { sql } from 'kysely'
@@ -306,7 +327,8 @@ export declare class AlterTableModifyColumnBuilder implements ColumnDefinitionBu
306
327
  * .execute()
307
328
  * ```
308
329
  *
309
- * Raw {@link sql} expressions are also supported:
330
+ * Values passed to `defaultTo` are interpreted as value literals by default. You can define
331
+ * an arbitrary SQL expression using the {@link sql} template tag:
310
332
  *
311
333
  * ```ts
312
334
  * import { sql } from 'kysely'
@@ -66,6 +66,13 @@ export class AlterTableBuilder {
66
66
  }),
67
67
  });
68
68
  }
69
+ /**
70
+ * See {@link CreateTableBuilder.addColumn}
71
+ *
72
+ * Unlike {@link CreateTableBuilder.addColumn} this method returns the column builder
73
+ * and doesn't take a callback as the last argument. This is because you can only
74
+ * add one column per `ALTER TABLE` query.
75
+ */
69
76
  addColumn(columnName, dataType) {
70
77
  return new AlterTableAddColumnBuilder({
71
78
  ...this.#props,
@@ -83,6 +90,9 @@ export class AlterTableBuilder {
83
90
  columnBuilder: new ColumnDefinitionBuilder(ColumnDefinitionNode.create(columnName, parseDataTypeExpression(dataType))),
84
91
  });
85
92
  }
93
+ /**
94
+ * See {@link CreateTableBuilder.addUniqueConstraint}
95
+ */
86
96
  addUniqueConstraint(constraintName, columns) {
87
97
  return new AlterTableExecutor({
88
98
  ...this.#props,
@@ -91,6 +101,9 @@ export class AlterTableBuilder {
91
101
  }),
92
102
  });
93
103
  }
104
+ /**
105
+ * See {@link CreateTableBuilder.addCheckConstraint}
106
+ */
94
107
  addCheckConstraint(constraintName, checkExpression) {
95
108
  return new AlterTableExecutor({
96
109
  ...this.#props,
@@ -99,6 +112,13 @@ export class AlterTableBuilder {
99
112
  }),
100
113
  });
101
114
  }
115
+ /**
116
+ * See {@link CreateTableBuilder.addForeignKeyConstraint}
117
+ *
118
+ * Unlike {@link CreateTableBuilder.addForeignKeyConstraint} this method returns
119
+ * the constraint builder and doesn't take a callback as the last argument. This
120
+ * is because you can only add one column per `ALTER TABLE` query.
121
+ */
102
122
  addForeignKeyConstraint(constraintName, columns, targetTable, targetColumns) {
103
123
  return new AlterTableAddForeignKeyConstraintBuilder({
104
124
  ...this.#props,
@@ -83,7 +83,8 @@ export interface ColumnDefinitionBuilderInterface {
83
83
  * .execute()
84
84
  * ```
85
85
  *
86
- * Raw {@link sql} expressions are also supported:
86
+ * Values passed to `defaultTo` are interpreted as value literals by default. You can define
87
+ * an arbitrary SQL expression using the {@link sql} template tag:
87
88
  *
88
89
  * ```ts
89
90
  * import { sql } from 'kysely'
@@ -241,7 +242,8 @@ export declare class ColumnDefinitionBuilder implements ColumnDefinitionBuilderI
241
242
  * .execute()
242
243
  * ```
243
244
  *
244
- * Raw {@link sql} expressions are also supported:
245
+ * Values passed to `defaultTo` are interpreted as value literals by default. You can define
246
+ * an arbitrary SQL expression using the {@link sql} template tag:
245
247
  *
246
248
  * ```ts
247
249
  * import { sql } from 'kysely'
@@ -136,7 +136,7 @@ export declare class CreateTableBuilder<TB extends string, C extends string = ne
136
136
  * )
137
137
  * ```
138
138
  *
139
- * Add constraintfor multiple columns:
139
+ * Add constraint for multiple columns:
140
140
  *
141
141
  * ```ts
142
142
  * addForeignKeyConstraint(
@@ -184,7 +184,7 @@ export class CreateTableBuilder {
184
184
  * )
185
185
  * ```
186
186
  *
187
- * Add constraintfor multiple columns:
187
+ * Add constraint for multiple columns:
188
188
  *
189
189
  * ```ts
190
190
  * addForeignKeyConstraint(
@@ -74,7 +74,7 @@ export declare type ExtractColumnType<DB, TB extends keyof DB, C> = {
74
74
  * movie: Movie
75
75
  * }
76
76
  *
77
- * type Columns = AnyColumn<Database, 'person' | 'pet'>
77
+ * type Columns = AnyColumnWithTable<Database, 'person' | 'pet'>
78
78
  *
79
79
  * // Columns == 'person.id' | 'pet.name' | 'pet.species'
80
80
  * ```