sumak 0.0.4 → 0.0.5
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 +255 -5
- package/dist/ast/expression.d.mts +26 -0
- package/dist/ast/expression.mjs +140 -0
- package/dist/ast/nodes.d.mts +298 -0
- package/dist/ast/nodes.mjs +59 -0
- package/dist/ast/transformer.d.mts +10 -0
- package/dist/ast/transformer.mjs +140 -0
- package/dist/ast/typed-expression.d.mts +37 -0
- package/dist/ast/typed-expression.mjs +77 -0
- package/dist/ast/visitor.d.mts +13 -0
- package/dist/ast/visitor.mjs +11 -0
- package/dist/builder/delete.d.mts +18 -0
- package/dist/builder/delete.mjs +94 -0
- package/dist/builder/eb.d.mts +210 -0
- package/dist/builder/eb.mjs +399 -0
- package/dist/builder/expression.d.mts +5 -0
- package/dist/builder/expression.mjs +10 -0
- package/dist/builder/insert.d.mts +40 -0
- package/dist/builder/insert.mjs +146 -0
- package/dist/builder/merge.d.mts +20 -0
- package/dist/builder/merge.mjs +100 -0
- package/dist/builder/raw.d.mts +2 -0
- package/dist/builder/raw.mjs +4 -0
- package/dist/builder/select.d.mts +38 -0
- package/dist/builder/select.mjs +242 -0
- package/dist/builder/typed-delete.d.mts +43 -0
- package/dist/builder/typed-delete.mjs +77 -0
- package/dist/builder/typed-insert.d.mts +74 -0
- package/dist/builder/typed-insert.mjs +136 -0
- package/dist/builder/typed-merge.d.mts +31 -0
- package/dist/builder/typed-merge.mjs +93 -0
- package/dist/builder/typed-select.d.mts +125 -0
- package/dist/builder/typed-select.mjs +217 -0
- package/dist/builder/typed-update.d.mts +55 -0
- package/dist/builder/typed-update.mjs +102 -0
- package/dist/builder/update.d.mts +18 -0
- package/dist/builder/update.mjs +102 -0
- package/dist/dialect/mssql.d.mts +2 -0
- package/dist/dialect/mssql.mjs +9 -0
- package/dist/dialect/mysql.d.mts +2 -0
- package/dist/dialect/mysql.mjs +9 -0
- package/dist/dialect/pg.d.mts +2 -0
- package/dist/dialect/pg.mjs +9 -0
- package/dist/dialect/sqlite.d.mts +2 -0
- package/dist/dialect/sqlite.mjs +9 -0
- package/dist/dialect/types.d.mts +6 -0
- package/dist/dialect/types.mjs +1 -0
- package/dist/errors.d.mts +12 -0
- package/dist/errors.mjs +24 -0
- package/dist/index.d.mts +49 -806
- package/dist/index.mjs +46 -3
- package/dist/mssql.d.mts +2 -2
- package/dist/mssql.mjs +2 -1
- package/dist/mysql.d.mts +2 -2
- package/dist/mysql.mjs +2 -1
- package/dist/pg.d.mts +2 -2
- package/dist/pg.mjs +2 -1
- package/dist/plugin/camel-case.d.mts +11 -0
- package/dist/plugin/camel-case.mjs +16 -0
- package/dist/plugin/hooks.d.mts +72 -0
- package/dist/plugin/hooks.mjs +49 -0
- package/dist/plugin/plugin-manager.d.mts +17 -0
- package/dist/plugin/plugin-manager.mjs +37 -0
- package/dist/plugin/soft-delete.d.mts +27 -0
- package/dist/plugin/soft-delete.mjs +52 -0
- package/dist/plugin/types.d.mts +19 -0
- package/dist/plugin/types.mjs +1 -0
- package/dist/plugin/with-schema.d.mts +21 -0
- package/dist/plugin/with-schema.mjs +53 -0
- package/dist/printer/base.d.mts +48 -0
- package/dist/printer/base.mjs +450 -0
- package/dist/printer/document.d.mts +45 -0
- package/dist/printer/document.mjs +153 -0
- package/dist/printer/formatter.d.mts +5 -0
- package/dist/printer/formatter.mjs +134 -0
- package/dist/printer/mssql.d.mts +10 -0
- package/dist/printer/mssql.mjs +161 -0
- package/dist/printer/mysql.d.mts +8 -0
- package/dist/printer/mysql.mjs +41 -0
- package/dist/printer/pg.d.mts +6 -0
- package/dist/printer/pg.mjs +9 -0
- package/dist/printer/sqlite.d.mts +8 -0
- package/dist/printer/sqlite.mjs +29 -0
- package/dist/printer/types.d.mts +11 -0
- package/dist/printer/types.mjs +1 -0
- package/dist/schema/column.d.mts +52 -0
- package/dist/schema/column.mjs +120 -0
- package/dist/schema/index.d.mts +6 -0
- package/dist/schema/index.mjs +4 -0
- package/dist/schema/table.d.mts +37 -0
- package/dist/schema/table.mjs +7 -0
- package/dist/schema/type-utils.d.mts +46 -0
- package/dist/schema/type-utils.mjs +1 -0
- package/dist/schema/types.d.mts +64 -0
- package/dist/schema/types.mjs +1 -0
- package/dist/schema.d.mts +2 -2
- package/dist/schema.mjs +1 -1
- package/dist/sqlite.d.mts +2 -2
- package/dist/sqlite.mjs +2 -1
- package/dist/sumak.d.mts +98 -0
- package/dist/sumak.mjs +132 -0
- package/dist/types.d.mts +14 -0
- package/dist/types.mjs +1 -0
- package/dist/utils/identifier.d.mts +3 -0
- package/dist/utils/identifier.mjs +14 -0
- package/dist/utils/param.d.mts +2 -0
- package/dist/utils/param.mjs +8 -0
- package/package.json +1 -1
- package/dist/_chunks/base.mjs +0 -1
- package/dist/_chunks/errors.mjs +0 -1
- package/dist/_chunks/index.d.mts +0 -136
- package/dist/_chunks/mssql.d.mts +0 -11
- package/dist/_chunks/mssql.mjs +0 -1
- package/dist/_chunks/mysql.d.mts +0 -9
- package/dist/_chunks/mysql.mjs +0 -1
- package/dist/_chunks/pg.d.mts +0 -7
- package/dist/_chunks/pg.mjs +0 -1
- package/dist/_chunks/schema.mjs +0 -1
- package/dist/_chunks/sqlite.d.mts +0 -9
- package/dist/_chunks/sqlite.mjs +0 -1
- package/dist/_chunks/types.d.mts +0 -338
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { unwrap } from "../ast/typed-expression.mjs";
|
|
2
|
+
import { createColumnProxies, resetParams } from "./eb.mjs";
|
|
3
|
+
import { SelectBuilder } from "./select.mjs";
|
|
4
|
+
|
|
5
|
+
export class TypedSelectBuilder {
|
|
6
|
+
|
|
7
|
+
_builder;
|
|
8
|
+
_table;
|
|
9
|
+
constructor(builder, table) {
|
|
10
|
+
this._builder = builder;
|
|
11
|
+
this._table = table ?? "";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
select(...cols) {
|
|
15
|
+
return new TypedSelectBuilder(this._builder.columns(...cols), this._table);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
selectAll() {
|
|
19
|
+
return new TypedSelectBuilder(this._builder.allColumns(), this._table);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
selectExpr(expr, alias) {
|
|
23
|
+
const node = unwrap(expr);
|
|
24
|
+
const aliased = aliasExpr(node, alias);
|
|
25
|
+
return new TypedSelectBuilder(this._builder.columns(aliased), this._table);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
distinct() {
|
|
29
|
+
return new TypedSelectBuilder(this._builder.distinct(), this._table);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
distinctOn(...cols) {
|
|
33
|
+
return new TypedSelectBuilder(this._builder.distinctOn(...cols), this._table);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
where(exprOrCallback) {
|
|
37
|
+
if (typeof exprOrCallback === "function") {
|
|
38
|
+
resetParams();
|
|
39
|
+
const cols = createColumnProxies(this._table);
|
|
40
|
+
const result = exprOrCallback(cols);
|
|
41
|
+
return new TypedSelectBuilder(this._builder.where(unwrap(result)), this._table);
|
|
42
|
+
}
|
|
43
|
+
return new TypedSelectBuilder(this._builder.where(unwrap(exprOrCallback)), this._table);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
innerJoin(table, onOrCallback) {
|
|
47
|
+
const on = resolveJoinOn(onOrCallback, this._table, table);
|
|
48
|
+
return new TypedSelectBuilder(this._builder.innerJoin(table, unwrap(on)), this._table);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
leftJoin(table, onOrCallback) {
|
|
52
|
+
const on = resolveJoinOn(onOrCallback, this._table, table);
|
|
53
|
+
return new TypedSelectBuilder(this._builder.leftJoin(table, unwrap(on)), this._table);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
rightJoin(table, onOrCallback) {
|
|
57
|
+
const on = resolveJoinOn(onOrCallback, this._table, table);
|
|
58
|
+
return new TypedSelectBuilder(this._builder.rightJoin(table, unwrap(on)), this._table);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
groupBy(...cols) {
|
|
62
|
+
return new TypedSelectBuilder(this._builder.groupBy(...cols), this._table);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
having(exprOrCallback) {
|
|
66
|
+
if (typeof exprOrCallback === "function") {
|
|
67
|
+
resetParams();
|
|
68
|
+
const cols = createColumnProxies(this._table);
|
|
69
|
+
const result = exprOrCallback(cols);
|
|
70
|
+
return new TypedSelectBuilder(this._builder.having(unwrap(result)), this._table);
|
|
71
|
+
}
|
|
72
|
+
return new TypedSelectBuilder(this._builder.having(unwrap(exprOrCallback)), this._table);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
orderBy(col, direction = "ASC", nulls) {
|
|
76
|
+
return new TypedSelectBuilder(this._builder.orderBy(col, direction, nulls), this._table);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
limit(n) {
|
|
80
|
+
return new TypedSelectBuilder(this._builder.limit({
|
|
81
|
+
type: "literal",
|
|
82
|
+
value: n
|
|
83
|
+
}), this._table);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
offset(n) {
|
|
87
|
+
return new TypedSelectBuilder(this._builder.offset({
|
|
88
|
+
type: "literal",
|
|
89
|
+
value: n
|
|
90
|
+
}), this._table);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
forSystemTime(clause) {
|
|
94
|
+
return new TypedSelectBuilder(this._builder.forSystemTime(clause), this._table);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
forUpdate() {
|
|
98
|
+
return new TypedSelectBuilder(this._builder.forUpdate(), this._table);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
forShare() {
|
|
102
|
+
return new TypedSelectBuilder(this._builder.forShare(), this._table);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
forNoKeyUpdate() {
|
|
106
|
+
return new TypedSelectBuilder(this._builder.forNoKeyUpdate(), this._table);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
forKeyShare() {
|
|
110
|
+
return new TypedSelectBuilder(this._builder.forKeyShare(), this._table);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
skipLocked() {
|
|
114
|
+
return new TypedSelectBuilder(this._builder.skipLocked(), this._table);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
noWait() {
|
|
118
|
+
return new TypedSelectBuilder(this._builder.noWait(), this._table);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
with(name, query, recursive = false) {
|
|
122
|
+
return new TypedSelectBuilder(this._builder.with(name, query, recursive), this._table);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
union(query) {
|
|
126
|
+
return new TypedSelectBuilder(this._builder.union(query.build()), this._table);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
unionAll(query) {
|
|
130
|
+
return new TypedSelectBuilder(this._builder.unionAll(query.build()), this._table);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
intersect(query) {
|
|
134
|
+
return new TypedSelectBuilder(this._builder.intersect(query.build()), this._table);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
intersectAll(query) {
|
|
138
|
+
return new TypedSelectBuilder(this._builder.intersectAll(query.build()), this._table);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
except(query) {
|
|
142
|
+
return new TypedSelectBuilder(this._builder.except(query.build()), this._table);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
exceptAll(query) {
|
|
146
|
+
return new TypedSelectBuilder(this._builder.exceptAll(query.build()), this._table);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
fullJoin(table, onOrCallback) {
|
|
150
|
+
const on = resolveJoinOn(onOrCallback, this._table, table);
|
|
151
|
+
return new TypedSelectBuilder(this._builder.join("FULL", table, unwrap(on)), this._table);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
crossJoin(table) {
|
|
155
|
+
return new TypedSelectBuilder(this._builder.join("CROSS", table), this._table);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
$if(condition, fn) {
|
|
159
|
+
if (condition) {
|
|
160
|
+
return fn(this);
|
|
161
|
+
}
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
build() {
|
|
166
|
+
return this._builder.build();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
compile(printer) {
|
|
170
|
+
return printer.print(this.build());
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
explain(options) {
|
|
174
|
+
const node = this.build();
|
|
175
|
+
const explainNode = {
|
|
176
|
+
type: "explain",
|
|
177
|
+
statement: node,
|
|
178
|
+
analyze: options?.analyze,
|
|
179
|
+
format: options?.format
|
|
180
|
+
};
|
|
181
|
+
return {
|
|
182
|
+
build: () => explainNode,
|
|
183
|
+
compile: (printer) => printer.print(explainNode)
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
import { Col } from "./eb.mjs";
|
|
188
|
+
function createJoinProxies(_leftTable, _rightTable) {
|
|
189
|
+
return new Proxy({}, { get(_target, tableName) {
|
|
190
|
+
return new Proxy({}, { get(_t2, colName) {
|
|
191
|
+
return new Col(colName, tableName);
|
|
192
|
+
} });
|
|
193
|
+
} });
|
|
194
|
+
}
|
|
195
|
+
function aliasExpr(node, alias) {
|
|
196
|
+
|
|
197
|
+
if (node.type === "column_ref" || node.type === "function_call" || node.type === "json_access" || node.type === "window_function") {
|
|
198
|
+
return {
|
|
199
|
+
...node,
|
|
200
|
+
alias
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const aliased = {
|
|
205
|
+
type: "aliased_expr",
|
|
206
|
+
expr: node,
|
|
207
|
+
alias
|
|
208
|
+
};
|
|
209
|
+
return aliased;
|
|
210
|
+
}
|
|
211
|
+
function resolveJoinOn(onOrCallback, leftTable, rightTable) {
|
|
212
|
+
if (typeof onOrCallback === "function") {
|
|
213
|
+
const proxies = createJoinProxies(leftTable, rightTable);
|
|
214
|
+
return onOrCallback(proxies);
|
|
215
|
+
}
|
|
216
|
+
return onOrCallback;
|
|
217
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { SelectNode, UpdateNode } from "../ast/nodes.mjs";
|
|
2
|
+
import type { Expression } from "../ast/typed-expression.mjs";
|
|
3
|
+
import type { Printer } from "../printer/types.mjs";
|
|
4
|
+
import type { SelectRow, Updateable } from "../schema/types.mjs";
|
|
5
|
+
import type { CompiledQuery } from "../types.mjs";
|
|
6
|
+
import type { WhereCallback } from "./eb.mjs";
|
|
7
|
+
import { UpdateBuilder } from "./update.mjs";
|
|
8
|
+
/**
|
|
9
|
+
* Type-safe UPDATE query builder.
|
|
10
|
+
*/
|
|
11
|
+
export declare class TypedUpdateBuilder<
|
|
12
|
+
DB,
|
|
13
|
+
TB extends keyof DB
|
|
14
|
+
> {
|
|
15
|
+
private _paramIdx;
|
|
16
|
+
constructor(table: TB & string, paramIdx?: number);
|
|
17
|
+
/**
|
|
18
|
+
* SET columns from an object. All keys optional (Updateable).
|
|
19
|
+
*/
|
|
20
|
+
set(values: Updateable<DB[TB]>): TypedUpdateBuilder<DB, TB>;
|
|
21
|
+
/**
|
|
22
|
+
* SET a single column with an expression.
|
|
23
|
+
*/
|
|
24
|
+
setExpr(column: keyof DB[TB] & string, value: Expression<any>): TypedUpdateBuilder<DB, TB>;
|
|
25
|
+
/**
|
|
26
|
+
* WHERE — callback or raw Expression.
|
|
27
|
+
*/
|
|
28
|
+
where(exprOrCallback: Expression<boolean> | WhereCallback<DB, TB>): TypedUpdateBuilder<DB, TB>;
|
|
29
|
+
private get _table();
|
|
30
|
+
/**
|
|
31
|
+
* RETURNING specific columns.
|
|
32
|
+
*/
|
|
33
|
+
returning<K extends keyof DB[TB] & string>(...cols: K[]): TypedUpdateReturningBuilder<DB, TB, Pick<SelectRow<DB, TB>, K>>;
|
|
34
|
+
/**
|
|
35
|
+
* RETURNING all columns.
|
|
36
|
+
*/
|
|
37
|
+
returningAll(): TypedUpdateReturningBuilder<DB, TB, SelectRow<DB, TB>>;
|
|
38
|
+
/** FROM clause (for UPDATE ... FROM ... WHERE joins). */
|
|
39
|
+
from<T extends keyof DB & string>(table: T): TypedUpdateBuilder<DB, TB>;
|
|
40
|
+
/** WITH (CTE) */
|
|
41
|
+
with(name: string, query: SelectNode, recursive?: boolean): TypedUpdateBuilder<DB, TB>;
|
|
42
|
+
/** Conditionally apply a transformation. */
|
|
43
|
+
$if(condition: boolean, fn: (qb: TypedUpdateBuilder<DB, TB>) => TypedUpdateBuilder<DB, TB>): TypedUpdateBuilder<DB, TB>;
|
|
44
|
+
build(): UpdateNode;
|
|
45
|
+
compile(printer: Printer): CompiledQuery;
|
|
46
|
+
}
|
|
47
|
+
export declare class TypedUpdateReturningBuilder<
|
|
48
|
+
DB,
|
|
49
|
+
_TB extends keyof DB,
|
|
50
|
+
_R
|
|
51
|
+
> {
|
|
52
|
+
constructor(builder: UpdateBuilder);
|
|
53
|
+
build(): UpdateNode;
|
|
54
|
+
compile(printer: Printer): CompiledQuery;
|
|
55
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { param, star } from "../ast/expression.mjs";
|
|
2
|
+
import { unwrap } from "../ast/typed-expression.mjs";
|
|
3
|
+
import { createColumnProxies, resetParams } from "./eb.mjs";
|
|
4
|
+
import { UpdateBuilder } from "./update.mjs";
|
|
5
|
+
|
|
6
|
+
export class TypedUpdateBuilder {
|
|
7
|
+
|
|
8
|
+
_builder;
|
|
9
|
+
_paramIdx;
|
|
10
|
+
constructor(table, paramIdx = 0) {
|
|
11
|
+
this._builder = new UpdateBuilder().table(table);
|
|
12
|
+
this._paramIdx = paramIdx;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
_with(builder, paramIdx) {
|
|
16
|
+
const t = new TypedUpdateBuilder("");
|
|
17
|
+
t._builder = builder;
|
|
18
|
+
t._paramIdx = paramIdx;
|
|
19
|
+
return t;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
set(values) {
|
|
23
|
+
let builder = this._builder;
|
|
24
|
+
let idx = this._paramIdx;
|
|
25
|
+
for (const [col, val] of Object.entries(values)) {
|
|
26
|
+
if (val !== undefined) {
|
|
27
|
+
builder = builder.set(col, param(idx, val));
|
|
28
|
+
idx++;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return this._with(builder, idx);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
setExpr(column, value) {
|
|
35
|
+
return this._with(this._builder.set(column, unwrap(value)), this._paramIdx);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
where(exprOrCallback) {
|
|
39
|
+
if (typeof exprOrCallback === "function") {
|
|
40
|
+
resetParams();
|
|
41
|
+
const cols = createColumnProxies(this._table);
|
|
42
|
+
const result = exprOrCallback(cols);
|
|
43
|
+
return this._with(this._builder.where(unwrap(result)), this._paramIdx);
|
|
44
|
+
}
|
|
45
|
+
return this._with(this._builder.where(unwrap(exprOrCallback)), this._paramIdx);
|
|
46
|
+
}
|
|
47
|
+
get _table() {
|
|
48
|
+
return this._builder.build().table.name;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
returning(...cols) {
|
|
52
|
+
const exprs = cols.map((c) => ({
|
|
53
|
+
type: "column_ref",
|
|
54
|
+
column: c
|
|
55
|
+
}));
|
|
56
|
+
return new TypedUpdateReturningBuilder(new UpdateBuilder({
|
|
57
|
+
...this._builder.build(),
|
|
58
|
+
returning: exprs
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
returningAll() {
|
|
63
|
+
return new TypedUpdateReturningBuilder(new UpdateBuilder({
|
|
64
|
+
...this._builder.build(),
|
|
65
|
+
returning: [star()]
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
from(table) {
|
|
70
|
+
return this._with(this._builder.from(table), this._paramIdx);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
with(name, query, recursive = false) {
|
|
74
|
+
return this._with(this._builder.with(name, query, recursive), this._paramIdx);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
$if(condition, fn) {
|
|
78
|
+
if (condition) {
|
|
79
|
+
return fn(this);
|
|
80
|
+
}
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
build() {
|
|
84
|
+
return this._builder.build();
|
|
85
|
+
}
|
|
86
|
+
compile(printer) {
|
|
87
|
+
return printer.print(this.build());
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
export class TypedUpdateReturningBuilder {
|
|
91
|
+
|
|
92
|
+
_builder;
|
|
93
|
+
constructor(builder) {
|
|
94
|
+
this._builder = builder;
|
|
95
|
+
}
|
|
96
|
+
build() {
|
|
97
|
+
return this._builder.build();
|
|
98
|
+
}
|
|
99
|
+
compile(printer) {
|
|
100
|
+
return printer.print(this.build());
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ExpressionNode, SelectNode, TableRefNode, UpdateNode } from "../ast/nodes.mjs";
|
|
2
|
+
import type { JoinType } from "../types.mjs";
|
|
3
|
+
export declare class UpdateBuilder {
|
|
4
|
+
private node;
|
|
5
|
+
constructor(node?: UpdateNode);
|
|
6
|
+
table(table: string | TableRefNode): UpdateBuilder;
|
|
7
|
+
set(column: string, value: ExpressionNode): UpdateBuilder;
|
|
8
|
+
where(expr: ExpressionNode): UpdateBuilder;
|
|
9
|
+
from(table: string | TableRefNode): UpdateBuilder;
|
|
10
|
+
/** Generic JOIN (MySQL pattern: UPDATE t JOIN other ON ... SET ...) */
|
|
11
|
+
join(type: JoinType, table: string | TableRefNode, on?: ExpressionNode): UpdateBuilder;
|
|
12
|
+
innerJoin(table: string | TableRefNode, on: ExpressionNode): UpdateBuilder;
|
|
13
|
+
leftJoin(table: string | TableRefNode, on: ExpressionNode): UpdateBuilder;
|
|
14
|
+
returning(...exprs: ExpressionNode[]): UpdateBuilder;
|
|
15
|
+
with(name: string, query: SelectNode, recursive?: boolean): UpdateBuilder;
|
|
16
|
+
build(): UpdateNode;
|
|
17
|
+
}
|
|
18
|
+
export declare function update(table: string | TableRefNode): UpdateBuilder;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { createUpdateNode } from "../ast/nodes.mjs";
|
|
2
|
+
export class UpdateBuilder {
|
|
3
|
+
node;
|
|
4
|
+
constructor(node) {
|
|
5
|
+
this.node = node ?? createUpdateNode({
|
|
6
|
+
type: "table_ref",
|
|
7
|
+
name: ""
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
table(table) {
|
|
11
|
+
const ref = typeof table === "string" ? {
|
|
12
|
+
type: "table_ref",
|
|
13
|
+
name: table
|
|
14
|
+
} : table;
|
|
15
|
+
return new UpdateBuilder({
|
|
16
|
+
...this.node,
|
|
17
|
+
table: ref
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
set(column, value) {
|
|
21
|
+
return new UpdateBuilder({
|
|
22
|
+
...this.node,
|
|
23
|
+
set: [...this.node.set, {
|
|
24
|
+
column,
|
|
25
|
+
value
|
|
26
|
+
}]
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
where(expr) {
|
|
30
|
+
if (this.node.where) {
|
|
31
|
+
return new UpdateBuilder({
|
|
32
|
+
...this.node,
|
|
33
|
+
where: {
|
|
34
|
+
type: "binary_op",
|
|
35
|
+
op: "AND",
|
|
36
|
+
left: this.node.where,
|
|
37
|
+
right: expr
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return new UpdateBuilder({
|
|
42
|
+
...this.node,
|
|
43
|
+
where: expr
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
from(table) {
|
|
47
|
+
const ref = typeof table === "string" ? {
|
|
48
|
+
type: "table_ref",
|
|
49
|
+
name: table
|
|
50
|
+
} : table;
|
|
51
|
+
return new UpdateBuilder({
|
|
52
|
+
...this.node,
|
|
53
|
+
from: ref
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
join(type, table, on) {
|
|
58
|
+
const tableRef = typeof table === "string" ? {
|
|
59
|
+
type: "table_ref",
|
|
60
|
+
name: table
|
|
61
|
+
} : table;
|
|
62
|
+
const join = {
|
|
63
|
+
type: "join",
|
|
64
|
+
joinType: type,
|
|
65
|
+
table: tableRef,
|
|
66
|
+
on
|
|
67
|
+
};
|
|
68
|
+
return new UpdateBuilder({
|
|
69
|
+
...this.node,
|
|
70
|
+
joins: [...this.node.joins, join]
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
innerJoin(table, on) {
|
|
74
|
+
return this.join("INNER", table, on);
|
|
75
|
+
}
|
|
76
|
+
leftJoin(table, on) {
|
|
77
|
+
return this.join("LEFT", table, on);
|
|
78
|
+
}
|
|
79
|
+
returning(...exprs) {
|
|
80
|
+
return new UpdateBuilder({
|
|
81
|
+
...this.node,
|
|
82
|
+
returning: [...this.node.returning, ...exprs]
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
with(name, query, recursive = false) {
|
|
86
|
+
const cte = {
|
|
87
|
+
name,
|
|
88
|
+
query,
|
|
89
|
+
recursive
|
|
90
|
+
};
|
|
91
|
+
return new UpdateBuilder({
|
|
92
|
+
...this.node,
|
|
93
|
+
ctes: [...this.node.ctes, cte]
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
build() {
|
|
97
|
+
return { ...this.node };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
export function update(table) {
|
|
101
|
+
return new UpdateBuilder().table(table);
|
|
102
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare class SumakError extends Error {
|
|
2
|
+
constructor(message: string);
|
|
3
|
+
}
|
|
4
|
+
export declare class InvalidExpressionError extends SumakError {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class UnsupportedDialectFeatureError extends SumakError {
|
|
8
|
+
constructor(dialect: string, feature: string);
|
|
9
|
+
}
|
|
10
|
+
export declare class EmptyQueryError extends SumakError {
|
|
11
|
+
constructor(queryType: string);
|
|
12
|
+
}
|
package/dist/errors.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export class SumakError extends Error {
|
|
2
|
+
constructor(message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = "SumakError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export class InvalidExpressionError extends SumakError {
|
|
8
|
+
constructor(message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "InvalidExpressionError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class UnsupportedDialectFeatureError extends SumakError {
|
|
14
|
+
constructor(dialect, feature) {
|
|
15
|
+
super(`${feature} is not supported in ${dialect}`);
|
|
16
|
+
this.name = "UnsupportedDialectFeatureError";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export class EmptyQueryError extends SumakError {
|
|
20
|
+
constructor(queryType) {
|
|
21
|
+
super(`Cannot build ${queryType}: missing required clauses`);
|
|
22
|
+
this.name = "EmptyQueryError";
|
|
23
|
+
}
|
|
24
|
+
}
|