@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
package/dist/esm/query/helper.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { QueryBuilder } from './index.js';
|
|
2
|
-
import {
|
|
2
|
+
import { cloneDefinition } from '../utilities.js';
|
|
3
3
|
|
|
4
4
|
// src/query/helper.ts
|
|
5
5
|
function alias(alias2) {
|
|
@@ -8,12 +8,9 @@ function alias(alias2) {
|
|
|
8
8
|
}
|
|
9
9
|
function clone() {
|
|
10
10
|
const query = new QueryBuilder(this.table);
|
|
11
|
-
Object.assign(query.definition,
|
|
11
|
+
Object.assign(query.definition, cloneDefinition(this.definition));
|
|
12
12
|
return query;
|
|
13
13
|
}
|
|
14
|
-
function rawCol(column) {
|
|
15
|
-
return column;
|
|
16
|
-
}
|
|
17
14
|
function col(column, alias2) {
|
|
18
15
|
return {
|
|
19
16
|
column,
|
|
@@ -28,4 +25,4 @@ function aggregateCol(fn, column, alias2) {
|
|
|
28
25
|
};
|
|
29
26
|
}
|
|
30
27
|
|
|
31
|
-
export { aggregateCol, alias, clone, col
|
|
28
|
+
export { aggregateCol, alias, clone, col };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
export { i as QueryBuilder } from '../index-CARBkj9h.js';
|
|
1
2
|
import '../column/index.js';
|
|
2
|
-
|
|
3
|
+
import '../types.js';
|
|
3
4
|
import './constants.js';
|
|
4
|
-
import '
|
|
5
|
+
import 'bun';
|
|
5
6
|
import '../column/constants.js';
|
|
7
|
+
import '../table/constants.js';
|
|
6
8
|
import '../column/types.js';
|
|
7
|
-
import '../types.js';
|
|
8
|
-
import 'bun';
|
package/dist/esm/query/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { quoteIdentifier } from '../utilities.js';
|
|
2
|
-
import {
|
|
2
|
+
import { where, having, or, whereGroup, orGroup, orNotGroup, whereNotGroup, orNot, havingNot, whereNot } from './condition/index.js';
|
|
3
3
|
import { AcceptedJoin, QueryType } from './constants.js';
|
|
4
4
|
import { alias, clone, aggregateCol, col } from './helper.js';
|
|
5
|
-
import {
|
|
6
|
-
import { toQuery, toString, exec } from './sql.js';
|
|
5
|
+
import { prepareJoin, addNoOnJoin } from './join.js';
|
|
6
|
+
import { toQuery, toString, toDebugString, explain, exec } from './sql.js';
|
|
7
7
|
import { getTimestamp, getParanoid } from './utilities.js';
|
|
8
8
|
|
|
9
9
|
// src/query/index.ts
|
|
@@ -16,90 +16,62 @@ var QueryBuilder = class {
|
|
|
16
16
|
clone;
|
|
17
17
|
toQuery;
|
|
18
18
|
toString;
|
|
19
|
+
toDebugString;
|
|
20
|
+
explain;
|
|
19
21
|
exec;
|
|
20
|
-
rawWhere;
|
|
21
|
-
rawAnd;
|
|
22
|
-
rawOr;
|
|
23
|
-
rawHaving;
|
|
24
22
|
where;
|
|
25
23
|
and;
|
|
26
24
|
or;
|
|
27
25
|
having;
|
|
26
|
+
whereGroup;
|
|
27
|
+
orGroup;
|
|
28
|
+
not;
|
|
28
29
|
constructor(table) {
|
|
29
30
|
this.hooks = {};
|
|
30
31
|
this.table = table;
|
|
31
32
|
this.definition = {
|
|
32
|
-
|
|
33
|
-
select: null,
|
|
34
|
-
having: null,
|
|
35
|
-
where: null,
|
|
36
|
-
params: null,
|
|
37
|
-
limit: null,
|
|
38
|
-
offset: null,
|
|
39
|
-
groupBy: null,
|
|
40
|
-
insertValues: null,
|
|
41
|
-
updateValues: null,
|
|
42
|
-
orderBy: null,
|
|
43
|
-
aggregates: null,
|
|
44
|
-
joins: null,
|
|
45
|
-
distinct: null,
|
|
46
|
-
baseAlias: table.name,
|
|
47
|
-
joinedTables: null,
|
|
48
|
-
withDeleted: null
|
|
33
|
+
baseAlias: table.name
|
|
49
34
|
};
|
|
50
35
|
this.alias = alias.bind(this);
|
|
51
36
|
this.clone = clone.bind(this);
|
|
52
37
|
this.toQuery = toQuery.bind(this);
|
|
53
38
|
this.toString = toString.bind(this);
|
|
39
|
+
this.toDebugString = toDebugString.bind(this);
|
|
40
|
+
this.explain = explain.bind(this);
|
|
54
41
|
this.exec = exec.bind(this);
|
|
55
|
-
this.rawWhere = rawWhere.bind(this);
|
|
56
|
-
this.rawHaving = rawHaving.bind(this);
|
|
57
|
-
this.rawAnd = this.rawWhere;
|
|
58
|
-
this.rawOr = rawOr.bind(this);
|
|
59
42
|
this.where = where.bind(this);
|
|
60
43
|
this.having = having.bind(this);
|
|
61
44
|
this.and = this.where;
|
|
62
45
|
this.or = or.bind(this);
|
|
46
|
+
this.whereGroup = whereGroup.bind(this);
|
|
47
|
+
this.orGroup = orGroup.bind(this);
|
|
48
|
+
this.not = {
|
|
49
|
+
where: whereNot.bind(this),
|
|
50
|
+
having: havingNot.bind(this),
|
|
51
|
+
or: orNot.bind(this),
|
|
52
|
+
whereGroup: whereNotGroup.bind(
|
|
53
|
+
this
|
|
54
|
+
),
|
|
55
|
+
orGroup: orNotGroup.bind(this)
|
|
56
|
+
};
|
|
63
57
|
}
|
|
64
|
-
leftJoin(joinTable, alias2
|
|
65
|
-
return
|
|
66
|
-
this,
|
|
67
|
-
AcceptedJoin.LEFT,
|
|
68
|
-
alias2,
|
|
69
|
-
joinTable,
|
|
70
|
-
baseColumn,
|
|
71
|
-
joinColumn
|
|
72
|
-
);
|
|
58
|
+
leftJoin(joinTable, alias2) {
|
|
59
|
+
return prepareJoin(this, AcceptedJoin.LEFT, joinTable, alias2);
|
|
73
60
|
}
|
|
74
|
-
rightJoin(joinTable, alias2
|
|
75
|
-
return
|
|
76
|
-
this,
|
|
77
|
-
AcceptedJoin.RIGHT,
|
|
78
|
-
alias2,
|
|
79
|
-
joinTable,
|
|
80
|
-
baseColumn,
|
|
81
|
-
joinColumn
|
|
82
|
-
);
|
|
61
|
+
rightJoin(joinTable, alias2) {
|
|
62
|
+
return prepareJoin(this, AcceptedJoin.RIGHT, joinTable, alias2);
|
|
83
63
|
}
|
|
84
|
-
innerJoin(joinTable, alias2
|
|
85
|
-
return
|
|
86
|
-
this,
|
|
87
|
-
AcceptedJoin.INNER,
|
|
88
|
-
alias2,
|
|
89
|
-
joinTable,
|
|
90
|
-
baseColumn,
|
|
91
|
-
joinColumn
|
|
92
|
-
);
|
|
64
|
+
innerJoin(joinTable, alias2) {
|
|
65
|
+
return prepareJoin(this, AcceptedJoin.INNER, joinTable, alias2);
|
|
93
66
|
}
|
|
94
|
-
|
|
95
|
-
return
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
);
|
|
67
|
+
fullJoin(joinTable, alias2) {
|
|
68
|
+
return prepareJoin(this, AcceptedJoin.FULL, joinTable, alias2);
|
|
69
|
+
}
|
|
70
|
+
crossJoin(joinTable, alias2) {
|
|
71
|
+
return addNoOnJoin(this, AcceptedJoin.CROSS, joinTable, alias2);
|
|
72
|
+
}
|
|
73
|
+
naturalJoin(joinTable, alias2) {
|
|
74
|
+
return addNoOnJoin(this, AcceptedJoin.NATURAL, joinTable, alias2);
|
|
103
75
|
}
|
|
104
76
|
distinct() {
|
|
105
77
|
this.definition.distinct = true;
|
|
@@ -161,17 +133,19 @@ var QueryBuilder = class {
|
|
|
161
133
|
updatedAt,
|
|
162
134
|
timestamp
|
|
163
135
|
} = getTimestamp(this.table);
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
136
|
+
values = values.map((row) => {
|
|
137
|
+
const fields = {};
|
|
138
|
+
for (const key in this.table.columns) {
|
|
139
|
+
fields[key] = row[key] ?? null;
|
|
140
|
+
}
|
|
141
|
+
if (isWithTimestamp && isHasCreatedAt) {
|
|
142
|
+
fields[createdAt] = row[createdAt] ?? timestamp;
|
|
143
|
+
}
|
|
144
|
+
if (isWithTimestamp && isHasUpdatedAt) {
|
|
145
|
+
fields[updatedAt] = row[updatedAt] ?? timestamp;
|
|
146
|
+
}
|
|
147
|
+
return fields;
|
|
148
|
+
});
|
|
175
149
|
this.definition.insertValues = values;
|
|
176
150
|
return this;
|
|
177
151
|
}
|
|
@@ -197,6 +171,17 @@ var QueryBuilder = class {
|
|
|
197
171
|
this.definition.queryType = QueryType.DELETE;
|
|
198
172
|
return this;
|
|
199
173
|
}
|
|
174
|
+
paginate(page, size) {
|
|
175
|
+
if (page < 1) {
|
|
176
|
+
throw new Error("Page number must be at least 1");
|
|
177
|
+
}
|
|
178
|
+
if (size < 1) {
|
|
179
|
+
throw new Error("Page size must be at least 1");
|
|
180
|
+
}
|
|
181
|
+
this.definition.limit = size;
|
|
182
|
+
this.definition.offset = (page - 1) * size;
|
|
183
|
+
return this;
|
|
184
|
+
}
|
|
200
185
|
infer() {
|
|
201
186
|
return null;
|
|
202
187
|
}
|
package/dist/esm/query/join.d.ts
CHANGED
|
@@ -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, h as StrictColumnSelector, i as QueryBuilder, J as JoinNode, k as QueryTransformerContract, l as QueryConditionContract } from '../index-CARBkj9h.js';
|
|
2
2
|
import { Column } from '../column/index.js';
|
|
3
3
|
import { AcceptedJoin } from './constants.js';
|
|
4
|
-
import '../column/constants.js';
|
|
5
|
-
import '../types.js';
|
|
6
4
|
import 'bun';
|
|
5
|
+
import '../types.js';
|
|
6
|
+
import '../column/constants.js';
|
|
7
7
|
import '../table/constants.js';
|
|
8
8
|
import '../column/types.js';
|
|
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/esm/query/join.js
CHANGED
|
@@ -1,17 +1,53 @@
|
|
|
1
|
+
import { QueryBuilder } from './index.js';
|
|
2
|
+
import { AstType } from './constants.js';
|
|
3
|
+
|
|
1
4
|
// src/query/join.ts
|
|
2
|
-
function
|
|
5
|
+
function addNoOnJoin(query, joinType, joinTable, alias) {
|
|
3
6
|
if (!query.definition.joins) query.definition.joins = [];
|
|
4
|
-
query.definition.joins.push(
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
query.definition.joins.push({
|
|
8
|
+
type: AstType.JOIN,
|
|
9
|
+
alias,
|
|
10
|
+
join: joinType,
|
|
11
|
+
table: joinTable
|
|
12
|
+
});
|
|
7
13
|
if (!query.definition.joinedTables) {
|
|
8
14
|
query.definition.joinedTables = {};
|
|
9
15
|
}
|
|
10
|
-
query.definition.joinedTables =
|
|
11
|
-
...query.definition.joinedTables,
|
|
12
|
-
[alias]: joinTable
|
|
13
|
-
};
|
|
16
|
+
query.definition.joinedTables[alias] = joinTable;
|
|
14
17
|
return query;
|
|
15
18
|
}
|
|
19
|
+
function prepareJoin(query, joinType, joinTable, alias) {
|
|
20
|
+
return {
|
|
21
|
+
on(callback) {
|
|
22
|
+
const sub = callback(new QueryBuilder(query.table));
|
|
23
|
+
const subDef = sub.definition;
|
|
24
|
+
if (!subDef.where) {
|
|
25
|
+
return query;
|
|
26
|
+
}
|
|
27
|
+
if (!query.definition.joins) {
|
|
28
|
+
query.definition.joins = [];
|
|
29
|
+
}
|
|
30
|
+
const on = subDef.where;
|
|
31
|
+
query.definition.joins.push({
|
|
32
|
+
type: AstType.JOIN,
|
|
33
|
+
alias,
|
|
34
|
+
join: joinType,
|
|
35
|
+
table: joinTable,
|
|
36
|
+
on
|
|
37
|
+
});
|
|
38
|
+
if (subDef.joins?.length) {
|
|
39
|
+
query.definition.joins.push(...subDef.joins);
|
|
40
|
+
}
|
|
41
|
+
if (!query.definition.joinedTables) {
|
|
42
|
+
query.definition.joinedTables = {};
|
|
43
|
+
}
|
|
44
|
+
query.definition.joinedTables[alias] = joinTable;
|
|
45
|
+
if (subDef.joinedTables) {
|
|
46
|
+
Object.assign(query.definition.joinedTables, subDef.joinedTables);
|
|
47
|
+
}
|
|
48
|
+
return query;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
16
52
|
|
|
17
|
-
export {
|
|
53
|
+
export { addNoOnJoin, prepareJoin };
|
package/dist/esm/query/sql.d.ts
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, h as StrictColumnSelector, i as QueryBuilder, E as ExplainOptions } from '../index-CARBkj9h.js';
|
|
3
3
|
import { Column } from '../column/index.js';
|
|
4
4
|
import { Dialect } from '../table/constants.js';
|
|
5
|
-
import '../column/constants.js';
|
|
6
5
|
import '../types.js';
|
|
7
6
|
import './constants.js';
|
|
7
|
+
import '../column/constants.js';
|
|
8
8
|
import '../column/types.js';
|
|
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/esm/query/sql.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { Dialect } from '../table/constants.js';
|
|
2
2
|
import { buildDeleteQuery, buildUpdateQuery, buildInsertQuery, buildSelectQuery } from './builder.js';
|
|
3
|
+
import { compileJoin, compileAst } from './compiler.js';
|
|
3
4
|
import { QueryType, QueryHooksType } from './constants.js';
|
|
4
|
-
import {
|
|
5
|
+
import { buildExplainQuery } from './explain.js';
|
|
6
|
+
import { getGroupByConditions, sanitizeParams, parseAliasedRow } from './utilities.js';
|
|
5
7
|
|
|
6
8
|
// src/query/sql.ts
|
|
7
9
|
function buildQuery(query) {
|
|
@@ -11,62 +13,97 @@ function buildQuery(query) {
|
|
|
11
13
|
return `$${index}`;
|
|
12
14
|
});
|
|
13
15
|
}
|
|
14
|
-
function toQuery(dialect) {
|
|
15
|
-
|
|
16
|
+
function toQuery(dialect = this.table.dialect) {
|
|
17
|
+
const params = [];
|
|
18
|
+
const parts = [];
|
|
16
19
|
switch (this.definition.queryType) {
|
|
17
20
|
case QueryType.SELECT:
|
|
18
|
-
|
|
21
|
+
parts.push(buildSelectQuery(this));
|
|
19
22
|
break;
|
|
20
23
|
case QueryType.INSERT:
|
|
21
|
-
|
|
24
|
+
parts.push(buildInsertQuery(this, params));
|
|
22
25
|
break;
|
|
23
26
|
case QueryType.UPDATE:
|
|
24
|
-
|
|
27
|
+
parts.push(buildUpdateQuery(this, params));
|
|
25
28
|
break;
|
|
26
29
|
case QueryType.DELETE:
|
|
27
|
-
|
|
30
|
+
parts.push(buildDeleteQuery(this));
|
|
28
31
|
break;
|
|
29
32
|
default:
|
|
30
33
|
throw new Error("No query type defined");
|
|
31
34
|
}
|
|
32
35
|
if (this.definition?.joins?.length) {
|
|
33
|
-
|
|
36
|
+
const joinParts = this.definition.joins.map(
|
|
37
|
+
(join) => compileJoin(dialect, join, params)
|
|
38
|
+
);
|
|
39
|
+
parts.push(...joinParts);
|
|
34
40
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
41
|
+
if (this.definition.where) {
|
|
42
|
+
const whereConditions = compileAst(dialect, this.definition.where, params);
|
|
43
|
+
parts.push(`WHERE ${whereConditions}`);
|
|
38
44
|
}
|
|
39
45
|
const groupByConditions = getGroupByConditions(this);
|
|
40
46
|
if (groupByConditions.length) {
|
|
41
|
-
|
|
47
|
+
parts.push(`GROUP BY ${groupByConditions.join(", ")}`);
|
|
42
48
|
}
|
|
43
|
-
if (this.definition?.having
|
|
44
|
-
|
|
49
|
+
if (this.definition?.having) {
|
|
50
|
+
const havingConditions = compileAst(
|
|
51
|
+
dialect,
|
|
52
|
+
this.definition.having,
|
|
53
|
+
params
|
|
54
|
+
);
|
|
55
|
+
parts.push(`HAVING ${havingConditions}`);
|
|
45
56
|
}
|
|
46
57
|
if (this.definition?.orderBy?.length) {
|
|
47
|
-
|
|
58
|
+
parts.push(
|
|
59
|
+
`ORDER BY ${this.definition.orderBy.map((order) => `${order.column} ${order.direction}`).join(", ")}`
|
|
60
|
+
);
|
|
48
61
|
}
|
|
49
|
-
if (this.definition?.limit
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
this.definition.params.push(this.definition.limit);
|
|
62
|
+
if (this.definition?.limit != null) {
|
|
63
|
+
parts.push("LIMIT ?");
|
|
64
|
+
params.push(this.definition.limit);
|
|
53
65
|
}
|
|
54
|
-
if (this.definition?.offset
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
this.definition.params.push(this.definition.offset);
|
|
66
|
+
if (this.definition?.offset != null) {
|
|
67
|
+
parts.push("OFFSET ?");
|
|
68
|
+
params.push(this.definition.offset);
|
|
58
69
|
}
|
|
59
70
|
if (this.definition.queryType === QueryType.UPDATE || this.definition.queryType === QueryType.DELETE) {
|
|
60
71
|
if (dialect !== Dialect.MYSQL) {
|
|
61
|
-
|
|
72
|
+
parts.push("RETURNING *");
|
|
62
73
|
}
|
|
63
74
|
}
|
|
64
|
-
sql = buildQuery(
|
|
65
|
-
return { query: sql
|
|
75
|
+
const sql = buildQuery(parts.join(" "));
|
|
76
|
+
return { query: `${sql};`, params };
|
|
66
77
|
}
|
|
67
78
|
function toString() {
|
|
68
79
|
return this.toQuery().query;
|
|
69
80
|
}
|
|
81
|
+
function toDebugString() {
|
|
82
|
+
const { query, params } = this.toQuery();
|
|
83
|
+
if (!params || params.length === 0) {
|
|
84
|
+
return query;
|
|
85
|
+
}
|
|
86
|
+
let debugQuery = query;
|
|
87
|
+
sanitizeParams(params).forEach((param, index) => {
|
|
88
|
+
const value = param === null ? "NULL" : `'${String(param).replace(/'/g, "''")}'`;
|
|
89
|
+
debugQuery = debugQuery.replace(
|
|
90
|
+
new RegExp(`\\$${index + 1}\\b`, "g"),
|
|
91
|
+
value
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
return debugQuery;
|
|
95
|
+
}
|
|
96
|
+
function explain(options = {}) {
|
|
97
|
+
const { query, params } = this.toQuery();
|
|
98
|
+
const explainPrefix = buildExplainQuery(this, options);
|
|
99
|
+
if (!this.table.client) {
|
|
100
|
+
throw new Error("Database client not defined");
|
|
101
|
+
}
|
|
102
|
+
return this.table.client.exec({
|
|
103
|
+
sql: `${explainPrefix}${query}`,
|
|
104
|
+
params
|
|
105
|
+
});
|
|
106
|
+
}
|
|
70
107
|
async function exec(tx) {
|
|
71
108
|
const client = this.table.client;
|
|
72
109
|
const dialect = this.table.dialect;
|
|
@@ -125,4 +162,4 @@ async function exec(tx) {
|
|
|
125
162
|
);
|
|
126
163
|
}
|
|
127
164
|
|
|
128
|
-
export { buildQuery, exec, toQuery, toString };
|
|
165
|
+
export { buildQuery, exec, explain, toDebugString, toQuery, toString };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import 'bun';
|
|
1
2
|
import '../column/index.js';
|
|
2
3
|
import '../column/constants.js';
|
|
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, j as SelectableColumn, h as StrictColumnSelector, W as WhereValue } from '../index-CARBkj9h.js';
|
|
4
5
|
import '../types.js';
|
|
5
6
|
import './constants.js';
|
|
6
7
|
import '../table/constants.js';
|
|
7
8
|
import '../column/types.js';
|
|
8
|
-
import 'bun';
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import { T as Table,
|
|
1
|
+
import { T as Table, Q as QueryDefinition, C as ColumnSelector, h as StrictColumnSelector, i as QueryBuilder, A as AliasedColumn, j as SelectableColumn } from '../index-CARBkj9h.js';
|
|
2
2
|
import { Column } from '../column/index.js';
|
|
3
|
-
import { Dialect } from '../table/constants.js';
|
|
4
|
-
import { AcceptedOperator } from './constants.js';
|
|
5
|
-
import '../column/constants.js';
|
|
6
|
-
import '../types.js';
|
|
7
3
|
import 'bun';
|
|
4
|
+
import '../types.js';
|
|
5
|
+
import './constants.js';
|
|
6
|
+
import '../column/constants.js';
|
|
7
|
+
import '../table/constants.js';
|
|
8
8
|
import '../column/types.js';
|
|
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 };
|