kysely 0.16.9 → 0.17.1
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/LICENSE +21 -0
- package/README.md +2 -2
- package/dist/cjs/dialect/mysql/mysql-adapter.js +3 -4
- 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 +1 -1
- package/dist/cjs/driver/single-connection-provider.d.ts +1 -1
- package/dist/cjs/driver/single-connection-provider.js +1 -1
- 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/delete-query-node.d.ts +7 -0
- package/dist/cjs/operation-node/delete-query-node.js +15 -0
- 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/query-builder/delete-query-builder.d.ts +77 -20
- package/dist/cjs/query-builder/delete-query-builder.js +81 -14
- 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 +11 -9
- 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 +24 -20
- package/dist/cjs/query-builder/on-conflict-builder.js +17 -17
- package/dist/cjs/query-builder/returning-interface.d.ts +2 -2
- package/dist/cjs/query-builder/select-query-builder.d.ts +43 -32
- package/dist/cjs/query-builder/select-query-builder.js +28 -26
- package/dist/cjs/query-builder/update-query-builder.d.ts +29 -25
- package/dist/cjs/query-builder/update-query-builder.js +22 -20
- package/dist/cjs/query-builder/where-interface.d.ts +20 -16
- package/dist/cjs/query-compiler/default-query-compiler.js +16 -7
- package/dist/cjs/query-creator.d.ts +24 -138
- package/dist/cjs/query-creator.js +19 -145
- package/dist/cjs/query-executor/default-query-executor.d.ts +14 -3
- package/dist/cjs/query-executor/default-query-executor.js +15 -17
- package/dist/cjs/query-executor/noop-query-executor.d.ts +13 -2
- package/dist/cjs/query-executor/noop-query-executor.js +9 -2
- 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 +14 -6
- package/dist/cjs/query-executor/query-executor.js +4 -2
- 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 +323 -0
- package/dist/cjs/raw-builder/sql.js +153 -0
- package/dist/cjs/schema/alter-table-builder.d.ts +20 -8
- package/dist/cjs/schema/alter-table-builder.js +3 -3
- package/dist/cjs/schema/column-definition-builder.d.ts +20 -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 +6 -2
- package/dist/cjs/schema/create-table-builder.js +8 -4
- 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/esm/dialect/mysql/mysql-adapter.js +3 -4
- 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 +1 -1
- package/dist/esm/driver/single-connection-provider.d.ts +1 -1
- package/dist/esm/driver/single-connection-provider.js +1 -1
- 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/delete-query-node.d.ts +7 -0
- package/dist/esm/operation-node/delete-query-node.js +15 -0
- 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/query-builder/delete-query-builder.d.ts +77 -20
- package/dist/esm/query-builder/delete-query-builder.js +81 -14
- 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 +11 -9
- 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 +24 -20
- package/dist/esm/query-builder/on-conflict-builder.js +17 -17
- package/dist/esm/query-builder/returning-interface.d.ts +2 -2
- package/dist/esm/query-builder/select-query-builder.d.ts +43 -32
- package/dist/esm/query-builder/select-query-builder.js +28 -26
- package/dist/esm/query-builder/update-query-builder.d.ts +29 -25
- package/dist/esm/query-builder/update-query-builder.js +22 -20
- package/dist/esm/query-builder/where-interface.d.ts +20 -16
- package/dist/esm/query-compiler/default-query-compiler.js +16 -7
- package/dist/esm/query-creator.d.ts +24 -138
- package/dist/esm/query-creator.js +19 -145
- package/dist/esm/query-executor/default-query-executor.d.ts +14 -3
- package/dist/esm/query-executor/default-query-executor.js +15 -17
- package/dist/esm/query-executor/noop-query-executor.d.ts +13 -2
- package/dist/esm/query-executor/noop-query-executor.js +8 -1
- 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 +14 -6
- package/dist/esm/query-executor/query-executor.js +4 -2
- 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 +323 -0
- package/dist/esm/raw-builder/sql.js +151 -0
- package/dist/esm/schema/alter-table-builder.d.ts +20 -8
- package/dist/esm/schema/alter-table-builder.js +3 -3
- package/dist/esm/schema/column-definition-builder.d.ts +20 -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 +6 -2
- package/dist/esm/schema/create-table-builder.js +8 -4
- 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/package.json +13 -13
- 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
|
@@ -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
|
-
}
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import { RawBuilder } from './raw-builder.js';
|
|
2
|
+
export interface Sql {
|
|
3
|
+
<T = unknown>(sqlFragments: TemplateStringsArray, ...parameters: unknown[]): RawBuilder<T>;
|
|
4
|
+
/**
|
|
5
|
+
* This can be used to add runtime column references to SQL snippets.
|
|
6
|
+
*
|
|
7
|
+
* By default `${}` substitutions in {@link sql} template strings get
|
|
8
|
+
* transformed into parameters. You can use this function to tell
|
|
9
|
+
* Kysely to interpret them as column references instead.
|
|
10
|
+
*
|
|
11
|
+
* WARNING! Using this with unchecked inputs WILL lead to SQL injection
|
|
12
|
+
* vulnerabilities. The input is not checked or escaped by Kysely in any way.
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* const columnRef = 'first_name'
|
|
16
|
+
*
|
|
17
|
+
* sql`select ${sql.ref(columnRef)} from person`
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* The generated SQL (PostgreSQL):
|
|
21
|
+
*
|
|
22
|
+
* ```sql
|
|
23
|
+
* select "first_name" from person
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* The refefences can also include a table name:
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* const columnRef = 'person.first_name'
|
|
30
|
+
*
|
|
31
|
+
* sql`select ${sql.ref(columnRef)}} from person`
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* The generated SQL (PostgreSQL):
|
|
35
|
+
*
|
|
36
|
+
* ```sql
|
|
37
|
+
* select "person"."first_name" from person
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* The refefences can also include a schema on supported databases:
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* const columnRef = 'public.person.first_name'
|
|
44
|
+
*
|
|
45
|
+
* sql`select ${sql.ref(columnRef)}} from person`
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* The generated SQL (PostgreSQL):
|
|
49
|
+
*
|
|
50
|
+
* ```sql
|
|
51
|
+
* select "public"."person"."first_name" from person
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
ref(columnReference: string): RawBuilder<unknown>;
|
|
55
|
+
/**
|
|
56
|
+
* This can be used to add runtime table references to SQL snippets.
|
|
57
|
+
*
|
|
58
|
+
* By default `${}` substitutions in {@link sql} template strings get
|
|
59
|
+
* transformed into parameters. You can use this function to tell
|
|
60
|
+
* Kysely to interpret them as table references instead.
|
|
61
|
+
*
|
|
62
|
+
* WARNING! Using this with unchecked inputs WILL lead to SQL injection
|
|
63
|
+
* vulnerabilities. The input is not checked or escaped by Kysely in any way.
|
|
64
|
+
*
|
|
65
|
+
* ```ts
|
|
66
|
+
* const table = 'person'
|
|
67
|
+
*
|
|
68
|
+
* sql`select first_name from ${sql.table(table)}`
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* The generated SQL (PostgreSQL):
|
|
72
|
+
*
|
|
73
|
+
* ```sql
|
|
74
|
+
* select first_name from "person"
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* The refefences can also include a schema on supported databases:
|
|
78
|
+
*
|
|
79
|
+
* ```ts
|
|
80
|
+
* const table = 'public.person'
|
|
81
|
+
*
|
|
82
|
+
* sql`select first_name from ${sql.table(table)}`
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* The generated SQL (PostgreSQL):
|
|
86
|
+
*
|
|
87
|
+
* ```sql
|
|
88
|
+
* select first_name from "public"."person"
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
table(tableReference: string): RawBuilder<unknown>;
|
|
92
|
+
/**
|
|
93
|
+
* This can be used to add arbitrary identifiers to SQL snippets.
|
|
94
|
+
*
|
|
95
|
+
* Does the same thing as {@link Sql.ref | ref} and {@link Sql.table | table}
|
|
96
|
+
* but can also be used for any other identifiers like index names.
|
|
97
|
+
*
|
|
98
|
+
* You should use {@link Sql.ref | ref} and {@link Sql.table | table}
|
|
99
|
+
* instead of this whenever possible as they produce a more sematic
|
|
100
|
+
* operation node tree.
|
|
101
|
+
*
|
|
102
|
+
* WARNING! Using this with unchecked inputs WILL lead to SQL injection
|
|
103
|
+
* vulnerabilities. The input is not checked or escaped by Kysely in any way.
|
|
104
|
+
*
|
|
105
|
+
* ```ts
|
|
106
|
+
* const indexName = 'person_first_name_index'
|
|
107
|
+
*
|
|
108
|
+
* sql`create index ${indexName} on person`
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* The generated SQL (PostgreSQL):
|
|
112
|
+
*
|
|
113
|
+
* ```sql
|
|
114
|
+
* create index "person_first_name_index" on person
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* Multiple identifiers get separated by dots:
|
|
118
|
+
*
|
|
119
|
+
* ```ts
|
|
120
|
+
* const schema = 'public'
|
|
121
|
+
* const columnName = 'first_name'
|
|
122
|
+
* const table = 'person'
|
|
123
|
+
*
|
|
124
|
+
* sql`select ${sql.id(schema, table, columnName)}} from ${sql.id(schema, table)}`
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* The generated SQL (PostgreSQL):
|
|
128
|
+
*
|
|
129
|
+
* ```sql
|
|
130
|
+
* select "public"."person"."first_name" from "public"."person"
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
id(...ids: string[]): RawBuilder<unknown>;
|
|
134
|
+
/**
|
|
135
|
+
* This can be used to add literal values to SQL snippets.
|
|
136
|
+
*
|
|
137
|
+
* WARNING! Using this with unchecked inputs WILL lead to SQL injection
|
|
138
|
+
* vulnerabilities. The input is not checked or escaped by Kysely in any way.
|
|
139
|
+
* You almost always want to use normal substitutions that get sent to the
|
|
140
|
+
* db as parameters.
|
|
141
|
+
*
|
|
142
|
+
* ```ts
|
|
143
|
+
* const firstName = 'first_name'
|
|
144
|
+
*
|
|
145
|
+
* sql`select * from person where first_name = ${sql.literal(firstName)}`
|
|
146
|
+
* ```
|
|
147
|
+
*
|
|
148
|
+
* The generated SQL (PostgreSQL):
|
|
149
|
+
*
|
|
150
|
+
* ```sql
|
|
151
|
+
* select * from person where first_name = 'first_name'
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
154
|
+
* As you can see from the example above, the value was added directly to
|
|
155
|
+
* the SQL string instead of as a parameter. Only use this function when
|
|
156
|
+
* something can't be sent as a parameter.
|
|
157
|
+
*/
|
|
158
|
+
literal(value: unknown): RawBuilder<unknown>;
|
|
159
|
+
/**
|
|
160
|
+
* This can be used to add arbitrary runtime SQL to SQL snippets.
|
|
161
|
+
*
|
|
162
|
+
* WARNING! Using this with unchecked inputs WILL lead to SQL injection
|
|
163
|
+
* vulnerabilities. The input is not checked or escaped by Kysely in any way.
|
|
164
|
+
*
|
|
165
|
+
* ```ts
|
|
166
|
+
* const firstName = "'first_name'"
|
|
167
|
+
*
|
|
168
|
+
* sql`select * from person where first_name = ${sql.raw(firstName)}`
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* The generated SQL (PostgreSQL):
|
|
172
|
+
*
|
|
173
|
+
* ```sql
|
|
174
|
+
* select * from person where first_name = 'first_name'
|
|
175
|
+
* ```
|
|
176
|
+
*
|
|
177
|
+
* Note that the difference to `sql.literal` is that this function
|
|
178
|
+
* doesn't assume the inputs are values. The input to this function
|
|
179
|
+
* can be any sql and it's simply glued to the parent string as-is.
|
|
180
|
+
*/
|
|
181
|
+
raw(anySql: string): RawBuilder<unknown>;
|
|
182
|
+
/**
|
|
183
|
+
* This can be used to add lists of things to SQL snippets.
|
|
184
|
+
*
|
|
185
|
+
* ### Examples
|
|
186
|
+
*
|
|
187
|
+
* ```ts
|
|
188
|
+
* function findByNicknames(nicknames: string[]): Promise<Person[]> {
|
|
189
|
+
* return db
|
|
190
|
+
* .selectFrom('person')
|
|
191
|
+
* .selectAll()
|
|
192
|
+
* .where('nicknames', '@>', sql`ARRAY[${sql.join(nicknames)}]`)
|
|
193
|
+
* .execute()
|
|
194
|
+
* }
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* The generated SQL (PostgreSQL):
|
|
198
|
+
*
|
|
199
|
+
* ```sql
|
|
200
|
+
* select * from "person"
|
|
201
|
+
* where "nicknames" @> ARRAY[$1, $2, $3, $4, $5, $6, $7, $8]
|
|
202
|
+
* ```
|
|
203
|
+
*
|
|
204
|
+
* The second argument is the joining SQL expression that defaults
|
|
205
|
+
* to
|
|
206
|
+
*
|
|
207
|
+
* ```ts
|
|
208
|
+
* sql`, `
|
|
209
|
+
* ```
|
|
210
|
+
*
|
|
211
|
+
* In addition to values, items in the list can be also {@link sql}
|
|
212
|
+
* expressions, queries or anything else the normal substitutions
|
|
213
|
+
* support:
|
|
214
|
+
*
|
|
215
|
+
* ```ts
|
|
216
|
+
* const things = [
|
|
217
|
+
* 123,
|
|
218
|
+
* sql`(1 == 1)`,
|
|
219
|
+
* db.selectFrom('person').selectAll(),
|
|
220
|
+
* sql.literal(false),
|
|
221
|
+
* sql.id('first_name')
|
|
222
|
+
* ]
|
|
223
|
+
*
|
|
224
|
+
* sql`BEFORE ${sql.array(things, sql`::varchar, `)} AFTER`
|
|
225
|
+
* ```
|
|
226
|
+
*
|
|
227
|
+
* The generated SQL (PostgreSQL):
|
|
228
|
+
*
|
|
229
|
+
* ```sql
|
|
230
|
+
* BEFORE $1::varchar, (1 == 1)::varchar, (select * from "person")::varchar, false::varchar, "first_name" AFTER
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
join(array: unknown[], separator?: RawBuilder<any>): RawBuilder<unknown>;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Template tag for creating raw SQL snippets and queries.
|
|
237
|
+
*
|
|
238
|
+
* ```ts
|
|
239
|
+
* import { sql } from 'kysely'
|
|
240
|
+
*
|
|
241
|
+
* const id = 123
|
|
242
|
+
*
|
|
243
|
+
* const snippet = sql<Person[]>`select * from person where id = ${id}`
|
|
244
|
+
* ```
|
|
245
|
+
*
|
|
246
|
+
* Substitutions (the things inside `${}`) are automatically passed to the database
|
|
247
|
+
* as parameters and are NOT interpolated to the SQL string. There's no need to worry
|
|
248
|
+
* about SQL injection vulnerabilities.
|
|
249
|
+
*
|
|
250
|
+
* If you need your substitutions to be interpreted as identifiers, value literals or
|
|
251
|
+
* lists of things, see the {@link Sql.ref}, {@link Sql.table}, {@link Sql.id},
|
|
252
|
+
* {@link Sql.literal}, {@link Sql.raw} and {@link Sql.join} functions.
|
|
253
|
+
*
|
|
254
|
+
* You can pass sql snippets returned by the `sql` tag pretty much anywhere. Whenever
|
|
255
|
+
* something can't be done using the Kysely API, you should be able to drop down to
|
|
256
|
+
* raw SQL using the `sql` tag. Here's an example query that uses raw sql in a bunch
|
|
257
|
+
* of methods:
|
|
258
|
+
*
|
|
259
|
+
* ```ts
|
|
260
|
+
* const persons = await db
|
|
261
|
+
* .selectFrom('person')
|
|
262
|
+
* .select(
|
|
263
|
+
* // If you use `sql` in a select statement, remember to call the `as`
|
|
264
|
+
* // method to give it an alias. Otherwise Kysely is not able to add
|
|
265
|
+
* // the column to the result type.
|
|
266
|
+
* sql<string>`concat(first_name, ' ', last_name)`.as('full_name')
|
|
267
|
+
* )
|
|
268
|
+
* .where(sql`birthdate between ${date1} and ${date2}`)
|
|
269
|
+
* // Here we assume we have list of nicknames for the person
|
|
270
|
+
* // (a list of strings) and we use the PostgreSQL `@>` operator
|
|
271
|
+
* // to test if all of them are valid nicknames for the user.
|
|
272
|
+
* .where('nicknames', '@>', sql`ARRAY[${sql.join(nicknames)}]`)
|
|
273
|
+
* .orderBy(sql`concat(first_name, ' ', last_name)`)
|
|
274
|
+
* .execute()
|
|
275
|
+
* ```
|
|
276
|
+
*
|
|
277
|
+
* The generated SQL (PostgreSQL):
|
|
278
|
+
*
|
|
279
|
+
* ```sql
|
|
280
|
+
* select concat(first_name, ' ', last_name) as "full_name"
|
|
281
|
+
* from "person"
|
|
282
|
+
* where birthdate between $1 and $2
|
|
283
|
+
* and "nicknames" @> ARRAY[$3, $4, $5, $6, $7, $8, $9, $10]
|
|
284
|
+
* order by concate(first_name, ' ', last_name)
|
|
285
|
+
* ```
|
|
286
|
+
*
|
|
287
|
+
* SQL snippets can be executed by calling the `execute` method and passing a `Kysely`
|
|
288
|
+
* instance as the only argument:
|
|
289
|
+
*
|
|
290
|
+
* ```ts
|
|
291
|
+
* const result = await sql<Person[]>`select * from person`.execute(db)
|
|
292
|
+
* ```
|
|
293
|
+
*
|
|
294
|
+
* You can merge other `sql` expressions and queries using substitutions:
|
|
295
|
+
*
|
|
296
|
+
* ```ts
|
|
297
|
+
* const petName = db.selectFrom('pet').select('name').limit(1)
|
|
298
|
+
* const fullName = sql`concat(first_name, ' ', last_name)`
|
|
299
|
+
*
|
|
300
|
+
* sql`
|
|
301
|
+
* select ${fullName} as full_name, ${petName} as pet_name
|
|
302
|
+
* from person
|
|
303
|
+
* `
|
|
304
|
+
* ```
|
|
305
|
+
*
|
|
306
|
+
* Substitutions also handle {@link ExpressionBuilder.ref},
|
|
307
|
+
* {@link DynamicModule.ref} and pretty much anything else you
|
|
308
|
+
* throw at it. Here's an example of calling a function in a
|
|
309
|
+
* type-safe way:
|
|
310
|
+
*
|
|
311
|
+
* ```ts
|
|
312
|
+
* db.selectFrom('person')
|
|
313
|
+
* .select([
|
|
314
|
+
* 'first_name',
|
|
315
|
+
* 'last_name',
|
|
316
|
+
* (eb) => sql<string>`concat(${eb.ref('first_name')}, ' ', ${eb.ref('last_name')})`.as('full_name')
|
|
317
|
+
* ])
|
|
318
|
+
* ```
|
|
319
|
+
*
|
|
320
|
+
* don't know if that amount of ceremony is worth the small increase in
|
|
321
|
+
* type-safety though... But it's possible.
|
|
322
|
+
*/
|
|
323
|
+
export declare const sql: Sql;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sql = void 0;
|
|
4
|
+
const identifier_node_js_1 = require("../operation-node/identifier-node.js");
|
|
5
|
+
const raw_node_js_1 = require("../operation-node/raw-node.js");
|
|
6
|
+
const value_node_js_1 = require("../operation-node/value-node.js");
|
|
7
|
+
const reference_parser_js_1 = require("../parser/reference-parser.js");
|
|
8
|
+
const table_parser_js_1 = require("../parser/table-parser.js");
|
|
9
|
+
const value_parser_js_1 = require("../parser/value-parser.js");
|
|
10
|
+
const query_id_js_1 = require("../util/query-id.js");
|
|
11
|
+
const raw_builder_js_1 = require("./raw-builder.js");
|
|
12
|
+
/**
|
|
13
|
+
* Template tag for creating raw SQL snippets and queries.
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { sql } from 'kysely'
|
|
17
|
+
*
|
|
18
|
+
* const id = 123
|
|
19
|
+
*
|
|
20
|
+
* const snippet = sql<Person[]>`select * from person where id = ${id}`
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* Substitutions (the things inside `${}`) are automatically passed to the database
|
|
24
|
+
* as parameters and are NOT interpolated to the SQL string. There's no need to worry
|
|
25
|
+
* about SQL injection vulnerabilities.
|
|
26
|
+
*
|
|
27
|
+
* If you need your substitutions to be interpreted as identifiers, value literals or
|
|
28
|
+
* lists of things, see the {@link Sql.ref}, {@link Sql.table}, {@link Sql.id},
|
|
29
|
+
* {@link Sql.literal}, {@link Sql.raw} and {@link Sql.join} functions.
|
|
30
|
+
*
|
|
31
|
+
* You can pass sql snippets returned by the `sql` tag pretty much anywhere. Whenever
|
|
32
|
+
* something can't be done using the Kysely API, you should be able to drop down to
|
|
33
|
+
* raw SQL using the `sql` tag. Here's an example query that uses raw sql in a bunch
|
|
34
|
+
* of methods:
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* const persons = await db
|
|
38
|
+
* .selectFrom('person')
|
|
39
|
+
* .select(
|
|
40
|
+
* // If you use `sql` in a select statement, remember to call the `as`
|
|
41
|
+
* // method to give it an alias. Otherwise Kysely is not able to add
|
|
42
|
+
* // the column to the result type.
|
|
43
|
+
* sql<string>`concat(first_name, ' ', last_name)`.as('full_name')
|
|
44
|
+
* )
|
|
45
|
+
* .where(sql`birthdate between ${date1} and ${date2}`)
|
|
46
|
+
* // Here we assume we have list of nicknames for the person
|
|
47
|
+
* // (a list of strings) and we use the PostgreSQL `@>` operator
|
|
48
|
+
* // to test if all of them are valid nicknames for the user.
|
|
49
|
+
* .where('nicknames', '@>', sql`ARRAY[${sql.join(nicknames)}]`)
|
|
50
|
+
* .orderBy(sql`concat(first_name, ' ', last_name)`)
|
|
51
|
+
* .execute()
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* The generated SQL (PostgreSQL):
|
|
55
|
+
*
|
|
56
|
+
* ```sql
|
|
57
|
+
* select concat(first_name, ' ', last_name) as "full_name"
|
|
58
|
+
* from "person"
|
|
59
|
+
* where birthdate between $1 and $2
|
|
60
|
+
* and "nicknames" @> ARRAY[$3, $4, $5, $6, $7, $8, $9, $10]
|
|
61
|
+
* order by concate(first_name, ' ', last_name)
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* SQL snippets can be executed by calling the `execute` method and passing a `Kysely`
|
|
65
|
+
* instance as the only argument:
|
|
66
|
+
*
|
|
67
|
+
* ```ts
|
|
68
|
+
* const result = await sql<Person[]>`select * from person`.execute(db)
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* You can merge other `sql` expressions and queries using substitutions:
|
|
72
|
+
*
|
|
73
|
+
* ```ts
|
|
74
|
+
* const petName = db.selectFrom('pet').select('name').limit(1)
|
|
75
|
+
* const fullName = sql`concat(first_name, ' ', last_name)`
|
|
76
|
+
*
|
|
77
|
+
* sql`
|
|
78
|
+
* select ${fullName} as full_name, ${petName} as pet_name
|
|
79
|
+
* from person
|
|
80
|
+
* `
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* Substitutions also handle {@link ExpressionBuilder.ref},
|
|
84
|
+
* {@link DynamicModule.ref} and pretty much anything else you
|
|
85
|
+
* throw at it. Here's an example of calling a function in a
|
|
86
|
+
* type-safe way:
|
|
87
|
+
*
|
|
88
|
+
* ```ts
|
|
89
|
+
* db.selectFrom('person')
|
|
90
|
+
* .select([
|
|
91
|
+
* 'first_name',
|
|
92
|
+
* 'last_name',
|
|
93
|
+
* (eb) => sql<string>`concat(${eb.ref('first_name')}, ' ', ${eb.ref('last_name')})`.as('full_name')
|
|
94
|
+
* ])
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
97
|
+
* don't know if that amount of ceremony is worth the small increase in
|
|
98
|
+
* type-safety though... But it's possible.
|
|
99
|
+
*/
|
|
100
|
+
exports.sql = Object.assign((sqlFragments, ...parameters) => {
|
|
101
|
+
return new raw_builder_js_1.RawBuilder({
|
|
102
|
+
queryId: (0, query_id_js_1.createQueryId)(),
|
|
103
|
+
rawNode: raw_node_js_1.RawNode.create(sqlFragments, parameters?.map(value_parser_js_1.parseValueExpression) ?? []),
|
|
104
|
+
});
|
|
105
|
+
}, {
|
|
106
|
+
ref(columnReference) {
|
|
107
|
+
return new raw_builder_js_1.RawBuilder({
|
|
108
|
+
queryId: (0, query_id_js_1.createQueryId)(),
|
|
109
|
+
rawNode: raw_node_js_1.RawNode.createWithChild((0, reference_parser_js_1.parseStringReference)(columnReference)),
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
table(tableReference) {
|
|
113
|
+
return new raw_builder_js_1.RawBuilder({
|
|
114
|
+
queryId: (0, query_id_js_1.createQueryId)(),
|
|
115
|
+
rawNode: raw_node_js_1.RawNode.createWithChild((0, table_parser_js_1.parseTable)(tableReference)),
|
|
116
|
+
});
|
|
117
|
+
},
|
|
118
|
+
id(...ids) {
|
|
119
|
+
const fragments = new Array(ids.length + 1).fill('.');
|
|
120
|
+
fragments[0] = '';
|
|
121
|
+
fragments[fragments.length - 1] = '';
|
|
122
|
+
return new raw_builder_js_1.RawBuilder({
|
|
123
|
+
queryId: (0, query_id_js_1.createQueryId)(),
|
|
124
|
+
rawNode: raw_node_js_1.RawNode.create(fragments, ids.map(identifier_node_js_1.IdentifierNode.create)),
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
literal(value) {
|
|
128
|
+
return new raw_builder_js_1.RawBuilder({
|
|
129
|
+
queryId: (0, query_id_js_1.createQueryId)(),
|
|
130
|
+
rawNode: raw_node_js_1.RawNode.createWithChild(value_node_js_1.ValueNode.createImmediate(value)),
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
raw(sql) {
|
|
134
|
+
return new raw_builder_js_1.RawBuilder({
|
|
135
|
+
queryId: (0, query_id_js_1.createQueryId)(),
|
|
136
|
+
rawNode: raw_node_js_1.RawNode.createWithSql(sql),
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
join(array, separator = (0, exports.sql) `, `) {
|
|
140
|
+
const nodes = new Array(2 * array.length - 1);
|
|
141
|
+
const sep = separator.toOperationNode();
|
|
142
|
+
for (let i = 0; i < array.length; ++i) {
|
|
143
|
+
nodes[2 * i] = (0, value_parser_js_1.parseValueExpression)(array[i]);
|
|
144
|
+
if (i !== array.length - 1) {
|
|
145
|
+
nodes[2 * i + 1] = sep;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return new raw_builder_js_1.RawBuilder({
|
|
149
|
+
queryId: (0, query_id_js_1.createQueryId)(),
|
|
150
|
+
rawNode: raw_node_js_1.RawNode.createWithChildren(nodes),
|
|
151
|
+
});
|
|
152
|
+
},
|
|
153
|
+
});
|