sumak 0.0.6 → 0.0.7
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 +657 -533
- package/dist/ast/ddl-nodes.d.mts +153 -0
- package/dist/ast/ddl-nodes.mjs +1 -0
- package/dist/builder/ddl/alter-table.d.mts +25 -0
- package/dist/builder/ddl/alter-table.mjs +146 -0
- package/dist/builder/ddl/create-index.d.mts +19 -0
- package/dist/builder/ddl/create-index.mjs +67 -0
- package/dist/builder/ddl/create-table.d.mts +40 -0
- package/dist/builder/ddl/create-table.mjs +186 -0
- package/dist/builder/ddl/create-view.d.mts +15 -0
- package/dist/builder/ddl/create-view.mjs +57 -0
- package/dist/builder/ddl/drop.d.mts +38 -0
- package/dist/builder/ddl/drop.mjs +133 -0
- package/dist/builder/delete.d.mts +1 -0
- package/dist/builder/delete.mjs +17 -0
- package/dist/builder/eb.d.mts +14 -0
- package/dist/builder/eb.mjs +25 -0
- package/dist/builder/select.d.mts +1 -0
- package/dist/builder/select.mjs +17 -0
- package/dist/builder/typed-delete.d.mts +8 -0
- package/dist/builder/typed-delete.mjs +14 -0
- package/dist/builder/typed-insert.d.mts +12 -0
- package/dist/builder/typed-insert.mjs +28 -0
- package/dist/builder/typed-select.d.mts +22 -0
- package/dist/builder/typed-select.mjs +38 -0
- package/dist/builder/typed-update.d.mts +8 -0
- package/dist/builder/typed-update.mjs +14 -0
- package/dist/builder/update.d.mts +1 -0
- package/dist/builder/update.mjs +17 -0
- package/dist/index.d.mts +10 -2
- package/dist/index.mjs +10 -2
- package/dist/printer/ddl.d.mts +21 -0
- package/dist/printer/ddl.mjs +223 -0
- package/dist/schema/column.d.mts +10 -1
- package/dist/schema/column.mjs +33 -2
- package/dist/schema/index.d.mts +1 -1
- package/dist/schema/index.mjs +1 -1
- package/dist/sumak.d.mts +47 -1
- package/dist/sumak.mjs +94 -2
- package/package.json +1 -1
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import type { ExpressionNode, SelectNode, TableRefNode } from "./nodes.mjs";
|
|
2
|
+
export type ForeignKeyAction = "NO ACTION" | "RESTRICT" | "CASCADE" | "SET NULL" | "SET DEFAULT";
|
|
3
|
+
export interface ColumnDefinitionNode {
|
|
4
|
+
type: "column_definition";
|
|
5
|
+
name: string;
|
|
6
|
+
dataType: string;
|
|
7
|
+
notNull?: boolean;
|
|
8
|
+
defaultTo?: ExpressionNode;
|
|
9
|
+
primaryKey?: boolean;
|
|
10
|
+
unique?: boolean;
|
|
11
|
+
check?: ExpressionNode;
|
|
12
|
+
autoIncrement?: boolean;
|
|
13
|
+
references?: {
|
|
14
|
+
table: string;
|
|
15
|
+
column: string;
|
|
16
|
+
onDelete?: ForeignKeyAction;
|
|
17
|
+
onUpdate?: ForeignKeyAction;
|
|
18
|
+
};
|
|
19
|
+
generatedAs?: {
|
|
20
|
+
expression: ExpressionNode;
|
|
21
|
+
stored?: boolean;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export interface PrimaryKeyConstraintNode {
|
|
25
|
+
type: "pk_constraint";
|
|
26
|
+
name?: string;
|
|
27
|
+
columns: string[];
|
|
28
|
+
}
|
|
29
|
+
export interface UniqueConstraintNode {
|
|
30
|
+
type: "unique_constraint";
|
|
31
|
+
name?: string;
|
|
32
|
+
columns: string[];
|
|
33
|
+
}
|
|
34
|
+
export interface CheckConstraintNode {
|
|
35
|
+
type: "check_constraint";
|
|
36
|
+
name?: string;
|
|
37
|
+
expression: ExpressionNode;
|
|
38
|
+
}
|
|
39
|
+
export interface ForeignKeyConstraintNode {
|
|
40
|
+
type: "fk_constraint";
|
|
41
|
+
name?: string;
|
|
42
|
+
columns: string[];
|
|
43
|
+
references: {
|
|
44
|
+
table: string;
|
|
45
|
+
columns: string[];
|
|
46
|
+
onDelete?: ForeignKeyAction;
|
|
47
|
+
onUpdate?: ForeignKeyAction;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export type TableConstraintNode = PrimaryKeyConstraintNode | UniqueConstraintNode | CheckConstraintNode | ForeignKeyConstraintNode;
|
|
51
|
+
export interface CreateTableNode {
|
|
52
|
+
type: "create_table";
|
|
53
|
+
table: TableRefNode;
|
|
54
|
+
columns: ColumnDefinitionNode[];
|
|
55
|
+
constraints: TableConstraintNode[];
|
|
56
|
+
ifNotExists?: boolean;
|
|
57
|
+
temporary?: boolean;
|
|
58
|
+
asSelect?: SelectNode;
|
|
59
|
+
}
|
|
60
|
+
export type AlterTableAction = {
|
|
61
|
+
kind: "add_column";
|
|
62
|
+
column: ColumnDefinitionNode;
|
|
63
|
+
} | {
|
|
64
|
+
kind: "drop_column";
|
|
65
|
+
column: string;
|
|
66
|
+
} | {
|
|
67
|
+
kind: "rename_column";
|
|
68
|
+
from: string;
|
|
69
|
+
to: string;
|
|
70
|
+
} | {
|
|
71
|
+
kind: "rename_table";
|
|
72
|
+
to: string;
|
|
73
|
+
} | {
|
|
74
|
+
kind: "alter_column";
|
|
75
|
+
column: string;
|
|
76
|
+
set: AlterColumnSet;
|
|
77
|
+
} | {
|
|
78
|
+
kind: "add_constraint";
|
|
79
|
+
constraint: TableConstraintNode;
|
|
80
|
+
} | {
|
|
81
|
+
kind: "drop_constraint";
|
|
82
|
+
name: string;
|
|
83
|
+
};
|
|
84
|
+
export type AlterColumnSet = {
|
|
85
|
+
type: "set_not_null";
|
|
86
|
+
} | {
|
|
87
|
+
type: "drop_not_null";
|
|
88
|
+
} | {
|
|
89
|
+
type: "set_default";
|
|
90
|
+
value: ExpressionNode;
|
|
91
|
+
} | {
|
|
92
|
+
type: "drop_default";
|
|
93
|
+
} | {
|
|
94
|
+
type: "set_data_type";
|
|
95
|
+
dataType: string;
|
|
96
|
+
};
|
|
97
|
+
export interface AlterTableNode {
|
|
98
|
+
type: "alter_table";
|
|
99
|
+
table: TableRefNode;
|
|
100
|
+
actions: AlterTableAction[];
|
|
101
|
+
}
|
|
102
|
+
export interface DropTableNode {
|
|
103
|
+
type: "drop_table";
|
|
104
|
+
table: TableRefNode;
|
|
105
|
+
ifExists?: boolean;
|
|
106
|
+
cascade?: boolean;
|
|
107
|
+
}
|
|
108
|
+
export interface CreateIndexNode {
|
|
109
|
+
type: "create_index";
|
|
110
|
+
name: string;
|
|
111
|
+
table: string;
|
|
112
|
+
columns: {
|
|
113
|
+
column: string;
|
|
114
|
+
direction?: "ASC" | "DESC";
|
|
115
|
+
}[];
|
|
116
|
+
expressions?: ExpressionNode[];
|
|
117
|
+
unique?: boolean;
|
|
118
|
+
ifNotExists?: boolean;
|
|
119
|
+
using?: string;
|
|
120
|
+
where?: ExpressionNode;
|
|
121
|
+
}
|
|
122
|
+
export interface DropIndexNode {
|
|
123
|
+
type: "drop_index";
|
|
124
|
+
name: string;
|
|
125
|
+
table?: string;
|
|
126
|
+
ifExists?: boolean;
|
|
127
|
+
cascade?: boolean;
|
|
128
|
+
}
|
|
129
|
+
export interface CreateViewNode {
|
|
130
|
+
type: "create_view";
|
|
131
|
+
name: string;
|
|
132
|
+
schema?: string;
|
|
133
|
+
columns?: string[];
|
|
134
|
+
asSelect: SelectNode;
|
|
135
|
+
orReplace?: boolean;
|
|
136
|
+
temporary?: boolean;
|
|
137
|
+
materialized?: boolean;
|
|
138
|
+
ifNotExists?: boolean;
|
|
139
|
+
}
|
|
140
|
+
export interface DropViewNode {
|
|
141
|
+
type: "drop_view";
|
|
142
|
+
name: string;
|
|
143
|
+
ifExists?: boolean;
|
|
144
|
+
cascade?: boolean;
|
|
145
|
+
materialized?: boolean;
|
|
146
|
+
}
|
|
147
|
+
export interface TruncateTableNode {
|
|
148
|
+
type: "truncate_table";
|
|
149
|
+
table: TableRefNode;
|
|
150
|
+
cascade?: boolean;
|
|
151
|
+
restartIdentity?: boolean;
|
|
152
|
+
}
|
|
153
|
+
export type DDLNode = CreateTableNode | AlterTableNode | DropTableNode | CreateIndexNode | DropIndexNode | CreateViewNode | DropViewNode | TruncateTableNode;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { AlterColumnSet, AlterTableNode, ForeignKeyAction, ForeignKeyConstraintNode } from "../../ast/ddl-nodes.mjs";
|
|
2
|
+
import { ColumnDefBuilder } from "./create-table.mjs";
|
|
3
|
+
export declare class AlterTableBuilder {
|
|
4
|
+
private readonly node;
|
|
5
|
+
constructor(table: string, schema?: string);
|
|
6
|
+
private withAction;
|
|
7
|
+
addColumn(name: string, dataType: string, build?: (col: ColumnDefBuilder) => ColumnDefBuilder): AlterTableBuilder;
|
|
8
|
+
dropColumn(name: string): AlterTableBuilder;
|
|
9
|
+
renameColumn(from: string, to: string): AlterTableBuilder;
|
|
10
|
+
renameTo(newName: string): AlterTableBuilder;
|
|
11
|
+
alterColumn(column: string, set: AlterColumnSet): AlterTableBuilder;
|
|
12
|
+
addPrimaryKeyConstraint(name: string | undefined, columns: string[]): AlterTableBuilder;
|
|
13
|
+
addUniqueConstraint(name: string | undefined, columns: string[]): AlterTableBuilder;
|
|
14
|
+
addForeignKeyConstraint(name: string | undefined, columns: string[], refTable: string, refColumns: string[], build?: (fk: ForeignKeyConstraintBuilder) => ForeignKeyConstraintBuilder): AlterTableBuilder;
|
|
15
|
+
dropConstraint(name: string): AlterTableBuilder;
|
|
16
|
+
build(): AlterTableNode;
|
|
17
|
+
}
|
|
18
|
+
export declare class ForeignKeyConstraintBuilder {
|
|
19
|
+
private readonly node;
|
|
20
|
+
constructor(node: ForeignKeyConstraintNode);
|
|
21
|
+
onDelete(action: ForeignKeyAction): ForeignKeyConstraintBuilder;
|
|
22
|
+
onUpdate(action: ForeignKeyAction): ForeignKeyConstraintBuilder;
|
|
23
|
+
build(): ForeignKeyConstraintNode;
|
|
24
|
+
}
|
|
25
|
+
export declare function alterTable(table: string, schema?: string): AlterTableBuilder;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { ColumnDefBuilder } from "./create-table.mjs";
|
|
2
|
+
export class AlterTableBuilder {
|
|
3
|
+
node;
|
|
4
|
+
constructor(table, schema) {
|
|
5
|
+
const ref = {
|
|
6
|
+
type: "table_ref",
|
|
7
|
+
name: table,
|
|
8
|
+
schema
|
|
9
|
+
};
|
|
10
|
+
this.node = {
|
|
11
|
+
type: "alter_table",
|
|
12
|
+
table: ref,
|
|
13
|
+
actions: []
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
withAction(action) {
|
|
17
|
+
const next = new AlterTableBuilder(this.node.table.name, this.node.table.schema);
|
|
18
|
+
return Object.assign(next, { node: {
|
|
19
|
+
...this.node,
|
|
20
|
+
actions: [...this.node.actions, action]
|
|
21
|
+
} });
|
|
22
|
+
}
|
|
23
|
+
addColumn(name, dataType, build) {
|
|
24
|
+
let colBuilder = new ColumnDefBuilder(name, dataType);
|
|
25
|
+
if (build) {
|
|
26
|
+
colBuilder = build(colBuilder);
|
|
27
|
+
}
|
|
28
|
+
const colDef = colBuilder._build();
|
|
29
|
+
return this.withAction({
|
|
30
|
+
kind: "add_column",
|
|
31
|
+
column: colDef
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
dropColumn(name) {
|
|
35
|
+
return this.withAction({
|
|
36
|
+
kind: "drop_column",
|
|
37
|
+
column: name
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
renameColumn(from, to) {
|
|
41
|
+
return this.withAction({
|
|
42
|
+
kind: "rename_column",
|
|
43
|
+
from,
|
|
44
|
+
to
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
renameTo(newName) {
|
|
48
|
+
return this.withAction({
|
|
49
|
+
kind: "rename_table",
|
|
50
|
+
to: newName
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
alterColumn(column, set) {
|
|
54
|
+
return this.withAction({
|
|
55
|
+
kind: "alter_column",
|
|
56
|
+
column,
|
|
57
|
+
set
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
addPrimaryKeyConstraint(name, columns) {
|
|
61
|
+
const constraint = {
|
|
62
|
+
type: "pk_constraint",
|
|
63
|
+
name,
|
|
64
|
+
columns
|
|
65
|
+
};
|
|
66
|
+
return this.withAction({
|
|
67
|
+
kind: "add_constraint",
|
|
68
|
+
constraint
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
addUniqueConstraint(name, columns) {
|
|
72
|
+
const constraint = {
|
|
73
|
+
type: "unique_constraint",
|
|
74
|
+
name,
|
|
75
|
+
columns
|
|
76
|
+
};
|
|
77
|
+
return this.withAction({
|
|
78
|
+
kind: "add_constraint",
|
|
79
|
+
constraint
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
addForeignKeyConstraint(name, columns, refTable, refColumns, build) {
|
|
83
|
+
let constraint = {
|
|
84
|
+
type: "fk_constraint",
|
|
85
|
+
name,
|
|
86
|
+
columns,
|
|
87
|
+
references: {
|
|
88
|
+
table: refTable,
|
|
89
|
+
columns: refColumns
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
if (build) {
|
|
93
|
+
const builder = build(new ForeignKeyConstraintBuilder(constraint));
|
|
94
|
+
constraint = builder.build();
|
|
95
|
+
}
|
|
96
|
+
return this.withAction({
|
|
97
|
+
kind: "add_constraint",
|
|
98
|
+
constraint
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
dropConstraint(name) {
|
|
102
|
+
return this.withAction({
|
|
103
|
+
kind: "drop_constraint",
|
|
104
|
+
name
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
build() {
|
|
108
|
+
return {
|
|
109
|
+
...this.node,
|
|
110
|
+
actions: [...this.node.actions]
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
export class ForeignKeyConstraintBuilder {
|
|
115
|
+
node;
|
|
116
|
+
constructor(node) {
|
|
117
|
+
this.node = node;
|
|
118
|
+
}
|
|
119
|
+
onDelete(action) {
|
|
120
|
+
return new ForeignKeyConstraintBuilder({
|
|
121
|
+
...this.node,
|
|
122
|
+
references: {
|
|
123
|
+
...this.node.references,
|
|
124
|
+
onDelete: action
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
onUpdate(action) {
|
|
129
|
+
return new ForeignKeyConstraintBuilder({
|
|
130
|
+
...this.node,
|
|
131
|
+
references: {
|
|
132
|
+
...this.node.references,
|
|
133
|
+
onUpdate: action
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
build() {
|
|
138
|
+
return {
|
|
139
|
+
...this.node,
|
|
140
|
+
references: { ...this.node.references }
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
export function alterTable(table, schema) {
|
|
145
|
+
return new AlterTableBuilder(table, schema);
|
|
146
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { CreateIndexNode } from "../../ast/ddl-nodes.mjs";
|
|
2
|
+
import type { ExpressionNode } from "../../ast/nodes.mjs";
|
|
3
|
+
export declare class CreateIndexBuilder {
|
|
4
|
+
private readonly node;
|
|
5
|
+
constructor(name: string);
|
|
6
|
+
constructor(node: CreateIndexNode);
|
|
7
|
+
on(table: string): CreateIndexBuilder;
|
|
8
|
+
column(col: string, direction?: "ASC" | "DESC"): CreateIndexBuilder;
|
|
9
|
+
columns(cols: (string | {
|
|
10
|
+
column: string;
|
|
11
|
+
direction?: "ASC" | "DESC";
|
|
12
|
+
})[]): CreateIndexBuilder;
|
|
13
|
+
unique(): CreateIndexBuilder;
|
|
14
|
+
ifNotExists(): CreateIndexBuilder;
|
|
15
|
+
using(method: string): CreateIndexBuilder;
|
|
16
|
+
where(expr: ExpressionNode): CreateIndexBuilder;
|
|
17
|
+
build(): CreateIndexNode;
|
|
18
|
+
}
|
|
19
|
+
export declare function createIndex(name: string): CreateIndexBuilder;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export class CreateIndexBuilder {
|
|
2
|
+
node;
|
|
3
|
+
constructor(nameOrNode) {
|
|
4
|
+
if (typeof nameOrNode === "string") {
|
|
5
|
+
this.node = {
|
|
6
|
+
type: "create_index",
|
|
7
|
+
name: nameOrNode,
|
|
8
|
+
table: "",
|
|
9
|
+
columns: []
|
|
10
|
+
};
|
|
11
|
+
} else {
|
|
12
|
+
this.node = nameOrNode;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
on(table) {
|
|
16
|
+
return new CreateIndexBuilder({
|
|
17
|
+
...this.node,
|
|
18
|
+
table
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
column(col, direction) {
|
|
22
|
+
return new CreateIndexBuilder({
|
|
23
|
+
...this.node,
|
|
24
|
+
columns: [...this.node.columns, {
|
|
25
|
+
column: col,
|
|
26
|
+
direction
|
|
27
|
+
}]
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
columns(cols) {
|
|
31
|
+
const normalized = cols.map((c) => typeof c === "string" ? { column: c } : c);
|
|
32
|
+
return new CreateIndexBuilder({
|
|
33
|
+
...this.node,
|
|
34
|
+
columns: [...this.node.columns, ...normalized]
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
unique() {
|
|
38
|
+
return new CreateIndexBuilder({
|
|
39
|
+
...this.node,
|
|
40
|
+
unique: true
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
ifNotExists() {
|
|
44
|
+
return new CreateIndexBuilder({
|
|
45
|
+
...this.node,
|
|
46
|
+
ifNotExists: true
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
using(method) {
|
|
50
|
+
return new CreateIndexBuilder({
|
|
51
|
+
...this.node,
|
|
52
|
+
using: method
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
where(expr) {
|
|
56
|
+
return new CreateIndexBuilder({
|
|
57
|
+
...this.node,
|
|
58
|
+
where: expr
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
build() {
|
|
62
|
+
return { ...this.node };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export function createIndex(name) {
|
|
66
|
+
return new CreateIndexBuilder(name);
|
|
67
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { CreateTableNode, ForeignKeyAction } from "../../ast/ddl-nodes.mjs";
|
|
2
|
+
import type { ExpressionNode, SelectNode } from "../../ast/nodes.mjs";
|
|
3
|
+
export declare class ForeignKeyBuilder {
|
|
4
|
+
private _onDelete?;
|
|
5
|
+
private _onUpdate?;
|
|
6
|
+
constructor(onDelete?: ForeignKeyAction, onUpdate?: ForeignKeyAction);
|
|
7
|
+
onDelete(action: ForeignKeyAction): ForeignKeyBuilder;
|
|
8
|
+
onUpdate(action: ForeignKeyAction): ForeignKeyBuilder;
|
|
9
|
+
}
|
|
10
|
+
export declare class ColumnDefBuilder {
|
|
11
|
+
private node;
|
|
12
|
+
constructor(name: string, dataType: string);
|
|
13
|
+
private clone;
|
|
14
|
+
notNull(): ColumnDefBuilder;
|
|
15
|
+
defaultTo(value: ExpressionNode): ColumnDefBuilder;
|
|
16
|
+
primaryKey(): ColumnDefBuilder;
|
|
17
|
+
unique(): ColumnDefBuilder;
|
|
18
|
+
check(expr: ExpressionNode): ColumnDefBuilder;
|
|
19
|
+
autoIncrement(): ColumnDefBuilder;
|
|
20
|
+
references(table: string, column: string): ColumnDefBuilder;
|
|
21
|
+
onDelete(action: ForeignKeyAction): ColumnDefBuilder;
|
|
22
|
+
onUpdate(action: ForeignKeyAction): ColumnDefBuilder;
|
|
23
|
+
generatedAlwaysAs(expr: ExpressionNode): ColumnDefBuilder;
|
|
24
|
+
stored(): ColumnDefBuilder;
|
|
25
|
+
}
|
|
26
|
+
export declare class CreateTableBuilder {
|
|
27
|
+
private node;
|
|
28
|
+
constructor(table: string, schema?: string);
|
|
29
|
+
private cloneWith;
|
|
30
|
+
ifNotExists(): CreateTableBuilder;
|
|
31
|
+
temporary(): CreateTableBuilder;
|
|
32
|
+
addColumn(name: string, dataType: string, build?: (col: ColumnDefBuilder) => ColumnDefBuilder): CreateTableBuilder;
|
|
33
|
+
addPrimaryKeyConstraint(name: string | undefined, columns: string[]): CreateTableBuilder;
|
|
34
|
+
addUniqueConstraint(name: string | undefined, columns: string[]): CreateTableBuilder;
|
|
35
|
+
addCheckConstraint(name: string | undefined, expression: ExpressionNode): CreateTableBuilder;
|
|
36
|
+
addForeignKeyConstraint(name: string | undefined, columns: string[], refTable: string, refColumns: string[], build?: (fk: ForeignKeyBuilder) => ForeignKeyBuilder): CreateTableBuilder;
|
|
37
|
+
asSelect(query: SelectNode): CreateTableBuilder;
|
|
38
|
+
build(): CreateTableNode;
|
|
39
|
+
}
|
|
40
|
+
export declare function createTable(table: string, schema?: string): CreateTableBuilder;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
|
|
2
|
+
export class ForeignKeyBuilder {
|
|
3
|
+
_onDelete;
|
|
4
|
+
_onUpdate;
|
|
5
|
+
constructor(onDelete, onUpdate) {
|
|
6
|
+
this._onDelete = onDelete;
|
|
7
|
+
this._onUpdate = onUpdate;
|
|
8
|
+
}
|
|
9
|
+
onDelete(action) {
|
|
10
|
+
return new ForeignKeyBuilder(action, this._onUpdate);
|
|
11
|
+
}
|
|
12
|
+
onUpdate(action) {
|
|
13
|
+
return new ForeignKeyBuilder(this._onDelete, action);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
_getOnDelete() {
|
|
17
|
+
return this._onDelete;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
_getOnUpdate() {
|
|
21
|
+
return this._onUpdate;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class ColumnDefBuilder {
|
|
26
|
+
node;
|
|
27
|
+
constructor(name, dataType) {
|
|
28
|
+
this.node = {
|
|
29
|
+
type: "column_definition",
|
|
30
|
+
name,
|
|
31
|
+
dataType
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
clone(patch) {
|
|
35
|
+
const builder = new ColumnDefBuilder(this.node.name, this.node.dataType);
|
|
36
|
+
builder.node = {
|
|
37
|
+
...this.node,
|
|
38
|
+
...patch
|
|
39
|
+
};
|
|
40
|
+
return builder;
|
|
41
|
+
}
|
|
42
|
+
notNull() {
|
|
43
|
+
return this.clone({ notNull: true });
|
|
44
|
+
}
|
|
45
|
+
defaultTo(value) {
|
|
46
|
+
return this.clone({ defaultTo: value });
|
|
47
|
+
}
|
|
48
|
+
primaryKey() {
|
|
49
|
+
return this.clone({ primaryKey: true });
|
|
50
|
+
}
|
|
51
|
+
unique() {
|
|
52
|
+
return this.clone({ unique: true });
|
|
53
|
+
}
|
|
54
|
+
check(expr) {
|
|
55
|
+
return this.clone({ check: expr });
|
|
56
|
+
}
|
|
57
|
+
autoIncrement() {
|
|
58
|
+
return this.clone({ autoIncrement: true });
|
|
59
|
+
}
|
|
60
|
+
references(table, column) {
|
|
61
|
+
return this.clone({ references: {
|
|
62
|
+
table,
|
|
63
|
+
column,
|
|
64
|
+
onDelete: this.node.references?.onDelete,
|
|
65
|
+
onUpdate: this.node.references?.onUpdate
|
|
66
|
+
} });
|
|
67
|
+
}
|
|
68
|
+
onDelete(action) {
|
|
69
|
+
return this.clone({ references: this.node.references ? {
|
|
70
|
+
...this.node.references,
|
|
71
|
+
onDelete: action
|
|
72
|
+
} : undefined });
|
|
73
|
+
}
|
|
74
|
+
onUpdate(action) {
|
|
75
|
+
return this.clone({ references: this.node.references ? {
|
|
76
|
+
...this.node.references,
|
|
77
|
+
onUpdate: action
|
|
78
|
+
} : undefined });
|
|
79
|
+
}
|
|
80
|
+
generatedAlwaysAs(expr) {
|
|
81
|
+
return this.clone({ generatedAs: {
|
|
82
|
+
expression: expr,
|
|
83
|
+
stored: this.node.generatedAs?.stored
|
|
84
|
+
} });
|
|
85
|
+
}
|
|
86
|
+
stored() {
|
|
87
|
+
return this.clone({ generatedAs: this.node.generatedAs ? {
|
|
88
|
+
...this.node.generatedAs,
|
|
89
|
+
stored: true
|
|
90
|
+
} : undefined });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
_build() {
|
|
94
|
+
return { ...this.node };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export class CreateTableBuilder {
|
|
99
|
+
node;
|
|
100
|
+
constructor(table, schema) {
|
|
101
|
+
const tableRef = {
|
|
102
|
+
type: "table_ref",
|
|
103
|
+
name: table,
|
|
104
|
+
schema
|
|
105
|
+
};
|
|
106
|
+
this.node = {
|
|
107
|
+
type: "create_table",
|
|
108
|
+
table: tableRef,
|
|
109
|
+
columns: [],
|
|
110
|
+
constraints: []
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
cloneWith(patch) {
|
|
114
|
+
const builder = Object.create(CreateTableBuilder.prototype);
|
|
115
|
+
builder.node = {
|
|
116
|
+
...this.node,
|
|
117
|
+
...patch
|
|
118
|
+
};
|
|
119
|
+
return builder;
|
|
120
|
+
}
|
|
121
|
+
ifNotExists() {
|
|
122
|
+
return this.cloneWith({ ifNotExists: true });
|
|
123
|
+
}
|
|
124
|
+
temporary() {
|
|
125
|
+
return this.cloneWith({ temporary: true });
|
|
126
|
+
}
|
|
127
|
+
addColumn(name, dataType, build) {
|
|
128
|
+
let colBuilder = new ColumnDefBuilder(name, dataType);
|
|
129
|
+
if (build) {
|
|
130
|
+
colBuilder = build(colBuilder);
|
|
131
|
+
}
|
|
132
|
+
return this.cloneWith({ columns: [...this.node.columns, colBuilder._build()] });
|
|
133
|
+
}
|
|
134
|
+
addPrimaryKeyConstraint(name, columns) {
|
|
135
|
+
const constraint = {
|
|
136
|
+
type: "pk_constraint",
|
|
137
|
+
name,
|
|
138
|
+
columns
|
|
139
|
+
};
|
|
140
|
+
return this.cloneWith({ constraints: [...this.node.constraints, constraint] });
|
|
141
|
+
}
|
|
142
|
+
addUniqueConstraint(name, columns) {
|
|
143
|
+
const constraint = {
|
|
144
|
+
type: "unique_constraint",
|
|
145
|
+
name,
|
|
146
|
+
columns
|
|
147
|
+
};
|
|
148
|
+
return this.cloneWith({ constraints: [...this.node.constraints, constraint] });
|
|
149
|
+
}
|
|
150
|
+
addCheckConstraint(name, expression) {
|
|
151
|
+
const constraint = {
|
|
152
|
+
type: "check_constraint",
|
|
153
|
+
name,
|
|
154
|
+
expression
|
|
155
|
+
};
|
|
156
|
+
return this.cloneWith({ constraints: [...this.node.constraints, constraint] });
|
|
157
|
+
}
|
|
158
|
+
addForeignKeyConstraint(name, columns, refTable, refColumns, build) {
|
|
159
|
+
let fkBuilder = new ForeignKeyBuilder();
|
|
160
|
+
if (build) {
|
|
161
|
+
fkBuilder = build(fkBuilder);
|
|
162
|
+
}
|
|
163
|
+
const constraint = {
|
|
164
|
+
type: "fk_constraint",
|
|
165
|
+
name,
|
|
166
|
+
columns,
|
|
167
|
+
references: {
|
|
168
|
+
table: refTable,
|
|
169
|
+
columns: refColumns,
|
|
170
|
+
onDelete: fkBuilder._getOnDelete(),
|
|
171
|
+
onUpdate: fkBuilder._getOnUpdate()
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
return this.cloneWith({ constraints: [...this.node.constraints, constraint] });
|
|
175
|
+
}
|
|
176
|
+
asSelect(query) {
|
|
177
|
+
return this.cloneWith({ asSelect: query });
|
|
178
|
+
}
|
|
179
|
+
build() {
|
|
180
|
+
return { ...this.node };
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function createTable(table, schema) {
|
|
185
|
+
return new CreateTableBuilder(table, schema);
|
|
186
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CreateViewNode } from "../../ast/ddl-nodes.mjs";
|
|
2
|
+
import type { SelectNode } from "../../ast/nodes.mjs";
|
|
3
|
+
export declare class CreateViewBuilder {
|
|
4
|
+
private readonly node;
|
|
5
|
+
constructor(name: string, schema?: string);
|
|
6
|
+
constructor(node: CreateViewNode);
|
|
7
|
+
orReplace(): CreateViewBuilder;
|
|
8
|
+
temporary(): CreateViewBuilder;
|
|
9
|
+
materialized(): CreateViewBuilder;
|
|
10
|
+
ifNotExists(): CreateViewBuilder;
|
|
11
|
+
columns(...cols: string[]): CreateViewBuilder;
|
|
12
|
+
asSelect(query: SelectNode): CreateViewBuilder;
|
|
13
|
+
build(): CreateViewNode;
|
|
14
|
+
}
|
|
15
|
+
export declare function createView(name: string, schema?: string): CreateViewBuilder;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export class CreateViewBuilder {
|
|
2
|
+
node;
|
|
3
|
+
constructor(nameOrNode, schema) {
|
|
4
|
+
if (typeof nameOrNode === "string") {
|
|
5
|
+
this.node = {
|
|
6
|
+
type: "create_view",
|
|
7
|
+
name: nameOrNode,
|
|
8
|
+
schema,
|
|
9
|
+
asSelect: undefined
|
|
10
|
+
};
|
|
11
|
+
} else {
|
|
12
|
+
this.node = nameOrNode;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
orReplace() {
|
|
16
|
+
return new CreateViewBuilder({
|
|
17
|
+
...this.node,
|
|
18
|
+
orReplace: true
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
temporary() {
|
|
22
|
+
return new CreateViewBuilder({
|
|
23
|
+
...this.node,
|
|
24
|
+
temporary: true
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
materialized() {
|
|
28
|
+
return new CreateViewBuilder({
|
|
29
|
+
...this.node,
|
|
30
|
+
materialized: true
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
ifNotExists() {
|
|
34
|
+
return new CreateViewBuilder({
|
|
35
|
+
...this.node,
|
|
36
|
+
ifNotExists: true
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
columns(...cols) {
|
|
40
|
+
return new CreateViewBuilder({
|
|
41
|
+
...this.node,
|
|
42
|
+
columns: [...this.node.columns ?? [], ...cols]
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
asSelect(query) {
|
|
46
|
+
return new CreateViewBuilder({
|
|
47
|
+
...this.node,
|
|
48
|
+
asSelect: query
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
build() {
|
|
52
|
+
return { ...this.node };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export function createView(name, schema) {
|
|
56
|
+
return new CreateViewBuilder(name, schema);
|
|
57
|
+
}
|