sumak 0.0.5 → 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 +694 -416
- package/dist/ast/ddl-nodes.d.mts +153 -0
- package/dist/ast/ddl-nodes.mjs +1 -0
- package/dist/ast/nodes.d.mts +12 -1
- 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 +3 -0
- package/dist/builder/delete.mjs +36 -0
- package/dist/builder/eb.d.mts +122 -6
- package/dist/builder/eb.mjs +273 -6
- package/dist/builder/select.d.mts +4 -0
- package/dist/builder/select.mjs +55 -0
- package/dist/builder/typed-delete.d.mts +14 -0
- package/dist/builder/typed-delete.mjs +26 -0
- package/dist/builder/typed-insert.d.mts +25 -1
- package/dist/builder/typed-insert.mjs +51 -0
- package/dist/builder/typed-select.d.mts +65 -4
- package/dist/builder/typed-select.mjs +132 -2
- package/dist/builder/typed-update.d.mts +12 -0
- package/dist/builder/typed-update.mjs +22 -0
- package/dist/builder/update.d.mts +3 -0
- package/dist/builder/update.mjs +36 -0
- package/dist/index.d.mts +12 -3
- package/dist/index.mjs +12 -2
- package/dist/printer/base.d.mts +2 -1
- package/dist/printer/base.mjs +25 -3
- 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 +59 -1
- package/dist/sumak.mjs +103 -2
- package/package.json +1 -1
|
@@ -28,6 +28,8 @@ export declare class TypedSelectBuilder<
|
|
|
28
28
|
Alias extends string,
|
|
29
29
|
T
|
|
30
30
|
>(expr: Expression<T>, alias: Alias): TypedSelectBuilder<DB, TB, O & Record<Alias, T>>;
|
|
31
|
+
/** Select multiple aliased expressions at once. */
|
|
32
|
+
selectExprs<Aliases extends Record<string, Expression<any>>>(exprs: Aliases): TypedSelectBuilder<DB, TB, O & { [K in keyof Aliases] : any }>;
|
|
31
33
|
/** DISTINCT */
|
|
32
34
|
distinct(): TypedSelectBuilder<DB, TB, O>;
|
|
33
35
|
/** DISTINCT ON (PG-specific) */
|
|
@@ -46,6 +48,10 @@ export declare class TypedSelectBuilder<
|
|
|
46
48
|
*/
|
|
47
49
|
where(exprOrCallback: Expression<boolean> | WhereCallback<DB, TB>): TypedSelectBuilder<DB, TB, O>;
|
|
48
50
|
/**
|
|
51
|
+
* OR WHERE — ORs with existing WHERE clause instead of AND.
|
|
52
|
+
*/
|
|
53
|
+
orWhere(exprOrCallback: Expression<boolean> | WhereCallback<DB, TB>): TypedSelectBuilder<DB, TB, O>;
|
|
54
|
+
/**
|
|
49
55
|
* INNER JOIN.
|
|
50
56
|
*
|
|
51
57
|
* ```ts
|
|
@@ -59,12 +65,12 @@ export declare class TypedSelectBuilder<
|
|
|
59
65
|
leftJoin<T extends keyof DB & string>(table: T, onOrCallback: Expression<boolean> | ((cols: JoinProxies<DB, TB, T>) => Expression<boolean>)): TypedSelectBuilder<DB, TB | T, O & Nullable<SelectRow<DB, T>>>;
|
|
60
66
|
/** RIGHT JOIN */
|
|
61
67
|
rightJoin<T extends keyof DB & string>(table: T, onOrCallback: Expression<boolean> | ((cols: JoinProxies<DB, TB, T>) => Expression<boolean>)): TypedSelectBuilder<DB, TB | T, Nullable<O> & SelectRow<DB, T>>;
|
|
62
|
-
/** GROUP BY */
|
|
63
|
-
groupBy(...cols: (keyof O & string)[]): TypedSelectBuilder<DB, TB, O>;
|
|
68
|
+
/** GROUP BY — accepts column names or expressions */
|
|
69
|
+
groupBy(...cols: ((keyof O & string) | Expression<any>)[]): TypedSelectBuilder<DB, TB, O>;
|
|
64
70
|
/** HAVING */
|
|
65
71
|
having(exprOrCallback: Expression<boolean> | WhereCallback<DB, TB>): TypedSelectBuilder<DB, TB, O>;
|
|
66
|
-
/** ORDER BY */
|
|
67
|
-
orderBy(col: keyof O & string
|
|
72
|
+
/** ORDER BY — accepts column name or expression */
|
|
73
|
+
orderBy(col: (keyof O & string) | Expression<any>, direction?: OrderDirection, nulls?: "FIRST" | "LAST"): TypedSelectBuilder<DB, TB, O>;
|
|
68
74
|
/** LIMIT */
|
|
69
75
|
limit(n: number): TypedSelectBuilder<DB, TB, O>;
|
|
70
76
|
/** OFFSET */
|
|
@@ -99,8 +105,63 @@ export declare class TypedSelectBuilder<
|
|
|
99
105
|
exceptAll(query: TypedSelectBuilder<DB, any, O>): TypedSelectBuilder<DB, TB, O>;
|
|
100
106
|
/** FULL JOIN — both sides become nullable. */
|
|
101
107
|
fullJoin<T extends keyof DB & string>(table: T, onOrCallback: Expression<boolean> | ((cols: JoinProxies<DB, TB, T>) => Expression<boolean>)): TypedSelectBuilder<DB, TB | T, Nullable<O> & Nullable<SelectRow<DB, T>>>;
|
|
108
|
+
/** INNER JOIN LATERAL (subquery) */
|
|
109
|
+
innerJoinLateral<
|
|
110
|
+
Alias extends string,
|
|
111
|
+
R
|
|
112
|
+
>(subquery: {
|
|
113
|
+
build(): SelectNode;
|
|
114
|
+
}, alias: Alias, on: Expression<boolean>): TypedSelectBuilder<DB, TB, O & Record<Alias, R>>;
|
|
115
|
+
/** LEFT JOIN LATERAL (subquery) */
|
|
116
|
+
leftJoinLateral<
|
|
117
|
+
Alias extends string,
|
|
118
|
+
R
|
|
119
|
+
>(subquery: {
|
|
120
|
+
build(): SelectNode;
|
|
121
|
+
}, alias: Alias, on: Expression<boolean>): TypedSelectBuilder<DB, TB, O & Partial<Record<Alias, R>>>;
|
|
122
|
+
/**
|
|
123
|
+
* INNER JOIN with alias — for self-joins.
|
|
124
|
+
*
|
|
125
|
+
* ```ts
|
|
126
|
+
* .innerJoinAs("users", "u2", ({ users, u2 }) => users.managerId.eqCol(u2.id))
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
innerJoinAs<
|
|
130
|
+
T extends keyof DB & string,
|
|
131
|
+
A extends string
|
|
132
|
+
>(table: T, alias: A, onCallback: (cols: { [Table in (TB | A) & string] : import("./eb.ts").ColumnProxies<DB, TB> }) => Expression<boolean>): TypedSelectBuilder<DB, TB | T, O & SelectRow<DB, T>>;
|
|
133
|
+
/**
|
|
134
|
+
* LEFT JOIN with alias — for self-joins.
|
|
135
|
+
*/
|
|
136
|
+
leftJoinAs<
|
|
137
|
+
T extends keyof DB & string,
|
|
138
|
+
A extends string
|
|
139
|
+
>(table: T, alias: A, onCallback: (cols: { [Table in (TB | A) & string] : import("./eb.ts").ColumnProxies<DB, TB> }) => Expression<boolean>): TypedSelectBuilder<DB, TB | T, O & Nullable<SelectRow<DB, T>>>;
|
|
102
140
|
/** CROSS JOIN — cartesian product. */
|
|
103
141
|
crossJoin<T extends keyof DB & string>(table: T): TypedSelectBuilder<DB, TB | T, O & SelectRow<DB, T>>;
|
|
142
|
+
/** CROSS JOIN LATERAL (subquery) */
|
|
143
|
+
crossJoinLateral<
|
|
144
|
+
Alias extends string,
|
|
145
|
+
R
|
|
146
|
+
>(subquery: {
|
|
147
|
+
build(): SelectNode;
|
|
148
|
+
}, alias: Alias): TypedSelectBuilder<DB, TB, O & Record<Alias, R>>;
|
|
149
|
+
/** Clear WHERE clause. */
|
|
150
|
+
clearWhere(): TypedSelectBuilder<DB, TB, O>;
|
|
151
|
+
/** Clear ORDER BY clause. */
|
|
152
|
+
clearOrderBy(): TypedSelectBuilder<DB, TB, O>;
|
|
153
|
+
/** Clear LIMIT. */
|
|
154
|
+
clearLimit(): TypedSelectBuilder<DB, TB, O>;
|
|
155
|
+
/** Clear OFFSET. */
|
|
156
|
+
clearOffset(): TypedSelectBuilder<DB, TB, O>;
|
|
157
|
+
/** Clear GROUP BY clause. */
|
|
158
|
+
clearGroupBy(): TypedSelectBuilder<DB, TB, O>;
|
|
159
|
+
/** Clear HAVING clause. */
|
|
160
|
+
clearHaving(): TypedSelectBuilder<DB, TB, O>;
|
|
161
|
+
/** Clear SELECT columns (resets to empty). */
|
|
162
|
+
clearSelect(): TypedSelectBuilder<DB, TB, O>;
|
|
163
|
+
/** Pipe builder through a function for reusable query fragments. */
|
|
164
|
+
$call<R>(fn: (qb: TypedSelectBuilder<DB, TB, O>) => R): R;
|
|
104
165
|
/** Conditionally apply a transformation. */
|
|
105
166
|
$if<O2>(condition: boolean, fn: (qb: TypedSelectBuilder<DB, TB, O>) => TypedSelectBuilder<DB, TB, O2>): TypedSelectBuilder<DB, TB, O | O2>;
|
|
106
167
|
/** Build the AST node. */
|
|
@@ -25,6 +25,16 @@ export class TypedSelectBuilder {
|
|
|
25
25
|
return new TypedSelectBuilder(this._builder.columns(aliased), this._table);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
selectExprs(exprs) {
|
|
29
|
+
let builder = this._builder;
|
|
30
|
+
for (const [alias, expr] of Object.entries(exprs)) {
|
|
31
|
+
const node = unwrap(expr);
|
|
32
|
+
const aliased = aliasExpr(node, alias);
|
|
33
|
+
builder = builder.columns(aliased);
|
|
34
|
+
}
|
|
35
|
+
return new TypedSelectBuilder(builder, this._table);
|
|
36
|
+
}
|
|
37
|
+
|
|
28
38
|
distinct() {
|
|
29
39
|
return new TypedSelectBuilder(this._builder.distinct(), this._table);
|
|
30
40
|
}
|
|
@@ -43,6 +53,16 @@ export class TypedSelectBuilder {
|
|
|
43
53
|
return new TypedSelectBuilder(this._builder.where(unwrap(exprOrCallback)), this._table);
|
|
44
54
|
}
|
|
45
55
|
|
|
56
|
+
orWhere(exprOrCallback) {
|
|
57
|
+
if (typeof exprOrCallback === "function") {
|
|
58
|
+
resetParams();
|
|
59
|
+
const cols = createColumnProxies(this._table);
|
|
60
|
+
const result = exprOrCallback(cols);
|
|
61
|
+
return new TypedSelectBuilder(this._builder.orWhere(unwrap(result)), this._table);
|
|
62
|
+
}
|
|
63
|
+
return new TypedSelectBuilder(this._builder.orWhere(unwrap(exprOrCallback)), this._table);
|
|
64
|
+
}
|
|
65
|
+
|
|
46
66
|
innerJoin(table, onOrCallback) {
|
|
47
67
|
const on = resolveJoinOn(onOrCallback, this._table, table);
|
|
48
68
|
return new TypedSelectBuilder(this._builder.innerJoin(table, unwrap(on)), this._table);
|
|
@@ -59,7 +79,8 @@ export class TypedSelectBuilder {
|
|
|
59
79
|
}
|
|
60
80
|
|
|
61
81
|
groupBy(...cols) {
|
|
62
|
-
|
|
82
|
+
const resolved = cols.map((c) => typeof c === "string" ? c : unwrap(c));
|
|
83
|
+
return new TypedSelectBuilder(this._builder.groupBy(...resolved), this._table);
|
|
63
84
|
}
|
|
64
85
|
|
|
65
86
|
having(exprOrCallback) {
|
|
@@ -73,7 +94,8 @@ export class TypedSelectBuilder {
|
|
|
73
94
|
}
|
|
74
95
|
|
|
75
96
|
orderBy(col, direction = "ASC", nulls) {
|
|
76
|
-
|
|
97
|
+
const expr = typeof col === "string" ? col : unwrap(col);
|
|
98
|
+
return new TypedSelectBuilder(this._builder.orderBy(expr, direction, nulls), this._table);
|
|
77
99
|
}
|
|
78
100
|
|
|
79
101
|
limit(n) {
|
|
@@ -151,10 +173,118 @@ export class TypedSelectBuilder {
|
|
|
151
173
|
return new TypedSelectBuilder(this._builder.join("FULL", table, unwrap(on)), this._table);
|
|
152
174
|
}
|
|
153
175
|
|
|
176
|
+
innerJoinLateral(subquery, alias, on) {
|
|
177
|
+
const sub = {
|
|
178
|
+
type: "subquery",
|
|
179
|
+
query: subquery.build(),
|
|
180
|
+
alias
|
|
181
|
+
};
|
|
182
|
+
return new TypedSelectBuilder(this._builder.innerJoinLateral(sub, unwrap(on)), this._table);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
leftJoinLateral(subquery, alias, on) {
|
|
186
|
+
const sub = {
|
|
187
|
+
type: "subquery",
|
|
188
|
+
query: subquery.build(),
|
|
189
|
+
alias
|
|
190
|
+
};
|
|
191
|
+
return new TypedSelectBuilder(this._builder.leftJoinLateral(sub, unwrap(on)), this._table);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
innerJoinAs(table, alias, onCallback) {
|
|
195
|
+
const proxies = new Proxy({}, { get(_target, tableName) {
|
|
196
|
+
return new Proxy({}, { get(_t2, colName) {
|
|
197
|
+
return new Col(colName, tableName);
|
|
198
|
+
} });
|
|
199
|
+
} });
|
|
200
|
+
const on = onCallback(proxies);
|
|
201
|
+
return new TypedSelectBuilder(this._builder.join("INNER", {
|
|
202
|
+
type: "table_ref",
|
|
203
|
+
name: table,
|
|
204
|
+
alias
|
|
205
|
+
}, unwrap(on)), this._table);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
leftJoinAs(table, alias, onCallback) {
|
|
209
|
+
const proxies = new Proxy({}, { get(_target, tableName) {
|
|
210
|
+
return new Proxy({}, { get(_t2, colName) {
|
|
211
|
+
return new Col(colName, tableName);
|
|
212
|
+
} });
|
|
213
|
+
} });
|
|
214
|
+
const on = onCallback(proxies);
|
|
215
|
+
return new TypedSelectBuilder(this._builder.join("LEFT", {
|
|
216
|
+
type: "table_ref",
|
|
217
|
+
name: table,
|
|
218
|
+
alias
|
|
219
|
+
}, unwrap(on)), this._table);
|
|
220
|
+
}
|
|
221
|
+
|
|
154
222
|
crossJoin(table) {
|
|
155
223
|
return new TypedSelectBuilder(this._builder.join("CROSS", table), this._table);
|
|
156
224
|
}
|
|
157
225
|
|
|
226
|
+
crossJoinLateral(subquery, alias) {
|
|
227
|
+
const sub = {
|
|
228
|
+
type: "subquery",
|
|
229
|
+
query: subquery.build(),
|
|
230
|
+
alias
|
|
231
|
+
};
|
|
232
|
+
return new TypedSelectBuilder(this._builder.crossJoinLateral(sub), this._table);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
clearWhere() {
|
|
236
|
+
return new TypedSelectBuilder(new SelectBuilder({
|
|
237
|
+
...this._builder.build(),
|
|
238
|
+
where: undefined
|
|
239
|
+
}), this._table);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
clearOrderBy() {
|
|
243
|
+
return new TypedSelectBuilder(new SelectBuilder({
|
|
244
|
+
...this._builder.build(),
|
|
245
|
+
orderBy: []
|
|
246
|
+
}), this._table);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
clearLimit() {
|
|
250
|
+
return new TypedSelectBuilder(new SelectBuilder({
|
|
251
|
+
...this._builder.build(),
|
|
252
|
+
limit: undefined
|
|
253
|
+
}), this._table);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
clearOffset() {
|
|
257
|
+
return new TypedSelectBuilder(new SelectBuilder({
|
|
258
|
+
...this._builder.build(),
|
|
259
|
+
offset: undefined
|
|
260
|
+
}), this._table);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
clearGroupBy() {
|
|
264
|
+
return new TypedSelectBuilder(new SelectBuilder({
|
|
265
|
+
...this._builder.build(),
|
|
266
|
+
groupBy: []
|
|
267
|
+
}), this._table);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
clearHaving() {
|
|
271
|
+
return new TypedSelectBuilder(new SelectBuilder({
|
|
272
|
+
...this._builder.build(),
|
|
273
|
+
having: undefined
|
|
274
|
+
}), this._table);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
clearSelect() {
|
|
278
|
+
return new TypedSelectBuilder(new SelectBuilder({
|
|
279
|
+
...this._builder.build(),
|
|
280
|
+
columns: []
|
|
281
|
+
}), this._table);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
$call(fn) {
|
|
285
|
+
return fn(this);
|
|
286
|
+
}
|
|
287
|
+
|
|
158
288
|
$if(condition, fn) {
|
|
159
289
|
if (condition) {
|
|
160
290
|
return fn(this);
|
|
@@ -35,6 +35,10 @@ export declare class TypedUpdateBuilder<
|
|
|
35
35
|
* RETURNING all columns.
|
|
36
36
|
*/
|
|
37
37
|
returningAll(): TypedUpdateReturningBuilder<DB, TB, SelectRow<DB, TB>>;
|
|
38
|
+
/** INNER JOIN for UPDATE (MySQL pattern) */
|
|
39
|
+
innerJoin(table: string, on: Expression<boolean>): TypedUpdateBuilder<DB, TB>;
|
|
40
|
+
/** LEFT JOIN for UPDATE */
|
|
41
|
+
leftJoin(table: string, on: Expression<boolean>): TypedUpdateBuilder<DB, TB>;
|
|
38
42
|
/** FROM clause (for UPDATE ... FROM ... WHERE joins). */
|
|
39
43
|
from<T extends keyof DB & string>(table: T): TypedUpdateBuilder<DB, TB>;
|
|
40
44
|
/** WITH (CTE) */
|
|
@@ -43,6 +47,14 @@ export declare class TypedUpdateBuilder<
|
|
|
43
47
|
$if(condition: boolean, fn: (qb: TypedUpdateBuilder<DB, TB>) => TypedUpdateBuilder<DB, TB>): TypedUpdateBuilder<DB, TB>;
|
|
44
48
|
build(): UpdateNode;
|
|
45
49
|
compile(printer: Printer): CompiledQuery;
|
|
50
|
+
/** EXPLAIN this query. */
|
|
51
|
+
explain(options?: {
|
|
52
|
+
analyze?: boolean;
|
|
53
|
+
format?: "TEXT" | "JSON" | "YAML" | "XML";
|
|
54
|
+
}): {
|
|
55
|
+
build(): import("../ast/nodes.ts").ExplainNode;
|
|
56
|
+
compile(printer: Printer): CompiledQuery;
|
|
57
|
+
};
|
|
46
58
|
}
|
|
47
59
|
export declare class TypedUpdateReturningBuilder<
|
|
48
60
|
DB,
|
|
@@ -66,6 +66,14 @@ export class TypedUpdateBuilder {
|
|
|
66
66
|
}));
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
innerJoin(table, on) {
|
|
70
|
+
return this._with(this._builder.innerJoin(table, unwrap(on)), this._paramIdx);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
leftJoin(table, on) {
|
|
74
|
+
return this._with(this._builder.leftJoin(table, unwrap(on)), this._paramIdx);
|
|
75
|
+
}
|
|
76
|
+
|
|
69
77
|
from(table) {
|
|
70
78
|
return this._with(this._builder.from(table), this._paramIdx);
|
|
71
79
|
}
|
|
@@ -86,6 +94,20 @@ export class TypedUpdateBuilder {
|
|
|
86
94
|
compile(printer) {
|
|
87
95
|
return printer.print(this.build());
|
|
88
96
|
}
|
|
97
|
+
|
|
98
|
+
explain(options) {
|
|
99
|
+
const node = this.build();
|
|
100
|
+
const explainNode = {
|
|
101
|
+
type: "explain",
|
|
102
|
+
statement: node,
|
|
103
|
+
analyze: options?.analyze,
|
|
104
|
+
format: options?.format
|
|
105
|
+
};
|
|
106
|
+
return {
|
|
107
|
+
build: () => explainNode,
|
|
108
|
+
compile: (p) => p.print(explainNode)
|
|
109
|
+
};
|
|
110
|
+
}
|
|
89
111
|
}
|
|
90
112
|
export class TypedUpdateReturningBuilder {
|
|
91
113
|
|
|
@@ -6,11 +6,14 @@ export declare class UpdateBuilder {
|
|
|
6
6
|
table(table: string | TableRefNode): UpdateBuilder;
|
|
7
7
|
set(column: string, value: ExpressionNode): UpdateBuilder;
|
|
8
8
|
where(expr: ExpressionNode): UpdateBuilder;
|
|
9
|
+
orWhere(expr: ExpressionNode): UpdateBuilder;
|
|
9
10
|
from(table: string | TableRefNode): UpdateBuilder;
|
|
10
11
|
/** Generic JOIN (MySQL pattern: UPDATE t JOIN other ON ... SET ...) */
|
|
11
12
|
join(type: JoinType, table: string | TableRefNode, on?: ExpressionNode): UpdateBuilder;
|
|
12
13
|
innerJoin(table: string | TableRefNode, on: ExpressionNode): UpdateBuilder;
|
|
13
14
|
leftJoin(table: string | TableRefNode, on: ExpressionNode): UpdateBuilder;
|
|
15
|
+
orderBy(expr: string | ExpressionNode, direction?: "ASC" | "DESC"): UpdateBuilder;
|
|
16
|
+
limit(n: ExpressionNode): UpdateBuilder;
|
|
14
17
|
returning(...exprs: ExpressionNode[]): UpdateBuilder;
|
|
15
18
|
with(name: string, query: SelectNode, recursive?: boolean): UpdateBuilder;
|
|
16
19
|
build(): UpdateNode;
|
package/dist/builder/update.mjs
CHANGED
|
@@ -43,6 +43,23 @@ export class UpdateBuilder {
|
|
|
43
43
|
where: expr
|
|
44
44
|
});
|
|
45
45
|
}
|
|
46
|
+
orWhere(expr) {
|
|
47
|
+
if (this.node.where) {
|
|
48
|
+
return new UpdateBuilder({
|
|
49
|
+
...this.node,
|
|
50
|
+
where: {
|
|
51
|
+
type: "binary_op",
|
|
52
|
+
op: "OR",
|
|
53
|
+
left: this.node.where,
|
|
54
|
+
right: expr
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return new UpdateBuilder({
|
|
59
|
+
...this.node,
|
|
60
|
+
where: expr
|
|
61
|
+
});
|
|
62
|
+
}
|
|
46
63
|
from(table) {
|
|
47
64
|
const ref = typeof table === "string" ? {
|
|
48
65
|
type: "table_ref",
|
|
@@ -76,6 +93,25 @@ export class UpdateBuilder {
|
|
|
76
93
|
leftJoin(table, on) {
|
|
77
94
|
return this.join("LEFT", table, on);
|
|
78
95
|
}
|
|
96
|
+
orderBy(expr, direction = "ASC") {
|
|
97
|
+
const node = {
|
|
98
|
+
expr: typeof expr === "string" ? {
|
|
99
|
+
type: "column_ref",
|
|
100
|
+
column: expr
|
|
101
|
+
} : expr,
|
|
102
|
+
direction
|
|
103
|
+
};
|
|
104
|
+
return new UpdateBuilder({
|
|
105
|
+
...this.node,
|
|
106
|
+
orderBy: [...this.node.orderBy ?? [], node]
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
limit(n) {
|
|
110
|
+
return new UpdateBuilder({
|
|
111
|
+
...this.node,
|
|
112
|
+
limit: n
|
|
113
|
+
});
|
|
114
|
+
}
|
|
79
115
|
returning(...exprs) {
|
|
80
116
|
return new UpdateBuilder({
|
|
81
117
|
...this.node,
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { AliasedExprNode, ArrayExprNode, ASTNode, BetweenNode, BinaryOpNode, CaseNode, CastNode, ColumnRefNode, CTENode, DeleteNode, ExistsNode, ExplainNode, ExpressionNode, FrameBound, FrameKind, FrameSpec, FullTextSearchMode, FullTextSearchNode, FunctionCallNode, InNode, InsertMode, InsertNode, IsNullNode, JoinNode, JsonAccessNode, LiteralNode, LockClause, LockMode, MergeNode, MergeWhenMatched, MergeWhenNotMatched, OnConflictNode, OrderByNode, ParamNode, RawNode, SelectNode, StarNode, SubqueryNode, TableRefNode, TemporalClause, UnaryOpNode, UpdateNode, WindowFunctionNode } from "./ast/nodes.mjs";
|
|
1
|
+
export type { AliasedExprNode, ArrayExprNode, ASTNode, BetweenNode, BinaryOpNode, CaseNode, CastNode, ColumnRefNode, CTENode, DeleteNode, ExistsNode, ExplainNode, ExpressionNode, FrameBound, FrameKind, FrameSpec, FullTextSearchMode, FullTextSearchNode, FunctionCallNode, InNode, InsertMode, InsertNode, IsNullNode, JoinNode, JsonAccessNode, LiteralNode, LockClause, LockMode, MergeNode, MergeWhenMatched, MergeWhenNotMatched, OnConflictNode, OrderByNode, ParamNode, RawNode, SelectNode, StarNode, SubqueryNode, TableRefNode, TupleNode, TemporalClause, UnaryOpNode, UpdateNode, WindowFunctionNode } from "./ast/nodes.mjs";
|
|
2
2
|
export { createDeleteNode, createInsertNode, createMergeNode, createSelectNode, createUpdateNode, tableRef } from "./ast/nodes.mjs";
|
|
3
3
|
export { between, binOp, col, colAs, eq, fn, gt, gte, inList, isNull, like, lit, lt, lte, neq, param, raw, star, subquery, unaryOp } from "./ast/expression.mjs";
|
|
4
4
|
export { ASTTransformer } from "./ast/transformer.mjs";
|
|
@@ -28,10 +28,11 @@ export type { Dialect } from "./dialect/types.mjs";
|
|
|
28
28
|
export { quoteIdentifier, quoteTableRef } from "./utils/identifier.mjs";
|
|
29
29
|
export { formatParam } from "./utils/param.mjs";
|
|
30
30
|
export type { CompiledQuery, DialectConfig, JoinType, OrderDirection, Primitive, SetOperator, SQLDialect } from "./types.mjs";
|
|
31
|
-
export { bigint, bigserial, boolean, bytea, char, ColumnBuilder, date, defineTable, doublePrecision, enumType, integer, json, jsonb, numeric, real, serial, smallint, text, time, timestamp, timestamptz, uuid, varchar } from "./schema/index.mjs";
|
|
31
|
+
export { bigint, bigserial, boolean, bytea, char, ColumnBuilder, date, defineTable, doublePrecision, enumType, integer, interval, json, jsonb, numeric, real, serial, smallint, text, time, timestamp, timestamptz, uuid, varchar } from "./schema/index.mjs";
|
|
32
32
|
export type { ColumnDef, ColumnType, Generated, GeneratedAlways, InferTable, Insertable, InsertType, Nullable, Selectable, SelectRow, SelectType, TableDefinition, Updateable, UpdateType } from "./schema/index.mjs";
|
|
33
|
-
export { abs, and, avg, case_, CaseBuilder, cast, ceil, coalesce, Col, concat, count, countDistinct, currentTimestamp, denseRank, exists, filter, floor, greatest, jsonRef, lag, lead, least, length, lower, max, min, not, notExists, now, ntile, nullif, or, over, rank, resetParams, round, rowNumber, sqlFn, substring, sum, textSearch, trim, upper, val, WindowBuilder } from "./builder/eb.mjs";
|
|
33
|
+
export { abs, add, aggOrderBy, and, arrayAgg, arrayContainedBy, arrayContains, arrayOverlaps, avg, avgDistinct, case_, CaseBuilder, cast, ceil, coalesce, Col, concat, count, countDistinct, currentTimestamp, denseRank, div, exists, filter, floor, greatest, jsonAgg, jsonBuildObject, jsonRef, lag, lead, least, length, lower, max, min, mod, mul, neg, not, notExists, now, ntile, nullif, or, over, rank, rawExpr, resetParams, round, rowNumber, sqlFn, stringAgg, subqueryExpr, sub, substring, sum, sumDistinct, textSearch, toJson, trim, tuple, upper, val, WindowBuilder } from "./builder/eb.mjs";
|
|
34
34
|
export type { ColumnProxies, WhereCallback } from "./builder/eb.mjs";
|
|
35
|
+
export { sql } from "./builder/sql.mjs";
|
|
35
36
|
export { sumak, Sumak } from "./sumak.mjs";
|
|
36
37
|
export type { SumakConfig } from "./sumak.mjs";
|
|
37
38
|
export { TypedSelectBuilder } from "./builder/typed-select.mjs";
|
|
@@ -46,4 +47,12 @@ export { SoftDeletePlugin } from "./plugin/soft-delete.mjs";
|
|
|
46
47
|
export { CamelCasePlugin } from "./plugin/camel-case.mjs";
|
|
47
48
|
export { Hookable } from "./plugin/hooks.mjs";
|
|
48
49
|
export type { HookContext, HookName, SumakHooks } from "./plugin/hooks.mjs";
|
|
50
|
+
export { CreateTableBuilder, ColumnDefBuilder, ForeignKeyBuilder } from "./builder/ddl/create-table.mjs";
|
|
51
|
+
export { AlterTableBuilder } from "./builder/ddl/alter-table.mjs";
|
|
52
|
+
export { CreateIndexBuilder } from "./builder/ddl/create-index.mjs";
|
|
53
|
+
export { CreateViewBuilder } from "./builder/ddl/create-view.mjs";
|
|
54
|
+
export { DropTableBuilder, DropIndexBuilder, DropViewBuilder, TruncateTableBuilder } from "./builder/ddl/drop.mjs";
|
|
55
|
+
export { DDLPrinter } from "./printer/ddl.mjs";
|
|
56
|
+
export { SchemaBuilder } from "./sumak.mjs";
|
|
57
|
+
export type { AlterColumnSet, AlterTableAction, AlterTableNode, CheckConstraintNode, ColumnDefinitionNode, CreateIndexNode, CreateTableNode, CreateViewNode, DDLNode, DropIndexNode, DropTableNode, DropViewNode, ForeignKeyAction, ForeignKeyConstraintNode, PrimaryKeyConstraintNode, TableConstraintNode, TruncateTableNode, UniqueConstraintNode } from "./ast/ddl-nodes.mjs";
|
|
49
58
|
export { EmptyQueryError, InvalidExpressionError, SumakError, UnsupportedDialectFeatureError } from "./errors.mjs";
|
package/dist/index.mjs
CHANGED
|
@@ -26,9 +26,11 @@ export { sqliteDialect } from "./dialect/sqlite.mjs";
|
|
|
26
26
|
export { quoteIdentifier, quoteTableRef } from "./utils/identifier.mjs";
|
|
27
27
|
export { formatParam } from "./utils/param.mjs";
|
|
28
28
|
|
|
29
|
-
export { bigint, bigserial, boolean, bytea, char, ColumnBuilder, date, defineTable, doublePrecision, enumType, integer, json, jsonb, numeric, real, serial, smallint, text, time, timestamp, timestamptz, uuid, varchar } from "./schema/index.mjs";
|
|
29
|
+
export { bigint, bigserial, boolean, bytea, char, ColumnBuilder, date, defineTable, doublePrecision, enumType, integer, interval, json, jsonb, numeric, real, serial, smallint, text, time, timestamp, timestamptz, uuid, varchar } from "./schema/index.mjs";
|
|
30
30
|
|
|
31
|
-
export { abs, and, avg, case_, CaseBuilder, cast, ceil, coalesce, Col, concat, count, countDistinct, currentTimestamp, denseRank, exists, filter, floor, greatest, jsonRef, lag, lead, least, length, lower, max, min, not, notExists, now, ntile, nullif, or, over, rank, resetParams, round, rowNumber, sqlFn, substring, sum, textSearch, trim, upper, val, WindowBuilder } from "./builder/eb.mjs";
|
|
31
|
+
export { abs, add, aggOrderBy, and, arrayAgg, arrayContainedBy, arrayContains, arrayOverlaps, avg, avgDistinct, case_, CaseBuilder, cast, ceil, coalesce, Col, concat, count, countDistinct, currentTimestamp, denseRank, div, exists, filter, floor, greatest, jsonAgg, jsonBuildObject, jsonRef, lag, lead, least, length, lower, max, min, mod, mul, neg, not, notExists, now, ntile, nullif, or, over, rank, rawExpr, resetParams, round, rowNumber, sqlFn, stringAgg, subqueryExpr, sub, substring, sum, sumDistinct, textSearch, toJson, trim, tuple, upper, val, WindowBuilder } from "./builder/eb.mjs";
|
|
32
|
+
|
|
33
|
+
export { sql } from "./builder/sql.mjs";
|
|
32
34
|
|
|
33
35
|
export { sumak, Sumak } from "./sumak.mjs";
|
|
34
36
|
export { TypedSelectBuilder } from "./builder/typed-select.mjs";
|
|
@@ -43,4 +45,12 @@ export { CamelCasePlugin } from "./plugin/camel-case.mjs";
|
|
|
43
45
|
|
|
44
46
|
export { Hookable } from "./plugin/hooks.mjs";
|
|
45
47
|
|
|
48
|
+
export { CreateTableBuilder, ColumnDefBuilder, ForeignKeyBuilder } from "./builder/ddl/create-table.mjs";
|
|
49
|
+
export { AlterTableBuilder } from "./builder/ddl/alter-table.mjs";
|
|
50
|
+
export { CreateIndexBuilder } from "./builder/ddl/create-index.mjs";
|
|
51
|
+
export { CreateViewBuilder } from "./builder/ddl/create-view.mjs";
|
|
52
|
+
export { DropTableBuilder, DropIndexBuilder, DropViewBuilder, TruncateTableBuilder } from "./builder/ddl/drop.mjs";
|
|
53
|
+
export { DDLPrinter } from "./printer/ddl.mjs";
|
|
54
|
+
export { SchemaBuilder } from "./sumak.mjs";
|
|
55
|
+
|
|
46
56
|
export { EmptyQueryError, InvalidExpressionError, SumakError, UnsupportedDialectFeatureError } from "./errors.mjs";
|
package/dist/printer/base.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AliasedExprNode, ArrayExprNode, ASTNode, BetweenNode, BinaryOpNode, CaseNode, CastNode, ColumnRefNode, CTENode, DeleteNode, ExistsNode, ExplainNode, ExpressionNode, FrameBound, FrameSpec, FunctionCallNode, InNode, InsertNode, IsNullNode, JoinNode, JsonAccessNode, LiteralNode, OnConflictNode, OrderByNode, ParamNode, RawNode, SelectNode, StarNode, SubqueryNode, TableRefNode, UnaryOpNode, UpdateNode, WindowFunctionNode } from "../ast/nodes.mjs";
|
|
1
|
+
import type { AliasedExprNode, ArrayExprNode, ASTNode, BetweenNode, BinaryOpNode, CaseNode, CastNode, ColumnRefNode, CTENode, DeleteNode, ExistsNode, ExplainNode, ExpressionNode, FrameBound, FrameSpec, FunctionCallNode, InNode, InsertNode, IsNullNode, JoinNode, JsonAccessNode, LiteralNode, OnConflictNode, OrderByNode, ParamNode, RawNode, SelectNode, StarNode, SubqueryNode, TableRefNode, TupleNode, UnaryOpNode, UpdateNode, WindowFunctionNode } from "../ast/nodes.mjs";
|
|
2
2
|
import type { FullTextSearchNode, MergeNode, MergeWhenMatched, MergeWhenNotMatched } from "../ast/nodes.mjs";
|
|
3
3
|
import type { CompiledQuery, SQLDialect } from "../types.mjs";
|
|
4
4
|
import type { Printer } from "./types.mjs";
|
|
@@ -43,6 +43,7 @@ export declare class BasePrinter implements Printer {
|
|
|
43
43
|
protected printMergeWhenMatched(when: MergeWhenMatched): string;
|
|
44
44
|
protected printMergeWhenNotMatched(when: MergeWhenNotMatched): string;
|
|
45
45
|
protected printAliasedExpr(node: AliasedExprNode): string;
|
|
46
|
+
protected printTuple(node: TupleNode): string;
|
|
46
47
|
protected printExplain(node: ExplainNode): string;
|
|
47
48
|
protected printFullTextSearch(node: FullTextSearchNode): string;
|
|
48
49
|
}
|
package/dist/printer/base.mjs
CHANGED
|
@@ -146,6 +146,12 @@ export class BasePrinter {
|
|
|
146
146
|
if (node.where) {
|
|
147
147
|
parts.push("WHERE", this.printExpression(node.where));
|
|
148
148
|
}
|
|
149
|
+
if (node.orderBy && node.orderBy.length > 0) {
|
|
150
|
+
parts.push("ORDER BY", node.orderBy.map((o) => this.printOrderBy(o)).join(", "));
|
|
151
|
+
}
|
|
152
|
+
if (node.limit) {
|
|
153
|
+
parts.push("LIMIT", this.printExpression(node.limit));
|
|
154
|
+
}
|
|
149
155
|
if (node.returning.length > 0) {
|
|
150
156
|
parts.push("RETURNING", node.returning.map((r) => this.printExpression(r)).join(", "));
|
|
151
157
|
}
|
|
@@ -166,6 +172,12 @@ export class BasePrinter {
|
|
|
166
172
|
if (node.where) {
|
|
167
173
|
parts.push("WHERE", this.printExpression(node.where));
|
|
168
174
|
}
|
|
175
|
+
if (node.orderBy && node.orderBy.length > 0) {
|
|
176
|
+
parts.push("ORDER BY", node.orderBy.map((o) => this.printOrderBy(o)).join(", "));
|
|
177
|
+
}
|
|
178
|
+
if (node.limit) {
|
|
179
|
+
parts.push("LIMIT", this.printExpression(node.limit));
|
|
180
|
+
}
|
|
169
181
|
if (node.returning.length > 0) {
|
|
170
182
|
parts.push("RETURNING", node.returning.map((r) => this.printExpression(r)).join(", "));
|
|
171
183
|
}
|
|
@@ -193,6 +205,7 @@ export class BasePrinter {
|
|
|
193
205
|
case "window_function": return this.printWindowFunction(node);
|
|
194
206
|
case "aliased_expr": return this.printAliasedExpr(node);
|
|
195
207
|
case "full_text_search": return this.printFullTextSearch(node);
|
|
208
|
+
case "tuple": return this.printTuple(node);
|
|
196
209
|
}
|
|
197
210
|
}
|
|
198
211
|
printColumnRef(node) {
|
|
@@ -219,7 +232,11 @@ export class BasePrinter {
|
|
|
219
232
|
}
|
|
220
233
|
printFunctionCall(node) {
|
|
221
234
|
const distinctPrefix = node.distinct ? "DISTINCT " : "";
|
|
222
|
-
let
|
|
235
|
+
let inner = `${distinctPrefix}${node.args.map((a) => this.printExpression(a)).join(", ")}`;
|
|
236
|
+
if (node.orderBy && node.orderBy.length > 0) {
|
|
237
|
+
inner += ` ORDER BY ${node.orderBy.map((o) => this.printOrderBy(o)).join(", ")}`;
|
|
238
|
+
}
|
|
239
|
+
let result = `${node.name}(${inner})`;
|
|
223
240
|
if (node.filter) {
|
|
224
241
|
result += ` FILTER (WHERE ${this.printExpression(node.filter)})`;
|
|
225
242
|
}
|
|
@@ -245,7 +262,8 @@ export class BasePrinter {
|
|
|
245
262
|
}
|
|
246
263
|
printBetween(node) {
|
|
247
264
|
const neg = node.negated ? "NOT " : "";
|
|
248
|
-
|
|
265
|
+
const sym = node.symmetric ? " SYMMETRIC" : "";
|
|
266
|
+
return `(${this.printExpression(node.expr)} ${neg}BETWEEN${sym} ${this.printExpression(node.low)} AND ${this.printExpression(node.high)})`;
|
|
249
267
|
}
|
|
250
268
|
printIn(node) {
|
|
251
269
|
const neg = node.negated ? "NOT " : "";
|
|
@@ -303,7 +321,8 @@ export class BasePrinter {
|
|
|
303
321
|
}
|
|
304
322
|
printJoin(node) {
|
|
305
323
|
const parts = [];
|
|
306
|
-
|
|
324
|
+
const lateral = node.lateral ? " LATERAL" : "";
|
|
325
|
+
parts.push(`${node.joinType} JOIN${lateral}`);
|
|
307
326
|
if (node.table.type === "subquery") {
|
|
308
327
|
parts.push(this.printSubquery(node.table));
|
|
309
328
|
} else {
|
|
@@ -426,6 +445,9 @@ export class BasePrinter {
|
|
|
426
445
|
printAliasedExpr(node) {
|
|
427
446
|
return `${this.printExpression(node.expr)} AS ${quoteIdentifier(node.alias, this.dialect)}`;
|
|
428
447
|
}
|
|
448
|
+
printTuple(node) {
|
|
449
|
+
return `(${node.elements.map((e) => this.printExpression(e)).join(", ")})`;
|
|
450
|
+
}
|
|
429
451
|
printExplain(node) {
|
|
430
452
|
const parts = ["EXPLAIN"];
|
|
431
453
|
if (node.analyze) {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { DDLNode } from "../ast/ddl-nodes.mjs";
|
|
2
|
+
import type { CompiledQuery, SQLDialect } from "../types.mjs";
|
|
3
|
+
export declare class DDLPrinter {
|
|
4
|
+
private dialect;
|
|
5
|
+
private params;
|
|
6
|
+
constructor(dialect: SQLDialect);
|
|
7
|
+
print(node: DDLNode): CompiledQuery;
|
|
8
|
+
private printNode;
|
|
9
|
+
private printCreateTable;
|
|
10
|
+
private printColumnDef;
|
|
11
|
+
private printConstraint;
|
|
12
|
+
private printForeignKeyConstraint;
|
|
13
|
+
private printAlterTable;
|
|
14
|
+
private printDropTable;
|
|
15
|
+
private printCreateIndex;
|
|
16
|
+
private printDropIndex;
|
|
17
|
+
private printCreateView;
|
|
18
|
+
private printDropView;
|
|
19
|
+
private printTruncateTable;
|
|
20
|
+
private printExpr;
|
|
21
|
+
}
|