kysely 0.8.0 → 0.9.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 +47 -8
- package/dist/cjs/dialect/mysql/mysql-driver.js +7 -7
- package/dist/cjs/dialect/mysql/mysql-query-compiler.d.ts +0 -2
- package/dist/cjs/dialect/mysql/mysql-query-compiler.js +0 -29
- package/dist/cjs/dialect/postgres/postgres-driver.js +5 -5
- package/dist/cjs/operation-node/create-schema-node.d.ts +1 -1
- package/dist/cjs/operation-node/create-schema-node.js +2 -2
- package/dist/cjs/operation-node/create-table-node.d.ts +1 -5
- package/dist/cjs/operation-node/create-table-node.js +1 -31
- package/dist/cjs/operation-node/join-node.d.ts +2 -1
- package/dist/cjs/operation-node/join-node.js +8 -6
- package/dist/cjs/operation-node/operation-node-source.d.ts +1 -1
- package/dist/cjs/operation-node/operation-node-transformer.js +2 -1
- package/dist/cjs/operation-node/primitive-value-list-node.js +1 -1
- package/dist/cjs/operation-node/query-node.d.ts +4 -3
- package/dist/cjs/operation-node/query-node.js +10 -2
- package/dist/cjs/operation-node/select-query-node.d.ts +2 -1
- package/dist/cjs/operation-node/select-query-node.js +10 -2
- package/dist/cjs/parser/group-by-parser.d.ts +4 -0
- package/dist/cjs/parser/group-by-parser.js +9 -0
- package/dist/cjs/parser/join-parser.d.ts +6 -3
- package/dist/cjs/parser/order-by-parser.d.ts +2 -4
- package/dist/cjs/parser/order-by-parser.js +6 -3
- package/dist/cjs/parser/reference-parser.d.ts +1 -1
- package/dist/cjs/parser/reference-parser.js +1 -1
- package/dist/cjs/parser/table-parser.d.ts +8 -8
- package/dist/cjs/query-builder/function-builder.d.ts +6 -6
- package/dist/cjs/query-builder/function-builder.js +7 -7
- package/dist/cjs/query-builder/join-builder.js +8 -8
- package/dist/cjs/query-builder/query-builder.js +20 -22
- package/dist/cjs/query-compiler/compiled-query.d.ts +1 -1
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +4 -3
- package/dist/cjs/query-compiler/default-query-compiler.js +23 -29
- package/dist/cjs/query-creator.d.ts +14 -11
- package/dist/cjs/query-creator.js +14 -11
- package/dist/cjs/raw-builder/raw-builder.d.ts +1 -1
- package/dist/cjs/raw-builder/raw-builder.js +6 -6
- package/dist/cjs/schema/alter-table-builder.d.ts +3 -0
- package/dist/cjs/schema/alter-table-builder.js +3 -0
- package/dist/cjs/schema/column-definition-builder.d.ts +14 -5
- package/dist/cjs/schema/create-schema-builder.js +1 -1
- package/dist/cjs/schema/create-table-builder.d.ts +55 -10
- package/dist/cjs/schema/create-table-builder.js +59 -17
- package/dist/cjs/schema/schema.d.ts +37 -2
- package/dist/cjs/schema/schema.js +36 -1
- package/dist/cjs/util/object-utils.d.ts +12 -11
- package/dist/cjs/util/object-utils.js +6 -2
- package/dist/esm/dialect/mysql/mysql-driver.js +8 -8
- package/dist/esm/dialect/mysql/mysql-query-compiler.d.ts +0 -2
- package/dist/esm/dialect/mysql/mysql-query-compiler.js +0 -29
- package/dist/esm/dialect/postgres/postgres-driver.js +5 -5
- package/dist/esm/operation-node/create-schema-node.d.ts +1 -1
- package/dist/esm/operation-node/create-schema-node.js +2 -2
- package/dist/esm/operation-node/create-table-node.d.ts +1 -5
- package/dist/esm/operation-node/create-table-node.js +1 -31
- package/dist/esm/operation-node/join-node.d.ts +2 -1
- package/dist/esm/operation-node/join-node.js +8 -6
- package/dist/esm/operation-node/operation-node-source.d.ts +1 -1
- package/dist/esm/operation-node/operation-node-transformer.js +2 -1
- package/dist/esm/operation-node/primitive-value-list-node.js +1 -1
- package/dist/esm/operation-node/query-node.d.ts +4 -3
- package/dist/esm/operation-node/query-node.js +10 -2
- package/dist/esm/operation-node/select-query-node.d.ts +2 -1
- package/dist/esm/operation-node/select-query-node.js +10 -2
- package/dist/esm/parser/group-by-parser.d.ts +4 -0
- package/dist/esm/parser/group-by-parser.js +5 -0
- package/dist/esm/parser/join-parser.d.ts +6 -3
- package/dist/esm/parser/order-by-parser.d.ts +2 -4
- package/dist/esm/parser/order-by-parser.js +6 -2
- package/dist/esm/parser/reference-parser.d.ts +1 -1
- package/dist/esm/parser/reference-parser.js +2 -2
- package/dist/esm/parser/table-parser.d.ts +8 -8
- package/dist/esm/query-builder/function-builder.d.ts +6 -6
- package/dist/esm/query-builder/function-builder.js +7 -7
- package/dist/esm/query-builder/join-builder.js +8 -8
- package/dist/esm/query-builder/query-builder.js +21 -23
- package/dist/esm/query-compiler/compiled-query.d.ts +1 -1
- package/dist/esm/query-compiler/default-query-compiler.d.ts +4 -3
- package/dist/esm/query-compiler/default-query-compiler.js +23 -29
- package/dist/esm/query-creator.d.ts +14 -11
- package/dist/esm/query-creator.js +14 -11
- package/dist/esm/raw-builder/raw-builder.d.ts +1 -1
- package/dist/esm/raw-builder/raw-builder.js +6 -6
- package/dist/esm/schema/alter-table-builder.d.ts +3 -0
- package/dist/esm/schema/alter-table-builder.js +3 -0
- package/dist/esm/schema/column-definition-builder.d.ts +14 -5
- package/dist/esm/schema/create-schema-builder.js +1 -1
- package/dist/esm/schema/create-table-builder.d.ts +55 -10
- package/dist/esm/schema/create-table-builder.js +60 -18
- package/dist/esm/schema/schema.d.ts +37 -2
- package/dist/esm/schema/schema.js +36 -1
- package/dist/esm/util/object-utils.d.ts +12 -11
- package/dist/esm/util/object-utils.js +4 -1
- package/package.json +1 -1
|
@@ -62,7 +62,7 @@ import { ModifyColumnNode } from '../operation-node/modify-column-node.js';
|
|
|
62
62
|
import { OnDuplicateKeyNode } from '../operation-node/on-duplicate-key-node.js';
|
|
63
63
|
export declare class DefaultQueryCompiler extends OperationNodeVisitor implements QueryCompiler {
|
|
64
64
|
#private;
|
|
65
|
-
protected get
|
|
65
|
+
protected get numParameters(): number;
|
|
66
66
|
compileQuery(node: RootOperationNode): CompiledQuery;
|
|
67
67
|
protected getSql(): string;
|
|
68
68
|
protected visitSelectQuery(node: SelectQueryNode): void;
|
|
@@ -92,6 +92,7 @@ export declare class DefaultQueryCompiler extends OperationNodeVisitor implement
|
|
|
92
92
|
protected visitTable(node: TableNode): void;
|
|
93
93
|
protected visitCreateTable(node: CreateTableNode): void;
|
|
94
94
|
protected visitColumnDefinition(node: ColumnDefinitionNode): void;
|
|
95
|
+
protected getAutoIncrement(): string;
|
|
95
96
|
protected visitReferences(node: ReferencesNode): void;
|
|
96
97
|
protected visitDropTable(node: DropTableNode): void;
|
|
97
98
|
protected visitDataType(node: DataTypeNode): void;
|
|
@@ -125,10 +126,10 @@ export declare class DefaultQueryCompiler extends OperationNodeVisitor implement
|
|
|
125
126
|
protected visitAddConstraint(node: AddConstraintNode): void;
|
|
126
127
|
protected visitDropConstraint(node: DropConstraintNode): void;
|
|
127
128
|
protected append(str: string): void;
|
|
128
|
-
protected appendValue(
|
|
129
|
+
protected appendValue(parameter: unknown): void;
|
|
129
130
|
protected getLeftIdentifierWrapper(): string;
|
|
130
131
|
protected getRightIdentifierWrapper(): string;
|
|
131
132
|
protected getCurrentParameterPlaceholder(): string;
|
|
132
|
-
protected
|
|
133
|
+
protected addParameter(parameter: unknown): void;
|
|
133
134
|
protected appendImmediateValue(value: PrimitiveValue): void;
|
|
134
135
|
}
|
|
@@ -1,25 +1,24 @@
|
|
|
1
|
-
import { DataTypeNode } from '../operation-node/data-type-node.js';
|
|
2
1
|
import { OperationNodeVisitor } from '../operation-node/operation-node-visitor.js';
|
|
3
2
|
import { QueryNode } from '../operation-node/query-node.js';
|
|
4
3
|
import { RawNode } from '../operation-node/raw-node.js';
|
|
5
4
|
import { getLast, freeze, isString, isNumber, isBoolean, isNull, isDate, isBigInt, } from '../util/object-utils.js';
|
|
6
5
|
export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
get
|
|
10
|
-
return this.#
|
|
6
|
+
#sql = '';
|
|
7
|
+
#parameters = [];
|
|
8
|
+
get numParameters() {
|
|
9
|
+
return this.#parameters.length;
|
|
11
10
|
}
|
|
12
11
|
compileQuery(node) {
|
|
13
|
-
this.#
|
|
14
|
-
this.#
|
|
12
|
+
this.#sql = '';
|
|
13
|
+
this.#parameters = [];
|
|
15
14
|
this.visitNode(node);
|
|
16
15
|
return freeze({
|
|
17
16
|
sql: this.getSql(),
|
|
18
|
-
|
|
17
|
+
parameters: this.#parameters,
|
|
19
18
|
});
|
|
20
19
|
}
|
|
21
20
|
getSql() {
|
|
22
|
-
return this.#
|
|
21
|
+
return this.#sql;
|
|
23
22
|
}
|
|
24
23
|
visitSelectQuery(node) {
|
|
25
24
|
// This is a sub query if this is not the root node.
|
|
@@ -289,19 +288,7 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
289
288
|
visitColumnDefinition(node) {
|
|
290
289
|
this.visitNode(node.column);
|
|
291
290
|
this.append(' ');
|
|
292
|
-
|
|
293
|
-
// Postgres overrides the data type for autoincrementing columns.
|
|
294
|
-
if (DataTypeNode.is(node.dataType) &&
|
|
295
|
-
node.dataType.dataType === 'bigint') {
|
|
296
|
-
this.append('bigserial');
|
|
297
|
-
}
|
|
298
|
-
else {
|
|
299
|
-
this.append('serial');
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
else {
|
|
303
|
-
this.visitNode(node.dataType);
|
|
304
|
-
}
|
|
291
|
+
this.visitNode(node.dataType);
|
|
305
292
|
if (node.defaultTo) {
|
|
306
293
|
this.append(' default ');
|
|
307
294
|
this.visitNode(node.defaultTo);
|
|
@@ -309,6 +296,10 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
309
296
|
if (!node.isNullable) {
|
|
310
297
|
this.append(' not null');
|
|
311
298
|
}
|
|
299
|
+
if (node.isAutoIncrementing) {
|
|
300
|
+
this.append(' ');
|
|
301
|
+
this.append(this.getAutoIncrement());
|
|
302
|
+
}
|
|
312
303
|
if (node.isUnique) {
|
|
313
304
|
this.append(' unique');
|
|
314
305
|
}
|
|
@@ -324,6 +315,9 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
324
315
|
this.visitNode(node.check);
|
|
325
316
|
}
|
|
326
317
|
}
|
|
318
|
+
getAutoIncrement() {
|
|
319
|
+
return 'auto_increment';
|
|
320
|
+
}
|
|
327
321
|
visitReferences(node) {
|
|
328
322
|
this.append('references ');
|
|
329
323
|
this.visitNode(node.table);
|
|
@@ -634,10 +628,10 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
634
628
|
this.visitNode(node.constraintName);
|
|
635
629
|
}
|
|
636
630
|
append(str) {
|
|
637
|
-
this.#
|
|
631
|
+
this.#sql += str;
|
|
638
632
|
}
|
|
639
|
-
appendValue(
|
|
640
|
-
this.
|
|
633
|
+
appendValue(parameter) {
|
|
634
|
+
this.addParameter(parameter);
|
|
641
635
|
this.append(this.getCurrentParameterPlaceholder());
|
|
642
636
|
}
|
|
643
637
|
getLeftIdentifierWrapper() {
|
|
@@ -647,10 +641,10 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
647
641
|
return '"';
|
|
648
642
|
}
|
|
649
643
|
getCurrentParameterPlaceholder() {
|
|
650
|
-
return '$' + this.
|
|
644
|
+
return '$' + this.numParameters;
|
|
651
645
|
}
|
|
652
|
-
|
|
653
|
-
this.#
|
|
646
|
+
addParameter(parameter) {
|
|
647
|
+
this.#parameters.push(parameter);
|
|
654
648
|
}
|
|
655
649
|
appendImmediateValue(value) {
|
|
656
650
|
if (isString(value)) {
|
|
@@ -678,7 +672,7 @@ const SELECT_MODIFIER_SQL = {
|
|
|
678
672
|
ForNoKeyUpdate: 'for no key update',
|
|
679
673
|
ForUpdate: 'for update',
|
|
680
674
|
ForShare: 'for share',
|
|
681
|
-
NoWait: '
|
|
675
|
+
NoWait: 'nowait',
|
|
682
676
|
SkipLocked: 'skip locked',
|
|
683
677
|
};
|
|
684
678
|
const JOIN_TYPE_SQL = {
|
|
@@ -10,7 +10,7 @@ export declare class QueryCreator<DB> {
|
|
|
10
10
|
#private;
|
|
11
11
|
constructor(props: QueryCreatorProps);
|
|
12
12
|
/**
|
|
13
|
-
* Creates a `select` query builder
|
|
13
|
+
* Creates a `select` query builder for the given table or tables.
|
|
14
14
|
*
|
|
15
15
|
* The tables passed to this method are built as the query's `from` clause.
|
|
16
16
|
*
|
|
@@ -221,6 +221,9 @@ export declare class QueryCreator<DB> {
|
|
|
221
221
|
* Sets the schema to be used for all table references that don't explicitly
|
|
222
222
|
* specify a schema.
|
|
223
223
|
*
|
|
224
|
+
* This only affects the query created through the builder returned from
|
|
225
|
+
* this method and doesn't modify the `db` instance.
|
|
226
|
+
*
|
|
224
227
|
* @example
|
|
225
228
|
* ```
|
|
226
229
|
* await db.withSchema('mammals')
|
|
@@ -261,10 +264,10 @@ export declare class QueryCreator<DB> {
|
|
|
261
264
|
* Provides a way to pass arbitrary SQL into your query and executing completely
|
|
262
265
|
* raw queries.
|
|
263
266
|
*
|
|
264
|
-
* You can use strings `?` and `??` in the `sql` to bind parameters such as
|
|
267
|
+
* You can use the strings `?` and `??` in the `sql` to bind parameters such as
|
|
265
268
|
* user input to the SQL. You should never EVER concatenate untrusted user
|
|
266
269
|
* input to the SQL string to avoid injection vulnerabilities. Instead use `?`
|
|
267
|
-
* in place of
|
|
270
|
+
* in place of a value and pass the actual value in the `parameters` list. See
|
|
268
271
|
* the examples below.
|
|
269
272
|
*
|
|
270
273
|
* You should only use `raw` when there is no other way to get the job done. This is
|
|
@@ -276,11 +279,11 @@ export declare class QueryCreator<DB> {
|
|
|
276
279
|
* Raw builder instances can be passed to pretty much anywhere: `select`, `where`,
|
|
277
280
|
* `*Join`, `groupBy`, `orderBy` etc. Just try it. If the method accepts it, it works.
|
|
278
281
|
*
|
|
279
|
-
* @param sql - The raw SQL. Special strings `?` and `??` can be used
|
|
280
|
-
*
|
|
282
|
+
* @param sql - The raw SQL. Special strings `?` and `??` can be used as parameter
|
|
283
|
+
* placeholders. `?` for values and `??` for identifiers such as column names
|
|
281
284
|
* or `column.table` references.
|
|
282
285
|
*
|
|
283
|
-
* @param params - The parameters that will be bound to the `?` and `??`
|
|
286
|
+
* @param params - The parameters that will be bound to the `?` and `??` placeholders in
|
|
284
287
|
* the sql string.
|
|
285
288
|
*
|
|
286
289
|
* @example
|
|
@@ -318,7 +321,7 @@ export declare class QueryCreator<DB> {
|
|
|
318
321
|
* the output type. In this case you need to use the `castTo` method on the query
|
|
319
322
|
* to specify a return type for the query.
|
|
320
323
|
*
|
|
321
|
-
* We could've also used `??`
|
|
324
|
+
* We could've also used a `??` placeholder to provide `first_name` and `last_name` like
|
|
322
325
|
* this:
|
|
323
326
|
*
|
|
324
327
|
* ```ts
|
|
@@ -333,9 +336,9 @@ export declare class QueryCreator<DB> {
|
|
|
333
336
|
*
|
|
334
337
|
* But it's often cleaner to just write the column names in the SQL. Again remember to
|
|
335
338
|
* never concatenate column names or any other untrusted user input to the SQL string or you
|
|
336
|
-
* are going to create an injection vulnerability. All user input should go to the
|
|
339
|
+
* are going to create an injection vulnerability. All user input should go to the parameters
|
|
337
340
|
* array, never to the SQL string directly. But if the column names or values are trusted
|
|
338
|
-
* and known at compile time, there is no reason to use
|
|
341
|
+
* and known at compile time, there is no reason to use parameters.
|
|
339
342
|
*
|
|
340
343
|
* @example
|
|
341
344
|
* Example of using `raw` in `where`:
|
|
@@ -361,7 +364,7 @@ export declare class QueryCreator<DB> {
|
|
|
361
364
|
*
|
|
362
365
|
* The function in the above example returns people that are older than the given number of
|
|
363
366
|
* years. The number of years in this example is an untrusted user input, and therefore we use
|
|
364
|
-
* a `?`
|
|
367
|
+
* a `?` placeholder for it.
|
|
365
368
|
*
|
|
366
369
|
* @example
|
|
367
370
|
* Example of creating a completely raw query from scratch:
|
|
@@ -375,7 +378,7 @@ export declare class QueryCreator<DB> {
|
|
|
375
378
|
* The result of `execute()` method is always an array. In this case the type of
|
|
376
379
|
* the `persons` variable is `Person[]`.
|
|
377
380
|
*/
|
|
378
|
-
raw<T = unknown>(sql: string,
|
|
381
|
+
raw<T = unknown>(sql: string, parameters?: any[]): RawBuilder<T>;
|
|
379
382
|
}
|
|
380
383
|
export interface QueryCreatorProps {
|
|
381
384
|
readonly executor: QueryExecutor;
|
|
@@ -155,6 +155,9 @@ export class QueryCreator {
|
|
|
155
155
|
* Sets the schema to be used for all table references that don't explicitly
|
|
156
156
|
* specify a schema.
|
|
157
157
|
*
|
|
158
|
+
* This only affects the query created through the builder returned from
|
|
159
|
+
* this method and doesn't modify the `db` instance.
|
|
160
|
+
*
|
|
158
161
|
* @example
|
|
159
162
|
* ```
|
|
160
163
|
* await db.withSchema('mammals')
|
|
@@ -200,10 +203,10 @@ export class QueryCreator {
|
|
|
200
203
|
* Provides a way to pass arbitrary SQL into your query and executing completely
|
|
201
204
|
* raw queries.
|
|
202
205
|
*
|
|
203
|
-
* You can use strings `?` and `??` in the `sql` to bind parameters such as
|
|
206
|
+
* You can use the strings `?` and `??` in the `sql` to bind parameters such as
|
|
204
207
|
* user input to the SQL. You should never EVER concatenate untrusted user
|
|
205
208
|
* input to the SQL string to avoid injection vulnerabilities. Instead use `?`
|
|
206
|
-
* in place of
|
|
209
|
+
* in place of a value and pass the actual value in the `parameters` list. See
|
|
207
210
|
* the examples below.
|
|
208
211
|
*
|
|
209
212
|
* You should only use `raw` when there is no other way to get the job done. This is
|
|
@@ -215,11 +218,11 @@ export class QueryCreator {
|
|
|
215
218
|
* Raw builder instances can be passed to pretty much anywhere: `select`, `where`,
|
|
216
219
|
* `*Join`, `groupBy`, `orderBy` etc. Just try it. If the method accepts it, it works.
|
|
217
220
|
*
|
|
218
|
-
* @param sql - The raw SQL. Special strings `?` and `??` can be used
|
|
219
|
-
*
|
|
221
|
+
* @param sql - The raw SQL. Special strings `?` and `??` can be used as parameter
|
|
222
|
+
* placeholders. `?` for values and `??` for identifiers such as column names
|
|
220
223
|
* or `column.table` references.
|
|
221
224
|
*
|
|
222
|
-
* @param params - The parameters that will be bound to the `?` and `??`
|
|
225
|
+
* @param params - The parameters that will be bound to the `?` and `??` placeholders in
|
|
223
226
|
* the sql string.
|
|
224
227
|
*
|
|
225
228
|
* @example
|
|
@@ -257,7 +260,7 @@ export class QueryCreator {
|
|
|
257
260
|
* the output type. In this case you need to use the `castTo` method on the query
|
|
258
261
|
* to specify a return type for the query.
|
|
259
262
|
*
|
|
260
|
-
* We could've also used `??`
|
|
263
|
+
* We could've also used a `??` placeholder to provide `first_name` and `last_name` like
|
|
261
264
|
* this:
|
|
262
265
|
*
|
|
263
266
|
* ```ts
|
|
@@ -272,9 +275,9 @@ export class QueryCreator {
|
|
|
272
275
|
*
|
|
273
276
|
* But it's often cleaner to just write the column names in the SQL. Again remember to
|
|
274
277
|
* never concatenate column names or any other untrusted user input to the SQL string or you
|
|
275
|
-
* are going to create an injection vulnerability. All user input should go to the
|
|
278
|
+
* are going to create an injection vulnerability. All user input should go to the parameters
|
|
276
279
|
* array, never to the SQL string directly. But if the column names or values are trusted
|
|
277
|
-
* and known at compile time, there is no reason to use
|
|
280
|
+
* and known at compile time, there is no reason to use parameters.
|
|
278
281
|
*
|
|
279
282
|
* @example
|
|
280
283
|
* Example of using `raw` in `where`:
|
|
@@ -300,7 +303,7 @@ export class QueryCreator {
|
|
|
300
303
|
*
|
|
301
304
|
* The function in the above example returns people that are older than the given number of
|
|
302
305
|
* years. The number of years in this example is an untrusted user input, and therefore we use
|
|
303
|
-
* a `?`
|
|
306
|
+
* a `?` placeholder for it.
|
|
304
307
|
*
|
|
305
308
|
* @example
|
|
306
309
|
* Example of creating a completely raw query from scratch:
|
|
@@ -314,12 +317,12 @@ export class QueryCreator {
|
|
|
314
317
|
* The result of `execute()` method is always an array. In this case the type of
|
|
315
318
|
* the `persons` variable is `Person[]`.
|
|
316
319
|
*/
|
|
317
|
-
raw(sql,
|
|
320
|
+
raw(sql, parameters) {
|
|
318
321
|
return new RawBuilder({
|
|
319
322
|
queryId: createQueryId(),
|
|
320
323
|
executor: this.#props.executor,
|
|
321
324
|
sql,
|
|
322
|
-
|
|
325
|
+
parameters,
|
|
323
326
|
});
|
|
324
327
|
}
|
|
325
328
|
}
|
|
@@ -25,24 +25,24 @@ export class RawBuilder {
|
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
27
|
toOperationNode() {
|
|
28
|
-
const
|
|
28
|
+
const parameterPlaceholderRegex = /(\?\??)/g;
|
|
29
29
|
const sql = this.#props.sql;
|
|
30
|
-
const
|
|
30
|
+
const parameters = this.#props.parameters ?? [];
|
|
31
31
|
const sqlFragments = [];
|
|
32
32
|
const argNodes = [];
|
|
33
33
|
let paramIdx = 0;
|
|
34
34
|
let sqlIdx = 0;
|
|
35
35
|
let match = null;
|
|
36
|
-
while ((match =
|
|
36
|
+
while ((match = parameterPlaceholderRegex.exec(sql))) {
|
|
37
37
|
const str = match[1];
|
|
38
|
-
if (paramIdx >=
|
|
39
|
-
throw new Error(`value not provided for all
|
|
38
|
+
if (paramIdx >= parameters.length) {
|
|
39
|
+
throw new Error(`value not provided for all placeholders in string ${sql}`);
|
|
40
40
|
}
|
|
41
41
|
if (match.index > 0 && sql[match.index - 1] === '\\') {
|
|
42
42
|
continue;
|
|
43
43
|
}
|
|
44
44
|
sqlFragments.push(sql.slice(sqlIdx, match.index).replaceAll('\\?', '?'));
|
|
45
|
-
argNodes.push(parseRawArg(str,
|
|
45
|
+
argNodes.push(parseRawArg(str, parameters[paramIdx]));
|
|
46
46
|
sqlIdx = match.index + str.length;
|
|
47
47
|
++paramIdx;
|
|
48
48
|
}
|
|
@@ -12,6 +12,9 @@ import { QueryId } from '../util/query-id.js';
|
|
|
12
12
|
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
13
13
|
import { DataTypeExpression } from '../parser/data-type-parser.js';
|
|
14
14
|
import { ForeignKeyConstraintBuilder, ForeignKeyConstraintBuilderInterface } from './foreign-key-constraint-builder.js';
|
|
15
|
+
/**
|
|
16
|
+
* This builder can be used to create a `alter table` query.
|
|
17
|
+
*/
|
|
15
18
|
export declare class AlterTableBuilder {
|
|
16
19
|
#private;
|
|
17
20
|
constructor(props: AlterTableBuilderProps);
|
|
@@ -20,6 +20,9 @@ import { UniqueConstraintNode } from '../operation-node/unique-constraint-node.j
|
|
|
20
20
|
import { CheckConstraintNode } from '../operation-node/check-constraint-node.js';
|
|
21
21
|
import { ForeignKeyConstraintNode } from '../operation-node/foreign-key-constraint-node.js';
|
|
22
22
|
import { ColumnNode } from '../operation-node/column-node.js';
|
|
23
|
+
/**
|
|
24
|
+
* This builder can be used to create a `alter table` query.
|
|
25
|
+
*/
|
|
23
26
|
export class AlterTableBuilder {
|
|
24
27
|
#props;
|
|
25
28
|
constructor(props) {
|
|
@@ -6,11 +6,11 @@ import { AnyRawBuilder } from '../util/type-utils.js';
|
|
|
6
6
|
import { DefaultValueExpression } from '../parser/default-value-parser.js';
|
|
7
7
|
export interface ColumnDefinitionBuilderInterface<R> {
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* Adds `auto_increment` or `autoincrement` to the column definition
|
|
10
|
+
* depending on the dialect.
|
|
10
11
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* have specified before.
|
|
12
|
+
* Some dialects like PostgreSQL don't support this. On PostgreSQL
|
|
13
|
+
* you can use the `serial` or `bigserial` data type instead.
|
|
14
14
|
*/
|
|
15
15
|
increments(): R;
|
|
16
16
|
/**
|
|
@@ -23,6 +23,10 @@ export interface ColumnDefinitionBuilderInterface<R> {
|
|
|
23
23
|
/**
|
|
24
24
|
* Adds a foreign key constraint for the column.
|
|
25
25
|
*
|
|
26
|
+
* If your database engine doesn't support foreign key constraints in the
|
|
27
|
+
* column definition (like MySQL 5) you need to call the table level
|
|
28
|
+
* {@link TableBuilder.addForeignKeyConstraint} method instead.
|
|
29
|
+
*
|
|
26
30
|
* @example
|
|
27
31
|
* ```ts
|
|
28
32
|
* col.references('person.id')
|
|
@@ -31,6 +35,11 @@ export interface ColumnDefinitionBuilderInterface<R> {
|
|
|
31
35
|
references(ref: string): R;
|
|
32
36
|
/**
|
|
33
37
|
* Adds an `on delete` constraint for the foreign key column.
|
|
38
|
+
*
|
|
39
|
+
* If your database engine doesn't support foreign key constraints in the
|
|
40
|
+
* column definition (like MySQL 5) you need to call the table level
|
|
41
|
+
* {@link TableBuilder.addForeignKeyConstraint} method instead.
|
|
42
|
+
*
|
|
34
43
|
*/
|
|
35
44
|
onDelete(onDelete: OnModifyForeignAction): R;
|
|
36
45
|
/**
|
|
@@ -64,7 +73,7 @@ export interface ColumnDefinitionBuilderInterface<R> {
|
|
|
64
73
|
* ```ts
|
|
65
74
|
* db.schema
|
|
66
75
|
* .createTable('pet')
|
|
67
|
-
* .addColumn('number_of_legs', (col) => col.check('number_of_legs < 5'
|
|
76
|
+
* .addColumn('number_of_legs', 'integer', (col) => col.check('number_of_legs < 5'))
|
|
68
77
|
* .execute()
|
|
69
78
|
* ```
|
|
70
79
|
*/
|
|
@@ -9,7 +9,7 @@ export class CreateSchemaBuilder {
|
|
|
9
9
|
ifNotExists() {
|
|
10
10
|
return new CreateSchemaBuilder({
|
|
11
11
|
...this.#props,
|
|
12
|
-
createSchemaNode: CreateSchemaNode.
|
|
12
|
+
createSchemaNode: CreateSchemaNode.cloneWith(this.#props.createSchemaNode, { modifier: 'IfNotExists' }),
|
|
13
13
|
});
|
|
14
14
|
}
|
|
15
15
|
toOperationNode() {
|
|
@@ -7,7 +7,10 @@ import { ColumnDefinitionBuilder } from './column-definition-builder.js';
|
|
|
7
7
|
import { QueryId } from '../util/query-id.js';
|
|
8
8
|
import { ForeignKeyConstraintBuilder } from './foreign-key-constraint-builder.js';
|
|
9
9
|
import { DataTypeExpression } from '../parser/data-type-parser.js';
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* This builder can be used to create a `create table` query.
|
|
12
|
+
*/
|
|
13
|
+
export declare class CreateTableBuilder<TB extends string, C extends string = never> implements OperationNodeSource, Compilable {
|
|
11
14
|
#private;
|
|
12
15
|
constructor(props: CreateTableBuilderProps);
|
|
13
16
|
/**
|
|
@@ -15,7 +18,7 @@ export declare class CreateTableBuilder implements OperationNodeSource, Compilab
|
|
|
15
18
|
*
|
|
16
19
|
* If the table already exists, no error is thrown if this method has been called.
|
|
17
20
|
*/
|
|
18
|
-
ifNotExists(): CreateTableBuilder
|
|
21
|
+
ifNotExists(): CreateTableBuilder<TB, C>;
|
|
19
22
|
/**
|
|
20
23
|
* Adds a column to the table.
|
|
21
24
|
*
|
|
@@ -23,44 +26,86 @@ export declare class CreateTableBuilder implements OperationNodeSource, Compilab
|
|
|
23
26
|
* ```ts
|
|
24
27
|
* await db.schema
|
|
25
28
|
* .createTable('person')
|
|
26
|
-
* .addColumn('
|
|
29
|
+
* .addColumn('id', 'integer', (col) => col.increments().primaryKey()),
|
|
27
30
|
* .addColumn('first_name', 'varchar(50), (col) => col.notNull())
|
|
28
|
-
* .addColumn('
|
|
29
|
-
* .addColumn('numeric(8, 2)'
|
|
31
|
+
* .addColumn('last_name', 'varchar')
|
|
32
|
+
* .addColumn('bank_balance', 'numeric(8, 2)')
|
|
30
33
|
* .addColumn('data', db.raw('customtype'))
|
|
34
|
+
* .addColumn('parent_id', 'integer', (col) =>
|
|
35
|
+
* col.references('person.id').onDelete('cascade'))
|
|
36
|
+
* )
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* With this method, it's once again good to remember that Kysely just builds the query
|
|
40
|
+
* that's as close to the structure of your builder method calls as possible, and doesn't
|
|
41
|
+
* provide the same API for all databses. For example, some databases like older MySQL
|
|
42
|
+
* don't support `references` statement in the column definition. Instead foreign key
|
|
43
|
+
* constraints need to be defined in at the level of the `create table` query. See
|
|
44
|
+
* the next example:
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* .addColumn('parent_id', 'integer')
|
|
49
|
+
* .addForeignKeyConstraint(
|
|
50
|
+
* 'person_parent_id_fk', ['parent_id'], 'person', ['id'],
|
|
51
|
+
* (cb) => cb.onDelete('cascade')
|
|
52
|
+
* )
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* Another good example is that PostgreSQL doesn't support the `auto_increment`
|
|
56
|
+
* keyword and you need to define an autoincrementing column for example using
|
|
57
|
+
* `serial`:
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* await db.schema
|
|
62
|
+
* .createTable('person')
|
|
63
|
+
* .addColumn('id', 'serial', (col) => col.primaryKey()),
|
|
31
64
|
* ```
|
|
32
65
|
*/
|
|
33
|
-
addColumn(columnName:
|
|
66
|
+
addColumn<CN extends string>(columnName: CN, dataType: DataTypeExpression, build?: ColumnBuilderCallback): CreateTableBuilder<TB, C | CN>;
|
|
34
67
|
/**
|
|
35
68
|
* Adds a primary key constraint for one or more columns.
|
|
36
69
|
*
|
|
70
|
+
* The constraint name can be anything you want, but it must be unique
|
|
71
|
+
* across the whole database.
|
|
72
|
+
*
|
|
37
73
|
* @example
|
|
38
74
|
* ```ts
|
|
39
75
|
* addPrimaryKeyConstraint('primary_key', ['first_name', 'last_name'])
|
|
40
76
|
* ```
|
|
41
77
|
*/
|
|
42
|
-
addPrimaryKeyConstraint(constraintName: string, columns:
|
|
78
|
+
addPrimaryKeyConstraint(constraintName: string, columns: C[]): CreateTableBuilder<TB, C>;
|
|
43
79
|
/**
|
|
44
80
|
* Adds a unique constraint for one or more columns.
|
|
45
81
|
*
|
|
82
|
+
* The constraint name can be anything you want, but it must be unique
|
|
83
|
+
* across the whole database.
|
|
84
|
+
*
|
|
46
85
|
* @example
|
|
47
86
|
* ```ts
|
|
48
87
|
* addUniqueConstraint('first_name_last_name_unique', ['first_name', 'last_name'])
|
|
49
88
|
* ```
|
|
50
89
|
*/
|
|
51
|
-
addUniqueConstraint(constraintName: string, columns:
|
|
90
|
+
addUniqueConstraint(constraintName: string, columns: C[]): CreateTableBuilder<TB, C>;
|
|
52
91
|
/**
|
|
53
92
|
* Adds a check constraint.
|
|
54
93
|
*
|
|
94
|
+
* The constraint name can be anything you want, but it must be unique
|
|
95
|
+
* across the whole database.
|
|
96
|
+
*
|
|
55
97
|
* @example
|
|
56
98
|
* ```ts
|
|
57
99
|
* addCheckConstraint('check_legs', 'number_of_legs < 5')
|
|
58
100
|
* ```
|
|
59
101
|
*/
|
|
60
|
-
addCheckConstraint(constraintName: string, checkExpression: string): CreateTableBuilder
|
|
102
|
+
addCheckConstraint(constraintName: string, checkExpression: string): CreateTableBuilder<TB, C>;
|
|
61
103
|
/**
|
|
62
104
|
* Adds a foreign key constraint.
|
|
63
105
|
*
|
|
106
|
+
* The constraint name can be anything you want, but it must be unique
|
|
107
|
+
* across the whole database.
|
|
108
|
+
*
|
|
64
109
|
* @example
|
|
65
110
|
* ```ts
|
|
66
111
|
* addForeignKeyConstraint(
|
|
@@ -82,7 +127,7 @@ export declare class CreateTableBuilder implements OperationNodeSource, Compilab
|
|
|
82
127
|
* )
|
|
83
128
|
* ```
|
|
84
129
|
*/
|
|
85
|
-
addForeignKeyConstraint(constraintName: string, columns:
|
|
130
|
+
addForeignKeyConstraint(constraintName: string, columns: C[], targetTable: string, targetColumns: string[], build?: ForeignKeyConstraintBuilderCallback): CreateTableBuilder<TB, C>;
|
|
86
131
|
toOperationNode(): CreateTableNode;
|
|
87
132
|
compile(): CompiledQuery;
|
|
88
133
|
execute(): Promise<void>;
|