kysely 0.16.11 → 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/README.md +2 -2
- package/dist/cjs/dialect/mysql/mysql-adapter.js +3 -4
- package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +19 -0
- package/dist/cjs/dialect/mysql/mysql-introspector.js +2 -1
- package/dist/cjs/dialect/postgres/postgres-adapter.js +3 -2
- package/dist/cjs/dialect/sqlite/sqlite-introspector.js +2 -1
- package/dist/cjs/driver/connection-provider.d.ts +1 -1
- 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/index-nodeless.d.ts +2 -0
- package/dist/cjs/index-nodeless.js +2 -0
- package/dist/cjs/kysely.d.ts +3 -6
- package/dist/cjs/kysely.js +30 -41
- package/dist/cjs/migration/migrator.js +4 -4
- package/dist/cjs/operation-node/operation-node-transformer.js +1 -1
- package/dist/cjs/operation-node/raw-node.d.ts +4 -2
- package/dist/cjs/operation-node/raw-node.js +8 -2
- package/dist/cjs/parser/complex-expression-parser.d.ts +5 -5
- package/dist/cjs/parser/complex-expression-parser.js +5 -4
- package/dist/cjs/parser/filter-parser.d.ts +9 -9
- package/dist/cjs/parser/filter-parser.js +31 -30
- package/dist/cjs/parser/group-by-parser.d.ts +1 -2
- package/dist/cjs/parser/group-by-parser.js +2 -2
- package/dist/cjs/parser/insert-values-parser.d.ts +1 -2
- package/dist/cjs/parser/insert-values-parser.js +6 -6
- package/dist/cjs/parser/join-parser.d.ts +1 -2
- package/dist/cjs/parser/join-parser.js +8 -7
- package/dist/cjs/parser/order-by-parser.d.ts +1 -2
- package/dist/cjs/parser/order-by-parser.js +4 -4
- package/dist/cjs/parser/parse-utils.d.ts +10 -0
- package/dist/cjs/parser/parse-utils.js +38 -0
- package/dist/cjs/parser/reference-parser.d.ts +2 -3
- package/dist/cjs/parser/reference-parser.js +5 -5
- package/dist/cjs/parser/select-parser.d.ts +1 -2
- package/dist/cjs/parser/select-parser.js +5 -5
- package/dist/cjs/parser/table-parser.d.ts +2 -3
- package/dist/cjs/parser/table-parser.js +5 -5
- package/dist/cjs/parser/update-set-parser.d.ts +1 -2
- package/dist/cjs/parser/update-set-parser.js +2 -2
- package/dist/cjs/parser/value-parser.d.ts +3 -4
- package/dist/cjs/parser/value-parser.js +7 -7
- package/dist/cjs/parser/with-parser.d.ts +1 -2
- package/dist/cjs/parser/with-parser.js +3 -2
- package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +2 -1
- package/dist/cjs/query-builder/delete-query-builder.d.ts +34 -23
- package/dist/cjs/query-builder/delete-query-builder.js +15 -15
- package/dist/cjs/query-builder/expression-builder.d.ts +4 -13
- package/dist/cjs/query-builder/expression-builder.js +12 -21
- package/dist/cjs/query-builder/function-builder.d.ts +2 -6
- package/dist/cjs/query-builder/function-builder.js +5 -8
- package/dist/cjs/query-builder/insert-query-builder.d.ts +12 -8
- package/dist/cjs/query-builder/insert-query-builder.js +8 -7
- package/dist/cjs/query-builder/join-builder.d.ts +0 -2
- package/dist/cjs/query-builder/join-builder.js +8 -8
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +46 -28
- package/dist/cjs/query-builder/on-conflict-builder.js +17 -17
- package/dist/cjs/query-builder/returning-interface.d.ts +3 -1
- package/dist/cjs/query-builder/select-query-builder.d.ts +52 -33
- package/dist/cjs/query-builder/select-query-builder.js +28 -26
- package/dist/cjs/query-builder/update-query-builder.d.ts +41 -28
- package/dist/cjs/query-builder/update-query-builder.js +22 -20
- package/dist/cjs/query-builder/where-interface.d.ts +31 -20
- package/dist/cjs/query-compiler/default-query-compiler.js +1 -1
- package/dist/cjs/query-creator.d.ts +10 -129
- package/dist/cjs/query-creator.js +5 -136
- package/dist/cjs/query-executor/default-query-executor.d.ts +21 -7
- package/dist/cjs/query-executor/default-query-executor.js +17 -19
- package/dist/cjs/query-executor/noop-query-executor.d.ts +20 -6
- package/dist/cjs/query-executor/noop-query-executor.js +11 -4
- 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-provider.d.ts +8 -0
- package/dist/cjs/query-executor/query-executor-provider.js +2 -0
- package/dist/cjs/query-executor/query-executor.d.ts +20 -13
- package/dist/cjs/query-executor/query-executor.js +0 -57
- package/dist/cjs/raw-builder/raw-builder.d.ts +42 -11
- package/dist/cjs/raw-builder/raw-builder.js +55 -56
- package/dist/cjs/raw-builder/sql.d.ts +331 -0
- package/dist/cjs/raw-builder/sql.js +161 -0
- package/dist/cjs/schema/alter-table-builder.d.ts +42 -8
- package/dist/cjs/schema/alter-table-builder.js +20 -0
- package/dist/cjs/schema/column-definition-builder.d.ts +22 -8
- package/dist/cjs/schema/create-index-builder.d.ts +3 -1
- package/dist/cjs/schema/create-index-builder.js +3 -1
- package/dist/cjs/schema/create-table-builder.d.ts +7 -3
- package/dist/cjs/schema/create-table-builder.js +7 -3
- package/dist/cjs/schema/create-view-builder.d.ts +3 -3
- package/dist/cjs/schema/create-view-builder.js +3 -3
- package/dist/cjs/util/query-id.js +10 -4
- package/dist/cjs/util/random-string.js +65 -2
- package/dist/cjs/util/type-utils.d.ts +1 -1
- package/dist/esm/dialect/mysql/mysql-adapter.js +3 -4
- package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +19 -0
- package/dist/esm/dialect/mysql/mysql-introspector.js +2 -1
- package/dist/esm/dialect/postgres/postgres-adapter.js +3 -2
- package/dist/esm/dialect/sqlite/sqlite-introspector.js +2 -1
- package/dist/esm/driver/connection-provider.d.ts +1 -1
- 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/index-nodeless.d.ts +2 -0
- package/dist/esm/index-nodeless.js +2 -0
- package/dist/esm/kysely.d.ts +3 -6
- package/dist/esm/kysely.js +30 -41
- package/dist/esm/migration/migrator.js +4 -4
- package/dist/esm/operation-node/operation-node-transformer.js +1 -1
- package/dist/esm/operation-node/raw-node.d.ts +4 -2
- package/dist/esm/operation-node/raw-node.js +8 -2
- package/dist/esm/parser/complex-expression-parser.d.ts +5 -5
- package/dist/esm/parser/complex-expression-parser.js +5 -4
- package/dist/esm/parser/filter-parser.d.ts +9 -9
- package/dist/esm/parser/filter-parser.js +31 -30
- package/dist/esm/parser/group-by-parser.d.ts +1 -2
- package/dist/esm/parser/group-by-parser.js +2 -2
- package/dist/esm/parser/insert-values-parser.d.ts +1 -2
- package/dist/esm/parser/insert-values-parser.js +6 -6
- package/dist/esm/parser/join-parser.d.ts +1 -2
- package/dist/esm/parser/join-parser.js +8 -7
- package/dist/esm/parser/order-by-parser.d.ts +1 -2
- package/dist/esm/parser/order-by-parser.js +4 -4
- package/dist/esm/parser/parse-utils.d.ts +10 -0
- package/dist/esm/parser/parse-utils.js +32 -0
- package/dist/esm/parser/reference-parser.d.ts +2 -3
- package/dist/esm/parser/reference-parser.js +5 -5
- package/dist/esm/parser/select-parser.d.ts +1 -2
- package/dist/esm/parser/select-parser.js +5 -5
- package/dist/esm/parser/table-parser.d.ts +2 -3
- package/dist/esm/parser/table-parser.js +5 -5
- package/dist/esm/parser/update-set-parser.d.ts +1 -2
- package/dist/esm/parser/update-set-parser.js +2 -2
- package/dist/esm/parser/value-parser.d.ts +3 -4
- package/dist/esm/parser/value-parser.js +7 -7
- package/dist/esm/parser/with-parser.d.ts +1 -2
- package/dist/esm/parser/with-parser.js +3 -2
- package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +2 -1
- package/dist/esm/query-builder/delete-query-builder.d.ts +34 -23
- package/dist/esm/query-builder/delete-query-builder.js +15 -15
- package/dist/esm/query-builder/expression-builder.d.ts +4 -13
- package/dist/esm/query-builder/expression-builder.js +12 -21
- package/dist/esm/query-builder/function-builder.d.ts +2 -6
- package/dist/esm/query-builder/function-builder.js +5 -8
- package/dist/esm/query-builder/insert-query-builder.d.ts +12 -8
- package/dist/esm/query-builder/insert-query-builder.js +8 -7
- package/dist/esm/query-builder/join-builder.d.ts +0 -2
- package/dist/esm/query-builder/join-builder.js +8 -8
- package/dist/esm/query-builder/on-conflict-builder.d.ts +46 -28
- package/dist/esm/query-builder/on-conflict-builder.js +17 -17
- package/dist/esm/query-builder/returning-interface.d.ts +3 -1
- package/dist/esm/query-builder/select-query-builder.d.ts +52 -33
- package/dist/esm/query-builder/select-query-builder.js +28 -26
- package/dist/esm/query-builder/update-query-builder.d.ts +41 -28
- package/dist/esm/query-builder/update-query-builder.js +22 -20
- package/dist/esm/query-builder/where-interface.d.ts +31 -20
- package/dist/esm/query-compiler/default-query-compiler.js +1 -1
- package/dist/esm/query-creator.d.ts +10 -129
- package/dist/esm/query-creator.js +5 -136
- package/dist/esm/query-executor/default-query-executor.d.ts +21 -7
- package/dist/esm/query-executor/default-query-executor.js +17 -19
- package/dist/esm/query-executor/noop-query-executor.d.ts +20 -6
- package/dist/esm/query-executor/noop-query-executor.js +10 -3
- 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-provider.d.ts +8 -0
- package/dist/esm/query-executor/query-executor-provider.js +2 -0
- package/dist/esm/query-executor/query-executor.d.ts +20 -13
- package/dist/esm/query-executor/query-executor.js +1 -55
- package/dist/esm/raw-builder/raw-builder.d.ts +42 -11
- package/dist/esm/raw-builder/raw-builder.js +56 -57
- package/dist/esm/raw-builder/sql.d.ts +331 -0
- package/dist/esm/raw-builder/sql.js +159 -0
- package/dist/esm/schema/alter-table-builder.d.ts +42 -8
- package/dist/esm/schema/alter-table-builder.js +20 -0
- package/dist/esm/schema/column-definition-builder.d.ts +22 -8
- package/dist/esm/schema/create-index-builder.d.ts +3 -1
- package/dist/esm/schema/create-index-builder.js +3 -1
- package/dist/esm/schema/create-table-builder.d.ts +7 -3
- package/dist/esm/schema/create-table-builder.js +7 -3
- package/dist/esm/schema/create-view-builder.d.ts +3 -3
- package/dist/esm/schema/create-view-builder.js +3 -3
- package/dist/esm/util/query-id.js +10 -4
- package/dist/esm/util/random-string.js +65 -2
- package/dist/esm/util/type-utils.d.ts +1 -1
- package/package.json +14 -14
- package/dist/cjs/parser/parse-context.d.ts +0 -55
- package/dist/cjs/parser/parse-context.js +0 -50
- package/dist/cjs/util/private-symbols.d.ts +0 -5
- package/dist/cjs/util/private-symbols.js +0 -8
- package/dist/esm/parser/parse-context.d.ts +0 -55
- package/dist/esm/parser/parse-context.js +0 -47
- package/dist/esm/util/private-symbols.d.ts +0 -5
- package/dist/esm/util/private-symbols.js +0 -6
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QueryExecutorBase = void 0;
|
|
4
|
+
const object_utils_js_1 = require("../util/object-utils.js");
|
|
5
|
+
const NO_PLUGINS = (0, object_utils_js_1.freeze)([]);
|
|
6
|
+
class QueryExecutorBase {
|
|
7
|
+
#plugins;
|
|
8
|
+
constructor(plugins = NO_PLUGINS) {
|
|
9
|
+
this.#plugins = plugins;
|
|
10
|
+
}
|
|
11
|
+
get plugins() {
|
|
12
|
+
return this.#plugins;
|
|
13
|
+
}
|
|
14
|
+
transformQuery(node, queryId) {
|
|
15
|
+
for (const plugin of this.#plugins) {
|
|
16
|
+
const transformedNode = plugin.transformQuery({ node, queryId });
|
|
17
|
+
// We need to do a runtime check here. There is no good way
|
|
18
|
+
// to write types that enforce this constraint.
|
|
19
|
+
if (transformedNode.kind === node.kind) {
|
|
20
|
+
node = transformedNode;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
throw new Error([
|
|
24
|
+
`KyselyPlugin.transformQuery must return a node`,
|
|
25
|
+
`of the same kind that was given to it.`,
|
|
26
|
+
`The plugin was given a ${node.kind}`,
|
|
27
|
+
`but it returned a ${transformedNode.kind}`,
|
|
28
|
+
].join(' '));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return node;
|
|
32
|
+
}
|
|
33
|
+
async executeQuery(compiledQuery, queryId) {
|
|
34
|
+
return await this.provideConnection(async (connection) => {
|
|
35
|
+
const result = await connection.executeQuery(compiledQuery);
|
|
36
|
+
return this.#transformResult(result, queryId);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
async #transformResult(result, queryId) {
|
|
40
|
+
for (const plugin of this.#plugins) {
|
|
41
|
+
result = await plugin.transformResult({ result, queryId });
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.QueryExecutorBase = QueryExecutorBase;
|
|
@@ -4,15 +4,21 @@ 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
|
+
import { DialectAdapter } from '../dialect/dialect-adapter.js';
|
|
7
8
|
/**
|
|
8
|
-
* This
|
|
9
|
+
* This interface abstracts away the details of how to compile a query into SQL
|
|
9
10
|
* and execute it. Instead of passing around all those details, {@link SelectQueryBuilder}
|
|
10
11
|
* and other classes that execute queries can just pass around and instance of
|
|
11
12
|
* `QueryExecutor`.
|
|
12
13
|
*/
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
|
|
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
|
+
*/
|
|
16
22
|
get plugins(): ReadonlyArray<KyselyPlugin>;
|
|
17
23
|
/**
|
|
18
24
|
* Given the query the user has built (expressed as an operation node tree)
|
|
@@ -25,32 +31,33 @@ export declare abstract class QueryExecutor {
|
|
|
25
31
|
* the output of {@link transformQuery} into this method but you can
|
|
26
32
|
* compile any query using this method.
|
|
27
33
|
*/
|
|
28
|
-
|
|
34
|
+
compileQuery(node: RootOperationNode, queryId: QueryId): CompiledQuery;
|
|
29
35
|
/**
|
|
30
36
|
* Executes a compiled query and runs the result through all plugins'
|
|
31
37
|
* `transformResult` method.
|
|
32
38
|
*/
|
|
33
39
|
executeQuery<R>(compiledQuery: CompiledQuery, queryId: QueryId): Promise<QueryResult<R>>;
|
|
34
|
-
/**
|
|
35
|
-
* Executes a compiled query.
|
|
36
|
-
*/
|
|
37
|
-
protected abstract executeQueryImpl<R>(compiledQuery: CompiledQuery, queryId: QueryId): Promise<QueryResult<R>>;
|
|
38
40
|
/**
|
|
39
41
|
* Returns a copy of this executor with a new connection provider.
|
|
40
42
|
*/
|
|
41
|
-
|
|
43
|
+
withConnectionProvider(connectionProvider: ConnectionProvider): QueryExecutor;
|
|
42
44
|
/**
|
|
43
45
|
* Returns a copy of this executor with a plugin added as the
|
|
44
46
|
* last plugin.
|
|
45
47
|
*/
|
|
46
|
-
|
|
48
|
+
withPlugin(plugin: KyselyPlugin): QueryExecutor;
|
|
49
|
+
/**
|
|
50
|
+
* Returns a copy of this executor with a list of plugins added
|
|
51
|
+
* as the last plugins.
|
|
52
|
+
*/
|
|
53
|
+
withPlugins(plugin: ReadonlyArray<KyselyPlugin>): QueryExecutor;
|
|
47
54
|
/**
|
|
48
55
|
* Returns a copy of this executor with a plugin added as the
|
|
49
56
|
* first plugin.
|
|
50
57
|
*/
|
|
51
|
-
|
|
58
|
+
withPluginAtFront(plugin: KyselyPlugin): QueryExecutor;
|
|
52
59
|
/**
|
|
53
60
|
* Returns a copy of this executor without any plugins.
|
|
54
61
|
*/
|
|
55
|
-
|
|
62
|
+
withoutPlugins(): QueryExecutor;
|
|
56
63
|
}
|
|
@@ -1,59 +1,2 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.QueryExecutor = void 0;
|
|
4
|
-
const object_utils_js_1 = require("../util/object-utils.js");
|
|
5
|
-
const NO_PLUGINS = (0, object_utils_js_1.freeze)([]);
|
|
6
|
-
/**
|
|
7
|
-
* This class abstracts away the details of how to compile a query into SQL
|
|
8
|
-
* and execute it. Instead of passing around all those details, {@link SelectQueryBuilder}
|
|
9
|
-
* and other classes that execute queries can just pass around and instance of
|
|
10
|
-
* `QueryExecutor`.
|
|
11
|
-
*/
|
|
12
|
-
class QueryExecutor {
|
|
13
|
-
#plugins;
|
|
14
|
-
constructor(plugins) {
|
|
15
|
-
this.#plugins = plugins ?? NO_PLUGINS;
|
|
16
|
-
}
|
|
17
|
-
get plugins() {
|
|
18
|
-
return this.#plugins;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Given the query the user has built (expressed as an operation node tree)
|
|
22
|
-
* this method runs it through all plugins' `transformQuery` methods and
|
|
23
|
-
* returns the result.
|
|
24
|
-
*/
|
|
25
|
-
transformQuery(node, queryId) {
|
|
26
|
-
for (const plugin of this.#plugins) {
|
|
27
|
-
const transformedNode = plugin.transformQuery({ node, queryId });
|
|
28
|
-
// We need to do a runtime check here. There is no good way
|
|
29
|
-
// to write types that enforce this constraint.
|
|
30
|
-
if (transformedNode.kind === node.kind) {
|
|
31
|
-
node = transformedNode;
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
throw new Error([
|
|
35
|
-
`KyselyPlugin.transformQuery must return a node`,
|
|
36
|
-
`of the same kind that was given to it.`,
|
|
37
|
-
`The plugin was given a ${node.kind}`,
|
|
38
|
-
`but it returned a ${transformedNode.kind}`,
|
|
39
|
-
].join(' '));
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return node;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Executes a compiled query and runs the result through all plugins'
|
|
46
|
-
* `transformResult` method.
|
|
47
|
-
*/
|
|
48
|
-
async executeQuery(compiledQuery, queryId) {
|
|
49
|
-
const result = await this.executeQueryImpl(compiledQuery, queryId);
|
|
50
|
-
return this.#transformResult(result, queryId);
|
|
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
|
-
}
|
|
59
|
-
exports.QueryExecutor = QueryExecutor;
|
|
@@ -2,25 +2,57 @@ import { QueryResult } from '../driver/database-connection.js';
|
|
|
2
2
|
import { AliasNode } from '../operation-node/alias-node.js';
|
|
3
3
|
import { OperationNodeSource } from '../operation-node/operation-node-source.js';
|
|
4
4
|
import { RawNode } from '../operation-node/raw-node.js';
|
|
5
|
-
import { CompiledQuery } from '../query-compiler/compiled-query.js';
|
|
6
|
-
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
7
|
-
import { QueryId } from '../util/query-id.js';
|
|
8
5
|
import { KyselyPlugin } from '../plugin/kysely-plugin.js';
|
|
6
|
+
import { QueryExecutorProvider } from '../query-executor/query-executor-provider.js';
|
|
7
|
+
import { QueryId } from '../util/query-id.js';
|
|
8
|
+
/**
|
|
9
|
+
* An instance of this class can be used to create raw SQL snippets or queries.
|
|
10
|
+
*
|
|
11
|
+
* You shouldn't need to create `RawBuilder` instances directly. Instead you should
|
|
12
|
+
* use the {@link sql} template tag.
|
|
13
|
+
*/
|
|
9
14
|
export declare class RawBuilder<O = unknown> implements OperationNodeSource {
|
|
10
15
|
#private;
|
|
11
16
|
constructor(props: RawBuilderProps);
|
|
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
|
+
*/
|
|
12
42
|
as<A extends string>(alias: A): AliasedRawBuilder<O, A>;
|
|
13
43
|
/**
|
|
14
44
|
* Change the output type of the raw expression.
|
|
15
45
|
*
|
|
16
|
-
* This doesn't
|
|
17
|
-
* of this `RawBuilder` with a new output type.
|
|
46
|
+
* This method call doesn't change the SQL in any way. This methods simply
|
|
47
|
+
* returns a copy of this `RawBuilder` with a new output type.
|
|
18
48
|
*/
|
|
19
49
|
castTo<T>(): RawBuilder<T>;
|
|
50
|
+
/**
|
|
51
|
+
* Adds a plugin for this SQL snippet.
|
|
52
|
+
*/
|
|
20
53
|
withPlugin(plugin: KyselyPlugin): RawBuilder<O>;
|
|
21
54
|
toOperationNode(): RawNode;
|
|
22
|
-
|
|
23
|
-
execute(): Promise<QueryResult<O>>;
|
|
55
|
+
execute(executorProvider: QueryExecutorProvider): Promise<QueryResult<O>>;
|
|
24
56
|
}
|
|
25
57
|
/**
|
|
26
58
|
* {@link RawBuilder} with an alias. The result of calling {@link RawBuilder.as}.
|
|
@@ -40,8 +72,7 @@ export declare class AliasedRawBuilder<O = unknown, A extends string = never> im
|
|
|
40
72
|
constructor(rawBuilder: RawBuilder<O>, alias: A);
|
|
41
73
|
}
|
|
42
74
|
export interface RawBuilderProps {
|
|
43
|
-
readonly queryId
|
|
44
|
-
readonly
|
|
45
|
-
readonly
|
|
46
|
-
readonly parameters?: ReadonlyArray<unknown>;
|
|
75
|
+
readonly queryId: QueryId;
|
|
76
|
+
readonly rawNode: RawNode;
|
|
77
|
+
readonly plugins?: ReadonlyArray<KyselyPlugin>;
|
|
47
78
|
}
|
|
@@ -2,75 +2,85 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AliasedRawBuilder = exports.RawBuilder = void 0;
|
|
4
4
|
const alias_node_js_1 = require("../operation-node/alias-node.js");
|
|
5
|
-
const operation_node_source_js_1 = require("../operation-node/operation-node-source.js");
|
|
6
|
-
const raw_node_js_1 = require("../operation-node/raw-node.js");
|
|
7
|
-
const value_node_js_1 = require("../operation-node/value-node.js");
|
|
8
|
-
const reference_parser_js_1 = require("../parser/reference-parser.js");
|
|
9
5
|
const prevent_await_js_1 = require("../util/prevent-await.js");
|
|
10
|
-
const query_id_js_1 = require("../util/query-id.js");
|
|
11
6
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
12
7
|
const noop_query_executor_js_1 = require("../query-executor/noop-query-executor.js");
|
|
8
|
+
/**
|
|
9
|
+
* An instance of this class can be used to create raw SQL snippets or queries.
|
|
10
|
+
*
|
|
11
|
+
* You shouldn't need to create `RawBuilder` instances directly. Instead you should
|
|
12
|
+
* use the {@link sql} template tag.
|
|
13
|
+
*/
|
|
13
14
|
class RawBuilder {
|
|
14
15
|
#props;
|
|
15
16
|
constructor(props) {
|
|
16
|
-
this.#props = (0, object_utils_js_1.freeze)(
|
|
17
|
-
queryId: props.queryId ?? (0, query_id_js_1.createQueryId)(),
|
|
18
|
-
executor: props.executor ?? new noop_query_executor_js_1.NoopQueryExecutor(),
|
|
19
|
-
sql: props.sql,
|
|
20
|
-
parameters: props.parameters,
|
|
21
|
-
});
|
|
17
|
+
this.#props = (0, object_utils_js_1.freeze)(props);
|
|
22
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Returns an aliased version of the SQL expression.
|
|
21
|
+
*
|
|
22
|
+
* In addition to slapping `as "the_alias"` to the end of the SQL,
|
|
23
|
+
* this method also provides strict typing:
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* const result = await db
|
|
27
|
+
* .selectFrom('person')
|
|
28
|
+
* .select(
|
|
29
|
+
* sql<string>`concat(first_name, ' ', last_name)`.as('full_name')
|
|
30
|
+
* )
|
|
31
|
+
* .executeTakeFirstOrThrow()
|
|
32
|
+
*
|
|
33
|
+
* // `full_name: string` field exists in the result type.
|
|
34
|
+
* console.log(result.full_name)
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* The generated SQL (PostgreSQL):
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* select concat(first_name, ' ', last_name) as "full_name"
|
|
41
|
+
* from "person"
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
23
44
|
as(alias) {
|
|
24
45
|
return new AliasedRawBuilder(this, alias);
|
|
25
46
|
}
|
|
26
47
|
/**
|
|
27
48
|
* Change the output type of the raw expression.
|
|
28
49
|
*
|
|
29
|
-
* This doesn't
|
|
30
|
-
* of this `RawBuilder` with a new output type.
|
|
50
|
+
* This method call doesn't change the SQL in any way. This methods simply
|
|
51
|
+
* returns a copy of this `RawBuilder` with a new output type.
|
|
31
52
|
*/
|
|
32
53
|
castTo() {
|
|
33
|
-
return new RawBuilder({
|
|
34
|
-
...this.#props,
|
|
35
|
-
});
|
|
54
|
+
return new RawBuilder({ ...this.#props });
|
|
36
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Adds a plugin for this SQL snippet.
|
|
58
|
+
*/
|
|
37
59
|
withPlugin(plugin) {
|
|
38
60
|
return new RawBuilder({
|
|
39
61
|
...this.#props,
|
|
40
|
-
|
|
62
|
+
plugins: this.#props.plugins !== undefined
|
|
63
|
+
? (0, object_utils_js_1.freeze)([...this.#props.plugins, plugin])
|
|
64
|
+
: (0, object_utils_js_1.freeze)([plugin]),
|
|
41
65
|
});
|
|
42
66
|
}
|
|
43
67
|
toOperationNode() {
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const argNodes = [];
|
|
49
|
-
let paramIdx = 0;
|
|
50
|
-
let sqlIdx = 0;
|
|
51
|
-
let match = null;
|
|
52
|
-
while ((match = parameterPlaceholderRegex.exec(sql))) {
|
|
53
|
-
const str = match[1];
|
|
54
|
-
if (paramIdx >= parameters.length) {
|
|
55
|
-
throw new Error(`value not provided for all placeholders in string ${sql}`);
|
|
56
|
-
}
|
|
57
|
-
if (match.index > 0 && sql[match.index - 1] === '\\') {
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
|
-
sqlFragments.push(sql.slice(sqlIdx, match.index).replace(/\\\?/g, '?'));
|
|
61
|
-
argNodes.push(parseRawArg(str, parameters[paramIdx]));
|
|
62
|
-
sqlIdx = match.index + str.length;
|
|
63
|
-
++paramIdx;
|
|
64
|
-
}
|
|
65
|
-
sqlFragments.push(sql.slice(sqlIdx));
|
|
66
|
-
const rawNode = raw_node_js_1.RawNode.create(sqlFragments, argNodes);
|
|
67
|
-
return this.#props.executor.transformQuery(rawNode, this.#props.queryId);
|
|
68
|
+
const executor = this.#props.plugins !== undefined
|
|
69
|
+
? noop_query_executor_js_1.NOOP_QUERY_EXECUTOR.withPlugins(this.#props.plugins)
|
|
70
|
+
: noop_query_executor_js_1.NOOP_QUERY_EXECUTOR;
|
|
71
|
+
return this.#toOperationNode(executor);
|
|
68
72
|
}
|
|
69
|
-
|
|
70
|
-
|
|
73
|
+
async execute(executorProvider) {
|
|
74
|
+
const executor = this.#props.plugins !== undefined
|
|
75
|
+
? executorProvider.getExecutor().withPlugins(this.#props.plugins)
|
|
76
|
+
: executorProvider.getExecutor();
|
|
77
|
+
return executor.executeQuery(this.#compile(executor), this.#props.queryId);
|
|
71
78
|
}
|
|
72
|
-
|
|
73
|
-
return
|
|
79
|
+
#toOperationNode(executor) {
|
|
80
|
+
return executor.transformQuery(this.#props.rawNode, this.#props.queryId);
|
|
81
|
+
}
|
|
82
|
+
#compile(executor) {
|
|
83
|
+
return executor.compileQuery(this.#toOperationNode(executor), this.#props.queryId);
|
|
74
84
|
}
|
|
75
85
|
}
|
|
76
86
|
exports.RawBuilder = RawBuilder;
|
|
@@ -101,14 +111,3 @@ class AliasedRawBuilder {
|
|
|
101
111
|
}
|
|
102
112
|
}
|
|
103
113
|
exports.AliasedRawBuilder = AliasedRawBuilder;
|
|
104
|
-
function parseRawArg(match, arg) {
|
|
105
|
-
if ((0, operation_node_source_js_1.isOperationNodeSource)(arg)) {
|
|
106
|
-
return arg.toOperationNode();
|
|
107
|
-
}
|
|
108
|
-
else if (match === '??') {
|
|
109
|
-
return (0, reference_parser_js_1.parseStringReference)(arg);
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
return value_node_js_1.ValueNode.create(arg);
|
|
113
|
-
}
|
|
114
|
-
}
|