kysely 0.9.6 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -13
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/operation-node/column-definition-node.d.ts +4 -4
- package/dist/cjs/operation-node/column-definition-node.js +4 -4
- package/dist/cjs/operation-node/create-schema-node.d.ts +1 -2
- package/dist/cjs/operation-node/create-table-node.d.ts +2 -3
- package/dist/cjs/operation-node/create-table-node.js +2 -2
- package/dist/cjs/operation-node/create-view-node.d.ts +22 -0
- package/dist/cjs/operation-node/create-view-node.js +25 -0
- package/dist/cjs/operation-node/drop-index-node.d.ts +1 -2
- package/dist/cjs/operation-node/drop-schema-node.d.ts +2 -3
- package/dist/cjs/operation-node/drop-schema-node.js +3 -3
- package/dist/cjs/operation-node/drop-table-node.d.ts +2 -3
- package/dist/cjs/operation-node/drop-table-node.js +2 -2
- package/dist/cjs/operation-node/drop-view-node.d.ts +17 -0
- package/dist/cjs/operation-node/drop-view-node.js +25 -0
- package/dist/cjs/operation-node/operation-node-transformer.d.ts +4 -0
- package/dist/cjs/operation-node/operation-node-transformer.js +29 -9
- package/dist/cjs/operation-node/operation-node-visitor.d.ts +4 -0
- package/dist/cjs/operation-node/operation-node-visitor.js +2 -0
- package/dist/cjs/operation-node/operation-node.d.ts +1 -1
- package/dist/cjs/operation-node/operator-node.d.ts +1 -1
- package/dist/cjs/operation-node/operator-node.js +5 -0
- package/dist/cjs/operation-node/select-query-node.d.ts +1 -1
- package/dist/cjs/operation-node/select-query-node.js +1 -1
- package/dist/cjs/plugin/immediate-value.ts/immediate-value-plugin.d.ts +17 -0
- package/dist/cjs/plugin/immediate-value.ts/immediate-value-plugin.js +22 -0
- package/dist/cjs/plugin/immediate-value.ts/immediate-value-transformer.d.ts +13 -0
- package/dist/cjs/plugin/immediate-value.ts/immediate-value-transformer.js +21 -0
- package/dist/cjs/plugin/with-schema/with-schema-transformer.d.ts +4 -0
- package/dist/cjs/plugin/with-schema/with-schema-transformer.js +6 -0
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +4 -0
- package/dist/cjs/query-compiler/default-query-compiler.js +42 -10
- package/dist/cjs/query-compiler/query-compiler.d.ts +3 -1
- package/dist/cjs/schema/alter-table-builder.d.ts +2 -2
- package/dist/cjs/schema/alter-table-builder.js +4 -4
- package/dist/cjs/schema/column-definition-builder.d.ts +2 -2
- package/dist/cjs/schema/column-definition-builder.js +5 -5
- package/dist/cjs/schema/create-schema-builder.js +1 -1
- package/dist/cjs/schema/create-table-builder.d.ts +1 -1
- package/dist/cjs/schema/create-table-builder.js +2 -2
- package/dist/cjs/schema/create-view-builder.d.ts +24 -0
- package/dist/cjs/schema/create-view-builder.js +73 -0
- package/dist/cjs/schema/drop-index-builder.js +1 -1
- package/dist/cjs/schema/drop-schema-builder.js +3 -1
- package/dist/cjs/schema/drop-table-builder.js +3 -1
- package/dist/cjs/schema/drop-view-builder.d.ts +20 -0
- package/dist/cjs/schema/drop-view-builder.js +39 -0
- package/dist/cjs/schema/schema.d.ts +30 -3
- package/dist/cjs/schema/schema.js +44 -3
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/operation-node/column-definition-node.d.ts +4 -4
- package/dist/esm/operation-node/column-definition-node.js +4 -4
- package/dist/esm/operation-node/create-schema-node.d.ts +1 -2
- package/dist/esm/operation-node/create-table-node.d.ts +2 -3
- package/dist/esm/operation-node/create-table-node.js +2 -2
- package/dist/esm/operation-node/create-view-node.d.ts +22 -0
- package/dist/esm/operation-node/create-view-node.js +22 -0
- package/dist/esm/operation-node/drop-index-node.d.ts +1 -2
- package/dist/esm/operation-node/drop-schema-node.d.ts +2 -3
- package/dist/esm/operation-node/drop-schema-node.js +3 -3
- package/dist/esm/operation-node/drop-table-node.d.ts +2 -3
- package/dist/esm/operation-node/drop-table-node.js +2 -2
- package/dist/esm/operation-node/drop-view-node.d.ts +17 -0
- package/dist/esm/operation-node/drop-view-node.js +22 -0
- package/dist/esm/operation-node/operation-node-transformer.d.ts +4 -0
- package/dist/esm/operation-node/operation-node-transformer.js +29 -9
- package/dist/esm/operation-node/operation-node-visitor.d.ts +4 -0
- package/dist/esm/operation-node/operation-node-visitor.js +2 -0
- package/dist/esm/operation-node/operation-node.d.ts +1 -1
- package/dist/esm/operation-node/operator-node.d.ts +1 -1
- package/dist/esm/operation-node/operator-node.js +5 -0
- package/dist/esm/operation-node/select-query-node.d.ts +1 -1
- package/dist/esm/operation-node/select-query-node.js +1 -1
- package/dist/esm/plugin/immediate-value.ts/immediate-value-plugin.d.ts +17 -0
- package/dist/esm/plugin/immediate-value.ts/immediate-value-plugin.js +18 -0
- package/dist/esm/plugin/immediate-value.ts/immediate-value-transformer.d.ts +13 -0
- package/dist/esm/plugin/immediate-value.ts/immediate-value-transformer.js +17 -0
- package/dist/esm/plugin/with-schema/with-schema-transformer.d.ts +4 -0
- package/dist/esm/plugin/with-schema/with-schema-transformer.js +6 -0
- package/dist/esm/query-compiler/default-query-compiler.d.ts +4 -0
- package/dist/esm/query-compiler/default-query-compiler.js +42 -10
- package/dist/esm/query-compiler/query-compiler.d.ts +3 -1
- package/dist/esm/schema/alter-table-builder.d.ts +2 -2
- package/dist/esm/schema/alter-table-builder.js +4 -4
- package/dist/esm/schema/column-definition-builder.d.ts +2 -2
- package/dist/esm/schema/column-definition-builder.js +5 -5
- package/dist/esm/schema/create-schema-builder.js +1 -1
- package/dist/esm/schema/create-table-builder.d.ts +1 -1
- package/dist/esm/schema/create-table-builder.js +2 -2
- package/dist/esm/schema/create-view-builder.d.ts +24 -0
- package/dist/esm/schema/create-view-builder.js +69 -0
- package/dist/esm/schema/drop-index-builder.js +1 -1
- package/dist/esm/schema/drop-schema-builder.js +3 -1
- package/dist/esm/schema/drop-table-builder.js +3 -1
- package/dist/esm/schema/drop-view-builder.d.ts +20 -0
- package/dist/esm/schema/drop-view-builder.js +35 -0
- package/dist/esm/schema/schema.d.ts +30 -3
- package/dist/esm/schema/schema.js +44 -3
- package/package.json +4 -1
|
@@ -35,7 +35,7 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
35
35
|
this.compileDistinctOn(node.distinctOnSelections);
|
|
36
36
|
this.append(' ');
|
|
37
37
|
}
|
|
38
|
-
if (node.
|
|
38
|
+
if (node.distinct) {
|
|
39
39
|
this.append('distinct');
|
|
40
40
|
this.append(' ');
|
|
41
41
|
}
|
|
@@ -287,7 +287,7 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
287
287
|
}
|
|
288
288
|
visitCreateTable(node) {
|
|
289
289
|
this.append('create table ');
|
|
290
|
-
if (node.
|
|
290
|
+
if (node.ifNotExists) {
|
|
291
291
|
this.append('if not exists ');
|
|
292
292
|
}
|
|
293
293
|
this.visitNode(node.table);
|
|
@@ -303,17 +303,17 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
303
303
|
this.append(' default ');
|
|
304
304
|
this.visitNode(node.defaultTo);
|
|
305
305
|
}
|
|
306
|
-
if (!node.
|
|
306
|
+
if (!node.nullable) {
|
|
307
307
|
this.append(' not null');
|
|
308
308
|
}
|
|
309
|
-
if (node.
|
|
309
|
+
if (node.autoIncrement) {
|
|
310
310
|
this.append(' ');
|
|
311
311
|
this.append(this.getAutoIncrement());
|
|
312
312
|
}
|
|
313
|
-
if (node.
|
|
313
|
+
if (node.unique) {
|
|
314
314
|
this.append(' unique');
|
|
315
315
|
}
|
|
316
|
-
if (node.
|
|
316
|
+
if (node.primaryKey) {
|
|
317
317
|
this.append(' primary key');
|
|
318
318
|
}
|
|
319
319
|
if (node.references) {
|
|
@@ -345,7 +345,7 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
345
345
|
}
|
|
346
346
|
visitDropTable(node) {
|
|
347
347
|
this.append('drop table ');
|
|
348
|
-
if (node.
|
|
348
|
+
if (node.ifExists) {
|
|
349
349
|
this.append('if exists ');
|
|
350
350
|
}
|
|
351
351
|
this.visitNode(node.table);
|
|
@@ -467,7 +467,7 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
467
467
|
}
|
|
468
468
|
visitDropIndex(node) {
|
|
469
469
|
this.append('drop index ');
|
|
470
|
-
if (node.
|
|
470
|
+
if (node.ifExists) {
|
|
471
471
|
this.append('if exists ');
|
|
472
472
|
}
|
|
473
473
|
this.visitNode(node.name);
|
|
@@ -478,14 +478,14 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
478
478
|
}
|
|
479
479
|
visitCreateSchema(node) {
|
|
480
480
|
this.append('create schema ');
|
|
481
|
-
if (node.
|
|
481
|
+
if (node.ifNotExists) {
|
|
482
482
|
this.append('if not exists ');
|
|
483
483
|
}
|
|
484
484
|
this.visitNode(node.schema);
|
|
485
485
|
}
|
|
486
486
|
visitDropSchema(node) {
|
|
487
487
|
this.append('drop schema ');
|
|
488
|
-
if (node.
|
|
488
|
+
if (node.ifExists) {
|
|
489
489
|
this.append('if exists ');
|
|
490
490
|
}
|
|
491
491
|
this.visitNode(node.schema);
|
|
@@ -644,6 +644,38 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
644
644
|
}
|
|
645
645
|
this.visitNode(node.union);
|
|
646
646
|
}
|
|
647
|
+
visitCreateView(node) {
|
|
648
|
+
this.append('create ');
|
|
649
|
+
if (node.orReplace) {
|
|
650
|
+
this.append('or replace ');
|
|
651
|
+
}
|
|
652
|
+
if (node.materialized) {
|
|
653
|
+
this.append('materialized ');
|
|
654
|
+
}
|
|
655
|
+
this.append('view ');
|
|
656
|
+
this.visitNode(node.name);
|
|
657
|
+
this.append(' ');
|
|
658
|
+
if (node.columns) {
|
|
659
|
+
this.append('(');
|
|
660
|
+
this.compileList(node.columns);
|
|
661
|
+
this.append(') ');
|
|
662
|
+
}
|
|
663
|
+
if (node.as) {
|
|
664
|
+
this.append('as ');
|
|
665
|
+
this.visitNode(node.as);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
visitDropView(node) {
|
|
669
|
+
this.append('drop ');
|
|
670
|
+
if (node.materialized) {
|
|
671
|
+
this.append('materialized ');
|
|
672
|
+
}
|
|
673
|
+
this.append('view ');
|
|
674
|
+
if (node.ifExists) {
|
|
675
|
+
this.append('if exists ');
|
|
676
|
+
}
|
|
677
|
+
this.visitNode(node.name);
|
|
678
|
+
}
|
|
647
679
|
append(str) {
|
|
648
680
|
this.#sql += str;
|
|
649
681
|
}
|
|
@@ -2,13 +2,15 @@ import { AlterTableNode } from '../operation-node/alter-table-node.js';
|
|
|
2
2
|
import { CreateIndexNode } from '../operation-node/create-index-node.js';
|
|
3
3
|
import { CreateSchemaNode } from '../operation-node/create-schema-node.js';
|
|
4
4
|
import { CreateTableNode } from '../operation-node/create-table-node.js';
|
|
5
|
+
import { CreateViewNode } from '../operation-node/create-view-node.js';
|
|
5
6
|
import { DropIndexNode } from '../operation-node/drop-index-node.js';
|
|
6
7
|
import { DropSchemaNode } from '../operation-node/drop-schema-node.js';
|
|
7
8
|
import { DropTableNode } from '../operation-node/drop-table-node.js';
|
|
9
|
+
import { DropViewNode } from '../operation-node/drop-view-node.js';
|
|
8
10
|
import { QueryNode } from '../operation-node/query-node.js';
|
|
9
11
|
import { RawNode } from '../operation-node/raw-node.js';
|
|
10
12
|
import { CompiledQuery } from './compiled-query.js';
|
|
11
|
-
export declare type RootOperationNode = QueryNode | CreateTableNode | CreateIndexNode |
|
|
13
|
+
export declare type RootOperationNode = QueryNode | CreateTableNode | CreateIndexNode | CreateSchemaNode | CreateViewNode | DropTableNode | DropIndexNode | DropSchemaNode | DropViewNode | AlterTableNode | RawNode;
|
|
12
14
|
/**
|
|
13
15
|
* a `QueryCompiler` compiles a query expressed as a tree of `OperationNodes` into SQL.
|
|
14
16
|
*/
|
|
@@ -63,7 +63,7 @@ export interface AlterTableExecutorProps extends AlterTableBuilderProps {
|
|
|
63
63
|
export declare class AlterTableAddColumnBuilder implements ColumnDefinitionBuilderInterface<AlterTableAddColumnBuilder>, OperationNodeSource, Compilable {
|
|
64
64
|
#private;
|
|
65
65
|
constructor(props: AlterTableAddColumnBuilderProps);
|
|
66
|
-
|
|
66
|
+
autoIncrement(): AlterTableAddColumnBuilder;
|
|
67
67
|
primaryKey(): AlterTableAddColumnBuilder;
|
|
68
68
|
references(ref: string): AlterTableAddColumnBuilder;
|
|
69
69
|
onDelete(onDelete: OnModifyForeignAction): AlterTableAddColumnBuilder;
|
|
@@ -82,7 +82,7 @@ export interface AlterTableAddColumnBuilderProps extends AlterTableBuilderProps
|
|
|
82
82
|
export declare class AlterTableModifyColumnBuilder implements ColumnDefinitionBuilderInterface<AlterTableModifyColumnBuilder>, OperationNodeSource, Compilable {
|
|
83
83
|
#private;
|
|
84
84
|
constructor(props: AlterTableModifyColumnBuilderProps);
|
|
85
|
-
|
|
85
|
+
autoIncrement(): AlterTableModifyColumnBuilder;
|
|
86
86
|
primaryKey(): AlterTableModifyColumnBuilder;
|
|
87
87
|
references(ref: string): AlterTableModifyColumnBuilder;
|
|
88
88
|
onDelete(onDelete: OnModifyForeignAction): AlterTableModifyColumnBuilder;
|
|
@@ -184,10 +184,10 @@ export class AlterTableAddColumnBuilder {
|
|
|
184
184
|
constructor(props) {
|
|
185
185
|
this.#props = freeze(props);
|
|
186
186
|
}
|
|
187
|
-
|
|
187
|
+
autoIncrement() {
|
|
188
188
|
return new AlterTableAddColumnBuilder({
|
|
189
189
|
...this.#props,
|
|
190
|
-
columnBuilder: this.#props.columnBuilder.
|
|
190
|
+
columnBuilder: this.#props.columnBuilder.autoIncrement(),
|
|
191
191
|
});
|
|
192
192
|
}
|
|
193
193
|
primaryKey() {
|
|
@@ -255,10 +255,10 @@ export class AlterTableModifyColumnBuilder {
|
|
|
255
255
|
constructor(props) {
|
|
256
256
|
this.#props = freeze(props);
|
|
257
257
|
}
|
|
258
|
-
|
|
258
|
+
autoIncrement() {
|
|
259
259
|
return new AlterTableModifyColumnBuilder({
|
|
260
260
|
...this.#props,
|
|
261
|
-
columnBuilder: this.#props.columnBuilder.
|
|
261
|
+
columnBuilder: this.#props.columnBuilder.autoIncrement(),
|
|
262
262
|
});
|
|
263
263
|
}
|
|
264
264
|
primaryKey() {
|
|
@@ -12,7 +12,7 @@ export interface ColumnDefinitionBuilderInterface<R> {
|
|
|
12
12
|
* Some dialects like PostgreSQL don't support this. On PostgreSQL
|
|
13
13
|
* you can use the `serial` or `bigserial` data type instead.
|
|
14
14
|
*/
|
|
15
|
-
|
|
15
|
+
autoIncrement(): R;
|
|
16
16
|
/**
|
|
17
17
|
* Makes the column the primary key.
|
|
18
18
|
*
|
|
@@ -82,7 +82,7 @@ export interface ColumnDefinitionBuilderInterface<R> {
|
|
|
82
82
|
export declare class ColumnDefinitionBuilder implements ColumnDefinitionBuilderInterface<ColumnDefinitionBuilder>, OperationNodeSource {
|
|
83
83
|
#private;
|
|
84
84
|
constructor(node: ColumnDefinitionNode);
|
|
85
|
-
|
|
85
|
+
autoIncrement(): ColumnDefinitionBuilder;
|
|
86
86
|
primaryKey(): ColumnDefinitionBuilder;
|
|
87
87
|
references(ref: string): ColumnDefinitionBuilder;
|
|
88
88
|
onDelete(onDelete: OnModifyForeignAction): ColumnDefinitionBuilder;
|
|
@@ -11,11 +11,11 @@ export class ColumnDefinitionBuilder {
|
|
|
11
11
|
constructor(node) {
|
|
12
12
|
this.#node = node;
|
|
13
13
|
}
|
|
14
|
-
|
|
15
|
-
return new ColumnDefinitionBuilder(ColumnDefinitionNode.cloneWith(this.#node, {
|
|
14
|
+
autoIncrement() {
|
|
15
|
+
return new ColumnDefinitionBuilder(ColumnDefinitionNode.cloneWith(this.#node, { autoIncrement: true }));
|
|
16
16
|
}
|
|
17
17
|
primaryKey() {
|
|
18
|
-
return new ColumnDefinitionBuilder(ColumnDefinitionNode.cloneWith(this.#node, {
|
|
18
|
+
return new ColumnDefinitionBuilder(ColumnDefinitionNode.cloneWith(this.#node, { primaryKey: true }));
|
|
19
19
|
}
|
|
20
20
|
references(ref) {
|
|
21
21
|
const references = parseStringReference(ref);
|
|
@@ -45,10 +45,10 @@ export class ColumnDefinitionBuilder {
|
|
|
45
45
|
}));
|
|
46
46
|
}
|
|
47
47
|
unique() {
|
|
48
|
-
return new ColumnDefinitionBuilder(ColumnDefinitionNode.cloneWith(this.#node, {
|
|
48
|
+
return new ColumnDefinitionBuilder(ColumnDefinitionNode.cloneWith(this.#node, { unique: true }));
|
|
49
49
|
}
|
|
50
50
|
notNull() {
|
|
51
|
-
return new ColumnDefinitionBuilder(ColumnDefinitionNode.cloneWith(this.#node, {
|
|
51
|
+
return new ColumnDefinitionBuilder(ColumnDefinitionNode.cloneWith(this.#node, { nullable: false }));
|
|
52
52
|
}
|
|
53
53
|
defaultTo(value) {
|
|
54
54
|
return new ColumnDefinitionBuilder(ColumnDefinitionNode.cloneWith(this.#node, {
|
|
@@ -9,7 +9,7 @@ export class CreateSchemaBuilder {
|
|
|
9
9
|
ifNotExists() {
|
|
10
10
|
return new CreateSchemaBuilder({
|
|
11
11
|
...this.#props,
|
|
12
|
-
createSchemaNode: CreateSchemaNode.cloneWith(this.#props.createSchemaNode, {
|
|
12
|
+
createSchemaNode: CreateSchemaNode.cloneWith(this.#props.createSchemaNode, { ifNotExists: true }),
|
|
13
13
|
});
|
|
14
14
|
}
|
|
15
15
|
toOperationNode() {
|
|
@@ -26,7 +26,7 @@ export declare class CreateTableBuilder<TB extends string, C extends string = ne
|
|
|
26
26
|
* ```ts
|
|
27
27
|
* await db.schema
|
|
28
28
|
* .createTable('person')
|
|
29
|
-
* .addColumn('id', 'integer', (col) => col.
|
|
29
|
+
* .addColumn('id', 'integer', (col) => col.autoIncrement().primaryKey()),
|
|
30
30
|
* .addColumn('first_name', 'varchar(50), (col) => col.notNull())
|
|
31
31
|
* .addColumn('last_name', 'varchar')
|
|
32
32
|
* .addColumn('bank_balance', 'numeric(8, 2)')
|
|
@@ -27,7 +27,7 @@ export class CreateTableBuilder {
|
|
|
27
27
|
ifNotExists() {
|
|
28
28
|
return new CreateTableBuilder({
|
|
29
29
|
...this.#props,
|
|
30
|
-
createTableNode: CreateTableNode.
|
|
30
|
+
createTableNode: CreateTableNode.cloneWithIfNotExists(this.#props.createTableNode, true),
|
|
31
31
|
});
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
@@ -37,7 +37,7 @@ export class CreateTableBuilder {
|
|
|
37
37
|
* ```ts
|
|
38
38
|
* await db.schema
|
|
39
39
|
* .createTable('person')
|
|
40
|
-
* .addColumn('id', 'integer', (col) => col.
|
|
40
|
+
* .addColumn('id', 'integer', (col) => col.autoIncrement().primaryKey()),
|
|
41
41
|
* .addColumn('first_name', 'varchar(50), (col) => col.notNull())
|
|
42
42
|
* .addColumn('last_name', 'varchar')
|
|
43
43
|
* .addColumn('bank_balance', 'numeric(8, 2)')
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { OperationNodeSource } from '../operation-node/operation-node-source.js';
|
|
2
|
+
import { CompiledQuery } from '../query-compiler/compiled-query.js';
|
|
3
|
+
import { Compilable } from '../util/compilable.js';
|
|
4
|
+
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
5
|
+
import { QueryId } from '../util/query-id.js';
|
|
6
|
+
import { CreateViewNode } from '../operation-node/create-view-node.js';
|
|
7
|
+
import { AnyQueryBuilder } from '../index.js';
|
|
8
|
+
import { AnyRawBuilder } from '../util/type-utils.js';
|
|
9
|
+
export declare class CreateViewBuilder implements OperationNodeSource, Compilable {
|
|
10
|
+
#private;
|
|
11
|
+
constructor(props: CreateViewBuilderProps);
|
|
12
|
+
materialized(): CreateViewBuilder;
|
|
13
|
+
orReplace(): CreateViewBuilder;
|
|
14
|
+
columns(columns: string[]): CreateViewBuilder;
|
|
15
|
+
as(query: AnyQueryBuilder | AnyRawBuilder): CreateViewBuilder;
|
|
16
|
+
toOperationNode(): CreateViewNode;
|
|
17
|
+
compile(): CompiledQuery;
|
|
18
|
+
execute(): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
export interface CreateViewBuilderProps {
|
|
21
|
+
readonly queryId: QueryId;
|
|
22
|
+
readonly executor: QueryExecutor;
|
|
23
|
+
readonly createViewNode: CreateViewNode;
|
|
24
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { preventAwait } from '../util/prevent-await.js';
|
|
2
|
+
import { freeze } from '../util/object-utils.js';
|
|
3
|
+
import { CreateViewNode } from '../operation-node/create-view-node.js';
|
|
4
|
+
import { parseColumnName } from '../parser/reference-parser.js';
|
|
5
|
+
import { QueryNode } from '../index.js';
|
|
6
|
+
import { ImmediateValuePlugin } from '../plugin/immediate-value.ts/immediate-value-plugin.js';
|
|
7
|
+
export class CreateViewBuilder {
|
|
8
|
+
#props;
|
|
9
|
+
constructor(props) {
|
|
10
|
+
this.#props = freeze({
|
|
11
|
+
...props,
|
|
12
|
+
// The select statement (defined using the `as` method) can't have parameters on some
|
|
13
|
+
// dialects. We add the `ImmediateValuePlugin` to make all parameters "immediate"
|
|
14
|
+
// meaning they are interpolated into the SQL instead of added as parameters.
|
|
15
|
+
//
|
|
16
|
+
// The values are therefore not escaped at all, which leads to SQL injection
|
|
17
|
+
// vulnerabilities if user input is passed into the query. However, the use
|
|
18
|
+
// case where user input is passed into a `create view` statement is a weird
|
|
19
|
+
// one, and we can pretty safely just ignore it.
|
|
20
|
+
executor: props.executor.withPlugin(new ImmediateValuePlugin()),
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
materialized() {
|
|
24
|
+
return new CreateViewBuilder({
|
|
25
|
+
...this.#props,
|
|
26
|
+
createViewNode: CreateViewNode.cloneWith(this.#props.createViewNode, {
|
|
27
|
+
materialized: true,
|
|
28
|
+
}),
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
orReplace() {
|
|
32
|
+
return new CreateViewBuilder({
|
|
33
|
+
...this.#props,
|
|
34
|
+
createViewNode: CreateViewNode.cloneWith(this.#props.createViewNode, {
|
|
35
|
+
orReplace: true,
|
|
36
|
+
}),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
columns(columns) {
|
|
40
|
+
return new CreateViewBuilder({
|
|
41
|
+
...this.#props,
|
|
42
|
+
createViewNode: CreateViewNode.cloneWith(this.#props.createViewNode, {
|
|
43
|
+
columns: columns.map(parseColumnName),
|
|
44
|
+
}),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
as(query) {
|
|
48
|
+
const queryNode = query.toOperationNode();
|
|
49
|
+
if (QueryNode.isMutating(queryNode)) {
|
|
50
|
+
throw new Error('only select statements are allowd in views');
|
|
51
|
+
}
|
|
52
|
+
return new CreateViewBuilder({
|
|
53
|
+
...this.#props,
|
|
54
|
+
createViewNode: CreateViewNode.cloneWith(this.#props.createViewNode, {
|
|
55
|
+
as: queryNode,
|
|
56
|
+
}),
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
toOperationNode() {
|
|
60
|
+
return this.#props.executor.transformQuery(this.#props.createViewNode, this.#props.queryId);
|
|
61
|
+
}
|
|
62
|
+
compile() {
|
|
63
|
+
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
|
|
64
|
+
}
|
|
65
|
+
async execute() {
|
|
66
|
+
await this.#props.executor.executeQuery(this.compile(), this.#props.queryId);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
preventAwait(CreateViewBuilder, "don't await CreateViewBuilder instances directly. To execute the query you need to call `execute`");
|
|
@@ -9,7 +9,9 @@ export class DropSchemaBuilder {
|
|
|
9
9
|
ifExists() {
|
|
10
10
|
return new DropSchemaBuilder({
|
|
11
11
|
...this.#props,
|
|
12
|
-
dropSchemaNode: DropSchemaNode.
|
|
12
|
+
dropSchemaNode: DropSchemaNode.cloneWith(this.#props.dropSchemaNode, {
|
|
13
|
+
ifExists: true,
|
|
14
|
+
}),
|
|
13
15
|
});
|
|
14
16
|
}
|
|
15
17
|
toOperationNode() {
|
|
@@ -9,7 +9,9 @@ export class DropTableBuilder {
|
|
|
9
9
|
ifExists() {
|
|
10
10
|
return new DropTableBuilder({
|
|
11
11
|
...this.#props,
|
|
12
|
-
dropTableNode: DropTableNode.
|
|
12
|
+
dropTableNode: DropTableNode.cloneWith(this.#props.dropTableNode, {
|
|
13
|
+
ifExists: true,
|
|
14
|
+
}),
|
|
13
15
|
});
|
|
14
16
|
}
|
|
15
17
|
toOperationNode() {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { OperationNodeSource } from '../operation-node/operation-node-source.js';
|
|
2
|
+
import { CompiledQuery } from '../query-compiler/compiled-query.js';
|
|
3
|
+
import { Compilable } from '../util/compilable.js';
|
|
4
|
+
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
5
|
+
import { QueryId } from '../util/query-id.js';
|
|
6
|
+
import { DropViewNode } from '../operation-node/drop-view-node.js';
|
|
7
|
+
export declare class DropViewBuilder implements OperationNodeSource, Compilable {
|
|
8
|
+
#private;
|
|
9
|
+
constructor(props: DropViewBuilderProps);
|
|
10
|
+
materialized(): DropViewBuilder;
|
|
11
|
+
ifExists(): DropViewBuilder;
|
|
12
|
+
toOperationNode(): DropViewNode;
|
|
13
|
+
compile(): CompiledQuery;
|
|
14
|
+
execute(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
export interface DropViewBuilderProps {
|
|
17
|
+
readonly queryId: QueryId;
|
|
18
|
+
readonly executor: QueryExecutor;
|
|
19
|
+
readonly dropViewNode: DropViewNode;
|
|
20
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { preventAwait } from '../util/prevent-await.js';
|
|
2
|
+
import { freeze } from '../util/object-utils.js';
|
|
3
|
+
import { DropViewNode } from '../operation-node/drop-view-node.js';
|
|
4
|
+
export class DropViewBuilder {
|
|
5
|
+
#props;
|
|
6
|
+
constructor(props) {
|
|
7
|
+
this.#props = freeze(props);
|
|
8
|
+
}
|
|
9
|
+
materialized() {
|
|
10
|
+
return new DropViewBuilder({
|
|
11
|
+
...this.#props,
|
|
12
|
+
dropViewNode: DropViewNode.cloneWith(this.#props.dropViewNode, {
|
|
13
|
+
materialized: true,
|
|
14
|
+
}),
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
ifExists() {
|
|
18
|
+
return new DropViewBuilder({
|
|
19
|
+
...this.#props,
|
|
20
|
+
dropViewNode: DropViewNode.cloneWith(this.#props.dropViewNode, {
|
|
21
|
+
ifExists: true,
|
|
22
|
+
}),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
toOperationNode() {
|
|
26
|
+
return this.#props.executor.transformQuery(this.#props.dropViewNode, this.#props.queryId);
|
|
27
|
+
}
|
|
28
|
+
compile() {
|
|
29
|
+
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
|
|
30
|
+
}
|
|
31
|
+
async execute() {
|
|
32
|
+
await this.#props.executor.executeQuery(this.compile(), this.#props.queryId);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
preventAwait(DropViewBuilder, "don't await DropViewBuilder instances directly. To execute the query you need to call `execute`");
|
|
@@ -6,6 +6,8 @@ import { CreateTableBuilder } from './create-table-builder.js';
|
|
|
6
6
|
import { DropIndexBuilder } from './drop-index-builder.js';
|
|
7
7
|
import { DropSchemaBuilder } from './drop-schema-builder.js';
|
|
8
8
|
import { DropTableBuilder } from './drop-table-builder.js';
|
|
9
|
+
import { CreateViewBuilder } from './create-view-builder.js';
|
|
10
|
+
import { DropViewBuilder } from './drop-view-builder.js';
|
|
9
11
|
/**
|
|
10
12
|
* Provides methods for building database schema.
|
|
11
13
|
*/
|
|
@@ -22,7 +24,7 @@ export declare class SchemaModule {
|
|
|
22
24
|
* ```ts
|
|
23
25
|
* await db.schema
|
|
24
26
|
* .createTable('person')
|
|
25
|
-
* .addColumn('id', 'integer', col => col.primaryKey().
|
|
27
|
+
* .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
|
|
26
28
|
* .addColumn('first_name', 'varchar', col => col.notNull())
|
|
27
29
|
* .addColumn('last_name', 'varchar', col => col.notNull())
|
|
28
30
|
* .addColumn('gender', 'varchar')
|
|
@@ -38,7 +40,7 @@ export declare class SchemaModule {
|
|
|
38
40
|
* ```ts
|
|
39
41
|
* await db.schema
|
|
40
42
|
* .createTable('pet')
|
|
41
|
-
* .addColumn('id', 'integer', col => col.primaryKey().
|
|
43
|
+
* .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
|
|
42
44
|
* .addColumn('owner_id', 'integer', col => col
|
|
43
45
|
* .references('person.id')
|
|
44
46
|
* .onDelete('cascade')
|
|
@@ -55,7 +57,7 @@ export declare class SchemaModule {
|
|
|
55
57
|
* ```ts
|
|
56
58
|
* await db.schema
|
|
57
59
|
* .createTable('pet')
|
|
58
|
-
* .addColumn('id', 'integer', col => col.primaryKey().
|
|
60
|
+
* .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
|
|
59
61
|
* .addColumn('owner_id', 'integer')
|
|
60
62
|
* .addForeignKeyConstraint(
|
|
61
63
|
* 'pet_owner_id_foreign', ['owner_id'], 'person', ['id'],
|
|
@@ -135,6 +137,31 @@ export declare class SchemaModule {
|
|
|
135
137
|
* ```
|
|
136
138
|
*/
|
|
137
139
|
alterTable(table: string): AlterTableBuilder;
|
|
140
|
+
/**
|
|
141
|
+
* Create a new view.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```ts
|
|
145
|
+
* await db.schema
|
|
146
|
+
* .createView('dogs')
|
|
147
|
+
* .orReplace()
|
|
148
|
+
* .as(db.selectFrom('pet').selectAll().where('species', '=', 'dog'))
|
|
149
|
+
* .execute()
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
createView(viewName: string): CreateViewBuilder;
|
|
153
|
+
/**
|
|
154
|
+
* Drop a view.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```ts
|
|
158
|
+
* await db.schema
|
|
159
|
+
* .dropView('dogs')
|
|
160
|
+
* .ifExists()
|
|
161
|
+
* .execute()
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
dropView(viewName: string): DropViewBuilder;
|
|
138
165
|
/**
|
|
139
166
|
* See {@link QueryCreator.withSchema}
|
|
140
167
|
*/
|
|
@@ -15,6 +15,10 @@ import { DropSchemaBuilder } from './drop-schema-builder.js';
|
|
|
15
15
|
import { DropTableBuilder } from './drop-table-builder.js';
|
|
16
16
|
import { createQueryId } from '../util/query-id.js';
|
|
17
17
|
import { WithSchemaPlugin } from '../plugin/with-schema/with-schema-plugin.js';
|
|
18
|
+
import { CreateViewBuilder } from './create-view-builder.js';
|
|
19
|
+
import { CreateViewNode } from '../operation-node/create-view-node.js';
|
|
20
|
+
import { DropViewBuilder } from './drop-view-builder.js';
|
|
21
|
+
import { DropViewNode } from '../operation-node/drop-view-node.js';
|
|
18
22
|
/**
|
|
19
23
|
* Provides methods for building database schema.
|
|
20
24
|
*/
|
|
@@ -33,7 +37,7 @@ export class SchemaModule {
|
|
|
33
37
|
* ```ts
|
|
34
38
|
* await db.schema
|
|
35
39
|
* .createTable('person')
|
|
36
|
-
* .addColumn('id', 'integer', col => col.primaryKey().
|
|
40
|
+
* .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
|
|
37
41
|
* .addColumn('first_name', 'varchar', col => col.notNull())
|
|
38
42
|
* .addColumn('last_name', 'varchar', col => col.notNull())
|
|
39
43
|
* .addColumn('gender', 'varchar')
|
|
@@ -49,7 +53,7 @@ export class SchemaModule {
|
|
|
49
53
|
* ```ts
|
|
50
54
|
* await db.schema
|
|
51
55
|
* .createTable('pet')
|
|
52
|
-
* .addColumn('id', 'integer', col => col.primaryKey().
|
|
56
|
+
* .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
|
|
53
57
|
* .addColumn('owner_id', 'integer', col => col
|
|
54
58
|
* .references('person.id')
|
|
55
59
|
* .onDelete('cascade')
|
|
@@ -66,7 +70,7 @@ export class SchemaModule {
|
|
|
66
70
|
* ```ts
|
|
67
71
|
* await db.schema
|
|
68
72
|
* .createTable('pet')
|
|
69
|
-
* .addColumn('id', 'integer', col => col.primaryKey().
|
|
73
|
+
* .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
|
|
70
74
|
* .addColumn('owner_id', 'integer')
|
|
71
75
|
* .addForeignKeyConstraint(
|
|
72
76
|
* 'pet_owner_id_foreign', ['owner_id'], 'person', ['id'],
|
|
@@ -188,6 +192,43 @@ export class SchemaModule {
|
|
|
188
192
|
alterTableNode: AlterTableNode.create(table),
|
|
189
193
|
});
|
|
190
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Create a new view.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```ts
|
|
200
|
+
* await db.schema
|
|
201
|
+
* .createView('dogs')
|
|
202
|
+
* .orReplace()
|
|
203
|
+
* .as(db.selectFrom('pet').selectAll().where('species', '=', 'dog'))
|
|
204
|
+
* .execute()
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
createView(viewName) {
|
|
208
|
+
return new CreateViewBuilder({
|
|
209
|
+
queryId: createQueryId(),
|
|
210
|
+
executor: this.#executor,
|
|
211
|
+
createViewNode: CreateViewNode.create(viewName),
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Drop a view.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* await db.schema
|
|
220
|
+
* .dropView('dogs')
|
|
221
|
+
* .ifExists()
|
|
222
|
+
* .execute()
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
dropView(viewName) {
|
|
226
|
+
return new DropViewBuilder({
|
|
227
|
+
queryId: createQueryId(),
|
|
228
|
+
executor: this.#executor,
|
|
229
|
+
dropViewNode: DropViewNode.create(viewName),
|
|
230
|
+
});
|
|
231
|
+
}
|
|
191
232
|
/**
|
|
192
233
|
* See {@link QueryCreator.withSchema}
|
|
193
234
|
*/
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kysely",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Type safe SQL query builder",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git://github.com/koskimas/kysely.git"
|
|
8
8
|
},
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=16.0.0"
|
|
11
|
+
},
|
|
9
12
|
"main": "dist/cjs/index.js",
|
|
10
13
|
"module": "dist/esm/index.js",
|
|
11
14
|
"exports": {
|