@ignisia/sql 0.2.0 → 0.2.2
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/dist/cjs/column/constants.js +101 -0
- package/dist/cjs/column/index.d.cts +42 -0
- package/dist/cjs/column/index.js +107 -0
- package/dist/cjs/column/types.d.cts +43 -0
- package/dist/cjs/column/types.js +4 -0
- package/dist/cjs/database/alter.d.cts +52 -0
- package/dist/cjs/database/alter.js +93 -0
- package/dist/cjs/database/column.d.cts +24 -0
- package/dist/cjs/database/column.js +37 -0
- package/dist/cjs/database/contract.d.cts +8 -0
- package/dist/cjs/database/contract.js +2 -0
- package/dist/cjs/database/index.d.cts +8 -0
- package/dist/cjs/database/index.js +94 -0
- package/dist/cjs/database/table.d.cts +21 -0
- package/dist/cjs/database/table.js +41 -0
- package/dist/cjs/database/types.d.cts +7 -0
- package/dist/cjs/database/types.js +2 -0
- package/dist/cjs/database/wrapper.d.cts +36 -0
- package/dist/cjs/database/wrapper.js +95 -0
- package/dist/cjs/index---zaMa69.d.cts +103 -0
- package/dist/cjs/index-CwiFQh0I.d.cts +358 -0
- package/dist/cjs/index.d.cts +10 -0
- package/dist/cjs/index.js +30 -0
- package/dist/cjs/migration/index.d.cts +30 -0
- package/dist/cjs/migration/index.js +50 -0
- package/dist/cjs/migration/runner.js +58 -0
- package/dist/cjs/migration/type.d.cts +19 -0
- package/dist/cjs/migration/type.js +2 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/query/builder.d.cts +14 -0
- package/dist/cjs/query/builder.js +71 -0
- package/dist/cjs/query/condition.d.cts +7 -0
- package/dist/cjs/query/condition.js +106 -0
- package/dist/cjs/query/constants.js +64 -0
- package/dist/cjs/query/contract.d.cts +7 -0
- package/dist/cjs/query/contract.js +2 -0
- package/dist/cjs/query/helper.d.cts +7 -0
- package/dist/cjs/query/helper.js +36 -0
- package/dist/cjs/query/index.d.cts +7 -0
- package/dist/cjs/query/index.js +197 -0
- package/dist/cjs/query/join.d.cts +16 -0
- package/dist/cjs/query/join.js +18 -0
- package/dist/cjs/query/sql.d.cts +17 -0
- package/dist/cjs/query/sql.js +104 -0
- package/dist/cjs/query/types.d.cts +7 -0
- package/dist/cjs/query/types.js +2 -0
- package/dist/cjs/query/utilities.d.cts +34 -0
- package/dist/cjs/query/utilities.js +184 -0
- package/dist/cjs/table/constants.js +8 -0
- package/dist/cjs/table/index.d.cts +7 -0
- package/dist/cjs/table/index.js +54 -0
- package/dist/cjs/table/types.d.cts +7 -0
- package/dist/cjs/table/types.js +2 -0
- package/dist/cjs/table/utilities.d.cts +7 -0
- package/dist/cjs/table/utilities.js +55 -0
- package/dist/cjs/types.js +2 -0
- package/dist/cjs/utilities.js +21 -0
- package/dist/esm/column/constants.d.ts +97 -0
- package/dist/{chunk-G3LSCLIQ.js → esm/column/constants.js} +2 -7
- package/dist/{chunk-GY7R637S.js → esm/column/index.js} +3 -10
- package/dist/esm/column/types.js +1 -0
- package/dist/{chunk-V4OMHVJN.js → esm/database/alter.js} +2 -10
- package/dist/{chunk-JF7OSNH4.js → esm/database/column.js} +2 -8
- package/dist/esm/database/contract.js +1 -0
- package/dist/{chunk-UI7U54OT.js → esm/database/index.js} +7 -26
- package/dist/{chunk-OYM2PNYZ.js → esm/database/table.js} +2 -8
- package/dist/esm/database/types.js +1 -0
- package/dist/{chunk-HKTHKQLK.js → esm/database/wrapper.js} +6 -11
- package/dist/esm/index.js +5 -0
- package/dist/{chunk-CIWX3UCZ.js → esm/migration/index.js} +1 -3
- package/dist/esm/migration/runner.d.ts +3 -0
- package/dist/{migration → esm/migration}/runner.js +4 -16
- package/dist/esm/migration/type.js +1 -0
- package/dist/esm/query/builder.js +67 -0
- package/dist/esm/query/condition.js +98 -0
- package/dist/esm/query/constants.d.ts +63 -0
- package/dist/{chunk-62FKD35V.js → esm/query/constants.js} +1 -10
- package/dist/esm/query/contract.js +1 -0
- package/dist/esm/query/helper.js +31 -0
- package/dist/esm/query/index.js +196 -0
- package/dist/{chunk-FYSNJAGD.js → esm/query/join.js} +1 -3
- package/dist/esm/query/sql.js +100 -0
- package/dist/esm/query/types.js +1 -0
- package/dist/esm/query/utilities.js +176 -0
- package/dist/esm/table/constants.d.ts +7 -0
- package/dist/{chunk-GLOHF5CP.js → esm/table/constants.js} +1 -3
- package/dist/{chunk-KVCIOW7L.js → esm/table/index.js} +3 -9
- package/dist/esm/table/types.js +1 -0
- package/dist/{chunk-WVJGTZFI.js → esm/table/utilities.js} +2 -11
- package/dist/esm/types.d.ts +3 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/utilities.d.ts +4 -0
- package/dist/{chunk-Y7FSRHH3.js → esm/utilities.js} +1 -4
- package/package.json +62 -4
- package/dist/chunk-EIUC7HJS.js +0 -686
- package/dist/column/constants.js +0 -9
- package/dist/column/index.js +0 -8
- package/dist/column/types.js +0 -2
- package/dist/database/alter.js +0 -15
- package/dist/database/column.js +0 -11
- package/dist/database/contract.js +0 -0
- package/dist/database/index.js +0 -19
- package/dist/database/table.js +0 -19
- package/dist/database/types.js +0 -0
- package/dist/database/wrapper.js +0 -9
- package/dist/index.js +0 -32
- package/dist/migration/index.js +0 -6
- package/dist/migration/type.js +0 -0
- package/dist/query/builder.js +0 -16
- package/dist/query/condition.js +0 -24
- package/dist/query/constants.js +0 -20
- package/dist/query/contract.js +0 -0
- package/dist/query/helper.js +0 -18
- package/dist/query/index.js +0 -10
- package/dist/query/join.js +0 -6
- package/dist/query/sql.js +0 -16
- package/dist/query/types.js +0 -0
- package/dist/query/utilities.js +0 -24
- package/dist/table/constants.js +0 -6
- package/dist/table/index.js +0 -14
- package/dist/table/types.js +0 -0
- package/dist/table/utilities.js +0 -15
- package/dist/types.js +0 -0
- package/dist/utilities.js +0 -8
- /package/dist/{column/constants.d.ts → cjs/column/constants.d.cts} +0 -0
- /package/dist/{migration/runner.d.ts → cjs/migration/runner.d.cts} +0 -0
- /package/dist/{query/constants.d.ts → cjs/query/constants.d.cts} +0 -0
- /package/dist/{table/constants.d.ts → cjs/table/constants.d.cts} +0 -0
- /package/dist/{types.d.ts → cjs/types.d.cts} +0 -0
- /package/dist/{utilities.d.ts → cjs/utilities.d.cts} +0 -0
- /package/dist/{column → esm/column}/index.d.ts +0 -0
- /package/dist/{column → esm/column}/types.d.ts +0 -0
- /package/dist/{database → esm/database}/alter.d.ts +0 -0
- /package/dist/{database → esm/database}/column.d.ts +0 -0
- /package/dist/{database → esm/database}/contract.d.ts +0 -0
- /package/dist/{database → esm/database}/index.d.ts +0 -0
- /package/dist/{database → esm/database}/table.d.ts +0 -0
- /package/dist/{database → esm/database}/types.d.ts +0 -0
- /package/dist/{database → esm/database}/wrapper.d.ts +0 -0
- /package/dist/{index-DFrpzXEn.d.ts → esm/index-DFrpzXEn.d.ts} +0 -0
- /package/dist/{index-FMT0YEO7.d.ts → esm/index-FMT0YEO7.d.ts} +0 -0
- /package/dist/{index.d.ts → esm/index.d.ts} +0 -0
- /package/dist/{migration → esm/migration}/index.d.ts +0 -0
- /package/dist/{migration → esm/migration}/type.d.ts +0 -0
- /package/dist/{query → esm/query}/builder.d.ts +0 -0
- /package/dist/{query → esm/query}/condition.d.ts +0 -0
- /package/dist/{query → esm/query}/contract.d.ts +0 -0
- /package/dist/{query → esm/query}/helper.d.ts +0 -0
- /package/dist/{query → esm/query}/index.d.ts +0 -0
- /package/dist/{query → esm/query}/join.d.ts +0 -0
- /package/dist/{query → esm/query}/sql.d.ts +0 -0
- /package/dist/{query → esm/query}/types.d.ts +0 -0
- /package/dist/{query → esm/query}/utilities.d.ts +0 -0
- /package/dist/{table → esm/table}/index.d.ts +0 -0
- /package/dist/{table → esm/table}/types.d.ts +0 -0
- /package/dist/{table → esm/table}/utilities.d.ts +0 -0
package/dist/chunk-EIUC7HJS.js
DELETED
|
@@ -1,686 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
addJoin
|
|
3
|
-
} from "./chunk-FYSNJAGD.js";
|
|
4
|
-
import {
|
|
5
|
-
AcceptedJoin,
|
|
6
|
-
AcceptedOperator,
|
|
7
|
-
ConditionClause,
|
|
8
|
-
LogicalOperator,
|
|
9
|
-
QueryHooksType,
|
|
10
|
-
QueryType
|
|
11
|
-
} from "./chunk-62FKD35V.js";
|
|
12
|
-
import {
|
|
13
|
-
deepClone,
|
|
14
|
-
quoteIdentifier
|
|
15
|
-
} from "./chunk-Y7FSRHH3.js";
|
|
16
|
-
import {
|
|
17
|
-
Dialect
|
|
18
|
-
} from "./chunk-GLOHF5CP.js";
|
|
19
|
-
|
|
20
|
-
// src/query/utilities.ts
|
|
21
|
-
function getTableColumnNames(column, baseAlias, baseTable, joinedTables) {
|
|
22
|
-
const [tableAlias] = column.split(".");
|
|
23
|
-
const isOnBase = tableAlias === baseAlias;
|
|
24
|
-
const from = isOnBase ? baseAlias : tableAlias;
|
|
25
|
-
const columns = isOnBase ? Object.keys(baseTable.columns) : Object.keys(joinedTables?.[from]?.columns ?? {});
|
|
26
|
-
return {
|
|
27
|
-
from,
|
|
28
|
-
columns
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
function getCondition(dialect, column, operator, value) {
|
|
32
|
-
switch (operator) {
|
|
33
|
-
case AcceptedOperator.EQ:
|
|
34
|
-
return `${column} = ?`;
|
|
35
|
-
case AcceptedOperator.NE:
|
|
36
|
-
return `${column} != ?`;
|
|
37
|
-
case AcceptedOperator.GT:
|
|
38
|
-
return `${column} > ?`;
|
|
39
|
-
case AcceptedOperator.LT:
|
|
40
|
-
return `${column} < ?`;
|
|
41
|
-
case AcceptedOperator.GTE:
|
|
42
|
-
return `${column} >= ?`;
|
|
43
|
-
case AcceptedOperator.LTE:
|
|
44
|
-
return `${column} <= ?`;
|
|
45
|
-
case AcceptedOperator.IN:
|
|
46
|
-
return `${column} IN (${value.map(() => "?").join(", ")})`;
|
|
47
|
-
case AcceptedOperator.NOT_IN:
|
|
48
|
-
return `${column} NOT IN (${value.map(() => "?").join(", ")})`;
|
|
49
|
-
case AcceptedOperator.LIKE:
|
|
50
|
-
return `${column} LIKE ?`;
|
|
51
|
-
case AcceptedOperator.NOT_LIKE:
|
|
52
|
-
return `${column} NOT LIKE ?`;
|
|
53
|
-
case AcceptedOperator.ILIKE:
|
|
54
|
-
if (dialect === Dialect.POSTGRES) {
|
|
55
|
-
return `${column} ILIKE ?`;
|
|
56
|
-
}
|
|
57
|
-
return `LOWER(${column}) LIKE LOWER(?)`;
|
|
58
|
-
case AcceptedOperator.NOT_ILIKE:
|
|
59
|
-
if (dialect === Dialect.POSTGRES) {
|
|
60
|
-
return `${column} NOT ILIKE ?`;
|
|
61
|
-
}
|
|
62
|
-
return `LOWER(${column}) NOT LIKE LOWER(?)`;
|
|
63
|
-
case AcceptedOperator.IS_NULL:
|
|
64
|
-
return `${column} IS NULL`;
|
|
65
|
-
case AcceptedOperator.IS_NOT_NULL:
|
|
66
|
-
return `${column} IS NOT NULL`;
|
|
67
|
-
case AcceptedOperator.BETWEEN:
|
|
68
|
-
return `${column} BETWEEN ? AND ?`;
|
|
69
|
-
case AcceptedOperator.NOT_BETWEEN:
|
|
70
|
-
return `${column} NOT BETWEEN ? AND ?`;
|
|
71
|
-
default:
|
|
72
|
-
throw new Error("Invalid operator");
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
function getTimestamp(table) {
|
|
76
|
-
const isWithTimestamp = !!table.timestamp;
|
|
77
|
-
const timestamp = /* @__PURE__ */ new Date();
|
|
78
|
-
let createdAt = "createdAt";
|
|
79
|
-
let updatedAt = "updatedAt";
|
|
80
|
-
if (isWithTimestamp) {
|
|
81
|
-
const isCustomTimestamp = typeof table.timestamp === "object";
|
|
82
|
-
if (isCustomTimestamp && table.timestamp.createdAt) {
|
|
83
|
-
createdAt = table.timestamp.createdAt;
|
|
84
|
-
}
|
|
85
|
-
if (isCustomTimestamp && table.timestamp.updatedAt) {
|
|
86
|
-
updatedAt = table.timestamp.updatedAt;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return {
|
|
90
|
-
isWithTimestamp,
|
|
91
|
-
timestamp,
|
|
92
|
-
createdAt,
|
|
93
|
-
updatedAt
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
function getParanoid(table) {
|
|
97
|
-
const isWithParanoid = !!table.paranoid;
|
|
98
|
-
const timestamp = /* @__PURE__ */ new Date();
|
|
99
|
-
let deletedAt = "deletedAt";
|
|
100
|
-
if (isWithParanoid) {
|
|
101
|
-
if (typeof table.paranoid === "string") {
|
|
102
|
-
deletedAt = table.paranoid;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return {
|
|
106
|
-
isWithParanoid,
|
|
107
|
-
timestamp,
|
|
108
|
-
deletedAt
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
function getWhereConditions(q) {
|
|
112
|
-
if (q.definition.queryType === QueryType.INSERT) return [];
|
|
113
|
-
const conditions = [];
|
|
114
|
-
const base = q.definition.baseAlias ?? q.table.name;
|
|
115
|
-
const { isWithParanoid, deletedAt } = getParanoid(q.table);
|
|
116
|
-
const withDeleted = !!q.definition.withDeleted;
|
|
117
|
-
const isHasConditions = !!q.definition.where?.length;
|
|
118
|
-
if (!withDeleted && isWithParanoid) {
|
|
119
|
-
const suffix = isHasConditions ? " AND" : "";
|
|
120
|
-
const column = `${base}.${quoteIdentifier(deletedAt)}`;
|
|
121
|
-
conditions.unshift(`${column} IS NULL${suffix}`);
|
|
122
|
-
}
|
|
123
|
-
if (q.definition.where?.length) {
|
|
124
|
-
conditions.push(...q.definition.where);
|
|
125
|
-
}
|
|
126
|
-
return conditions;
|
|
127
|
-
}
|
|
128
|
-
function getGroupByConditions(q) {
|
|
129
|
-
if (q.definition.queryType !== QueryType.SELECT) return [];
|
|
130
|
-
if (q.definition.groupBy?.length) return q.definition.groupBy;
|
|
131
|
-
if (q.definition.aggregates?.length) {
|
|
132
|
-
if (q.definition.select?.length)
|
|
133
|
-
return q.definition.select.map((col2) => {
|
|
134
|
-
if (typeof col2 === "string" && col2.endsWith("*")) {
|
|
135
|
-
const { from: from2, columns } = getTableColumnNames(
|
|
136
|
-
col2,
|
|
137
|
-
q.definition.baseAlias ?? q.table.name,
|
|
138
|
-
q.table,
|
|
139
|
-
q.definition.joinedTables ?? {}
|
|
140
|
-
);
|
|
141
|
-
return columns.map((column) => `${from2}.${quoteIdentifier(column)}`).join(" ");
|
|
142
|
-
}
|
|
143
|
-
return col2;
|
|
144
|
-
});
|
|
145
|
-
const from = q.definition.baseAlias ?? q.table.name;
|
|
146
|
-
return Object.keys(q.table.columns).map(
|
|
147
|
-
(col2) => `${from}.${quoteIdentifier(col2)}`
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
return [];
|
|
151
|
-
}
|
|
152
|
-
function getTableSelectName(q) {
|
|
153
|
-
if (!q.definition.baseAlias) return q.table.name;
|
|
154
|
-
return `${q.table.name} AS ${q.definition.baseAlias}`;
|
|
155
|
-
}
|
|
156
|
-
function parseAliasedRow({
|
|
157
|
-
row,
|
|
158
|
-
selects,
|
|
159
|
-
root = null
|
|
160
|
-
}) {
|
|
161
|
-
let result = {};
|
|
162
|
-
for (const key in row) {
|
|
163
|
-
const [table, column] = key.split(".");
|
|
164
|
-
if (!column) {
|
|
165
|
-
const alias2 = selects.find(
|
|
166
|
-
(s) => typeof s === "object" && s.as === table
|
|
167
|
-
);
|
|
168
|
-
if (alias2) {
|
|
169
|
-
const [oriTab] = alias2.column.split(".");
|
|
170
|
-
if (!result[oriTab]) result[oriTab] = {};
|
|
171
|
-
result[oriTab][table] = row[key];
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
result[key] = row[key];
|
|
175
|
-
continue;
|
|
176
|
-
}
|
|
177
|
-
if (!result[table]) result[table] = {};
|
|
178
|
-
result[table][column] = row[key];
|
|
179
|
-
}
|
|
180
|
-
if (root) {
|
|
181
|
-
result = {
|
|
182
|
-
...result,
|
|
183
|
-
...result[root]
|
|
184
|
-
};
|
|
185
|
-
delete result[root];
|
|
186
|
-
}
|
|
187
|
-
return result;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// src/query/condition.ts
|
|
191
|
-
function addRawCondition(query, clause, column, logical, params) {
|
|
192
|
-
const validClause = clause.toLowerCase();
|
|
193
|
-
if (!query.definition[validClause]) query.definition[validClause] = [];
|
|
194
|
-
const condition = column(rawCol);
|
|
195
|
-
const logicalPrefix = query.definition[validClause].length > 0 ? logical : "";
|
|
196
|
-
query.definition[validClause].push(`${logicalPrefix} ${condition}`.trim());
|
|
197
|
-
if (!query.definition.params) query.definition.params = [];
|
|
198
|
-
if (typeof params === "undefined") {
|
|
199
|
-
return query;
|
|
200
|
-
}
|
|
201
|
-
if (Array.isArray(params)) {
|
|
202
|
-
query.definition.params.push(...params);
|
|
203
|
-
} else {
|
|
204
|
-
query.definition.params.push(params);
|
|
205
|
-
}
|
|
206
|
-
return query;
|
|
207
|
-
}
|
|
208
|
-
function rawWhere(column, params) {
|
|
209
|
-
return addRawCondition(
|
|
210
|
-
this,
|
|
211
|
-
ConditionClause.WHERE,
|
|
212
|
-
column,
|
|
213
|
-
LogicalOperator.AND,
|
|
214
|
-
params
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
function rawOr(column, params) {
|
|
218
|
-
return addRawCondition(
|
|
219
|
-
this,
|
|
220
|
-
ConditionClause.WHERE,
|
|
221
|
-
column,
|
|
222
|
-
LogicalOperator.OR,
|
|
223
|
-
params
|
|
224
|
-
);
|
|
225
|
-
}
|
|
226
|
-
function rawHaving(column, params) {
|
|
227
|
-
return addRawCondition(
|
|
228
|
-
this,
|
|
229
|
-
ConditionClause.HAVING,
|
|
230
|
-
column,
|
|
231
|
-
LogicalOperator.AND,
|
|
232
|
-
params
|
|
233
|
-
);
|
|
234
|
-
}
|
|
235
|
-
function addCondition(query, clause, column, operator, value, logical) {
|
|
236
|
-
const validClause = clause.toLowerCase();
|
|
237
|
-
const condition = getCondition(query.table.dialect, column, operator, value);
|
|
238
|
-
if (!query.definition[validClause]) query.definition[validClause] = [];
|
|
239
|
-
const logicalPrefix = query.definition[validClause].length > 0 ? logical : "";
|
|
240
|
-
query.definition[validClause].push(`${logicalPrefix} ${condition}`.trim());
|
|
241
|
-
if (operator === AcceptedOperator.IS_NULL || operator === AcceptedOperator.IS_NOT_NULL) {
|
|
242
|
-
return query;
|
|
243
|
-
}
|
|
244
|
-
if (!query.definition.params) query.definition.params = [];
|
|
245
|
-
if (Array.isArray(value)) {
|
|
246
|
-
query.definition.params.push(...value);
|
|
247
|
-
} else {
|
|
248
|
-
query.definition.params.push(value);
|
|
249
|
-
}
|
|
250
|
-
return query;
|
|
251
|
-
}
|
|
252
|
-
function where(column, operator, value) {
|
|
253
|
-
return addCondition(
|
|
254
|
-
this,
|
|
255
|
-
ConditionClause.WHERE,
|
|
256
|
-
column,
|
|
257
|
-
operator,
|
|
258
|
-
value,
|
|
259
|
-
LogicalOperator.AND
|
|
260
|
-
);
|
|
261
|
-
}
|
|
262
|
-
function or(column, operator, value) {
|
|
263
|
-
return addCondition(
|
|
264
|
-
this,
|
|
265
|
-
ConditionClause.WHERE,
|
|
266
|
-
column,
|
|
267
|
-
operator,
|
|
268
|
-
value,
|
|
269
|
-
LogicalOperator.OR
|
|
270
|
-
);
|
|
271
|
-
}
|
|
272
|
-
function having(column, operator, value) {
|
|
273
|
-
return addCondition(
|
|
274
|
-
this,
|
|
275
|
-
ConditionClause.HAVING,
|
|
276
|
-
column,
|
|
277
|
-
operator,
|
|
278
|
-
value,
|
|
279
|
-
LogicalOperator.AND
|
|
280
|
-
);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// src/query/builder.ts
|
|
284
|
-
function buildSelectQuery(q) {
|
|
285
|
-
const from = getTableSelectName(q);
|
|
286
|
-
const columns = [];
|
|
287
|
-
if (q.definition.select?.length) {
|
|
288
|
-
for (const col2 of q.definition.select) {
|
|
289
|
-
if (typeof col2 === "object") {
|
|
290
|
-
const alias2 = quoteIdentifier(col2.as.replace(/"/g, ""));
|
|
291
|
-
columns.push(`${col2.column} AS ${alias2}`);
|
|
292
|
-
continue;
|
|
293
|
-
}
|
|
294
|
-
if (!col2.endsWith("*")) {
|
|
295
|
-
const alias2 = quoteIdentifier(col2.replace(/"/g, ""));
|
|
296
|
-
columns.push(`${col2} AS ${alias2}`);
|
|
297
|
-
continue;
|
|
298
|
-
}
|
|
299
|
-
columns.push(col2);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
if (q.definition?.aggregates) {
|
|
303
|
-
for (const aggregate of q.definition.aggregates) {
|
|
304
|
-
columns.push(
|
|
305
|
-
`${aggregate.fn}(${aggregate.column}) AS ${quoteIdentifier(aggregate.as)}`
|
|
306
|
-
);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
const distinct = q.definition.distinct ? "DISTINCT " : "";
|
|
310
|
-
return `SELECT ${distinct}${columns.join(", ")} FROM ${from}`;
|
|
311
|
-
}
|
|
312
|
-
function buildInsertQuery(q) {
|
|
313
|
-
const rows = q.definition?.insertValues;
|
|
314
|
-
if (!rows?.length) {
|
|
315
|
-
throw new Error(`INSERT requires values`);
|
|
316
|
-
}
|
|
317
|
-
const keys = Object.keys(rows[0]);
|
|
318
|
-
const columns = keys.map(quoteIdentifier).join(", ");
|
|
319
|
-
const rowPlaceholders = `(${keys.map(() => "?").join(", ")})`;
|
|
320
|
-
const placeholders = rows.map(() => rowPlaceholders).join(", ");
|
|
321
|
-
q.definition.params = rows.flatMap(
|
|
322
|
-
(row) => keys.map((key) => row[key])
|
|
323
|
-
);
|
|
324
|
-
return `INSERT INTO ${q.table.name} (${columns}) VALUES ${placeholders} RETURNING *`;
|
|
325
|
-
}
|
|
326
|
-
function buildUpdateQuery(q) {
|
|
327
|
-
if (!q.definition?.updateValues) {
|
|
328
|
-
throw new Error(`UPDATE requires values`);
|
|
329
|
-
}
|
|
330
|
-
let keys = Object.keys(q.definition.updateValues);
|
|
331
|
-
const updateParams = keys.map(
|
|
332
|
-
(key) => q.definition.updateValues[key]
|
|
333
|
-
);
|
|
334
|
-
keys = keys.map(quoteIdentifier);
|
|
335
|
-
if (q.definition?.params) {
|
|
336
|
-
q.definition.params = [...updateParams, ...q.definition.params];
|
|
337
|
-
} else {
|
|
338
|
-
q.definition.params = updateParams;
|
|
339
|
-
}
|
|
340
|
-
return `UPDATE ${q.table.name} SET ${keys.map((key) => `${key} = ?`.trim()).join(", ")}`;
|
|
341
|
-
}
|
|
342
|
-
function buildDeleteQuery(q) {
|
|
343
|
-
return `DELETE FROM ${q.table.name}`;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
// src/query/sql.ts
|
|
347
|
-
function buildQuery(query) {
|
|
348
|
-
let index = 0;
|
|
349
|
-
return query.replace(/\?/g, () => {
|
|
350
|
-
index++;
|
|
351
|
-
return `$${index}`;
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
function toQuery() {
|
|
355
|
-
let sql = "";
|
|
356
|
-
switch (this.definition.queryType) {
|
|
357
|
-
case QueryType.SELECT:
|
|
358
|
-
sql = buildSelectQuery(this);
|
|
359
|
-
break;
|
|
360
|
-
case QueryType.INSERT:
|
|
361
|
-
sql = buildInsertQuery(this);
|
|
362
|
-
break;
|
|
363
|
-
case QueryType.UPDATE:
|
|
364
|
-
sql = buildUpdateQuery(this);
|
|
365
|
-
break;
|
|
366
|
-
case QueryType.DELETE:
|
|
367
|
-
sql = buildDeleteQuery(this);
|
|
368
|
-
break;
|
|
369
|
-
default:
|
|
370
|
-
throw new Error("No query type defined");
|
|
371
|
-
}
|
|
372
|
-
if (this.definition?.joins?.length) {
|
|
373
|
-
sql += ` ${this.definition.joins.join(" ")}`;
|
|
374
|
-
}
|
|
375
|
-
const whereConditions = getWhereConditions(this);
|
|
376
|
-
if (whereConditions.length) {
|
|
377
|
-
sql += ` WHERE ${whereConditions.join(" ")}`;
|
|
378
|
-
}
|
|
379
|
-
const groupByConditions = getGroupByConditions(this);
|
|
380
|
-
if (groupByConditions.length) {
|
|
381
|
-
sql += ` GROUP BY ${groupByConditions.join(", ")}`;
|
|
382
|
-
}
|
|
383
|
-
if (this.definition?.having?.length) {
|
|
384
|
-
sql += ` HAVING ${this.definition.having.join(" ")}`;
|
|
385
|
-
}
|
|
386
|
-
if (this.definition?.orderBy?.length) {
|
|
387
|
-
sql += ` ORDER BY ${this.definition.orderBy.map((order) => [order.column, order.direction].join(" ")).join(", ")}`;
|
|
388
|
-
}
|
|
389
|
-
if (this.definition?.limit !== null) {
|
|
390
|
-
sql += ` LIMIT ?`;
|
|
391
|
-
if (!this.definition.params) this.definition.params = [];
|
|
392
|
-
this.definition.params.push(this.definition.limit);
|
|
393
|
-
}
|
|
394
|
-
if (this.definition?.offset !== null) {
|
|
395
|
-
sql += ` OFFSET ?`;
|
|
396
|
-
if (!this.definition.params) this.definition.params = [];
|
|
397
|
-
this.definition.params.push(this.definition.offset);
|
|
398
|
-
}
|
|
399
|
-
if (this.definition.queryType === QueryType.UPDATE || this.definition.queryType === QueryType.DELETE) {
|
|
400
|
-
sql += ` RETURNING *`;
|
|
401
|
-
}
|
|
402
|
-
sql = buildQuery(sql);
|
|
403
|
-
return { query: sql + ";", params: this.definition.params };
|
|
404
|
-
}
|
|
405
|
-
function toString() {
|
|
406
|
-
return this.toQuery().query;
|
|
407
|
-
}
|
|
408
|
-
async function exec() {
|
|
409
|
-
if (!this.table.client) throw new Error("Database client not defined");
|
|
410
|
-
const { query, params } = this.toQuery();
|
|
411
|
-
if (this.hooks?.before?.size) {
|
|
412
|
-
for (const hook of this.hooks.before.values()) {
|
|
413
|
-
hook({
|
|
414
|
-
query,
|
|
415
|
-
params,
|
|
416
|
-
type: this.definition.queryType,
|
|
417
|
-
hook: QueryHooksType.BEFORE
|
|
418
|
-
});
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
const result = await this.table.client.exec(query, params);
|
|
422
|
-
if (this.hooks?.after?.size) {
|
|
423
|
-
for (const hook of this.hooks.after.values()) {
|
|
424
|
-
hook({
|
|
425
|
-
query,
|
|
426
|
-
params,
|
|
427
|
-
type: this.definition.queryType,
|
|
428
|
-
hook: QueryHooksType.AFTER
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
return result.map(
|
|
433
|
-
(r) => parseAliasedRow({
|
|
434
|
-
row: r,
|
|
435
|
-
selects: this.definition.select ?? [],
|
|
436
|
-
root: this.definition?.baseAlias ?? this.table.name
|
|
437
|
-
})
|
|
438
|
-
);
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
// src/query/index.ts
|
|
442
|
-
var QueryBuilder2 = class {
|
|
443
|
-
hooks;
|
|
444
|
-
table;
|
|
445
|
-
definition;
|
|
446
|
-
_output;
|
|
447
|
-
alias;
|
|
448
|
-
clone;
|
|
449
|
-
toQuery;
|
|
450
|
-
toString;
|
|
451
|
-
exec;
|
|
452
|
-
rawWhere;
|
|
453
|
-
rawAnd;
|
|
454
|
-
rawOr;
|
|
455
|
-
rawHaving;
|
|
456
|
-
where;
|
|
457
|
-
and;
|
|
458
|
-
or;
|
|
459
|
-
having;
|
|
460
|
-
constructor(table) {
|
|
461
|
-
this.hooks = {};
|
|
462
|
-
this.table = table;
|
|
463
|
-
this.definition = {
|
|
464
|
-
queryType: null,
|
|
465
|
-
select: null,
|
|
466
|
-
having: null,
|
|
467
|
-
where: null,
|
|
468
|
-
params: null,
|
|
469
|
-
limit: null,
|
|
470
|
-
offset: null,
|
|
471
|
-
groupBy: null,
|
|
472
|
-
insertValues: null,
|
|
473
|
-
updateValues: null,
|
|
474
|
-
orderBy: null,
|
|
475
|
-
aggregates: null,
|
|
476
|
-
joins: null,
|
|
477
|
-
distinct: null,
|
|
478
|
-
baseAlias: table.name,
|
|
479
|
-
joinedTables: null,
|
|
480
|
-
withDeleted: null
|
|
481
|
-
};
|
|
482
|
-
this.alias = alias.bind(this);
|
|
483
|
-
this.clone = clone.bind(this);
|
|
484
|
-
this.toQuery = toQuery.bind(this);
|
|
485
|
-
this.toString = toString.bind(this);
|
|
486
|
-
this.exec = exec.bind(this);
|
|
487
|
-
this.rawWhere = rawWhere.bind(this);
|
|
488
|
-
this.rawHaving = rawHaving.bind(this);
|
|
489
|
-
this.rawAnd = this.rawWhere;
|
|
490
|
-
this.rawOr = rawOr.bind(this);
|
|
491
|
-
this.where = where.bind(this);
|
|
492
|
-
this.having = having.bind(this);
|
|
493
|
-
this.and = this.where;
|
|
494
|
-
this.or = or.bind(this);
|
|
495
|
-
}
|
|
496
|
-
leftJoin(joinTable, alias2, baseColumn, joinColumn) {
|
|
497
|
-
return addJoin(
|
|
498
|
-
this,
|
|
499
|
-
AcceptedJoin.LEFT,
|
|
500
|
-
alias2,
|
|
501
|
-
joinTable,
|
|
502
|
-
baseColumn,
|
|
503
|
-
joinColumn
|
|
504
|
-
);
|
|
505
|
-
}
|
|
506
|
-
rightJoin(joinTable, alias2, baseColumn, joinColumn) {
|
|
507
|
-
return addJoin(
|
|
508
|
-
this,
|
|
509
|
-
AcceptedJoin.RIGHT,
|
|
510
|
-
alias2,
|
|
511
|
-
joinTable,
|
|
512
|
-
baseColumn,
|
|
513
|
-
joinColumn
|
|
514
|
-
);
|
|
515
|
-
}
|
|
516
|
-
innerJoin(joinTable, alias2, baseColumn, joinColumn) {
|
|
517
|
-
return addJoin(
|
|
518
|
-
this,
|
|
519
|
-
AcceptedJoin.INNER,
|
|
520
|
-
alias2,
|
|
521
|
-
joinTable,
|
|
522
|
-
baseColumn,
|
|
523
|
-
joinColumn
|
|
524
|
-
);
|
|
525
|
-
}
|
|
526
|
-
naturalJoin(joinTable, alias2, baseColumn, joinColumn) {
|
|
527
|
-
return addJoin(
|
|
528
|
-
this,
|
|
529
|
-
AcceptedJoin.NATURAL,
|
|
530
|
-
alias2,
|
|
531
|
-
joinTable,
|
|
532
|
-
baseColumn,
|
|
533
|
-
joinColumn
|
|
534
|
-
);
|
|
535
|
-
}
|
|
536
|
-
distinct() {
|
|
537
|
-
this.definition.distinct = true;
|
|
538
|
-
return this;
|
|
539
|
-
}
|
|
540
|
-
aggregate(...aggregates) {
|
|
541
|
-
this.definition.aggregates = aggregates.map(
|
|
542
|
-
(aggregate) => aggregate(aggregateCol)
|
|
543
|
-
);
|
|
544
|
-
return this;
|
|
545
|
-
}
|
|
546
|
-
groupBy(...columns) {
|
|
547
|
-
this.definition.groupBy = columns;
|
|
548
|
-
return this;
|
|
549
|
-
}
|
|
550
|
-
limit(limit) {
|
|
551
|
-
this.definition.limit = limit;
|
|
552
|
-
return this;
|
|
553
|
-
}
|
|
554
|
-
offset(offset) {
|
|
555
|
-
this.definition.offset = offset;
|
|
556
|
-
return this;
|
|
557
|
-
}
|
|
558
|
-
orderBy(...orderBy) {
|
|
559
|
-
if (!this.definition.orderBy) this.definition.orderBy = [];
|
|
560
|
-
this.definition.orderBy.push(...orderBy);
|
|
561
|
-
return this;
|
|
562
|
-
}
|
|
563
|
-
withDeleted() {
|
|
564
|
-
this.definition.withDeleted = true;
|
|
565
|
-
return this;
|
|
566
|
-
}
|
|
567
|
-
select(...columns) {
|
|
568
|
-
if (!columns.length) {
|
|
569
|
-
const base = this.definition.baseAlias ?? this.table.name;
|
|
570
|
-
columns = Object.keys(this.table.columns).map(
|
|
571
|
-
(colName) => `${base}.${quoteIdentifier(colName)}`
|
|
572
|
-
);
|
|
573
|
-
} else {
|
|
574
|
-
columns = columns.map((column) => {
|
|
575
|
-
if (typeof column === "function") {
|
|
576
|
-
return column(col);
|
|
577
|
-
}
|
|
578
|
-
return column;
|
|
579
|
-
});
|
|
580
|
-
}
|
|
581
|
-
this.definition.select = columns;
|
|
582
|
-
this.definition.queryType = QueryType.SELECT;
|
|
583
|
-
return this;
|
|
584
|
-
}
|
|
585
|
-
insert(...values) {
|
|
586
|
-
this.definition.queryType = QueryType.INSERT;
|
|
587
|
-
if (!this.definition.insertValues) this.definition.insertValues = [];
|
|
588
|
-
const { isWithTimestamp, createdAt, updatedAt, timestamp } = getTimestamp(
|
|
589
|
-
this.table
|
|
590
|
-
);
|
|
591
|
-
if (isWithTimestamp) {
|
|
592
|
-
values = values.map((row) => ({
|
|
593
|
-
...row,
|
|
594
|
-
[createdAt]: row[createdAt] ?? timestamp,
|
|
595
|
-
[updatedAt]: row[updatedAt] ?? timestamp
|
|
596
|
-
}));
|
|
597
|
-
}
|
|
598
|
-
this.definition.insertValues = values;
|
|
599
|
-
return this;
|
|
600
|
-
}
|
|
601
|
-
update(values) {
|
|
602
|
-
const { isWithTimestamp, updatedAt, timestamp } = getTimestamp(this.table);
|
|
603
|
-
if (isWithTimestamp) {
|
|
604
|
-
values = {
|
|
605
|
-
...values,
|
|
606
|
-
[updatedAt]: values[updatedAt] ?? timestamp
|
|
607
|
-
};
|
|
608
|
-
}
|
|
609
|
-
this.definition.queryType = QueryType.UPDATE;
|
|
610
|
-
this.definition.updateValues = values;
|
|
611
|
-
return this;
|
|
612
|
-
}
|
|
613
|
-
delete() {
|
|
614
|
-
const { isWithParanoid, deletedAt, timestamp } = getParanoid(this.table);
|
|
615
|
-
if (isWithParanoid) {
|
|
616
|
-
return this.update({
|
|
617
|
-
[deletedAt]: timestamp
|
|
618
|
-
});
|
|
619
|
-
}
|
|
620
|
-
this.definition.queryType = QueryType.DELETE;
|
|
621
|
-
return this;
|
|
622
|
-
}
|
|
623
|
-
infer() {
|
|
624
|
-
return null;
|
|
625
|
-
}
|
|
626
|
-
};
|
|
627
|
-
|
|
628
|
-
// src/query/helper.ts
|
|
629
|
-
function alias(alias2) {
|
|
630
|
-
this.definition.baseAlias = alias2;
|
|
631
|
-
return this;
|
|
632
|
-
}
|
|
633
|
-
function clone() {
|
|
634
|
-
const query = new QueryBuilder2(this.table);
|
|
635
|
-
Object.assign(query.definition, deepClone(this.definition));
|
|
636
|
-
return query;
|
|
637
|
-
}
|
|
638
|
-
function rawCol(column) {
|
|
639
|
-
return column;
|
|
640
|
-
}
|
|
641
|
-
function col(column, alias2) {
|
|
642
|
-
return {
|
|
643
|
-
column,
|
|
644
|
-
as: alias2
|
|
645
|
-
};
|
|
646
|
-
}
|
|
647
|
-
function aggregateCol(fn, column, alias2) {
|
|
648
|
-
return {
|
|
649
|
-
column,
|
|
650
|
-
as: alias2 ?? fn.toLowerCase(),
|
|
651
|
-
fn
|
|
652
|
-
};
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
export {
|
|
656
|
-
alias,
|
|
657
|
-
clone,
|
|
658
|
-
rawCol,
|
|
659
|
-
col,
|
|
660
|
-
aggregateCol,
|
|
661
|
-
getTableColumnNames,
|
|
662
|
-
getCondition,
|
|
663
|
-
getTimestamp,
|
|
664
|
-
getParanoid,
|
|
665
|
-
getWhereConditions,
|
|
666
|
-
getGroupByConditions,
|
|
667
|
-
getTableSelectName,
|
|
668
|
-
parseAliasedRow,
|
|
669
|
-
addRawCondition,
|
|
670
|
-
rawWhere,
|
|
671
|
-
rawOr,
|
|
672
|
-
rawHaving,
|
|
673
|
-
addCondition,
|
|
674
|
-
where,
|
|
675
|
-
or,
|
|
676
|
-
having,
|
|
677
|
-
buildSelectQuery,
|
|
678
|
-
buildInsertQuery,
|
|
679
|
-
buildUpdateQuery,
|
|
680
|
-
buildDeleteQuery,
|
|
681
|
-
buildQuery,
|
|
682
|
-
toQuery,
|
|
683
|
-
toString,
|
|
684
|
-
exec,
|
|
685
|
-
QueryBuilder2 as QueryBuilder
|
|
686
|
-
};
|
package/dist/column/constants.js
DELETED
package/dist/column/index.js
DELETED
package/dist/column/types.js
DELETED
package/dist/database/alter.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
alterColumnType,
|
|
3
|
-
dropColumnDefault,
|
|
4
|
-
dropColumnNotNull,
|
|
5
|
-
setColumnDefault,
|
|
6
|
-
setColumnNotNull
|
|
7
|
-
} from "../chunk-V4OMHVJN.js";
|
|
8
|
-
import "../chunk-GLOHF5CP.js";
|
|
9
|
-
export {
|
|
10
|
-
alterColumnType,
|
|
11
|
-
dropColumnDefault,
|
|
12
|
-
dropColumnNotNull,
|
|
13
|
-
setColumnDefault,
|
|
14
|
-
setColumnNotNull
|
|
15
|
-
};
|