@ignisia/sql 0.2.2 → 0.4.0
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/constants.d.cts +58 -5
- package/dist/cjs/column/constants.js +79 -22
- package/dist/cjs/column/index.d.cts +1 -0
- package/dist/cjs/column/index.js +31 -18
- package/dist/cjs/column/utils.d.cts +133 -0
- package/dist/cjs/column/utils.js +34 -0
- package/dist/cjs/database/alter.d.cts +3 -2
- package/dist/cjs/database/alter.js +15 -15
- package/dist/cjs/database/column.d.cts +3 -2
- package/dist/cjs/database/column.js +9 -7
- package/dist/cjs/database/contract.d.cts +3 -2
- package/dist/cjs/database/index.d.cts +3 -2
- package/dist/cjs/database/index.js +15 -2
- package/dist/cjs/database/table.d.cts +3 -2
- package/dist/cjs/database/table.js +12 -4
- package/dist/cjs/database/types.d.cts +2 -1
- package/dist/cjs/database/wrapper.d.cts +17 -21
- package/dist/cjs/database/wrapper.js +28 -52
- package/dist/cjs/index-CHxuUiO4.d.cts +472 -0
- package/dist/cjs/{index---zaMa69.d.cts → index-CZhrzE5r.d.cts} +5 -3
- package/dist/cjs/index.d.cts +3 -2
- package/dist/cjs/migration/index.d.cts +3 -2
- package/dist/cjs/migration/runner.js +1 -1
- package/dist/cjs/migration/type.d.cts +3 -2
- package/dist/cjs/query/builder.d.cts +3 -2
- package/dist/cjs/query/builder.js +1 -1
- package/dist/cjs/query/condition/common.d.cts +41 -0
- package/dist/cjs/query/condition/common.js +62 -0
- package/dist/cjs/query/condition/core.d.cts +8 -0
- package/dist/cjs/query/condition/core.js +57 -0
- package/dist/cjs/query/condition/index.d.cts +10 -0
- package/dist/cjs/query/condition/index.js +33 -0
- package/dist/cjs/query/condition/not.d.cts +34 -0
- package/dist/cjs/query/condition/not.js +51 -0
- package/dist/cjs/query/condition/raw.d.cts +8 -0
- package/dist/cjs/query/condition/raw.js +54 -0
- package/dist/cjs/query/constants.d.cts +27 -1
- package/dist/cjs/query/constants.js +28 -2
- package/dist/cjs/query/contract.d.cts +4 -3
- 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 -2
- package/dist/cjs/query/helper.js +1 -1
- package/dist/cjs/query/index.d.cts +3 -2
- package/dist/cjs/query/index.js +69 -65
- package/dist/cjs/query/join.d.cts +18 -5
- package/dist/cjs/query/join.js +38 -9
- package/dist/cjs/query/sql.d.cts +9 -6
- package/dist/cjs/query/sql.js +80 -21
- package/dist/cjs/query/types.d.cts +2 -1
- package/dist/cjs/query/utilities.d.cts +7 -3
- package/dist/cjs/query/utilities.js +86 -5
- package/dist/cjs/table/constants.d.cts +1 -0
- package/dist/cjs/table/constants.js +1 -0
- package/dist/cjs/table/index.d.cts +3 -2
- package/dist/cjs/table/index.js +29 -9
- package/dist/cjs/table/types.d.cts +3 -2
- package/dist/cjs/table/utilities.d.cts +2 -1
- 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/constants.d.ts +58 -5
- package/dist/esm/column/constants.js +78 -23
- package/dist/esm/column/index.d.ts +1 -0
- package/dist/esm/column/index.js +31 -18
- package/dist/esm/column/utils.d.ts +133 -0
- package/dist/esm/column/utils.js +32 -0
- package/dist/esm/database/alter.d.ts +3 -2
- package/dist/esm/database/alter.js +15 -15
- package/dist/esm/database/column.d.ts +3 -2
- package/dist/esm/database/column.js +9 -7
- package/dist/esm/database/contract.d.ts +3 -2
- package/dist/esm/database/index.d.ts +3 -2
- package/dist/esm/database/index.js +17 -4
- package/dist/esm/database/table.d.ts +3 -2
- package/dist/esm/database/table.js +12 -4
- package/dist/esm/database/types.d.ts +2 -1
- package/dist/esm/database/wrapper.d.ts +17 -21
- package/dist/esm/database/wrapper.js +27 -53
- package/dist/esm/index-CjurLJdK.d.ts +472 -0
- package/dist/esm/{index-DFrpzXEn.d.ts → index-DgOs61lH.d.ts} +5 -3
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/migration/index.d.ts +3 -2
- package/dist/esm/migration/runner.js +1 -1
- package/dist/esm/migration/type.d.ts +3 -2
- package/dist/esm/query/builder.d.ts +3 -2
- package/dist/esm/query/builder.js +1 -1
- package/dist/esm/query/condition/common.d.ts +41 -0
- package/dist/esm/query/condition/common.js +56 -0
- package/dist/esm/query/condition/core.d.ts +8 -0
- package/dist/esm/query/condition/core.js +55 -0
- package/dist/esm/query/condition/index.d.ts +10 -0
- package/dist/esm/query/condition/index.js +4 -0
- package/dist/esm/query/condition/not.d.ts +34 -0
- package/dist/esm/query/condition/not.js +46 -0
- package/dist/esm/query/condition/raw.d.ts +8 -0
- package/dist/esm/query/condition/raw.js +50 -0
- package/dist/esm/query/constants.d.ts +27 -1
- package/dist/esm/query/constants.js +27 -3
- package/dist/esm/query/contract.d.ts +4 -3
- 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 -2
- package/dist/esm/query/helper.js +2 -2
- package/dist/esm/query/index.d.ts +3 -2
- package/dist/esm/query/index.js +72 -68
- package/dist/esm/query/join.d.ts +18 -5
- package/dist/esm/query/join.js +37 -9
- package/dist/esm/query/sql.d.ts +9 -6
- package/dist/esm/query/sql.js +80 -23
- package/dist/esm/query/types.d.ts +2 -1
- package/dist/esm/query/utilities.d.ts +7 -3
- package/dist/esm/query/utilities.js +86 -6
- package/dist/esm/table/constants.d.ts +1 -0
- package/dist/esm/table/constants.js +1 -0
- package/dist/esm/table/index.d.ts +3 -2
- package/dist/esm/table/index.js +29 -9
- package/dist/esm/table/types.d.ts +3 -2
- package/dist/esm/table/utilities.d.ts +2 -1
- 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 +8 -2
- package/dist/cjs/index-CwiFQh0I.d.cts +0 -358
- package/dist/cjs/query/condition.d.cts +0 -7
- package/dist/cjs/query/condition.js +0 -106
- package/dist/esm/index-FMT0YEO7.d.ts +0 -358
- package/dist/esm/query/condition.d.ts +0 -7
- package/dist/esm/query/condition.js +0 -98
package/dist/cjs/query/index.js
CHANGED
|
@@ -17,6 +17,8 @@ class QueryBuilder {
|
|
|
17
17
|
clone;
|
|
18
18
|
toQuery;
|
|
19
19
|
toString;
|
|
20
|
+
toDebugString;
|
|
21
|
+
explain;
|
|
20
22
|
exec;
|
|
21
23
|
rawWhere;
|
|
22
24
|
rawAnd;
|
|
@@ -25,33 +27,23 @@ class QueryBuilder {
|
|
|
25
27
|
where;
|
|
26
28
|
and;
|
|
27
29
|
or;
|
|
30
|
+
on;
|
|
28
31
|
having;
|
|
32
|
+
whereGroup;
|
|
33
|
+
orGroup;
|
|
34
|
+
not;
|
|
29
35
|
constructor(table) {
|
|
30
36
|
this.hooks = {};
|
|
31
37
|
this.table = table;
|
|
32
38
|
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
|
|
39
|
+
baseAlias: table.name
|
|
50
40
|
};
|
|
51
41
|
this.alias = helper.alias.bind(this);
|
|
52
42
|
this.clone = helper.clone.bind(this);
|
|
53
43
|
this.toQuery = sql.toQuery.bind(this);
|
|
54
44
|
this.toString = sql.toString.bind(this);
|
|
45
|
+
this.toDebugString = sql.toDebugString.bind(this);
|
|
46
|
+
this.explain = sql.explain.bind(this);
|
|
55
47
|
this.exec = sql.exec.bind(this);
|
|
56
48
|
this.rawWhere = condition.rawWhere.bind(this);
|
|
57
49
|
this.rawHaving = condition.rawHaving.bind(this);
|
|
@@ -61,46 +53,36 @@ class QueryBuilder {
|
|
|
61
53
|
this.having = condition.having.bind(this);
|
|
62
54
|
this.and = this.where;
|
|
63
55
|
this.or = condition.or.bind(this);
|
|
56
|
+
this.on = condition.on.bind(this);
|
|
57
|
+
this.whereGroup = condition.whereGroup.bind(this);
|
|
58
|
+
this.orGroup = condition.orGroup.bind(this);
|
|
59
|
+
this.not = {
|
|
60
|
+
where: condition.whereNot.bind(this),
|
|
61
|
+
having: condition.havingNot.bind(this),
|
|
62
|
+
or: condition.orNot.bind(this),
|
|
63
|
+
whereGroup: condition.whereNotGroup.bind(
|
|
64
|
+
this
|
|
65
|
+
),
|
|
66
|
+
orGroup: condition.orNotGroup.bind(this)
|
|
67
|
+
};
|
|
64
68
|
}
|
|
65
|
-
leftJoin(joinTable, alias2
|
|
66
|
-
return join.
|
|
67
|
-
this,
|
|
68
|
-
constants.AcceptedJoin.LEFT,
|
|
69
|
-
alias2,
|
|
70
|
-
joinTable,
|
|
71
|
-
baseColumn,
|
|
72
|
-
joinColumn
|
|
73
|
-
);
|
|
69
|
+
leftJoin(joinTable, alias2) {
|
|
70
|
+
return join.prepareJoin(this, constants.AcceptedJoin.LEFT, joinTable, alias2);
|
|
74
71
|
}
|
|
75
|
-
rightJoin(joinTable, alias2
|
|
76
|
-
return join.
|
|
77
|
-
this,
|
|
78
|
-
constants.AcceptedJoin.RIGHT,
|
|
79
|
-
alias2,
|
|
80
|
-
joinTable,
|
|
81
|
-
baseColumn,
|
|
82
|
-
joinColumn
|
|
83
|
-
);
|
|
72
|
+
rightJoin(joinTable, alias2) {
|
|
73
|
+
return join.prepareJoin(this, constants.AcceptedJoin.RIGHT, joinTable, alias2);
|
|
84
74
|
}
|
|
85
|
-
innerJoin(joinTable, alias2
|
|
86
|
-
return join.
|
|
87
|
-
this,
|
|
88
|
-
constants.AcceptedJoin.INNER,
|
|
89
|
-
alias2,
|
|
90
|
-
joinTable,
|
|
91
|
-
baseColumn,
|
|
92
|
-
joinColumn
|
|
93
|
-
);
|
|
75
|
+
innerJoin(joinTable, alias2) {
|
|
76
|
+
return join.prepareJoin(this, constants.AcceptedJoin.INNER, joinTable, alias2);
|
|
94
77
|
}
|
|
95
|
-
|
|
96
|
-
return join.
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
);
|
|
78
|
+
fullJoin(joinTable, alias2) {
|
|
79
|
+
return join.prepareJoin(this, constants.AcceptedJoin.FULL, joinTable, alias2);
|
|
80
|
+
}
|
|
81
|
+
crossJoin(joinTable, alias2) {
|
|
82
|
+
return join.addNoOnJoin(this, constants.AcceptedJoin.CROSS, joinTable, alias2);
|
|
83
|
+
}
|
|
84
|
+
naturalJoin(joinTable, alias2) {
|
|
85
|
+
return join.prepareJoin(this, constants.AcceptedJoin.NATURAL, joinTable, alias2);
|
|
104
86
|
}
|
|
105
87
|
distinct() {
|
|
106
88
|
this.definition.distinct = true;
|
|
@@ -154,22 +136,33 @@ class QueryBuilder {
|
|
|
154
136
|
insert(...values) {
|
|
155
137
|
this.definition.queryType = constants.QueryType.INSERT;
|
|
156
138
|
if (!this.definition.insertValues) this.definition.insertValues = [];
|
|
157
|
-
const {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
139
|
+
const {
|
|
140
|
+
isWithTimestamp,
|
|
141
|
+
isHasCreatedAt,
|
|
142
|
+
isHasUpdatedAt,
|
|
143
|
+
createdAt,
|
|
144
|
+
updatedAt,
|
|
145
|
+
timestamp
|
|
146
|
+
} = utilities$1.getTimestamp(this.table);
|
|
147
|
+
values = values.map((row) => {
|
|
148
|
+
const fields = {};
|
|
149
|
+
for (const key in this.table.columns) {
|
|
150
|
+
fields[key] = row[key] ?? null;
|
|
151
|
+
}
|
|
152
|
+
if (isWithTimestamp && isHasCreatedAt) {
|
|
153
|
+
fields[createdAt] = row[createdAt] ?? timestamp;
|
|
154
|
+
}
|
|
155
|
+
if (isWithTimestamp && isHasUpdatedAt) {
|
|
156
|
+
fields[updatedAt] = row[updatedAt] ?? timestamp;
|
|
157
|
+
}
|
|
158
|
+
return fields;
|
|
159
|
+
});
|
|
167
160
|
this.definition.insertValues = values;
|
|
168
161
|
return this;
|
|
169
162
|
}
|
|
170
163
|
update(values) {
|
|
171
|
-
const { isWithTimestamp, updatedAt, timestamp } = utilities$1.getTimestamp(this.table);
|
|
172
|
-
if (isWithTimestamp) {
|
|
164
|
+
const { isWithTimestamp, isHasUpdatedAt, updatedAt, timestamp } = utilities$1.getTimestamp(this.table);
|
|
165
|
+
if (isWithTimestamp && isHasUpdatedAt) {
|
|
173
166
|
values = {
|
|
174
167
|
...values,
|
|
175
168
|
[updatedAt]: values[updatedAt] ?? timestamp
|
|
@@ -189,6 +182,17 @@ class QueryBuilder {
|
|
|
189
182
|
this.definition.queryType = constants.QueryType.DELETE;
|
|
190
183
|
return this;
|
|
191
184
|
}
|
|
185
|
+
paginate(page, size) {
|
|
186
|
+
if (page < 1) {
|
|
187
|
+
throw new Error("Page number must be at least 1");
|
|
188
|
+
}
|
|
189
|
+
if (size < 1) {
|
|
190
|
+
throw new Error("Page size must be at least 1");
|
|
191
|
+
}
|
|
192
|
+
this.definition.limit = size;
|
|
193
|
+
this.definition.offset = (page - 1) * size;
|
|
194
|
+
return this;
|
|
195
|
+
}
|
|
192
196
|
infer() {
|
|
193
197
|
return null;
|
|
194
198
|
}
|
|
@@ -1,16 +1,29 @@
|
|
|
1
|
-
import { T as Table, Q as QueryDefinition, C as ColumnSelector,
|
|
1
|
+
import { T as Table, Q as QueryDefinition, C as ColumnSelector, k as StrictColumnSelector, l as QueryBuilder, n as QueryTransformerContract, o as QueryConditionContract } from '../index-CHxuUiO4.cjs';
|
|
2
2
|
import { Column } from '../column/index.cjs';
|
|
3
3
|
import { AcceptedJoin } from './constants.cjs';
|
|
4
|
-
import '
|
|
4
|
+
import 'bun';
|
|
5
5
|
import '../types.cjs';
|
|
6
|
+
import '../column/constants.cjs';
|
|
6
7
|
import '../table/constants.cjs';
|
|
7
8
|
import '../column/types.cjs';
|
|
8
9
|
|
|
9
|
-
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 & {
|
|
10
11
|
[K in JoinAlias]: JoinTable;
|
|
11
|
-
}>(query: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, joinType: JoinType,
|
|
12
|
+
}>(query: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, joinType: JoinType, joinTable: JoinTable, alias: JoinAlias): QueryBuilder<Alias, TableRef, FinalJoinedTables, Omit<Definition, "joins" | "joinedTables"> & {
|
|
12
13
|
joins: string[];
|
|
13
14
|
joinedTables: FinalJoinedTables;
|
|
14
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: string[];
|
|
19
|
+
joinedTables: ReturnedJoinedTables;
|
|
20
|
+
}, ColumnSelector<Alias, TableRef, ReturnedJoinedTables>, StrictColumnSelector<Alias, TableRef, ReturnedJoinedTables>, QueryTransformerContract<Alias, TableRef, ReturnedJoinedTables, Omit<Definition, "joins" | "joinedTables"> & {
|
|
21
|
+
joins: string[];
|
|
22
|
+
joinedTables: ReturnedJoinedTables;
|
|
23
|
+
}, ColumnSelector<Alias, TableRef, ReturnedJoinedTables>, StrictColumnSelector<Alias, TableRef, ReturnedJoinedTables>>, QueryConditionContract<Alias, TableRef, ReturnedJoinedTables, Omit<Definition, "joins" | "joinedTables"> & {
|
|
24
|
+
joins: string[];
|
|
25
|
+
joinedTables: ReturnedJoinedTables;
|
|
26
|
+
}, ColumnSelector<Alias, TableRef, ReturnedJoinedTables>, StrictColumnSelector<Alias, TableRef, ReturnedJoinedTables>>>;
|
|
27
|
+
};
|
|
15
28
|
|
|
16
|
-
export {
|
|
29
|
+
export { addNoOnJoin, prepareJoin };
|
package/dist/cjs/query/join.js
CHANGED
|
@@ -1,18 +1,47 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
var _ = require('.');
|
|
4
|
+
|
|
5
|
+
function addNoOnJoin(query, joinType, joinTable, alias) {
|
|
4
6
|
if (!query.definition.joins) query.definition.joins = [];
|
|
5
|
-
query.definition.joins.push(
|
|
6
|
-
`${joinType} JOIN ${joinTable.name} AS ${alias} ON ${baseColumn} = ${joinColumn}`
|
|
7
|
-
);
|
|
7
|
+
query.definition.joins.push(`${joinType} JOIN ${joinTable.name} AS ${alias}`);
|
|
8
8
|
if (!query.definition.joinedTables) {
|
|
9
9
|
query.definition.joinedTables = {};
|
|
10
10
|
}
|
|
11
|
-
query.definition.joinedTables =
|
|
12
|
-
...query.definition.joinedTables,
|
|
13
|
-
[alias]: joinTable
|
|
14
|
-
};
|
|
11
|
+
query.definition.joinedTables[alias] = joinTable;
|
|
15
12
|
return query;
|
|
16
13
|
}
|
|
14
|
+
function prepareJoin(query, joinType, joinTable, alias) {
|
|
15
|
+
return {
|
|
16
|
+
on(callback) {
|
|
17
|
+
const sub = callback(new _.QueryBuilder(query.table));
|
|
18
|
+
const subDef = sub.definition;
|
|
19
|
+
if (!subDef.where?.length) {
|
|
20
|
+
return query;
|
|
21
|
+
}
|
|
22
|
+
const grouped = `(${subDef.where.join(" ")})`;
|
|
23
|
+
if (!query.definition.joins) query.definition.joins = [];
|
|
24
|
+
query.definition.joins.push(
|
|
25
|
+
`${joinType} JOIN ${joinTable.name} AS ${alias} ON ${grouped}`
|
|
26
|
+
);
|
|
27
|
+
if (subDef.joins?.length) {
|
|
28
|
+
query.definition.joins.push(...subDef.joins);
|
|
29
|
+
}
|
|
30
|
+
if (subDef.params?.length) {
|
|
31
|
+
if (!query.definition.params) query.definition.params = [];
|
|
32
|
+
query.definition.params.push(...subDef.params);
|
|
33
|
+
}
|
|
34
|
+
if (!query.definition.joinedTables) {
|
|
35
|
+
query.definition.joinedTables = {};
|
|
36
|
+
}
|
|
37
|
+
query.definition.joinedTables[alias] = joinTable;
|
|
38
|
+
if (subDef.joinedTables) {
|
|
39
|
+
Object.assign(query.definition.joinedTables, subDef.joinedTables);
|
|
40
|
+
}
|
|
41
|
+
return query;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
17
45
|
|
|
18
|
-
exports.
|
|
46
|
+
exports.addNoOnJoin = addNoOnJoin;
|
|
47
|
+
exports.prepareJoin = prepareJoin;
|
package/dist/cjs/query/sql.d.cts
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TransactionSQL } from 'bun';
|
|
2
|
+
import { T as Table, Q as QueryDefinition, C as ColumnSelector, k as StrictColumnSelector, l as QueryBuilder, E as ExplainOptions } from '../index-CHxuUiO4.cjs';
|
|
2
3
|
import { Column } from '../column/index.cjs';
|
|
3
|
-
import '../
|
|
4
|
+
import { Dialect } from '../table/constants.cjs';
|
|
4
5
|
import '../types.cjs';
|
|
5
6
|
import './constants.cjs';
|
|
6
|
-
import '../
|
|
7
|
+
import '../column/constants.cjs';
|
|
7
8
|
import '../column/types.cjs';
|
|
8
9
|
|
|
9
10
|
declare function buildQuery(query: string): string;
|
|
10
|
-
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): {
|
|
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): {
|
|
11
12
|
query: string;
|
|
12
13
|
params: unknown[] | null | undefined;
|
|
13
14
|
};
|
|
14
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;
|
|
15
|
-
declare function
|
|
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>;
|
|
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>;
|
|
16
19
|
|
|
17
|
-
export { buildQuery, exec, toQuery, toString };
|
|
20
|
+
export { buildQuery, exec, explain, toDebugString, toQuery, toString };
|
package/dist/cjs/query/sql.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var constants$1 = require('../table/constants');
|
|
3
4
|
var builder = require('./builder');
|
|
4
5
|
var constants = require('./constants');
|
|
6
|
+
var explain$1 = require('./explain');
|
|
5
7
|
var utilities = require('./utilities');
|
|
6
8
|
|
|
7
9
|
function buildQuery(query) {
|
|
@@ -11,80 +13,135 @@ function buildQuery(query) {
|
|
|
11
13
|
return `$${index}`;
|
|
12
14
|
});
|
|
13
15
|
}
|
|
14
|
-
function toQuery() {
|
|
15
|
-
|
|
16
|
+
function toQuery(dialect) {
|
|
17
|
+
const parts = [];
|
|
16
18
|
switch (this.definition.queryType) {
|
|
17
19
|
case constants.QueryType.SELECT:
|
|
18
|
-
|
|
20
|
+
parts.push(builder.buildSelectQuery(this));
|
|
19
21
|
break;
|
|
20
22
|
case constants.QueryType.INSERT:
|
|
21
|
-
|
|
23
|
+
parts.push(builder.buildInsertQuery(this));
|
|
22
24
|
break;
|
|
23
25
|
case constants.QueryType.UPDATE:
|
|
24
|
-
|
|
26
|
+
parts.push(builder.buildUpdateQuery(this));
|
|
25
27
|
break;
|
|
26
28
|
case constants.QueryType.DELETE:
|
|
27
|
-
|
|
29
|
+
parts.push(builder.buildDeleteQuery(this));
|
|
28
30
|
break;
|
|
29
31
|
default:
|
|
30
32
|
throw new Error("No query type defined");
|
|
31
33
|
}
|
|
32
34
|
if (this.definition?.joins?.length) {
|
|
33
|
-
|
|
35
|
+
parts.push(this.definition.joins.join(" "));
|
|
34
36
|
}
|
|
35
37
|
const whereConditions = utilities.getWhereConditions(this);
|
|
36
38
|
if (whereConditions.length) {
|
|
37
|
-
|
|
39
|
+
parts.push(`WHERE ${whereConditions.join(" ")}`);
|
|
38
40
|
}
|
|
39
41
|
const groupByConditions = utilities.getGroupByConditions(this);
|
|
40
42
|
if (groupByConditions.length) {
|
|
41
|
-
|
|
43
|
+
parts.push(`GROUP BY ${groupByConditions.join(", ")}`);
|
|
42
44
|
}
|
|
43
45
|
if (this.definition?.having?.length) {
|
|
44
|
-
|
|
46
|
+
parts.push(`HAVING ${this.definition.having.join(" ")}`);
|
|
45
47
|
}
|
|
46
48
|
if (this.definition?.orderBy?.length) {
|
|
47
|
-
|
|
49
|
+
parts.push(
|
|
50
|
+
`ORDER BY ${this.definition.orderBy.map((order) => `${order.column} ${order.direction}`).join(", ")}`
|
|
51
|
+
);
|
|
48
52
|
}
|
|
49
53
|
if (this.definition?.limit !== null) {
|
|
50
|
-
|
|
54
|
+
parts.push("LIMIT ?");
|
|
51
55
|
if (!this.definition.params) this.definition.params = [];
|
|
52
56
|
this.definition.params.push(this.definition.limit);
|
|
53
57
|
}
|
|
54
58
|
if (this.definition?.offset !== null) {
|
|
55
|
-
|
|
59
|
+
parts.push("OFFSET ?");
|
|
56
60
|
if (!this.definition.params) this.definition.params = [];
|
|
57
61
|
this.definition.params.push(this.definition.offset);
|
|
58
62
|
}
|
|
59
63
|
if (this.definition.queryType === constants.QueryType.UPDATE || this.definition.queryType === constants.QueryType.DELETE) {
|
|
60
|
-
|
|
64
|
+
if (dialect !== constants$1.Dialect.MYSQL) {
|
|
65
|
+
parts.push("RETURNING *");
|
|
66
|
+
}
|
|
61
67
|
}
|
|
62
|
-
sql = buildQuery(
|
|
63
|
-
return { query: sql
|
|
68
|
+
const sql = buildQuery(parts.join(" "));
|
|
69
|
+
return { query: `${sql};`, params: this.definition.params };
|
|
64
70
|
}
|
|
65
71
|
function toString() {
|
|
66
72
|
return this.toQuery().query;
|
|
67
73
|
}
|
|
68
|
-
|
|
69
|
-
|
|
74
|
+
function toDebugString() {
|
|
75
|
+
const { query, params } = this.toQuery();
|
|
76
|
+
if (!params || params.length === 0) {
|
|
77
|
+
return query;
|
|
78
|
+
}
|
|
79
|
+
let debugQuery = query;
|
|
80
|
+
utilities.sanitizeParams(params).forEach((param, index) => {
|
|
81
|
+
const value = param === null ? "NULL" : `'${String(param).replace(/'/g, "''")}'`;
|
|
82
|
+
debugQuery = debugQuery.replace(
|
|
83
|
+
new RegExp(`\\$${index + 1}\\b`, "g"),
|
|
84
|
+
value
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
return debugQuery;
|
|
88
|
+
}
|
|
89
|
+
function explain(options = {}) {
|
|
70
90
|
const { query, params } = this.toQuery();
|
|
91
|
+
const explainPrefix = explain$1.buildExplainQuery(this, options);
|
|
92
|
+
if (!this.table.client) {
|
|
93
|
+
throw new Error("Database client not defined");
|
|
94
|
+
}
|
|
95
|
+
return this.table.client.exec({
|
|
96
|
+
sql: `${explainPrefix}${query}`,
|
|
97
|
+
params
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
async function exec(tx) {
|
|
101
|
+
const client = this.table.client;
|
|
102
|
+
const dialect = this.table.dialect;
|
|
103
|
+
const queryType = this.definition.queryType;
|
|
104
|
+
const isUpdate = queryType === constants.QueryType.UPDATE;
|
|
105
|
+
const isDelete = queryType === constants.QueryType.DELETE;
|
|
106
|
+
const isReturning = isUpdate || isDelete;
|
|
107
|
+
const isMySQL = dialect === constants$1.Dialect.MYSQL;
|
|
108
|
+
if (!client) {
|
|
109
|
+
throw new Error("Database client not defined");
|
|
110
|
+
}
|
|
111
|
+
if (!queryType) {
|
|
112
|
+
throw new Error("No query type defined");
|
|
113
|
+
}
|
|
114
|
+
const { query, params } = this.toQuery(dialect);
|
|
71
115
|
if (this.hooks?.before?.size) {
|
|
72
116
|
for (const hook of this.hooks.before.values()) {
|
|
73
117
|
hook({
|
|
74
118
|
query,
|
|
75
119
|
params,
|
|
76
|
-
type:
|
|
120
|
+
type: queryType,
|
|
77
121
|
hook: constants.QueryHooksType.BEFORE
|
|
78
122
|
});
|
|
79
123
|
}
|
|
80
124
|
}
|
|
81
|
-
|
|
125
|
+
let result = await client.exec({
|
|
126
|
+
sql: query,
|
|
127
|
+
params,
|
|
128
|
+
tx
|
|
129
|
+
});
|
|
130
|
+
if (isMySQL && isReturning) {
|
|
131
|
+
const query2 = this.clone();
|
|
132
|
+
query2.definition.queryType = constants.QueryType.SELECT;
|
|
133
|
+
result = await client.exec({
|
|
134
|
+
sql: query2.toQuery().query,
|
|
135
|
+
params,
|
|
136
|
+
tx
|
|
137
|
+
});
|
|
138
|
+
}
|
|
82
139
|
if (this.hooks?.after?.size) {
|
|
83
140
|
for (const hook of this.hooks.after.values()) {
|
|
84
141
|
hook({
|
|
85
142
|
query,
|
|
86
143
|
params,
|
|
87
|
-
type:
|
|
144
|
+
type: queryType,
|
|
88
145
|
hook: constants.QueryHooksType.AFTER
|
|
89
146
|
});
|
|
90
147
|
}
|
|
@@ -100,5 +157,7 @@ async function exec() {
|
|
|
100
157
|
|
|
101
158
|
exports.buildQuery = buildQuery;
|
|
102
159
|
exports.exec = exec;
|
|
160
|
+
exports.explain = explain;
|
|
161
|
+
exports.toDebugString = toDebugString;
|
|
103
162
|
exports.toQuery = toQuery;
|
|
104
163
|
exports.toString = toString;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import 'bun';
|
|
1
2
|
import '../column/index.cjs';
|
|
2
3
|
import '../column/constants.cjs';
|
|
3
|
-
export {
|
|
4
|
+
export { u as AcceptedInsertValues, t as AcceptedOrderBy, v as AcceptedUpdateValues, w as AggregateColumn, A as AliasedColumn, C as ColumnSelector, E as ExplainOptions, Q as QueryDefinition, p as QueryHooks, y as QueryOutput, s as QueryRunHooks, z as QueryRunHooksOptions, R as RawColumn, x as SelectQueryOutput, m as SelectableColumn, k as StrictColumnSelector, W as WhereValue } from '../index-CHxuUiO4.cjs';
|
|
4
5
|
import '../types.cjs';
|
|
5
6
|
import './constants.cjs';
|
|
6
7
|
import '../table/constants.cjs';
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { T as Table, W as WhereValue, Q as QueryDefinition, C as ColumnSelector,
|
|
1
|
+
import { T as Table, W as WhereValue, Q as QueryDefinition, C as ColumnSelector, k as StrictColumnSelector, l as QueryBuilder, A as AliasedColumn, m as SelectableColumn } from '../index-CHxuUiO4.cjs';
|
|
2
2
|
import { Column } from '../column/index.cjs';
|
|
3
3
|
import { Dialect } from '../table/constants.cjs';
|
|
4
4
|
import { AcceptedOperator } from './constants.cjs';
|
|
5
|
-
import '
|
|
5
|
+
import 'bun';
|
|
6
6
|
import '../types.cjs';
|
|
7
|
+
import '../column/constants.cjs';
|
|
7
8
|
import '../column/types.cjs';
|
|
8
9
|
|
|
9
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): {
|
|
@@ -16,6 +17,8 @@ declare function getTimestamp<TableRef extends Table<string, Record<string, Colu
|
|
|
16
17
|
timestamp: Date;
|
|
17
18
|
createdAt: string;
|
|
18
19
|
updatedAt: string;
|
|
20
|
+
isHasUpdatedAt: boolean;
|
|
21
|
+
isHasCreatedAt: boolean;
|
|
19
22
|
};
|
|
20
23
|
declare function getParanoid<TableRef extends Table<string, Record<string, Column>>>(table: TableRef): {
|
|
21
24
|
isWithParanoid: boolean;
|
|
@@ -30,5 +33,6 @@ declare function parseAliasedRow({ row, selects, root, }: {
|
|
|
30
33
|
selects: SelectableColumn<string>[];
|
|
31
34
|
root?: string | null;
|
|
32
35
|
}): Record<string, any>;
|
|
36
|
+
declare function sanitizeParams(params: unknown[]): (string | number | bigint | boolean | null)[];
|
|
33
37
|
|
|
34
|
-
export { getCondition, getGroupByConditions, getParanoid, getTableColumnNames, getTableSelectName, getTimestamp, getWhereConditions, parseAliasedRow };
|
|
38
|
+
export { getCondition, getGroupByConditions, getParanoid, getTableColumnNames, getTableSelectName, getTimestamp, getWhereConditions, parseAliasedRow, sanitizeParams };
|
|
@@ -55,6 +55,58 @@ function getCondition(dialect, column, operator, value) {
|
|
|
55
55
|
return `${column} BETWEEN ? AND ?`;
|
|
56
56
|
case constants.AcceptedOperator.NOT_BETWEEN:
|
|
57
57
|
return `${column} NOT BETWEEN ? AND ?`;
|
|
58
|
+
case constants.AcceptedOperator.STARTS_WITH:
|
|
59
|
+
return `${column} LIKE ?`;
|
|
60
|
+
case constants.AcceptedOperator.ENDS_WITH:
|
|
61
|
+
return `${column} LIKE ?`;
|
|
62
|
+
case constants.AcceptedOperator.REG_EXP: {
|
|
63
|
+
switch (dialect) {
|
|
64
|
+
case constants$1.Dialect.POSTGRES:
|
|
65
|
+
return `${column} ~ ?`;
|
|
66
|
+
case constants$1.Dialect.MYSQL:
|
|
67
|
+
return `${column} REGEXP ?`;
|
|
68
|
+
case constants$1.Dialect.SQLITE:
|
|
69
|
+
return `${column} GLOB ?`;
|
|
70
|
+
default:
|
|
71
|
+
throw new Error("Operator not supported");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
case constants.AcceptedOperator.NOT_REG_EXP: {
|
|
75
|
+
switch (dialect) {
|
|
76
|
+
case constants$1.Dialect.POSTGRES:
|
|
77
|
+
return `${column} !~ ?`;
|
|
78
|
+
case constants$1.Dialect.MYSQL:
|
|
79
|
+
return `${column} NOT REGEXP ?`;
|
|
80
|
+
case constants$1.Dialect.SQLITE:
|
|
81
|
+
return `${column} NOT GLOB ?`;
|
|
82
|
+
default:
|
|
83
|
+
throw new Error("Operator not supported");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
case constants.AcceptedOperator.RLIKE: {
|
|
87
|
+
switch (dialect) {
|
|
88
|
+
case constants$1.Dialect.POSTGRES:
|
|
89
|
+
return `${column} ~* ?`;
|
|
90
|
+
case constants$1.Dialect.MYSQL:
|
|
91
|
+
return `${column} RLIKE ?`;
|
|
92
|
+
case constants$1.Dialect.SQLITE:
|
|
93
|
+
return `${column} GLOB ?`;
|
|
94
|
+
default:
|
|
95
|
+
throw new Error("Operator not supported");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
case constants.AcceptedOperator.NOT_RLIKE: {
|
|
99
|
+
switch (dialect) {
|
|
100
|
+
case constants$1.Dialect.POSTGRES:
|
|
101
|
+
return `${column} !~* ?`;
|
|
102
|
+
case constants$1.Dialect.MYSQL:
|
|
103
|
+
return `${column} NOT RLIKE ?`;
|
|
104
|
+
case constants$1.Dialect.SQLITE:
|
|
105
|
+
return `${column} NOT GLOB ?`;
|
|
106
|
+
default:
|
|
107
|
+
throw new Error("Operator not supported");
|
|
108
|
+
}
|
|
109
|
+
}
|
|
58
110
|
default:
|
|
59
111
|
throw new Error("Invalid operator");
|
|
60
112
|
}
|
|
@@ -62,22 +114,32 @@ function getCondition(dialect, column, operator, value) {
|
|
|
62
114
|
function getTimestamp(table) {
|
|
63
115
|
const isWithTimestamp = !!table.timestamp;
|
|
64
116
|
const timestamp = /* @__PURE__ */ new Date();
|
|
117
|
+
let isHasCreatedAt = true;
|
|
118
|
+
let isHasUpdatedAt = true;
|
|
65
119
|
let createdAt = "createdAt";
|
|
66
120
|
let updatedAt = "updatedAt";
|
|
67
121
|
if (isWithTimestamp) {
|
|
68
122
|
const isCustomTimestamp = typeof table.timestamp === "object";
|
|
69
|
-
if (isCustomTimestamp
|
|
70
|
-
|
|
123
|
+
if (isCustomTimestamp) {
|
|
124
|
+
if (typeof table.timestamp.createdAt === "string") {
|
|
125
|
+
createdAt = table.timestamp.createdAt;
|
|
126
|
+
}
|
|
127
|
+
isHasCreatedAt = table.timestamp.createdAt !== false;
|
|
71
128
|
}
|
|
72
|
-
if (isCustomTimestamp
|
|
73
|
-
|
|
129
|
+
if (isCustomTimestamp) {
|
|
130
|
+
if (typeof table.timestamp.updatedAt === "string") {
|
|
131
|
+
updatedAt = table.timestamp.updatedAt;
|
|
132
|
+
}
|
|
133
|
+
isHasUpdatedAt = table.timestamp.updatedAt !== false;
|
|
74
134
|
}
|
|
75
135
|
}
|
|
76
136
|
return {
|
|
77
137
|
isWithTimestamp,
|
|
78
138
|
timestamp,
|
|
79
139
|
createdAt,
|
|
80
|
-
updatedAt
|
|
140
|
+
updatedAt,
|
|
141
|
+
isHasUpdatedAt,
|
|
142
|
+
isHasCreatedAt
|
|
81
143
|
};
|
|
82
144
|
}
|
|
83
145
|
function getParanoid(table) {
|
|
@@ -173,6 +235,24 @@ function parseAliasedRow({
|
|
|
173
235
|
}
|
|
174
236
|
return result;
|
|
175
237
|
}
|
|
238
|
+
function sanitizeParams(params) {
|
|
239
|
+
return params.map((param) => {
|
|
240
|
+
if (param === null || typeof param === "string" || typeof param === "number" || typeof param === "boolean" || typeof param === "bigint") {
|
|
241
|
+
return param;
|
|
242
|
+
}
|
|
243
|
+
if (param === void 0) {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
if (param instanceof Date) {
|
|
247
|
+
return param.toISOString();
|
|
248
|
+
}
|
|
249
|
+
try {
|
|
250
|
+
return JSON.stringify(param);
|
|
251
|
+
} catch {
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
}
|
|
176
256
|
|
|
177
257
|
exports.getCondition = getCondition;
|
|
178
258
|
exports.getGroupByConditions = getGroupByConditions;
|
|
@@ -182,3 +262,4 @@ exports.getTableSelectName = getTableSelectName;
|
|
|
182
262
|
exports.getTimestamp = getTimestamp;
|
|
183
263
|
exports.getWhereConditions = getWhereConditions;
|
|
184
264
|
exports.parseAliasedRow = parseAliasedRow;
|
|
265
|
+
exports.sanitizeParams = sanitizeParams;
|