@ignisia/sql 0.3.0 → 0.4.1
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 +1 -1
- package/dist/cjs/column/index.js +2 -9
- package/dist/cjs/database/alter.d.cts +2 -2
- package/dist/cjs/database/column.d.cts +2 -2
- package/dist/cjs/database/contract.d.cts +2 -2
- package/dist/cjs/database/index.d.cts +2 -2
- package/dist/cjs/database/index.js +1 -1
- package/dist/cjs/database/table.d.cts +2 -2
- package/dist/cjs/database/types.d.cts +1 -1
- package/dist/cjs/database/wrapper.d.cts +3 -3
- package/dist/cjs/database/wrapper.js +1 -17
- package/dist/cjs/{index-2jl8MRfX.d.cts → index-BjgeY76W.d.cts} +2 -2
- package/dist/cjs/{index-CnQVnCEI.d.cts → index-K07efKgt.d.cts} +194 -86
- package/dist/cjs/index.d.cts +2 -2
- package/dist/cjs/migration/index.d.cts +2 -2
- package/dist/cjs/migration/type.d.cts +2 -2
- package/dist/cjs/query/ast.d.cts +8 -0
- package/dist/cjs/query/ast.js +2 -0
- package/dist/cjs/query/builder.d.cts +5 -5
- package/dist/cjs/query/builder.js +8 -10
- package/dist/cjs/query/compiler.d.cts +13 -0
- package/dist/cjs/query/compiler.js +114 -0
- package/dist/cjs/query/condition/common.d.cts +46 -0
- package/dist/cjs/query/condition/common.js +50 -0
- package/dist/cjs/query/condition/core.d.cts +8 -0
- package/dist/cjs/query/condition/core.js +91 -0
- package/dist/cjs/query/condition/index.d.cts +10 -0
- package/dist/cjs/query/condition/index.js +26 -0
- package/dist/cjs/query/condition/not.d.cts +46 -0
- package/dist/cjs/query/condition/not.js +51 -0
- package/dist/cjs/query/constants.d.cts +31 -6
- package/dist/cjs/query/constants.js +30 -5
- package/dist/cjs/query/contract.d.cts +2 -2
- package/dist/cjs/query/explain.d.cts +12 -0
- package/dist/cjs/query/explain.js +65 -0
- package/dist/cjs/query/helper.d.cts +3 -3
- package/dist/cjs/query/helper.js +1 -5
- package/dist/cjs/query/index.d.cts +4 -4
- package/dist/cjs/query/index.js +57 -72
- package/dist/cjs/query/join.d.cts +19 -7
- package/dist/cjs/query/join.js +46 -9
- package/dist/cjs/query/sql.d.cts +6 -4
- package/dist/cjs/query/sql.js +64 -25
- package/dist/cjs/query/types.d.cts +2 -2
- package/dist/cjs/query/utilities.d.cts +7 -8
- package/dist/cjs/query/utilities.js +21 -66
- package/dist/cjs/table/index.d.cts +3 -3
- package/dist/cjs/table/types.d.cts +2 -2
- package/dist/cjs/table/utilities.d.cts +2 -2
- package/dist/cjs/types.d.cts +5 -1
- package/dist/cjs/utilities.d.cts +2 -1
- package/dist/cjs/utilities.js +22 -0
- package/dist/esm/column/index.js +3 -10
- package/dist/esm/database/alter.d.ts +2 -2
- package/dist/esm/database/column.d.ts +2 -2
- package/dist/esm/database/contract.d.ts +2 -2
- package/dist/esm/database/index.d.ts +2 -2
- package/dist/esm/database/index.js +2 -2
- package/dist/esm/database/table.d.ts +2 -2
- package/dist/esm/database/types.d.ts +1 -1
- package/dist/esm/database/wrapper.d.ts +3 -3
- package/dist/esm/database/wrapper.js +1 -17
- package/dist/esm/{index-BXOAxB_h.d.ts → index-B4y4GiNB.d.ts} +2 -2
- package/dist/esm/{index-BdpoD4zk.d.ts → index-CARBkj9h.d.ts} +194 -86
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/migration/index.d.ts +2 -2
- package/dist/esm/migration/type.d.ts +2 -2
- package/dist/esm/query/ast.d.ts +8 -0
- package/dist/esm/query/ast.js +1 -0
- package/dist/esm/query/builder.d.ts +5 -5
- package/dist/esm/query/builder.js +8 -10
- package/dist/esm/query/compiler.d.ts +13 -0
- package/dist/esm/query/compiler.js +112 -0
- package/dist/esm/query/condition/common.d.ts +46 -0
- package/dist/esm/query/condition/common.js +45 -0
- package/dist/esm/query/condition/core.d.ts +8 -0
- package/dist/esm/query/condition/core.js +89 -0
- package/dist/esm/query/condition/index.d.ts +10 -0
- package/dist/esm/query/condition/index.js +3 -0
- package/dist/esm/query/condition/not.d.ts +46 -0
- package/dist/esm/query/condition/not.js +46 -0
- package/dist/esm/query/constants.d.ts +31 -6
- package/dist/esm/query/constants.js +28 -6
- package/dist/esm/query/contract.d.ts +2 -2
- package/dist/esm/query/explain.d.ts +12 -0
- package/dist/esm/query/explain.js +64 -0
- package/dist/esm/query/helper.d.ts +3 -3
- package/dist/esm/query/helper.js +3 -6
- package/dist/esm/query/index.d.ts +4 -4
- package/dist/esm/query/index.js +60 -75
- package/dist/esm/query/join.d.ts +19 -7
- package/dist/esm/query/join.js +45 -9
- package/dist/esm/query/sql.d.ts +6 -4
- package/dist/esm/query/sql.js +64 -27
- package/dist/esm/query/types.d.ts +2 -2
- package/dist/esm/query/utilities.d.ts +7 -8
- package/dist/esm/query/utilities.js +22 -66
- package/dist/esm/table/index.d.ts +3 -3
- package/dist/esm/table/types.d.ts +2 -2
- package/dist/esm/table/utilities.d.ts +2 -2
- package/dist/esm/types.d.ts +5 -1
- package/dist/esm/utilities.d.ts +2 -1
- package/dist/esm/utilities.js +22 -1
- package/package.json +11 -5
- package/dist/cjs/query/condition.d.cts +0 -8
- package/dist/cjs/query/condition.js +0 -109
- package/dist/esm/query/condition.d.ts +0 -8
- package/dist/esm/query/condition.js +0 -101
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var constants = require('../table/constants');
|
|
4
|
+
var constants$1 = require('./constants');
|
|
5
|
+
|
|
6
|
+
function buildPostgresExplainQuery(options) {
|
|
7
|
+
const clauses = [];
|
|
8
|
+
if (options?.format) {
|
|
9
|
+
clauses.push(`${constants$1.ExplainClause.FORMAT} ${options.format}`);
|
|
10
|
+
}
|
|
11
|
+
if (options?.analyze) {
|
|
12
|
+
clauses.push(constants$1.ExplainClause.ANALYZE);
|
|
13
|
+
if (options?.summary != null) {
|
|
14
|
+
clauses.push(
|
|
15
|
+
`${constants$1.ExplainClause.SUMMARY} ${options.summary ? "ON" : "OFF"}`
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
if (options?.timing != null)
|
|
19
|
+
clauses.push(`${constants$1.ExplainClause.TIMING} ${options.timing ? "ON" : "OFF"}`);
|
|
20
|
+
}
|
|
21
|
+
if (options?.verbose) {
|
|
22
|
+
clauses.push(`${constants$1.ExplainClause.VERBOSE} ${options.verbose ? "ON" : "OFF"}`);
|
|
23
|
+
}
|
|
24
|
+
if (options?.costs != null) {
|
|
25
|
+
clauses.push(`${constants$1.ExplainClause.COSTS} ${options.costs ? "ON" : "OFF"}`);
|
|
26
|
+
}
|
|
27
|
+
if (options?.buffers != null) {
|
|
28
|
+
clauses.push(`${constants$1.ExplainClause.BUFFERS} ${options.buffers ? "ON" : "OFF"}`);
|
|
29
|
+
}
|
|
30
|
+
if (clauses.length === 0) {
|
|
31
|
+
return "EXPLAIN ";
|
|
32
|
+
}
|
|
33
|
+
return `EXPLAIN (${clauses.join(", ")})`;
|
|
34
|
+
}
|
|
35
|
+
function buildMySqlExplainQuery(options) {
|
|
36
|
+
const clauses = [];
|
|
37
|
+
if (options.analyze) {
|
|
38
|
+
clauses.push(constants$1.ExplainClause.ANALYZE);
|
|
39
|
+
}
|
|
40
|
+
if (options.format) {
|
|
41
|
+
clauses.push(`${constants$1.ExplainClause.FORMAT}=${options.format}`);
|
|
42
|
+
}
|
|
43
|
+
if (clauses.length > 1) {
|
|
44
|
+
throw new Error("MySQL EXPLAIN only support one clauses at a time");
|
|
45
|
+
}
|
|
46
|
+
return `EXPLAIN ${clauses.join(" ")}`;
|
|
47
|
+
}
|
|
48
|
+
function buildSqliteExplainQuery() {
|
|
49
|
+
const clauses = ["QUERY PLAN"];
|
|
50
|
+
return `EXPLAIN ${clauses.join(" ")}`;
|
|
51
|
+
}
|
|
52
|
+
function buildExplainQuery(q, options) {
|
|
53
|
+
switch (q.table.dialect) {
|
|
54
|
+
case constants.Dialect.POSTGRES:
|
|
55
|
+
return buildPostgresExplainQuery(options);
|
|
56
|
+
case constants.Dialect.MYSQL:
|
|
57
|
+
return buildMySqlExplainQuery(options);
|
|
58
|
+
case constants.Dialect.SQLITE:
|
|
59
|
+
return buildSqliteExplainQuery();
|
|
60
|
+
default:
|
|
61
|
+
throw new Error(`Dialect ${q.table.dialect} is not supported`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
exports.buildExplainQuery = buildExplainQuery;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { a1 as aggregateCol, _ as alias, $ as clone, a0 as col } from '../index-K07efKgt.cjs';
|
|
2
2
|
import '../column/index.cjs';
|
|
3
3
|
import './constants.cjs';
|
|
4
|
-
import '../column/constants.cjs';
|
|
5
|
-
import '../types.cjs';
|
|
6
4
|
import 'bun';
|
|
5
|
+
import '../types.cjs';
|
|
6
|
+
import '../column/constants.cjs';
|
|
7
7
|
import '../table/constants.cjs';
|
|
8
8
|
import '../column/types.cjs';
|
package/dist/cjs/query/helper.js
CHANGED
|
@@ -9,12 +9,9 @@ function alias(alias2) {
|
|
|
9
9
|
}
|
|
10
10
|
function clone() {
|
|
11
11
|
const query = new _.QueryBuilder(this.table);
|
|
12
|
-
Object.assign(query.definition, utilities.
|
|
12
|
+
Object.assign(query.definition, utilities.cloneDefinition(this.definition));
|
|
13
13
|
return query;
|
|
14
14
|
}
|
|
15
|
-
function rawCol(column) {
|
|
16
|
-
return column;
|
|
17
|
-
}
|
|
18
15
|
function col(column, alias2) {
|
|
19
16
|
return {
|
|
20
17
|
column,
|
|
@@ -33,4 +30,3 @@ exports.aggregateCol = aggregateCol;
|
|
|
33
30
|
exports.alias = alias;
|
|
34
31
|
exports.clone = clone;
|
|
35
32
|
exports.col = col;
|
|
36
|
-
exports.rawCol = rawCol;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
export { c as QueryBuilder } from '../index-K07efKgt.cjs';
|
|
1
2
|
import '../column/index.cjs';
|
|
2
|
-
|
|
3
|
+
import '../types.cjs';
|
|
3
4
|
import './constants.cjs';
|
|
4
|
-
import '
|
|
5
|
+
import 'bun';
|
|
5
6
|
import '../column/constants.cjs';
|
|
7
|
+
import '../table/constants.cjs';
|
|
6
8
|
import '../column/types.cjs';
|
|
7
|
-
import '../types.cjs';
|
|
8
|
-
import 'bun';
|
package/dist/cjs/query/index.js
CHANGED
|
@@ -17,90 +17,62 @@ class QueryBuilder {
|
|
|
17
17
|
clone;
|
|
18
18
|
toQuery;
|
|
19
19
|
toString;
|
|
20
|
+
toDebugString;
|
|
21
|
+
explain;
|
|
20
22
|
exec;
|
|
21
|
-
rawWhere;
|
|
22
|
-
rawAnd;
|
|
23
|
-
rawOr;
|
|
24
|
-
rawHaving;
|
|
25
23
|
where;
|
|
26
24
|
and;
|
|
27
25
|
or;
|
|
28
26
|
having;
|
|
27
|
+
whereGroup;
|
|
28
|
+
orGroup;
|
|
29
|
+
not;
|
|
29
30
|
constructor(table) {
|
|
30
31
|
this.hooks = {};
|
|
31
32
|
this.table = table;
|
|
32
33
|
this.definition = {
|
|
33
|
-
|
|
34
|
-
select: null,
|
|
35
|
-
having: null,
|
|
36
|
-
where: null,
|
|
37
|
-
params: null,
|
|
38
|
-
limit: null,
|
|
39
|
-
offset: null,
|
|
40
|
-
groupBy: null,
|
|
41
|
-
insertValues: null,
|
|
42
|
-
updateValues: null,
|
|
43
|
-
orderBy: null,
|
|
44
|
-
aggregates: null,
|
|
45
|
-
joins: null,
|
|
46
|
-
distinct: null,
|
|
47
|
-
baseAlias: table.name,
|
|
48
|
-
joinedTables: null,
|
|
49
|
-
withDeleted: null
|
|
34
|
+
baseAlias: table.name
|
|
50
35
|
};
|
|
51
36
|
this.alias = helper.alias.bind(this);
|
|
52
37
|
this.clone = helper.clone.bind(this);
|
|
53
38
|
this.toQuery = sql.toQuery.bind(this);
|
|
54
39
|
this.toString = sql.toString.bind(this);
|
|
40
|
+
this.toDebugString = sql.toDebugString.bind(this);
|
|
41
|
+
this.explain = sql.explain.bind(this);
|
|
55
42
|
this.exec = sql.exec.bind(this);
|
|
56
|
-
this.rawWhere = condition.rawWhere.bind(this);
|
|
57
|
-
this.rawHaving = condition.rawHaving.bind(this);
|
|
58
|
-
this.rawAnd = this.rawWhere;
|
|
59
|
-
this.rawOr = condition.rawOr.bind(this);
|
|
60
43
|
this.where = condition.where.bind(this);
|
|
61
44
|
this.having = condition.having.bind(this);
|
|
62
45
|
this.and = this.where;
|
|
63
46
|
this.or = condition.or.bind(this);
|
|
47
|
+
this.whereGroup = condition.whereGroup.bind(this);
|
|
48
|
+
this.orGroup = condition.orGroup.bind(this);
|
|
49
|
+
this.not = {
|
|
50
|
+
where: condition.whereNot.bind(this),
|
|
51
|
+
having: condition.havingNot.bind(this),
|
|
52
|
+
or: condition.orNot.bind(this),
|
|
53
|
+
whereGroup: condition.whereNotGroup.bind(
|
|
54
|
+
this
|
|
55
|
+
),
|
|
56
|
+
orGroup: condition.orNotGroup.bind(this)
|
|
57
|
+
};
|
|
64
58
|
}
|
|
65
|
-
leftJoin(joinTable, alias2
|
|
66
|
-
return join.
|
|
67
|
-
this,
|
|
68
|
-
constants.AcceptedJoin.LEFT,
|
|
69
|
-
alias2,
|
|
70
|
-
joinTable,
|
|
71
|
-
baseColumn,
|
|
72
|
-
joinColumn
|
|
73
|
-
);
|
|
59
|
+
leftJoin(joinTable, alias2) {
|
|
60
|
+
return join.prepareJoin(this, constants.AcceptedJoin.LEFT, joinTable, alias2);
|
|
74
61
|
}
|
|
75
|
-
rightJoin(joinTable, alias2
|
|
76
|
-
return join.
|
|
77
|
-
this,
|
|
78
|
-
constants.AcceptedJoin.RIGHT,
|
|
79
|
-
alias2,
|
|
80
|
-
joinTable,
|
|
81
|
-
baseColumn,
|
|
82
|
-
joinColumn
|
|
83
|
-
);
|
|
62
|
+
rightJoin(joinTable, alias2) {
|
|
63
|
+
return join.prepareJoin(this, constants.AcceptedJoin.RIGHT, joinTable, alias2);
|
|
84
64
|
}
|
|
85
|
-
innerJoin(joinTable, alias2
|
|
86
|
-
return join.
|
|
87
|
-
this,
|
|
88
|
-
constants.AcceptedJoin.INNER,
|
|
89
|
-
alias2,
|
|
90
|
-
joinTable,
|
|
91
|
-
baseColumn,
|
|
92
|
-
joinColumn
|
|
93
|
-
);
|
|
65
|
+
innerJoin(joinTable, alias2) {
|
|
66
|
+
return join.prepareJoin(this, constants.AcceptedJoin.INNER, joinTable, alias2);
|
|
94
67
|
}
|
|
95
|
-
|
|
96
|
-
return join.
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
);
|
|
68
|
+
fullJoin(joinTable, alias2) {
|
|
69
|
+
return join.prepareJoin(this, constants.AcceptedJoin.FULL, joinTable, alias2);
|
|
70
|
+
}
|
|
71
|
+
crossJoin(joinTable, alias2) {
|
|
72
|
+
return join.addNoOnJoin(this, constants.AcceptedJoin.CROSS, joinTable, alias2);
|
|
73
|
+
}
|
|
74
|
+
naturalJoin(joinTable, alias2) {
|
|
75
|
+
return join.addNoOnJoin(this, constants.AcceptedJoin.NATURAL, joinTable, alias2);
|
|
104
76
|
}
|
|
105
77
|
distinct() {
|
|
106
78
|
this.definition.distinct = true;
|
|
@@ -162,17 +134,19 @@ class QueryBuilder {
|
|
|
162
134
|
updatedAt,
|
|
163
135
|
timestamp
|
|
164
136
|
} = utilities$1.getTimestamp(this.table);
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
137
|
+
values = values.map((row) => {
|
|
138
|
+
const fields = {};
|
|
139
|
+
for (const key in this.table.columns) {
|
|
140
|
+
fields[key] = row[key] ?? null;
|
|
141
|
+
}
|
|
142
|
+
if (isWithTimestamp && isHasCreatedAt) {
|
|
143
|
+
fields[createdAt] = row[createdAt] ?? timestamp;
|
|
144
|
+
}
|
|
145
|
+
if (isWithTimestamp && isHasUpdatedAt) {
|
|
146
|
+
fields[updatedAt] = row[updatedAt] ?? timestamp;
|
|
147
|
+
}
|
|
148
|
+
return fields;
|
|
149
|
+
});
|
|
176
150
|
this.definition.insertValues = values;
|
|
177
151
|
return this;
|
|
178
152
|
}
|
|
@@ -198,6 +172,17 @@ class QueryBuilder {
|
|
|
198
172
|
this.definition.queryType = constants.QueryType.DELETE;
|
|
199
173
|
return this;
|
|
200
174
|
}
|
|
175
|
+
paginate(page, size) {
|
|
176
|
+
if (page < 1) {
|
|
177
|
+
throw new Error("Page number must be at least 1");
|
|
178
|
+
}
|
|
179
|
+
if (size < 1) {
|
|
180
|
+
throw new Error("Page size must be at least 1");
|
|
181
|
+
}
|
|
182
|
+
this.definition.limit = size;
|
|
183
|
+
this.definition.offset = (page - 1) * size;
|
|
184
|
+
return this;
|
|
185
|
+
}
|
|
201
186
|
infer() {
|
|
202
187
|
return null;
|
|
203
188
|
}
|
|
@@ -1,17 +1,29 @@
|
|
|
1
|
-
import { T as Table, Q as QueryDefinition, C as ColumnSelector,
|
|
1
|
+
import { T as Table, Q as QueryDefinition, C as ColumnSelector, S as StrictColumnSelector, c as QueryBuilder, J as JoinNode, e as QueryTransformerContract, f as QueryConditionContract } from '../index-K07efKgt.cjs';
|
|
2
2
|
import { Column } from '../column/index.cjs';
|
|
3
3
|
import { AcceptedJoin } from './constants.cjs';
|
|
4
|
-
import '../column/constants.cjs';
|
|
5
|
-
import '../types.cjs';
|
|
6
4
|
import 'bun';
|
|
5
|
+
import '../types.cjs';
|
|
6
|
+
import '../column/constants.cjs';
|
|
7
7
|
import '../table/constants.cjs';
|
|
8
8
|
import '../column/types.cjs';
|
|
9
9
|
|
|
10
|
-
declare function
|
|
10
|
+
declare function addNoOnJoin<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, JoinType extends AcceptedJoin, JoinTable extends Table<string, Record<string, Column>>, JoinAlias extends string, FinalJoinedTables extends JoinedTables & {
|
|
11
11
|
[K in JoinAlias]: JoinTable;
|
|
12
|
-
}>(query: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, joinType: JoinType,
|
|
13
|
-
joins:
|
|
12
|
+
}>(query: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, joinType: JoinType, joinTable: JoinTable, alias: JoinAlias): QueryBuilder<Alias, TableRef, FinalJoinedTables, Omit<Definition, "joins" | "joinedTables"> & {
|
|
13
|
+
joins: JoinNode[];
|
|
14
14
|
joinedTables: FinalJoinedTables;
|
|
15
15
|
}>;
|
|
16
|
+
declare function prepareJoin<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, JoinType extends AcceptedJoin, JoinTable extends Table<string, Record<string, Column>>, JoinAlias extends string>(query: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, joinType: JoinType, joinTable: JoinTable, alias: JoinAlias): {
|
|
17
|
+
on<FinalJoinedTables extends JoinedTables & { [K in JoinAlias]: JoinTable; }, ReturnedJoinedTables extends FinalJoinedTables = FinalJoinedTables>(callback: (q: QueryBuilder<Alias, TableRef, FinalJoinedTables>) => QueryBuilder<Alias, TableRef, ReturnedJoinedTables>): QueryBuilder<Alias, TableRef, ReturnedJoinedTables, Omit<Definition, "joins" | "joinedTables"> & {
|
|
18
|
+
joins: JoinNode[];
|
|
19
|
+
joinedTables: ReturnedJoinedTables;
|
|
20
|
+
}, ColumnSelector<Alias, TableRef, ReturnedJoinedTables>, StrictColumnSelector<Alias, TableRef, ReturnedJoinedTables>, QueryTransformerContract<Alias, TableRef, ReturnedJoinedTables, Omit<Definition, "joins" | "joinedTables"> & {
|
|
21
|
+
joins: JoinNode[];
|
|
22
|
+
joinedTables: ReturnedJoinedTables;
|
|
23
|
+
}, ColumnSelector<Alias, TableRef, ReturnedJoinedTables>, StrictColumnSelector<Alias, TableRef, ReturnedJoinedTables>>, QueryConditionContract<Alias, TableRef, ReturnedJoinedTables, Omit<Definition, "joins" | "joinedTables"> & {
|
|
24
|
+
joins: JoinNode[];
|
|
25
|
+
joinedTables: ReturnedJoinedTables;
|
|
26
|
+
}, ColumnSelector<Alias, TableRef, ReturnedJoinedTables>, StrictColumnSelector<Alias, TableRef, ReturnedJoinedTables>>>;
|
|
27
|
+
};
|
|
16
28
|
|
|
17
|
-
export {
|
|
29
|
+
export { addNoOnJoin, prepareJoin };
|
package/dist/cjs/query/join.js
CHANGED
|
@@ -1,18 +1,55 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
var _ = require('.');
|
|
4
|
+
var constants = require('./constants');
|
|
5
|
+
|
|
6
|
+
function addNoOnJoin(query, joinType, joinTable, alias) {
|
|
4
7
|
if (!query.definition.joins) query.definition.joins = [];
|
|
5
|
-
query.definition.joins.push(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
query.definition.joins.push({
|
|
9
|
+
type: constants.AstType.JOIN,
|
|
10
|
+
alias,
|
|
11
|
+
join: joinType,
|
|
12
|
+
table: joinTable
|
|
13
|
+
});
|
|
8
14
|
if (!query.definition.joinedTables) {
|
|
9
15
|
query.definition.joinedTables = {};
|
|
10
16
|
}
|
|
11
|
-
query.definition.joinedTables =
|
|
12
|
-
...query.definition.joinedTables,
|
|
13
|
-
[alias]: joinTable
|
|
14
|
-
};
|
|
17
|
+
query.definition.joinedTables[alias] = joinTable;
|
|
15
18
|
return query;
|
|
16
19
|
}
|
|
20
|
+
function prepareJoin(query, joinType, joinTable, alias) {
|
|
21
|
+
return {
|
|
22
|
+
on(callback) {
|
|
23
|
+
const sub = callback(new _.QueryBuilder(query.table));
|
|
24
|
+
const subDef = sub.definition;
|
|
25
|
+
if (!subDef.where) {
|
|
26
|
+
return query;
|
|
27
|
+
}
|
|
28
|
+
if (!query.definition.joins) {
|
|
29
|
+
query.definition.joins = [];
|
|
30
|
+
}
|
|
31
|
+
const on = subDef.where;
|
|
32
|
+
query.definition.joins.push({
|
|
33
|
+
type: constants.AstType.JOIN,
|
|
34
|
+
alias,
|
|
35
|
+
join: joinType,
|
|
36
|
+
table: joinTable,
|
|
37
|
+
on
|
|
38
|
+
});
|
|
39
|
+
if (subDef.joins?.length) {
|
|
40
|
+
query.definition.joins.push(...subDef.joins);
|
|
41
|
+
}
|
|
42
|
+
if (!query.definition.joinedTables) {
|
|
43
|
+
query.definition.joinedTables = {};
|
|
44
|
+
}
|
|
45
|
+
query.definition.joinedTables[alias] = joinTable;
|
|
46
|
+
if (subDef.joinedTables) {
|
|
47
|
+
Object.assign(query.definition.joinedTables, subDef.joinedTables);
|
|
48
|
+
}
|
|
49
|
+
return query;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
17
53
|
|
|
18
|
-
exports.
|
|
54
|
+
exports.addNoOnJoin = addNoOnJoin;
|
|
55
|
+
exports.prepareJoin = prepareJoin;
|
package/dist/cjs/query/sql.d.cts
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import { TransactionSQL } from 'bun';
|
|
2
|
-
import { T as Table, Q as QueryDefinition, C as ColumnSelector,
|
|
2
|
+
import { T as Table, Q as QueryDefinition, C as ColumnSelector, S as StrictColumnSelector, c as QueryBuilder, E as ExplainOptions } from '../index-K07efKgt.cjs';
|
|
3
3
|
import { Column } from '../column/index.cjs';
|
|
4
4
|
import { Dialect } from '../table/constants.cjs';
|
|
5
|
-
import '../column/constants.cjs';
|
|
6
5
|
import '../types.cjs';
|
|
7
6
|
import './constants.cjs';
|
|
7
|
+
import '../column/constants.cjs';
|
|
8
8
|
import '../column/types.cjs';
|
|
9
9
|
|
|
10
10
|
declare function buildQuery(query: string): string;
|
|
11
11
|
declare function toQuery<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>(this: Query, dialect?: Dialect | null): {
|
|
12
12
|
query: string;
|
|
13
|
-
params: unknown[]
|
|
13
|
+
params: unknown[];
|
|
14
14
|
};
|
|
15
15
|
declare function toString<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>(this: Query): string;
|
|
16
|
+
declare function toDebugString<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>(this: Query): string;
|
|
17
|
+
declare function explain<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>(this: Query, options?: ExplainOptions): Promise<unknown>;
|
|
16
18
|
declare function exec<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, Output extends Query['_output'] = Query['_output']>(this: Query, tx?: TransactionSQL | null): Promise<Output>;
|
|
17
19
|
|
|
18
|
-
export { buildQuery, exec, toQuery, toString };
|
|
20
|
+
export { buildQuery, exec, explain, toDebugString, toQuery, toString };
|
package/dist/cjs/query/sql.js
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
var constants$1 = require('../table/constants');
|
|
4
4
|
var builder = require('./builder');
|
|
5
|
+
var compiler = require('./compiler');
|
|
5
6
|
var constants = require('./constants');
|
|
7
|
+
var explain$1 = require('./explain');
|
|
6
8
|
var utilities = require('./utilities');
|
|
7
9
|
|
|
8
10
|
function buildQuery(query) {
|
|
@@ -12,62 +14,97 @@ function buildQuery(query) {
|
|
|
12
14
|
return `$${index}`;
|
|
13
15
|
});
|
|
14
16
|
}
|
|
15
|
-
function toQuery(dialect) {
|
|
16
|
-
|
|
17
|
+
function toQuery(dialect = this.table.dialect) {
|
|
18
|
+
const params = [];
|
|
19
|
+
const parts = [];
|
|
17
20
|
switch (this.definition.queryType) {
|
|
18
21
|
case constants.QueryType.SELECT:
|
|
19
|
-
|
|
22
|
+
parts.push(builder.buildSelectQuery(this));
|
|
20
23
|
break;
|
|
21
24
|
case constants.QueryType.INSERT:
|
|
22
|
-
|
|
25
|
+
parts.push(builder.buildInsertQuery(this, params));
|
|
23
26
|
break;
|
|
24
27
|
case constants.QueryType.UPDATE:
|
|
25
|
-
|
|
28
|
+
parts.push(builder.buildUpdateQuery(this, params));
|
|
26
29
|
break;
|
|
27
30
|
case constants.QueryType.DELETE:
|
|
28
|
-
|
|
31
|
+
parts.push(builder.buildDeleteQuery(this));
|
|
29
32
|
break;
|
|
30
33
|
default:
|
|
31
34
|
throw new Error("No query type defined");
|
|
32
35
|
}
|
|
33
36
|
if (this.definition?.joins?.length) {
|
|
34
|
-
|
|
37
|
+
const joinParts = this.definition.joins.map(
|
|
38
|
+
(join) => compiler.compileJoin(dialect, join, params)
|
|
39
|
+
);
|
|
40
|
+
parts.push(...joinParts);
|
|
35
41
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
42
|
+
if (this.definition.where) {
|
|
43
|
+
const whereConditions = compiler.compileAst(dialect, this.definition.where, params);
|
|
44
|
+
parts.push(`WHERE ${whereConditions}`);
|
|
39
45
|
}
|
|
40
46
|
const groupByConditions = utilities.getGroupByConditions(this);
|
|
41
47
|
if (groupByConditions.length) {
|
|
42
|
-
|
|
48
|
+
parts.push(`GROUP BY ${groupByConditions.join(", ")}`);
|
|
43
49
|
}
|
|
44
|
-
if (this.definition?.having
|
|
45
|
-
|
|
50
|
+
if (this.definition?.having) {
|
|
51
|
+
const havingConditions = compiler.compileAst(
|
|
52
|
+
dialect,
|
|
53
|
+
this.definition.having,
|
|
54
|
+
params
|
|
55
|
+
);
|
|
56
|
+
parts.push(`HAVING ${havingConditions}`);
|
|
46
57
|
}
|
|
47
58
|
if (this.definition?.orderBy?.length) {
|
|
48
|
-
|
|
59
|
+
parts.push(
|
|
60
|
+
`ORDER BY ${this.definition.orderBy.map((order) => `${order.column} ${order.direction}`).join(", ")}`
|
|
61
|
+
);
|
|
49
62
|
}
|
|
50
|
-
if (this.definition?.limit
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
this.definition.params.push(this.definition.limit);
|
|
63
|
+
if (this.definition?.limit != null) {
|
|
64
|
+
parts.push("LIMIT ?");
|
|
65
|
+
params.push(this.definition.limit);
|
|
54
66
|
}
|
|
55
|
-
if (this.definition?.offset
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this.definition.params.push(this.definition.offset);
|
|
67
|
+
if (this.definition?.offset != null) {
|
|
68
|
+
parts.push("OFFSET ?");
|
|
69
|
+
params.push(this.definition.offset);
|
|
59
70
|
}
|
|
60
71
|
if (this.definition.queryType === constants.QueryType.UPDATE || this.definition.queryType === constants.QueryType.DELETE) {
|
|
61
72
|
if (dialect !== constants$1.Dialect.MYSQL) {
|
|
62
|
-
|
|
73
|
+
parts.push("RETURNING *");
|
|
63
74
|
}
|
|
64
75
|
}
|
|
65
|
-
sql = buildQuery(
|
|
66
|
-
return { query: sql
|
|
76
|
+
const sql = buildQuery(parts.join(" "));
|
|
77
|
+
return { query: `${sql};`, params };
|
|
67
78
|
}
|
|
68
79
|
function toString() {
|
|
69
80
|
return this.toQuery().query;
|
|
70
81
|
}
|
|
82
|
+
function toDebugString() {
|
|
83
|
+
const { query, params } = this.toQuery();
|
|
84
|
+
if (!params || params.length === 0) {
|
|
85
|
+
return query;
|
|
86
|
+
}
|
|
87
|
+
let debugQuery = query;
|
|
88
|
+
utilities.sanitizeParams(params).forEach((param, index) => {
|
|
89
|
+
const value = param === null ? "NULL" : `'${String(param).replace(/'/g, "''")}'`;
|
|
90
|
+
debugQuery = debugQuery.replace(
|
|
91
|
+
new RegExp(`\\$${index + 1}\\b`, "g"),
|
|
92
|
+
value
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
return debugQuery;
|
|
96
|
+
}
|
|
97
|
+
function explain(options = {}) {
|
|
98
|
+
const { query, params } = this.toQuery();
|
|
99
|
+
const explainPrefix = explain$1.buildExplainQuery(this, options);
|
|
100
|
+
if (!this.table.client) {
|
|
101
|
+
throw new Error("Database client not defined");
|
|
102
|
+
}
|
|
103
|
+
return this.table.client.exec({
|
|
104
|
+
sql: `${explainPrefix}${query}`,
|
|
105
|
+
params
|
|
106
|
+
});
|
|
107
|
+
}
|
|
71
108
|
async function exec(tx) {
|
|
72
109
|
const client = this.table.client;
|
|
73
110
|
const dialect = this.table.dialect;
|
|
@@ -128,5 +165,7 @@ async function exec(tx) {
|
|
|
128
165
|
|
|
129
166
|
exports.buildQuery = buildQuery;
|
|
130
167
|
exports.exec = exec;
|
|
168
|
+
exports.explain = explain;
|
|
169
|
+
exports.toDebugString = toDebugString;
|
|
131
170
|
exports.toQuery = toQuery;
|
|
132
171
|
exports.toString = toString;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import 'bun';
|
|
1
2
|
import '../column/index.cjs';
|
|
2
3
|
import '../column/constants.cjs';
|
|
3
|
-
export {
|
|
4
|
+
export { r as AcceptedInsertValues, q as AcceptedOrderBy, s as AcceptedUpdateValues, t as AggregateColumn, A as AliasedColumn, C as ColumnSelector, E as ExplainOptions, Q as QueryDefinition, n as QueryHooks, v as QueryOutput, p as QueryRunHooks, w as QueryRunHooksOptions, R as RawColumn, u as SelectQueryOutput, d as SelectableColumn, S as StrictColumnSelector, W as WhereValue } from '../index-K07efKgt.cjs';
|
|
4
5
|
import '../types.cjs';
|
|
5
6
|
import './constants.cjs';
|
|
6
7
|
import '../table/constants.cjs';
|
|
7
8
|
import '../column/types.cjs';
|
|
8
|
-
import 'bun';
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import { T as Table,
|
|
1
|
+
import { T as Table, Q as QueryDefinition, C as ColumnSelector, S as StrictColumnSelector, c as QueryBuilder, A as AliasedColumn, d as SelectableColumn } from '../index-K07efKgt.cjs';
|
|
2
2
|
import { Column } from '../column/index.cjs';
|
|
3
|
-
import { Dialect } from '../table/constants.cjs';
|
|
4
|
-
import { AcceptedOperator } from './constants.cjs';
|
|
5
|
-
import '../column/constants.cjs';
|
|
6
|
-
import '../types.cjs';
|
|
7
3
|
import 'bun';
|
|
4
|
+
import '../types.cjs';
|
|
5
|
+
import './constants.cjs';
|
|
6
|
+
import '../column/constants.cjs';
|
|
7
|
+
import '../table/constants.cjs';
|
|
8
8
|
import '../column/types.cjs';
|
|
9
9
|
|
|
10
10
|
declare function getTableColumnNames<ColName extends string, BaseAlias extends string, BaseTable extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>>(column: ColName, baseAlias: BaseAlias, baseTable: BaseTable, joinedTables: JoinedTables): {
|
|
11
11
|
from: string;
|
|
12
12
|
columns: string[];
|
|
13
13
|
};
|
|
14
|
-
declare function getCondition<DbDialect extends Dialect, ColName extends string, Operator extends AcceptedOperator, Value extends WhereValue<Column>[Operator]>(dialect: DbDialect, column: ColName, operator: Operator, value: Value): string;
|
|
15
14
|
declare function getTimestamp<TableRef extends Table<string, Record<string, Column>>>(table: TableRef): {
|
|
16
15
|
isWithTimestamp: boolean;
|
|
17
16
|
timestamp: Date;
|
|
@@ -25,7 +24,6 @@ declare function getParanoid<TableRef extends Table<string, Record<string, Colum
|
|
|
25
24
|
timestamp: Date;
|
|
26
25
|
deletedAt: string;
|
|
27
26
|
};
|
|
28
|
-
declare function getWhereConditions<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>(q: Query): string[];
|
|
29
27
|
declare function getGroupByConditions<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>(q: Query): (string | AliasedColumn<ColumnSelector<Alias, TableRef, JoinedTables>, string>)[];
|
|
30
28
|
declare function getTableSelectName<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>(q: Query): string;
|
|
31
29
|
declare function parseAliasedRow({ row, selects, root, }: {
|
|
@@ -33,5 +31,6 @@ declare function parseAliasedRow({ row, selects, root, }: {
|
|
|
33
31
|
selects: SelectableColumn<string>[];
|
|
34
32
|
root?: string | null;
|
|
35
33
|
}): Record<string, any>;
|
|
34
|
+
declare function sanitizeParams(params: unknown[]): (string | number | bigint | boolean | null)[];
|
|
36
35
|
|
|
37
|
-
export {
|
|
36
|
+
export { getGroupByConditions, getParanoid, getTableColumnNames, getTableSelectName, getTimestamp, parseAliasedRow, sanitizeParams };
|