kysely 0.17.1 → 0.17.2
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.
- package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +19 -0
- package/dist/cjs/driver/default-connection-provider.d.ts +1 -1
- package/dist/cjs/driver/default-connection-provider.js +2 -2
- package/dist/cjs/driver/dummy-driver.d.ts +8 -0
- package/dist/cjs/driver/dummy-driver.js +8 -0
- package/dist/cjs/driver/single-connection-provider.d.ts +1 -1
- package/dist/cjs/driver/single-connection-provider.js +2 -2
- package/dist/cjs/parser/complex-expression-parser.d.ts +1 -1
- package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +2 -1
- package/dist/cjs/query-builder/delete-query-builder.d.ts +12 -5
- package/dist/cjs/query-builder/expression-builder.d.ts +1 -1
- package/dist/cjs/query-builder/expression-builder.js +2 -2
- package/dist/cjs/query-builder/function-builder.js +2 -2
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +24 -10
- package/dist/cjs/query-builder/select-query-builder.d.ts +12 -5
- package/dist/cjs/query-builder/update-query-builder.d.ts +12 -5
- package/dist/cjs/query-builder/where-interface.d.ts +12 -5
- package/dist/cjs/query-executor/default-query-executor.d.ts +8 -5
- package/dist/cjs/query-executor/default-query-executor.js +2 -2
- package/dist/cjs/query-executor/noop-query-executor.d.ts +7 -4
- package/dist/cjs/query-executor/noop-query-executor.js +2 -2
- package/dist/cjs/query-executor/query-executor-base.d.ts +62 -0
- package/dist/cjs/query-executor/query-executor-base.js +46 -0
- package/dist/cjs/query-executor/query-executor.d.ts +16 -17
- package/dist/cjs/query-executor/query-executor.js +0 -59
- package/dist/cjs/raw-builder/sql.d.ts +11 -3
- package/dist/cjs/raw-builder/sql.js +10 -2
- package/dist/cjs/schema/alter-table-builder.d.ts +24 -2
- package/dist/cjs/schema/alter-table-builder.js +20 -0
- package/dist/cjs/schema/column-definition-builder.d.ts +4 -2
- package/dist/cjs/schema/create-table-builder.d.ts +1 -1
- package/dist/cjs/schema/create-table-builder.js +1 -1
- package/dist/cjs/util/type-utils.d.ts +1 -1
- package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +19 -0
- package/dist/esm/driver/default-connection-provider.d.ts +1 -1
- package/dist/esm/driver/default-connection-provider.js +2 -2
- package/dist/esm/driver/dummy-driver.d.ts +8 -0
- package/dist/esm/driver/dummy-driver.js +8 -0
- package/dist/esm/driver/single-connection-provider.d.ts +1 -1
- package/dist/esm/driver/single-connection-provider.js +2 -2
- package/dist/esm/parser/complex-expression-parser.d.ts +1 -1
- package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +2 -1
- package/dist/esm/query-builder/delete-query-builder.d.ts +12 -5
- package/dist/esm/query-builder/expression-builder.d.ts +1 -1
- package/dist/esm/query-builder/expression-builder.js +1 -1
- package/dist/esm/query-builder/function-builder.js +1 -1
- package/dist/esm/query-builder/on-conflict-builder.d.ts +24 -10
- package/dist/esm/query-builder/select-query-builder.d.ts +12 -5
- package/dist/esm/query-builder/update-query-builder.d.ts +12 -5
- package/dist/esm/query-builder/where-interface.d.ts +12 -5
- package/dist/esm/query-executor/default-query-executor.d.ts +8 -5
- package/dist/esm/query-executor/default-query-executor.js +2 -2
- package/dist/esm/query-executor/noop-query-executor.d.ts +7 -4
- package/dist/esm/query-executor/noop-query-executor.js +2 -2
- package/dist/esm/query-executor/query-executor-base.d.ts +62 -0
- package/dist/esm/query-executor/query-executor-base.js +43 -0
- package/dist/esm/query-executor/query-executor.d.ts +16 -17
- package/dist/esm/query-executor/query-executor.js +1 -57
- package/dist/esm/raw-builder/sql.d.ts +11 -3
- package/dist/esm/raw-builder/sql.js +10 -2
- package/dist/esm/schema/alter-table-builder.d.ts +24 -2
- package/dist/esm/schema/alter-table-builder.js +20 -0
- package/dist/esm/schema/column-definition-builder.d.ts +4 -2
- package/dist/esm/schema/create-table-builder.d.ts +1 -1
- package/dist/esm/schema/create-table-builder.js +1 -1
- package/dist/esm/util/type-utils.d.ts +1 -1
- package/package.json +9 -9
|
@@ -45,7 +45,13 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
45
45
|
* ```
|
|
46
46
|
*
|
|
47
47
|
* Operator can be any supported operator or if the typings don't support it
|
|
48
|
-
* you can always use
|
|
48
|
+
* you can always use
|
|
49
|
+
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* sql`your operator`
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* The next example uses the `>` operator:
|
|
49
55
|
*
|
|
50
56
|
* ```ts
|
|
51
57
|
* const persons = await db
|
|
@@ -61,8 +67,8 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
61
67
|
* select * from "person" where "id" > $1
|
|
62
68
|
* ```
|
|
63
69
|
*
|
|
64
|
-
*
|
|
65
|
-
* conditional
|
|
70
|
+
* `where` methods don't change the type of the query. You can add
|
|
71
|
+
* conditional statements easily by doing something like this:
|
|
66
72
|
*
|
|
67
73
|
* ```ts
|
|
68
74
|
* let query = db
|
|
@@ -70,6 +76,8 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
70
76
|
* .selectAll()
|
|
71
77
|
*
|
|
72
78
|
* if (firstName) {
|
|
79
|
+
* // The query builder is immutable. Remember to reassign
|
|
80
|
+
* // the result back to the query variable.
|
|
73
81
|
* query = query.where('first_name', '=', firstName)
|
|
74
82
|
* }
|
|
75
83
|
*
|
|
@@ -77,8 +85,7 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
77
85
|
* ```
|
|
78
86
|
*
|
|
79
87
|
* This is true for basically all methods execpt the `select` and
|
|
80
|
-
* `returning
|
|
81
|
-
* query.
|
|
88
|
+
* `returning`, that __do__ change the return type of the query.
|
|
82
89
|
*
|
|
83
90
|
* Both the first and third argument can also be subqueries.
|
|
84
91
|
* A subquery is defined by passing a function and calling
|
|
@@ -46,7 +46,13 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
46
46
|
* ```
|
|
47
47
|
*
|
|
48
48
|
* Operator can be any supported operator or if the typings don't support it
|
|
49
|
-
* you can always use
|
|
49
|
+
* you can always use
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* sql`your operator`
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* The next example uses the `>` operator:
|
|
50
56
|
*
|
|
51
57
|
* ```ts
|
|
52
58
|
* const persons = await db
|
|
@@ -62,8 +68,8 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
62
68
|
* select * from "person" where "id" > $1
|
|
63
69
|
* ```
|
|
64
70
|
*
|
|
65
|
-
*
|
|
66
|
-
* conditional
|
|
71
|
+
* `where` methods don't change the type of the query. You can add
|
|
72
|
+
* conditional statements easily by doing something like this:
|
|
67
73
|
*
|
|
68
74
|
* ```ts
|
|
69
75
|
* let query = db
|
|
@@ -71,6 +77,8 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
71
77
|
* .selectAll()
|
|
72
78
|
*
|
|
73
79
|
* if (firstName) {
|
|
80
|
+
* // The query builder is immutable. Remember to reassign
|
|
81
|
+
* // the result back to the query variable.
|
|
74
82
|
* query = query.where('first_name', '=', firstName)
|
|
75
83
|
* }
|
|
76
84
|
*
|
|
@@ -78,8 +86,7 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
78
86
|
* ```
|
|
79
87
|
*
|
|
80
88
|
* This is true for basically all methods execpt the `select` and
|
|
81
|
-
* `returning
|
|
82
|
-
* query.
|
|
89
|
+
* `returning`, that __do__ change the return type of the query.
|
|
83
90
|
*
|
|
84
91
|
* Both the first and third argument can also be subqueries.
|
|
85
92
|
* A subquery is defined by passing a function and calling
|
|
@@ -26,7 +26,13 @@ export interface WhereInterface<DB, TB extends keyof DB> {
|
|
|
26
26
|
* ```
|
|
27
27
|
*
|
|
28
28
|
* Operator can be any supported operator or if the typings don't support it
|
|
29
|
-
* you can always use
|
|
29
|
+
* you can always use
|
|
30
|
+
*
|
|
31
|
+
* ```ts
|
|
32
|
+
* sql`your operator`
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* The next example uses the `>` operator:
|
|
30
36
|
*
|
|
31
37
|
* ```ts
|
|
32
38
|
* const persons = await db
|
|
@@ -42,8 +48,8 @@ export interface WhereInterface<DB, TB extends keyof DB> {
|
|
|
42
48
|
* select * from "person" where "id" > $1
|
|
43
49
|
* ```
|
|
44
50
|
*
|
|
45
|
-
*
|
|
46
|
-
* conditional
|
|
51
|
+
* `where` methods don't change the type of the query. You can add
|
|
52
|
+
* conditional statements easily by doing something like this:
|
|
47
53
|
*
|
|
48
54
|
* ```ts
|
|
49
55
|
* let query = db
|
|
@@ -51,6 +57,8 @@ export interface WhereInterface<DB, TB extends keyof DB> {
|
|
|
51
57
|
* .selectAll()
|
|
52
58
|
*
|
|
53
59
|
* if (firstName) {
|
|
60
|
+
* // The query builder is immutable. Remember to reassign
|
|
61
|
+
* // the result back to the query variable.
|
|
54
62
|
* query = query.where('first_name', '=', firstName)
|
|
55
63
|
* }
|
|
56
64
|
*
|
|
@@ -58,8 +66,7 @@ export interface WhereInterface<DB, TB extends keyof DB> {
|
|
|
58
66
|
* ```
|
|
59
67
|
*
|
|
60
68
|
* This is true for basically all methods execpt the `select` and
|
|
61
|
-
* `returning
|
|
62
|
-
* query.
|
|
69
|
+
* `returning`, that __do__ change the return type of the query.
|
|
63
70
|
*
|
|
64
71
|
* Both the first and third argument can also be subqueries.
|
|
65
72
|
* A subquery is defined by passing a function and calling
|
|
@@ -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 {
|
|
7
|
-
import { DialectAdapter } from '../
|
|
8
|
-
export declare class DefaultQueryExecutor extends
|
|
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):
|
|
44
|
+
withConnectionProvider(connectionProvider: ConnectionProvider): DefaultQueryExecutor;
|
|
42
45
|
/**
|
|
43
46
|
* Returns a copy of this executor without any plugins.
|
|
44
47
|
*/
|
|
45
|
-
withoutPlugins():
|
|
48
|
+
withoutPlugins(): DefaultQueryExecutor;
|
|
46
49
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="./default-query-executor.d.ts" />
|
|
2
|
-
import {
|
|
3
|
-
export class DefaultQueryExecutor extends
|
|
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
|
|
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():
|
|
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():
|
|
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 {
|
|
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
|
|
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 {
|
|
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
|
|
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
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
58
|
+
withPluginAtFront(plugin: KyselyPlugin): QueryExecutor;
|
|
60
59
|
/**
|
|
61
60
|
* Returns a copy of this executor without any plugins.
|
|
62
61
|
*/
|
|
63
|
-
|
|
62
|
+
withoutPlugins(): QueryExecutor;
|
|
64
63
|
}
|
|
@@ -1,58 +1,2 @@
|
|
|
1
1
|
/// <reference types="./query-executor.d.ts" />
|
|
2
|
-
|
|
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 {};
|
|
@@ -221,7 +221,7 @@ export interface Sql {
|
|
|
221
221
|
* sql.id('first_name')
|
|
222
222
|
* ]
|
|
223
223
|
*
|
|
224
|
-
* sql`BEFORE ${sql.
|
|
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
|
|
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) =>
|
|
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
|
|
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) =>
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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'
|