sql-love 1.0.3 → 1.0.4
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 +52 -32
- package/dist/index.cjs +16 -0
- package/dist/index.d.ts +732 -9
- package/dist/index.mjs +16 -0
- package/package.json +27 -10
- package/.github/workflows/node.js.yml +0 -37
- package/.prettierrc.json +0 -13
- package/CHANGELOG.md +0 -39
- package/CONTRIBUTING.md +0 -85
- package/assets/sql-love-logo.png +0 -0
- package/codecov.yml +0 -7
- package/dist/index.js +0 -9
- package/dist/src/Parser/Parser.d.ts +0 -157
- package/dist/src/Parser/Parser.js +0 -304
- package/dist/src/SelectBuilder/SelectBuilder.d.ts +0 -379
- package/dist/src/SelectBuilder/SelectBuilder.js +0 -810
- package/dist/src/extractGrouped/extractGrouped.d.ts +0 -17
- package/dist/src/extractGrouped/extractGrouped.js +0 -31
- package/dist/src/extractIndexed/extractIndexed.d.ts +0 -12
- package/dist/src/extractIndexed/extractIndexed.js +0 -23
- package/dist/src/extractLookup/extractLookup.d.ts +0 -9
- package/dist/src/extractLookup/extractLookup.js +0 -21
- package/dist/src/getPagination/getPagination.d.ts +0 -16
- package/dist/src/getPagination/getPagination.js +0 -40
- package/dist/src/runMysql/runMysql.d.ts +0 -58
- package/dist/src/runMysql/runMysql.js +0 -61
- package/dist/src/runPrisma/runPrisma.d.ts +0 -41
- package/dist/src/runPrisma/runPrisma.js +0 -34
- package/dist/src/substrCount/substrCount.d.ts +0 -6
- package/dist/src/substrCount/substrCount.js +0 -14
- package/dist/src/toSafeJson/toSafeJson.d.ts +0 -25
- package/dist/src/toSafeJson/toSafeJson.js +0 -45
- package/index.ts +0 -19
- package/src/Parser/Parser.spec.ts +0 -226
- package/src/Parser/Parser.ts +0 -325
- package/src/SelectBuilder/SelectBuilder.spec.ts +0 -785
- package/src/SelectBuilder/SelectBuilder.ts +0 -914
- package/src/extractGrouped/extractGrouped.spec.ts +0 -43
- package/src/extractGrouped/extractGrouped.ts +0 -36
- package/src/extractIndexed/extractIndexed.spec.ts +0 -40
- package/src/extractIndexed/extractIndexed.ts +0 -28
- package/src/extractLookup/extractLookup.spec.ts +0 -38
- package/src/extractLookup/extractLookup.ts +0 -33
- package/src/getPagination/getPagination.spec.ts +0 -75
- package/src/getPagination/getPagination.ts +0 -42
- package/src/runMysql/runMysql.spec.ts +0 -286
- package/src/runMysql/runMysql.ts +0 -106
- package/src/runPrisma/runPrisma.spec.ts +0 -99
- package/src/runPrisma/runPrisma.ts +0 -75
- package/src/substrCount/substrCount.spec.ts +0 -29
- package/src/substrCount/substrCount.ts +0 -14
- package/src/toSafeJson/toSafeJson.spec.ts +0 -28
- package/src/toSafeJson/toSafeJson.ts +0 -56
- package/tsconfig.build.json +0 -10
- package/tsconfig.json +0 -22
- package/vitest.config.ts +0 -11
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,732 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
// Generated by dts-bundle-generator v9.5.1
|
|
2
|
+
|
|
3
|
+
export type FieldName = "option" | "options" | "select" | "column" | "columns" | "table" | "tables" | "join" | "joins" | "where" | "wheres" | "having" | "havings" | "groupBy" | "groupBys" | "orderBy" | "orderBys" | "limit" | "offset" | "page";
|
|
4
|
+
export type EngineStyle = "mssql" | "mysql" | "oracle" | "pg";
|
|
5
|
+
export type WhereCriteria = [
|
|
6
|
+
string,
|
|
7
|
+
string,
|
|
8
|
+
any
|
|
9
|
+
] | [
|
|
10
|
+
string,
|
|
11
|
+
any
|
|
12
|
+
] | [
|
|
13
|
+
string
|
|
14
|
+
] | [
|
|
15
|
+
Record<string, any>
|
|
16
|
+
];
|
|
17
|
+
/**
|
|
18
|
+
* Build a select query
|
|
19
|
+
* Class Select
|
|
20
|
+
*/
|
|
21
|
+
export declare class SelectBuilder {
|
|
22
|
+
/**
|
|
23
|
+
* The list of values to bind to the query
|
|
24
|
+
* @private
|
|
25
|
+
*/
|
|
26
|
+
_bindings: any[];
|
|
27
|
+
/**
|
|
28
|
+
* The list of strings to come immediately after "SELECT"
|
|
29
|
+
* and before column names
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
32
|
+
_options: string[];
|
|
33
|
+
/**
|
|
34
|
+
* The list of column names to select
|
|
35
|
+
* @private
|
|
36
|
+
*/
|
|
37
|
+
_tables: string[];
|
|
38
|
+
/**
|
|
39
|
+
* The list of tables in the FROM clause
|
|
40
|
+
* @private
|
|
41
|
+
*/
|
|
42
|
+
_columns: string[];
|
|
43
|
+
/**
|
|
44
|
+
* The list of JOIN strings to add
|
|
45
|
+
* @private
|
|
46
|
+
*/
|
|
47
|
+
_joins: string[];
|
|
48
|
+
/**
|
|
49
|
+
* The list of WHERE clauses to join with AND
|
|
50
|
+
* @private
|
|
51
|
+
*/
|
|
52
|
+
_wheres: string[];
|
|
53
|
+
/**
|
|
54
|
+
* The list of GROUP BY clauses to join with commas
|
|
55
|
+
* @private
|
|
56
|
+
*/
|
|
57
|
+
_groupBys: string[];
|
|
58
|
+
/**
|
|
59
|
+
* The list of HAVING clauses to join with AND
|
|
60
|
+
* @private
|
|
61
|
+
*/
|
|
62
|
+
_havings: string[];
|
|
63
|
+
/**
|
|
64
|
+
* The list of ORDER BY clauses to join with commas
|
|
65
|
+
* @private
|
|
66
|
+
*/
|
|
67
|
+
_orderBys: string[];
|
|
68
|
+
/**
|
|
69
|
+
* The LIMIT to use
|
|
70
|
+
* @private
|
|
71
|
+
*/
|
|
72
|
+
_limit: number;
|
|
73
|
+
/**
|
|
74
|
+
* The OFFSET to use
|
|
75
|
+
* @private
|
|
76
|
+
*/
|
|
77
|
+
_offset: number;
|
|
78
|
+
/**
|
|
79
|
+
* The page used to construct an OFFSET based on the LIMIT
|
|
80
|
+
* @private
|
|
81
|
+
*/
|
|
82
|
+
_page: number;
|
|
83
|
+
/**
|
|
84
|
+
* Select constructor
|
|
85
|
+
* @param [sql] A base SQL query to parse
|
|
86
|
+
* @param [bindings] Placeholders to bind to the query
|
|
87
|
+
*/
|
|
88
|
+
constructor(sql?: string, bindings?: Record<string, any>);
|
|
89
|
+
/**
|
|
90
|
+
* Set the default database engine style (mssql, mysql, oracle, pg)
|
|
91
|
+
* @param engine
|
|
92
|
+
*/
|
|
93
|
+
static setDefaultEngine(engine: EngineStyle): typeof SelectBuilder;
|
|
94
|
+
/**
|
|
95
|
+
* Get the SQL as a pretty-printed string alongside an array of bindings
|
|
96
|
+
* @param engine The database engine style (mssql, mysql, oracle, pg)
|
|
97
|
+
* @return The SQL query as a string
|
|
98
|
+
*/
|
|
99
|
+
compile({ engine, bindingIndex, }?: {
|
|
100
|
+
engine?: EngineStyle;
|
|
101
|
+
bindingIndex?: number;
|
|
102
|
+
}): {
|
|
103
|
+
sql: string;
|
|
104
|
+
bindings: any;
|
|
105
|
+
};
|
|
106
|
+
_compileLIMIT(engineStyle: EngineStyle, limit: number): string;
|
|
107
|
+
_compileOFFSET(engineStyle: EngineStyle, offset: number): string;
|
|
108
|
+
/**
|
|
109
|
+
* Get SQL needed to return the found rows of this query
|
|
110
|
+
* @param engineStyle The database engine style (mssql, mysql, oracle, pg)
|
|
111
|
+
* @param countExpr The expression to use inside the COUNT()
|
|
112
|
+
*/
|
|
113
|
+
compileCount({ countExpr, engine, bindingIndex, }?: {
|
|
114
|
+
countExpr?: string;
|
|
115
|
+
engine?: EngineStyle;
|
|
116
|
+
bindingIndex?: number;
|
|
117
|
+
}): {
|
|
118
|
+
sql: string;
|
|
119
|
+
bindings: any;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Get SQL needed to union this query with another
|
|
123
|
+
* @param query The query to union
|
|
124
|
+
* @param engine The database engine style (mssql, mysql, oracle, pg)
|
|
125
|
+
*/
|
|
126
|
+
compileUnion(query: SelectBuilder, { engine, bindingIndex, }?: {
|
|
127
|
+
engine?: EngineStyle;
|
|
128
|
+
bindingIndex?: number;
|
|
129
|
+
}): {
|
|
130
|
+
sql: string;
|
|
131
|
+
bindings: any[];
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* @param [fieldOrFields] If given, reset the given component(s), otherwise reset all query components
|
|
135
|
+
* Valid components: option, column, table, where, orWhere, having, groupBy, orderBy, limit, offset, page
|
|
136
|
+
* @return This object
|
|
137
|
+
*/
|
|
138
|
+
reset(fieldOrFields?: FieldName | FieldName[]): this;
|
|
139
|
+
/**
|
|
140
|
+
* Clone this builder object
|
|
141
|
+
*/
|
|
142
|
+
getClone(): SelectBuilder;
|
|
143
|
+
/**
|
|
144
|
+
* Build a version of this query that simply returns COUNT(*)
|
|
145
|
+
* @param [countExpr="*"] Use to specify `DISTINCT colname` if needed
|
|
146
|
+
*/
|
|
147
|
+
getFoundRowsQuery(countExpr?: string): SelectBuilder;
|
|
148
|
+
/**
|
|
149
|
+
* Add an array of column names to fetch
|
|
150
|
+
* @param columnNames The names of columns
|
|
151
|
+
*/
|
|
152
|
+
select(columnNames: string[]): this;
|
|
153
|
+
/**
|
|
154
|
+
* Add an array of column names to fetch
|
|
155
|
+
* @param columnNames The names of columns
|
|
156
|
+
*/
|
|
157
|
+
columns(columnNames: string[]): this;
|
|
158
|
+
/**
|
|
159
|
+
* Add a column name to fetch
|
|
160
|
+
* @param columnName The name of the column
|
|
161
|
+
*/
|
|
162
|
+
column(columnName: string): this;
|
|
163
|
+
/**
|
|
164
|
+
* Add an option expression such as "TOP 10" or "SQL_CALC_FOUND_ROWS"
|
|
165
|
+
* @param optionExpression Expression to go after "SELECT" and before column list
|
|
166
|
+
*/
|
|
167
|
+
option(optionExpression: string): this;
|
|
168
|
+
/**
|
|
169
|
+
* Add a table to the "FROM" clause (same as .from())
|
|
170
|
+
* @param {String} tableName The name of the table to query
|
|
171
|
+
* @return {Select}
|
|
172
|
+
*/
|
|
173
|
+
table(tableName: string): this;
|
|
174
|
+
/**
|
|
175
|
+
* Add multiple table to the "FROM" clause
|
|
176
|
+
* @param {Array} tableNames The names of the tables to query
|
|
177
|
+
* @return {Select}
|
|
178
|
+
*/
|
|
179
|
+
tables(tableNames: string[]): this;
|
|
180
|
+
/**
|
|
181
|
+
* Add a table to the "FROM" clause (same as .table())
|
|
182
|
+
* @param {String} tableName The name of the table to query
|
|
183
|
+
* @return {Select}
|
|
184
|
+
*/
|
|
185
|
+
from(tableName: string): this;
|
|
186
|
+
/**
|
|
187
|
+
* Add the given join type expression
|
|
188
|
+
* @param joinType The type of join (e.g. "INNER JOIN", "LEFT JOIN", etc.)
|
|
189
|
+
* @param expression The expression following the INNER JOIN keyword
|
|
190
|
+
* @param bindings A list of values to bind to ? placeholders in the expression
|
|
191
|
+
* @example
|
|
192
|
+
* query.join('posts p ON p.id = c.post_id');
|
|
193
|
+
* query.join('posts p ON p.id = c.post_id AND p.deleted_at < ?', [myDate]);
|
|
194
|
+
*/
|
|
195
|
+
_addJoin(joinType: string, expression: string, bindings?: any[]): void;
|
|
196
|
+
/**
|
|
197
|
+
* Add a JOIN expression
|
|
198
|
+
* @param expression The expression following the INNER JOIN keyword
|
|
199
|
+
* @param bindings A list of values to bind to ? placeholders in the expression
|
|
200
|
+
* @example
|
|
201
|
+
* query.join('posts p ON p.id = c.post_id');
|
|
202
|
+
* query.join('posts p ON p.id = c.post_id AND p.deleted_at < ?', [myDate]);
|
|
203
|
+
*/
|
|
204
|
+
join(expression: string, bindings?: any[]): this;
|
|
205
|
+
/**
|
|
206
|
+
* Add a LEFT JOIN expression
|
|
207
|
+
* @param expression The expression following the LEFT JOIN keyword
|
|
208
|
+
* @param bindings A list of values to bind to ? placeholders in the expression
|
|
209
|
+
* @example
|
|
210
|
+
* query.leftJoin('posts p ON p.id = c.post_id');
|
|
211
|
+
* query.leftJoin('posts p ON p.id = c.post_id AND p.deleted_at < ?', [myDate]);
|
|
212
|
+
*/
|
|
213
|
+
leftJoin(expression: string, bindings?: any[]): this;
|
|
214
|
+
/**
|
|
215
|
+
* Add a FULL JOIN expression
|
|
216
|
+
* @param expression The expression following the FULL JOIN keyword
|
|
217
|
+
* @param bindings A list of values to bind to ? placeholders in the expression
|
|
218
|
+
* @example
|
|
219
|
+
* query.fullJoin('posts p ON p.id = c.post_id');
|
|
220
|
+
* query.fullJoin('posts p ON p.id = c.post_id AND p.deleted_at < ?', [myDate]);
|
|
221
|
+
*/
|
|
222
|
+
fullJoin(expression: string, bindings?: any[]): this;
|
|
223
|
+
/**
|
|
224
|
+
* Add a RIGHT JOIN expression
|
|
225
|
+
* @param expression The expression following the RIGHT JOIN keyword
|
|
226
|
+
* @param bindings A list of values to bind to ? placeholders in the expression
|
|
227
|
+
* @example
|
|
228
|
+
* query.rightJoin('posts p ON p.id = c.post_id');
|
|
229
|
+
* query.rightJoin('posts p ON p.id = c.post_id AND p.deleted_at < ?', [myDate]);
|
|
230
|
+
*/
|
|
231
|
+
rightJoin(expression: string, bindings?: any[]): this;
|
|
232
|
+
/**
|
|
233
|
+
* Add a CROSS JOIN expression
|
|
234
|
+
* @param expression The expression following the CROSS JOIN keyword
|
|
235
|
+
* @param bindings A list of values to bind to ? placeholders in the expression
|
|
236
|
+
* @example
|
|
237
|
+
* query.crossJoin('posts p ON p.id = c.post_id');
|
|
238
|
+
* query.crossJoin('posts p ON p.id = c.post_id AND p.deleted_at < ?', [myDate]);
|
|
239
|
+
*/
|
|
240
|
+
crossJoin(expression: string, bindings?: any[]): this;
|
|
241
|
+
/**
|
|
242
|
+
* Add an INNER JOIN expression (same as ->join())
|
|
243
|
+
* @param expression The expression following the INNER JOIN keyword
|
|
244
|
+
* @param bindings A list of values to bind to ? placeholders in the expression
|
|
245
|
+
* @example
|
|
246
|
+
* query.innerJoin('posts p ON p.id = c.post_id');
|
|
247
|
+
* query.innerJoin('posts p ON p.id = c.post_id AND p.deleted_at < ?', [myDate]);
|
|
248
|
+
*/
|
|
249
|
+
innerJoin(expression: string, bindings?: any[]): this;
|
|
250
|
+
/**
|
|
251
|
+
* Add a LEFT OUTER JOIN expression
|
|
252
|
+
* @param expression The expression following the LEFT OUTER JOIN keyword
|
|
253
|
+
* @param bindings A list of values to bind to ? placeholders in the expression
|
|
254
|
+
* @example
|
|
255
|
+
* query.leftOuterJoin('posts p ON p.id = c.post_id');
|
|
256
|
+
* query.leftOuterJoin('posts p ON p.id = c.post_id AND p.deleted_at < ?', [myDate]);
|
|
257
|
+
*/
|
|
258
|
+
leftOuterJoin(expression: string, bindings?: any[]): this;
|
|
259
|
+
/**
|
|
260
|
+
* Add a FULL OUTER JOIN expression
|
|
261
|
+
* @param expression The expression following the FULL OUTER JOIN keyword
|
|
262
|
+
* @param bindings A list of values to bind to ? placeholders in the expression
|
|
263
|
+
* @example
|
|
264
|
+
* query.fullOuterJoin('posts p ON p.id = c.post_id');
|
|
265
|
+
* query.fullOuterJoin('posts p ON p.id = c.post_id AND p.deleted_at < ?', [myDate]);
|
|
266
|
+
*/
|
|
267
|
+
fullOuterJoin(expression: string, bindings?: any[]): this;
|
|
268
|
+
/**
|
|
269
|
+
* Add a RIGHT OUTER JOIN expression
|
|
270
|
+
* @param expression The expression following the RIGHT OUTER JOIN keyword
|
|
271
|
+
* @param bindings A list of values to bind to ? placeholders in the expression
|
|
272
|
+
* @example
|
|
273
|
+
* query.rightOuterJoin('posts p ON p.id = c.post_id');
|
|
274
|
+
* query.rightOuterJoin('posts p ON p.id = c.post_id AND p.deleted_at < ?', [myDate]);
|
|
275
|
+
*/
|
|
276
|
+
rightOuterJoin(expression: string, bindings?: any[]): this;
|
|
277
|
+
/**
|
|
278
|
+
* Remove a join condition with the specified table
|
|
279
|
+
* @param table The name of the table or tables in the first part of the join statement
|
|
280
|
+
*/
|
|
281
|
+
unjoin(table: string | string[]): this;
|
|
282
|
+
/**
|
|
283
|
+
* Utility function to add conditions for a clause (WHERE, HAVING)
|
|
284
|
+
* @param collection The collection to add the clauses to (e.g. this._wheres or this._havings)
|
|
285
|
+
* @param criteria Criteria adhering to WhereCriteria type
|
|
286
|
+
* @example The following are equivalent
|
|
287
|
+
* this._conditions(this._wheres, ['deleted_at IS NULL']);
|
|
288
|
+
* this._conditions(this._wheres, ['deleted_at', null]);
|
|
289
|
+
* this._conditions(this._wheres, ['deleted_at', '=', null]);
|
|
290
|
+
* @example More examples
|
|
291
|
+
* this._conditions(this._wheres, ['fname', 'LIKE', 'joe']); // `fname` LIKE 'joe'
|
|
292
|
+
* this._conditions(this._wheres, ['fname', 'LIKE ?', 'joe']); // `fname` LIKE 'joe'
|
|
293
|
+
* this._conditions(this._wheres, ['fname LIKE %?%', 'joe']); // `fname` LIKE '%joe%'
|
|
294
|
+
* this._conditions(this._wheres, ['fname LIKE ?%', 'joe']); // `fname` LIKE 'joe%'
|
|
295
|
+
* this._conditions(this._wheres, ['fname', 'LIKE ?%', 'joe']); // `fname` LIKE 'joe%'
|
|
296
|
+
* this._conditions(this._wheres, ['price >', 10]); // `price` > 10
|
|
297
|
+
* this._conditions(this._wheres, ['price', '>', 10]); // `price` > 10
|
|
298
|
+
* this._conditions(this._wheres, ['price =', 10]); // `price` = 10
|
|
299
|
+
* this._conditions(this._wheres, ['price !=', 10]); // `price` != 10
|
|
300
|
+
* this._conditions(this._wheres, ['price', 10]); // `price` = 10
|
|
301
|
+
* this._conditions(this._wheres, ['price', '=', 10]); // `price` = 10
|
|
302
|
+
* this._conditions(this._wheres, ['price', '!=', 10]); // `price` != 10
|
|
303
|
+
* this._conditions(this._wheres, ['price', 'BETWEEN', [10,20]]); // `price` BETWEEN 10 AND 20
|
|
304
|
+
* this._conditions(this._wheres, ['price', 'NOT BETWEEN', [10,20]]); // `price` NOT BETWEEN 10 AND 20
|
|
305
|
+
* this._conditions(this._wheres, ['price', [10,20]]); // `price` IN(10,20)
|
|
306
|
+
* this._conditions(this._wheres, ['price', '=', [10,20]]); // `price` IN(10,20)
|
|
307
|
+
* this._conditions(this._wheres, ['price', 'IN', [10,20]]); // `price` IN(10,20)
|
|
308
|
+
* this._conditions(this._wheres, ['price', 'NOT IN', [10,20]]); // `price` NOT IN(10,20)
|
|
309
|
+
*/
|
|
310
|
+
_conditions(collection: string[], criteria: WhereCriteria): this;
|
|
311
|
+
/**
|
|
312
|
+
* Add a group by column or expression
|
|
313
|
+
* @param column The name of a column (or expression) to group by
|
|
314
|
+
*/
|
|
315
|
+
groupBy(column: string): this;
|
|
316
|
+
/**
|
|
317
|
+
* Add WHERE clauses to conditions (See _conditions for usage)
|
|
318
|
+
*/
|
|
319
|
+
where(...args: WhereCriteria): this;
|
|
320
|
+
/**
|
|
321
|
+
* Add a WHERE clause with a BETWEEN condition
|
|
322
|
+
* @param column The column name
|
|
323
|
+
* @param twoValueArray The two values to be between
|
|
324
|
+
*/
|
|
325
|
+
whereBetween(column: string, twoValueArray: [
|
|
326
|
+
any,
|
|
327
|
+
any
|
|
328
|
+
]): this;
|
|
329
|
+
/**
|
|
330
|
+
* Add a series of WHERE clauses joined by OR (See _conditions for usage)
|
|
331
|
+
* @param conditions
|
|
332
|
+
*/
|
|
333
|
+
_orConditions(conditions: WhereCriteria[] | Record<string, any>[] | Record<string, any>): string;
|
|
334
|
+
/**
|
|
335
|
+
* Add WHERE conditions to place inside an OR block (See _conditions for usage)
|
|
336
|
+
* @param conditions A list or object where each item is an array with parameters that would be taken by where()
|
|
337
|
+
*/
|
|
338
|
+
orWhere(conditions: WhereCriteria[] | Record<string, any>[] | Record<string, any>): this;
|
|
339
|
+
/**
|
|
340
|
+
* Add an OR with conditions under the HAVING clause
|
|
341
|
+
* @param conditions A list or object where each item is an array with parameters that would be taken by having()
|
|
342
|
+
*/
|
|
343
|
+
orHaving(conditions: WhereCriteria[] | Record<string, any>[] | Record<string, any>): this;
|
|
344
|
+
/**
|
|
345
|
+
* Add a HAVING condition (See _conditions for usage)
|
|
346
|
+
*/
|
|
347
|
+
having(...args: WhereCriteria): this;
|
|
348
|
+
/**
|
|
349
|
+
* Add a column or expression to order by
|
|
350
|
+
* @param column The column name or expression to sort by. Include DESC or prefix with - to sort descending
|
|
351
|
+
* @param [direction] The direction to sort by (ASC or DESC)
|
|
352
|
+
* @example
|
|
353
|
+
* query.sortField('modified_at'); // ORDER BY modified_at
|
|
354
|
+
* query.sortField('-modified_at'); // ORDER BY modified_at DESC
|
|
355
|
+
* query.sortField('COUNT(*)'); // ORDER BY COUNT(*)
|
|
356
|
+
* query.sortField('COUNT(*) DESC'); // ORDER BY COUNT(*) DESC
|
|
357
|
+
*/
|
|
358
|
+
orderBy(column: string, direction?: "ASC" | "DESC"): this;
|
|
359
|
+
/**
|
|
360
|
+
* Sort by the given column, with a map of columns to translate
|
|
361
|
+
* @param fieldName The file name such as "created_at" or "-created_at" for descending
|
|
362
|
+
* @param [mapNames] Column names to translate from field to column expression
|
|
363
|
+
* @example
|
|
364
|
+
* query.sortField('-modified_at'); // ORDER BY modified_at DESC
|
|
365
|
+
* query.sortField('-created', {'created': 'created_at'}); // ORDER BY created_at DESC
|
|
366
|
+
*/
|
|
367
|
+
sortField(fieldName: string, mapNames?: Record<string, string>): this;
|
|
368
|
+
/**
|
|
369
|
+
* Check to see if the given string is 0 or all digits not starting with 0
|
|
370
|
+
* @param string The input string
|
|
371
|
+
* @private
|
|
372
|
+
*/
|
|
373
|
+
_isEntirelyDigits(string: string): boolean;
|
|
374
|
+
/**
|
|
375
|
+
* Check to see if the given string is all digits
|
|
376
|
+
* @param string The input string
|
|
377
|
+
* @private
|
|
378
|
+
*/
|
|
379
|
+
_isEntirelyDigitsNoZeros(string: string): boolean;
|
|
380
|
+
/**
|
|
381
|
+
* Limit results to the given number
|
|
382
|
+
* @param max The number to limit by (placeholder string or integer greater than 0)
|
|
383
|
+
*/
|
|
384
|
+
limit(max: number | string): this;
|
|
385
|
+
/**
|
|
386
|
+
* Fetch results from the given offset
|
|
387
|
+
* @param number The offset (placeholder string or integer greater than or equal to 0)
|
|
388
|
+
*/
|
|
389
|
+
offset(number: number | string): this;
|
|
390
|
+
/**
|
|
391
|
+
* Set the offset based on the limit with the given page number
|
|
392
|
+
* @param number The page number (integer greater than 0)
|
|
393
|
+
*/
|
|
394
|
+
page(number: number | string): this;
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Parse SQL and populate onto a Select query object
|
|
398
|
+
*/
|
|
399
|
+
export declare class Parser {
|
|
400
|
+
query: SelectBuilder;
|
|
401
|
+
/**
|
|
402
|
+
* Create a new instance
|
|
403
|
+
* @param query A Select object on which to build parsed conditions
|
|
404
|
+
*/
|
|
405
|
+
constructor(query: SelectBuilder);
|
|
406
|
+
/**
|
|
407
|
+
* Strip single-line and multi-line comment blocks
|
|
408
|
+
* @param sql The SQL string
|
|
409
|
+
* @return The SQL string without comments
|
|
410
|
+
* @private
|
|
411
|
+
*/
|
|
412
|
+
_stripComments(sql: string): string;
|
|
413
|
+
/**
|
|
414
|
+
* Before splitting into SQL clauses, extract some regex-able subQueries
|
|
415
|
+
* @param sql The unparsed sql string
|
|
416
|
+
* @return An array with new sql and subQueries
|
|
417
|
+
* @private
|
|
418
|
+
*/
|
|
419
|
+
_extractSubQueries(sql: string): {
|
|
420
|
+
sql: string;
|
|
421
|
+
subQueries: {};
|
|
422
|
+
};
|
|
423
|
+
/**
|
|
424
|
+
* Inject column subQueries back into this object
|
|
425
|
+
* @param subQueries The lookup of extracted subQueries
|
|
426
|
+
* @private
|
|
427
|
+
*/
|
|
428
|
+
_injectSubQueries(subQueries: Record<string, string>): void;
|
|
429
|
+
/**
|
|
430
|
+
* Split SQL into clauses (used by parse())
|
|
431
|
+
* @param sql The SQL to split
|
|
432
|
+
* @return
|
|
433
|
+
* @private
|
|
434
|
+
*/
|
|
435
|
+
_split(sql: string): string[];
|
|
436
|
+
/**
|
|
437
|
+
* Get a QuickSelect object representing the given SQL SELECT statement
|
|
438
|
+
* @param rawSql The raw SQL for the SELECT statement
|
|
439
|
+
* @return True if the SQL was parsed successfully
|
|
440
|
+
*/
|
|
441
|
+
parse(rawSql: string): boolean;
|
|
442
|
+
/**
|
|
443
|
+
* Handle SQL_CALC_FOUND_ROWS and column names
|
|
444
|
+
* @param clause The clause after the SELECT
|
|
445
|
+
* @private
|
|
446
|
+
*/
|
|
447
|
+
_handleSELECT(clause: string): void;
|
|
448
|
+
/**
|
|
449
|
+
* Handle table names
|
|
450
|
+
* @param clause The clause after the FROM
|
|
451
|
+
* @private
|
|
452
|
+
*/
|
|
453
|
+
_handleFROM(clause: string): void;
|
|
454
|
+
/**
|
|
455
|
+
* Handle INNER JOIN statements
|
|
456
|
+
* @param clause The clause after the INNER JOIN
|
|
457
|
+
* @private
|
|
458
|
+
*/
|
|
459
|
+
_handleINNER_JOIN(clause: string): void;
|
|
460
|
+
/**
|
|
461
|
+
* Handle LEFT JOIN statements
|
|
462
|
+
* @param clause The clause after the LEFT JOIN
|
|
463
|
+
* @private
|
|
464
|
+
*/
|
|
465
|
+
_handleLEFT_JOIN(clause: string): void;
|
|
466
|
+
/**
|
|
467
|
+
* Handle LEFT OUTER JOIN statements
|
|
468
|
+
* @param clause The clause after the LEFT OUTER JOIN
|
|
469
|
+
* @private
|
|
470
|
+
*/
|
|
471
|
+
_handleLEFT_OUTER_JOIN(clause: string): void;
|
|
472
|
+
/**
|
|
473
|
+
* Handle RIGHT JOIN statements
|
|
474
|
+
* @param clause The clause after the RIGHT JOIN
|
|
475
|
+
* @private
|
|
476
|
+
*/
|
|
477
|
+
_handleRIGHT_JOIN(clause: string): void;
|
|
478
|
+
/**
|
|
479
|
+
* Handle RIGHT OUTER JOIN statements
|
|
480
|
+
* @param clause The clause after the RIGHT OUTER JOIN
|
|
481
|
+
* @private
|
|
482
|
+
*/
|
|
483
|
+
_handleRIGHT_OUTER_JOIN(clause: string): void;
|
|
484
|
+
/**
|
|
485
|
+
* Handle CROSS JOIN statements
|
|
486
|
+
* @param clause The clause after the CROSS JOIN
|
|
487
|
+
* @private
|
|
488
|
+
*/
|
|
489
|
+
_handleCROSS_JOIN(clause: string): void;
|
|
490
|
+
/**
|
|
491
|
+
* Handle FULL JOIN statements
|
|
492
|
+
* @param clause The clause after the FULL JOIN
|
|
493
|
+
* @private
|
|
494
|
+
*/
|
|
495
|
+
_handleFULL_JOIN(clause: string): void;
|
|
496
|
+
/**
|
|
497
|
+
* Handle FULL OUTER JOIN statements
|
|
498
|
+
* @param clause The clause after the FULL OUTER JOIN
|
|
499
|
+
* @private
|
|
500
|
+
*/
|
|
501
|
+
_handleFULL_OUTER_JOIN(clause: string): void;
|
|
502
|
+
/**
|
|
503
|
+
* Handle WHERE conditions
|
|
504
|
+
* @param clause All the conditions after WHERE
|
|
505
|
+
* @private
|
|
506
|
+
*/
|
|
507
|
+
_handleWHERE(clause: string): void;
|
|
508
|
+
/**
|
|
509
|
+
* Handle HAVING statements
|
|
510
|
+
* @param clause All the conditions after HAVING
|
|
511
|
+
* @private
|
|
512
|
+
*/
|
|
513
|
+
_handleHAVING(clause: string): void;
|
|
514
|
+
/**
|
|
515
|
+
* Build a conditions list
|
|
516
|
+
* @param type Either where or having
|
|
517
|
+
* @param clause The expressions following the type keyword
|
|
518
|
+
* @private
|
|
519
|
+
*/
|
|
520
|
+
_handleConditions(type: "where" | "having", clause: string): void;
|
|
521
|
+
/**
|
|
522
|
+
* Handle GROUP BY statements
|
|
523
|
+
* @param clause The clauses after the GROUP BY
|
|
524
|
+
* @private
|
|
525
|
+
*/
|
|
526
|
+
_handleGROUP_BY(clause: string): void;
|
|
527
|
+
/**
|
|
528
|
+
* Handle ORDER BY statements
|
|
529
|
+
* @param clause The clause after the ORDER BY
|
|
530
|
+
* @private
|
|
531
|
+
*/
|
|
532
|
+
_handleORDER_BY(clause: string): void;
|
|
533
|
+
/**
|
|
534
|
+
* Handle LIMIT statements including "LIMIT #" and "LIMIT #, #"
|
|
535
|
+
* @param clause The clause after the LIMIT
|
|
536
|
+
* @private
|
|
537
|
+
*/
|
|
538
|
+
_handleLIMIT(clause: string): void;
|
|
539
|
+
/**
|
|
540
|
+
* Handle OFFSET statements
|
|
541
|
+
* @param clause The number after the OFFSET
|
|
542
|
+
* @private
|
|
543
|
+
*/
|
|
544
|
+
_handleOFFSET(clause: string): void;
|
|
545
|
+
/**
|
|
546
|
+
* Handle OFFSET statements
|
|
547
|
+
* @param clause The number after the OFFSET
|
|
548
|
+
* @private
|
|
549
|
+
*/
|
|
550
|
+
_handleFETCH_NEXT(clause: string): void;
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* Group database rows by a column value
|
|
554
|
+
* @param field The database column to group by
|
|
555
|
+
* @param records Records from the database
|
|
556
|
+
* @example
|
|
557
|
+
* extractGrouped('org', users); // result:
|
|
558
|
+
* {
|
|
559
|
+
* "Marketing": [
|
|
560
|
+
* { id: 1, name: "John", org: "Marketing" },
|
|
561
|
+
* { id: 2, name: "Jane", org: "Marketing" },
|
|
562
|
+
* ],
|
|
563
|
+
* "Finance": [
|
|
564
|
+
* { id: 3, name: "Jose", org: "Finance" },
|
|
565
|
+
* ],
|
|
566
|
+
* }
|
|
567
|
+
*/
|
|
568
|
+
export function extractGrouped<DbRecord extends Record<string, any>>(field: keyof DbRecord, records: DbRecord[]): Record<string, DbRecord[]>;
|
|
569
|
+
/**
|
|
570
|
+
* Index each database row by a column value
|
|
571
|
+
* @param field The database column to index by
|
|
572
|
+
* @param records Records from the database
|
|
573
|
+
* @example
|
|
574
|
+
* extractIndexed('id', users); // result:
|
|
575
|
+
* {
|
|
576
|
+
* "1": { id: 1, name: "John" },
|
|
577
|
+
* "2": { id: 2, name: "Jane" },
|
|
578
|
+
* }
|
|
579
|
+
*/
|
|
580
|
+
export function extractIndexed<DbRecord extends Record<string, any>>(field: keyof DbRecord, records: DbRecord[]): Record<string, DbRecord>;
|
|
581
|
+
/**
|
|
582
|
+
* Given an array of records, extract an object with keyField-valueField pairs
|
|
583
|
+
* @param records An array of records
|
|
584
|
+
* @param keyField The field to use as the key
|
|
585
|
+
* @param valueField The field to use as the value
|
|
586
|
+
*/
|
|
587
|
+
export function extractLookup<T extends Array<Record<K | V, any>>, K extends keyof T[number], V extends keyof T[number]>(keyField: K, valueField: V, records: T): {
|
|
588
|
+
[key in T[number][K]]: T[number][V];
|
|
589
|
+
};
|
|
590
|
+
/**
|
|
591
|
+
* Given a foundRows number of records and a limit, return the foundRows number of pages
|
|
592
|
+
* @param query The query that was just run
|
|
593
|
+
* @param total The number of records if there were no limit
|
|
594
|
+
*/
|
|
595
|
+
export function getPagination(query: SelectBuilder, total: BigInt | number): {
|
|
596
|
+
page: number;
|
|
597
|
+
prevPage: number;
|
|
598
|
+
nextPage: number;
|
|
599
|
+
perPage: number;
|
|
600
|
+
numPages: number;
|
|
601
|
+
total: number;
|
|
602
|
+
isFirst: boolean;
|
|
603
|
+
isLast: boolean;
|
|
604
|
+
};
|
|
605
|
+
export interface MysqlClient {
|
|
606
|
+
query: (sql: string, bindings: any[], callback: (err: Error | null, results: Array<Record<string, any>>, fields: any[]) => void) => void;
|
|
607
|
+
}
|
|
608
|
+
export interface MysqlAsyncClient {
|
|
609
|
+
query: (sql: string, bindings: any[]) => Promise<[
|
|
610
|
+
Array<Record<string, any>>,
|
|
611
|
+
any[]
|
|
612
|
+
]>;
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* Run the given query with mysql and return the results
|
|
616
|
+
* @param mysqlClient A client created with mysql.createConnection() or mysql.createPool()
|
|
617
|
+
* @param query The SelectBuilder query to run
|
|
618
|
+
*/
|
|
619
|
+
export declare function runMysql(mysqlClient: MysqlClient, query: SelectBuilder): Promise<[
|
|
620
|
+
Array<Record<string, any>>,
|
|
621
|
+
any[]
|
|
622
|
+
]>;
|
|
623
|
+
/**
|
|
624
|
+
* Run the given query with mysql and return the results, count, and pagination
|
|
625
|
+
* @param mysqlClient A client created with mysql.createConnection() or mysql.createPool()
|
|
626
|
+
* @param query The SelectBuilder query to run
|
|
627
|
+
* @param options
|
|
628
|
+
* @param options.countExpr The SQL expression to use for the count (default "*")
|
|
629
|
+
*/
|
|
630
|
+
export declare function runMysqlWithCount(mysqlClient: MysqlClient, query: SelectBuilder, { countExpr }?: {
|
|
631
|
+
countExpr?: string;
|
|
632
|
+
}): Promise<{
|
|
633
|
+
records: Record<string, any>[];
|
|
634
|
+
total: any;
|
|
635
|
+
pagination: {
|
|
636
|
+
page: number;
|
|
637
|
+
prevPage: number;
|
|
638
|
+
nextPage: number;
|
|
639
|
+
perPage: number;
|
|
640
|
+
numPages: number;
|
|
641
|
+
total: number;
|
|
642
|
+
isFirst: boolean;
|
|
643
|
+
isLast: boolean;
|
|
644
|
+
};
|
|
645
|
+
fields: any[];
|
|
646
|
+
}>;
|
|
647
|
+
export declare function runMysqlAsync(mysqlClient: MysqlAsyncClient, query: SelectBuilder): Promise<[
|
|
648
|
+
Array<Record<string, any>>,
|
|
649
|
+
any[]
|
|
650
|
+
]>;
|
|
651
|
+
export declare function runMysqlAsyncWithCount(mysqlClient: MysqlAsyncClient, query: SelectBuilder, { engine, countExpr, }?: {
|
|
652
|
+
engine?: EngineStyle;
|
|
653
|
+
countExpr?: string;
|
|
654
|
+
}): Promise<{
|
|
655
|
+
records: Record<string, any>[];
|
|
656
|
+
total: any;
|
|
657
|
+
pagination: {
|
|
658
|
+
page: number;
|
|
659
|
+
prevPage: number;
|
|
660
|
+
nextPage: number;
|
|
661
|
+
perPage: number;
|
|
662
|
+
numPages: number;
|
|
663
|
+
total: number;
|
|
664
|
+
isFirst: boolean;
|
|
665
|
+
isLast: boolean;
|
|
666
|
+
};
|
|
667
|
+
fields: any[];
|
|
668
|
+
}>;
|
|
669
|
+
export interface PrismaClient {
|
|
670
|
+
$queryRawUnsafe: (sql: string, ...bindings: any[]) => Promise<Array<Record<string, any>>>;
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Run the given query with prisma and return the results
|
|
674
|
+
* @param prisma The prisma client instance
|
|
675
|
+
* @param query The SelectBuilder query to run
|
|
676
|
+
* @param options
|
|
677
|
+
* @param options.engine The engine style to use ("mssql" | "mysql" | "oracle" | "pg")
|
|
678
|
+
*/
|
|
679
|
+
export declare function runPrisma<T extends Array<Record<string, any>> = Array<Record<string, any>>>(prisma: PrismaClient, query: SelectBuilder, { engine }?: {
|
|
680
|
+
engine?: EngineStyle;
|
|
681
|
+
}): Promise<T>;
|
|
682
|
+
/**
|
|
683
|
+
* Run the given query with prisma and return the results, count, and pagination
|
|
684
|
+
* @param prisma The prisma client instance
|
|
685
|
+
* @param query The SelectBuilder query to run
|
|
686
|
+
* @param options
|
|
687
|
+
* @param options.engine The engine style to use ("mssql" | "mysql" | "oracle" | "pg")
|
|
688
|
+
* @param options.countExpr The SQL expression to use for the count (default "*")
|
|
689
|
+
*/
|
|
690
|
+
export declare function runPrismaWithCount<T extends Array<Record<string, any>> = Array<Record<string, any>>>(prisma: PrismaClient, query: SelectBuilder, { engine, countExpr, }?: {
|
|
691
|
+
engine?: EngineStyle;
|
|
692
|
+
countExpr?: string;
|
|
693
|
+
}): Promise<{
|
|
694
|
+
records: T;
|
|
695
|
+
total: number;
|
|
696
|
+
pagination: {
|
|
697
|
+
page: number;
|
|
698
|
+
prevPage: number;
|
|
699
|
+
nextPage: number;
|
|
700
|
+
perPage: number;
|
|
701
|
+
numPages: number;
|
|
702
|
+
total: number;
|
|
703
|
+
isFirst: boolean;
|
|
704
|
+
isLast: boolean;
|
|
705
|
+
};
|
|
706
|
+
}>;
|
|
707
|
+
export type SafeValue<T> = T extends BigInt ? number | string : T;
|
|
708
|
+
export type SafeRecord<T> = Record<string, SafeValue<T>>;
|
|
709
|
+
export type SafeRecords<T> = Array<SafeRecord<T>>;
|
|
710
|
+
/**
|
|
711
|
+
* Convert a value to a value that can be safely serialized to JSON
|
|
712
|
+
* @param value
|
|
713
|
+
*/
|
|
714
|
+
export declare function toSafeJsonValue<T>(value: T): SafeValue<T>;
|
|
715
|
+
/**
|
|
716
|
+
* Convert a record to one that can be safely serialized to JSON
|
|
717
|
+
* @param record
|
|
718
|
+
*/
|
|
719
|
+
export declare function toSafeJsonRecord<T extends Record<string, any>>(record: T): SafeRecord<T>;
|
|
720
|
+
/**
|
|
721
|
+
* Convert a recordset to one that can be safely serialized to JSON
|
|
722
|
+
* @param records
|
|
723
|
+
*/
|
|
724
|
+
export declare function toSafeJsonRecords<T extends Record<string, any>>(records: T[]): SafeRecords<T>;
|
|
725
|
+
/**
|
|
726
|
+
* JSON stringify a value or data structure, ensuring first that all values are
|
|
727
|
+
* safe to serialize
|
|
728
|
+
* @param records
|
|
729
|
+
*/
|
|
730
|
+
export declare function toSafeJsonString(records: Array<Record<string, any>>): string;
|
|
731
|
+
|
|
732
|
+
export {};
|