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