knex 0.21.20 → 0.21.21

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.
Files changed (141) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/CONTRIBUTING.md +184 -184
  3. package/LICENSE +22 -22
  4. package/README.md +95 -95
  5. package/bin/cli.js +414 -414
  6. package/bin/utils/cli-config-utils.js +151 -151
  7. package/bin/utils/constants.js +7 -7
  8. package/bin/utils/migrationsLister.js +37 -37
  9. package/knex.js +8 -8
  10. package/lib/client.js +413 -413
  11. package/lib/config-resolver.js +61 -61
  12. package/lib/constants.js +44 -44
  13. package/lib/dialects/mssql/index.js +390 -390
  14. package/lib/dialects/mssql/query/compiler.js +444 -444
  15. package/lib/dialects/mssql/schema/columncompiler.js +103 -103
  16. package/lib/dialects/mssql/schema/compiler.js +59 -59
  17. package/lib/dialects/mssql/schema/tablecompiler.js +245 -245
  18. package/lib/dialects/mssql/transaction.js +97 -97
  19. package/lib/dialects/mysql/index.js +191 -191
  20. package/lib/dialects/mysql/query/compiler.js +142 -142
  21. package/lib/dialects/mysql/schema/columncompiler.js +171 -171
  22. package/lib/dialects/mysql/schema/compiler.js +60 -60
  23. package/lib/dialects/mysql/schema/tablecompiler.js +262 -262
  24. package/lib/dialects/mysql/transaction.js +48 -48
  25. package/lib/dialects/mysql2/index.js +35 -35
  26. package/lib/dialects/mysql2/transaction.js +46 -46
  27. package/lib/dialects/oracle/DEAD_CODE.md +5 -5
  28. package/lib/dialects/oracle/formatter.js +20 -20
  29. package/lib/dialects/oracle/index.js +79 -79
  30. package/lib/dialects/oracle/query/compiler.js +327 -327
  31. package/lib/dialects/oracle/schema/columnbuilder.js +18 -18
  32. package/lib/dialects/oracle/schema/columncompiler.js +139 -139
  33. package/lib/dialects/oracle/schema/compiler.js +81 -81
  34. package/lib/dialects/oracle/schema/tablecompiler.js +165 -165
  35. package/lib/dialects/oracle/schema/trigger.js +126 -126
  36. package/lib/dialects/oracle/utils.js +86 -86
  37. package/lib/dialects/oracledb/index.js +489 -489
  38. package/lib/dialects/oracledb/query/compiler.js +363 -363
  39. package/lib/dialects/oracledb/schema/columncompiler.js +35 -35
  40. package/lib/dialects/oracledb/transaction.js +76 -76
  41. package/lib/dialects/oracledb/utils.js +14 -14
  42. package/lib/dialects/postgres/index.js +319 -319
  43. package/lib/dialects/postgres/query/compiler.js +206 -206
  44. package/lib/dialects/postgres/schema/columncompiler.js +125 -125
  45. package/lib/dialects/postgres/schema/compiler.js +109 -109
  46. package/lib/dialects/postgres/schema/tablecompiler.js +183 -183
  47. package/lib/dialects/redshift/index.js +73 -73
  48. package/lib/dialects/redshift/query/compiler.js +119 -119
  49. package/lib/dialects/redshift/schema/columnbuilder.js +20 -20
  50. package/lib/dialects/redshift/schema/columncompiler.js +60 -60
  51. package/lib/dialects/redshift/schema/compiler.js +14 -14
  52. package/lib/dialects/redshift/schema/tablecompiler.js +123 -123
  53. package/lib/dialects/redshift/transaction.js +18 -18
  54. package/lib/dialects/sqlite3/formatter.js +21 -21
  55. package/lib/dialects/sqlite3/index.js +169 -169
  56. package/lib/dialects/sqlite3/query/compiler.js +222 -222
  57. package/lib/dialects/sqlite3/schema/columncompiler.js +27 -27
  58. package/lib/dialects/sqlite3/schema/compiler.js +49 -49
  59. package/lib/dialects/sqlite3/schema/ddl.js +525 -525
  60. package/lib/dialects/sqlite3/schema/tablecompiler.js +238 -238
  61. package/lib/formatter.js +295 -295
  62. package/lib/functionhelper.js +14 -14
  63. package/lib/helpers.js +92 -92
  64. package/lib/index.js +3 -3
  65. package/lib/interface.js +115 -115
  66. package/lib/knex.js +42 -42
  67. package/lib/logger.js +76 -76
  68. package/lib/migrate/MigrationGenerator.js +82 -82
  69. package/lib/migrate/Migrator.js +611 -611
  70. package/lib/migrate/configuration-merger.js +60 -60
  71. package/lib/migrate/migrate-stub.js +17 -17
  72. package/lib/migrate/migration-list-resolver.js +36 -36
  73. package/lib/migrate/sources/fs-migrations.js +99 -99
  74. package/lib/migrate/stub/cjs.stub +15 -15
  75. package/lib/migrate/stub/coffee.stub +13 -13
  76. package/lib/migrate/stub/eg.stub +14 -14
  77. package/lib/migrate/stub/js.stub +15 -15
  78. package/lib/migrate/stub/knexfile-coffee.stub +34 -34
  79. package/lib/migrate/stub/knexfile-eg.stub +43 -43
  80. package/lib/migrate/stub/knexfile-js.stub +44 -44
  81. package/lib/migrate/stub/knexfile-ls.stub +35 -35
  82. package/lib/migrate/stub/knexfile-ts.stub +44 -44
  83. package/lib/migrate/stub/ls.stub +14 -14
  84. package/lib/migrate/stub/ts.stub +21 -21
  85. package/lib/migrate/table-creator.js +67 -67
  86. package/lib/migrate/table-resolver.js +27 -27
  87. package/lib/query/builder.js +1372 -1372
  88. package/lib/query/compiler.js +889 -889
  89. package/lib/query/constants.js +13 -13
  90. package/lib/query/joinclause.js +263 -263
  91. package/lib/query/methods.js +92 -92
  92. package/lib/query/string.js +190 -190
  93. package/lib/raw.js +188 -188
  94. package/lib/ref.js +39 -39
  95. package/lib/runner.js +285 -285
  96. package/lib/schema/builder.js +82 -82
  97. package/lib/schema/columnbuilder.js +117 -117
  98. package/lib/schema/columncompiler.js +177 -177
  99. package/lib/schema/compiler.js +101 -101
  100. package/lib/schema/helpers.js +51 -51
  101. package/lib/schema/tablebuilder.js +288 -288
  102. package/lib/schema/tablecompiler.js +296 -296
  103. package/lib/seed/Seeder.js +203 -203
  104. package/lib/seed/seed-stub.js +13 -13
  105. package/lib/seed/stub/coffee.stub +9 -9
  106. package/lib/seed/stub/eg.stub +11 -11
  107. package/lib/seed/stub/js.stub +13 -13
  108. package/lib/seed/stub/ls.stub +11 -11
  109. package/lib/seed/stub/ts.stub +13 -13
  110. package/lib/transaction.js +363 -363
  111. package/lib/util/batchInsert.js +59 -59
  112. package/lib/util/delay.js +6 -6
  113. package/lib/util/fake-client.js +9 -9
  114. package/lib/util/finally-mixin.js +13 -13
  115. package/lib/util/fs.js +76 -76
  116. package/lib/util/import-file.js +13 -13
  117. package/lib/util/is-module-type.js +14 -14
  118. package/lib/util/is.js +32 -32
  119. package/lib/util/make-knex.js +338 -338
  120. package/lib/util/nanoid.js +29 -29
  121. package/lib/util/noop.js +1 -1
  122. package/lib/util/parse-connection.js +66 -66
  123. package/lib/util/save-async-stack.js +14 -14
  124. package/lib/util/template.js +52 -52
  125. package/lib/util/timeout.js +29 -29
  126. package/lib/util/timestamp.js +16 -16
  127. package/package.json +1 -1
  128. package/scripts/build.js +125 -125
  129. package/scripts/docker-compose.yml +111 -111
  130. package/scripts/next-release-howto.md +24 -24
  131. package/scripts/release.sh +34 -34
  132. package/scripts/runkit-example.js +34 -34
  133. package/scripts/stress-test/README.txt +18 -18
  134. package/scripts/stress-test/docker-compose.yml +47 -47
  135. package/scripts/stress-test/knex-stress-test.js +196 -196
  136. package/scripts/stress-test/mysql2-random-hanging-every-now-and-then.js +145 -145
  137. package/scripts/stress-test/mysql2-sudden-exit-without-error.js +100 -100
  138. package/scripts/stress-test/reconnect-test-mysql-based-drivers.js +184 -184
  139. package/types/index.d.ts +2249 -2249
  140. package/types/result.d.ts +27 -27
  141. package/types/tables.d.ts +4 -4
package/lib/formatter.js CHANGED
@@ -1,295 +1,295 @@
1
- const QueryBuilder = require('./query/builder');
2
- const Raw = require('./raw');
3
- const transform = require('lodash/transform');
4
-
5
- // Valid values for the `order by` clause generation.
6
- const orderBys = ['asc', 'desc'];
7
-
8
- // Turn this into a lookup map
9
- const operators = transform(
10
- [
11
- '=',
12
- '<',
13
- '>',
14
- '<=',
15
- '>=',
16
- '<>',
17
- '!=',
18
- 'like',
19
- 'not like',
20
- 'between',
21
- 'not between',
22
- 'ilike',
23
- 'not ilike',
24
- 'exists',
25
- 'not exist',
26
- 'rlike',
27
- 'not rlike',
28
- 'regexp',
29
- 'not regexp',
30
- '&',
31
- '|',
32
- '^',
33
- '<<',
34
- '>>',
35
- '~',
36
- '~*',
37
- '!~',
38
- '!~*',
39
- '#',
40
- '&&',
41
- '@>',
42
- '<@',
43
- '||',
44
- '&<',
45
- '&>',
46
- '-|-',
47
- '@@',
48
- '!!',
49
- ['?', '\\?'],
50
- ['?|', '\\?|'],
51
- ['?&', '\\?&'],
52
- ],
53
- (result, key) => {
54
- if (Array.isArray(key)) {
55
- result[key[0]] = key[1];
56
- } else {
57
- result[key] = key;
58
- }
59
- },
60
- {}
61
- );
62
-
63
- class Formatter {
64
- constructor(client, builder) {
65
- this.client = client;
66
- this.builder = builder;
67
- this.bindings = [];
68
- }
69
-
70
- // Accepts a string or array of columns to wrap as appropriate.
71
- columnize(target) {
72
- const columns = Array.isArray(target) ? target : [target];
73
- let str = '',
74
- i = -1;
75
- while (++i < columns.length) {
76
- if (i > 0) str += ', ';
77
- str += this.wrap(columns[i]);
78
- }
79
- return str;
80
- }
81
-
82
- // Turns a list of values into a list of ?'s, joining them with commas unless
83
- // a "joining" value is specified (e.g. ' and ')
84
- parameterize(values, notSetValue) {
85
- if (typeof values === 'function') return this.parameter(values);
86
- values = Array.isArray(values) ? values : [values];
87
- let str = '',
88
- i = -1;
89
- while (++i < values.length) {
90
- if (i > 0) str += ', ';
91
- str += this.parameter(values[i] === undefined ? notSetValue : values[i]);
92
- }
93
- return str;
94
- }
95
-
96
- // Formats `values` into a parenthesized list of parameters for a `VALUES`
97
- // clause.
98
- //
99
- // [1, 2] -> '(?, ?)'
100
- // [[1, 2], [3, 4]] -> '((?, ?), (?, ?))'
101
- // knex('table') -> '(select * from "table")'
102
- // knex.raw('select ?', 1) -> '(select ?)'
103
- //
104
- values(values) {
105
- if (Array.isArray(values)) {
106
- if (Array.isArray(values[0])) {
107
- return `(${values
108
- .map((value) => `(${this.parameterize(value)})`)
109
- .join(', ')})`;
110
- }
111
- return `(${this.parameterize(values)})`;
112
- }
113
-
114
- if (values instanceof Raw) {
115
- return `(${this.parameter(values)})`;
116
- }
117
-
118
- return this.parameter(values);
119
- }
120
-
121
- // Checks whether a value is a function... if it is, we compile it
122
- // otherwise we check whether it's a raw
123
- parameter(value) {
124
- if (typeof value === 'function') {
125
- return this.outputQuery(this.compileCallback(value), true);
126
- }
127
- return this.unwrapRaw(value, true) || '?';
128
- }
129
-
130
- unwrapRaw(value, isParameter) {
131
- let query;
132
- if (value instanceof QueryBuilder) {
133
- query = this.client.queryCompiler(value).toSQL();
134
- if (query.bindings) {
135
- this.bindings = this.bindings.concat(query.bindings);
136
- }
137
- return this.outputQuery(query, isParameter);
138
- }
139
- if (value instanceof Raw) {
140
- value.client = this.client;
141
- if (this.builder._queryContext) {
142
- value.queryContext = () => {
143
- return this.builder._queryContext;
144
- };
145
- }
146
-
147
- query = value.toSQL();
148
- if (query.bindings) {
149
- this.bindings = this.bindings.concat(query.bindings);
150
- }
151
- return query.sql;
152
- }
153
- if (isParameter) {
154
- this.bindings.push(value);
155
- }
156
- }
157
-
158
- /**
159
- * Creates SQL for a parameter, which might be passed to where() or .with() or
160
- * pretty much anywhere in API.
161
- *
162
- * @param query Callback (for where or complete builder), Raw or QueryBuilder
163
- * @param method Optional at least 'select' or 'update' are valid
164
- */
165
- rawOrFn(value, method) {
166
- if (typeof value === 'function') {
167
- return this.outputQuery(this.compileCallback(value, method));
168
- }
169
- return this.unwrapRaw(value) || '';
170
- }
171
-
172
- // Puts the appropriate wrapper around a value depending on the database
173
- // engine, unless it's a knex.raw value, in which case it's left alone.
174
- wrap(value, isParameter) {
175
- const raw = this.unwrapRaw(value, isParameter);
176
- if (raw) return raw;
177
- switch (typeof value) {
178
- case 'function':
179
- return this.outputQuery(this.compileCallback(value), true);
180
- case 'object':
181
- return this.parseObject(value);
182
- case 'number':
183
- return value;
184
- default:
185
- return this.wrapString(value + '');
186
- }
187
- }
188
-
189
- wrapAsIdentifier(value) {
190
- const queryContext = this.builder.queryContext();
191
- return this.client.wrapIdentifier((value || '').trim(), queryContext);
192
- }
193
-
194
- alias(first, second) {
195
- return first + ' as ' + second;
196
- }
197
-
198
- operator(value) {
199
- const raw = this.unwrapRaw(value);
200
- if (raw) return raw;
201
- const operator = operators[(value || '').toLowerCase()];
202
- if (!operator) {
203
- throw new TypeError(`The operator "${value}" is not permitted`);
204
- }
205
- return operator;
206
- }
207
-
208
- // Specify the direction of the ordering.
209
- direction(value) {
210
- const raw = this.unwrapRaw(value);
211
- if (raw) return raw;
212
- return orderBys.indexOf((value || '').toLowerCase()) !== -1 ? value : 'asc';
213
- }
214
-
215
- // Compiles a callback using the query builder.
216
- compileCallback(callback, method) {
217
- const { client } = this;
218
-
219
- // Build the callback
220
- const builder = client.queryBuilder();
221
- callback.call(builder, builder);
222
-
223
- // Compile the callback, using the current formatter (to track all bindings).
224
- const compiler = client.queryCompiler(builder);
225
- compiler.formatter = this;
226
-
227
- // Return the compiled & parameterized sql.
228
- return compiler.toSQL(method || builder._method || 'select');
229
- }
230
-
231
- // Ensures the query is aliased if necessary.
232
- outputQuery(compiled, isParameter) {
233
- let sql = compiled.sql || '';
234
- if (sql) {
235
- if (
236
- (compiled.method === 'select' || compiled.method === 'first') &&
237
- (isParameter || compiled.as)
238
- ) {
239
- sql = `(${sql})`;
240
- if (compiled.as) return this.alias(sql, this.wrap(compiled.as));
241
- }
242
- }
243
- return sql;
244
- }
245
-
246
- // Key-value notation for alias
247
- parseObject(obj) {
248
- const ret = [];
249
- for (const alias in obj) {
250
- const queryOrIdentifier = obj[alias];
251
- // Avoids double aliasing for subqueries
252
- if (typeof queryOrIdentifier === 'function') {
253
- const compiled = this.compileCallback(queryOrIdentifier);
254
- compiled.as = alias; // enforces the object's alias
255
- ret.push(this.outputQuery(compiled, true));
256
- } else if (queryOrIdentifier instanceof QueryBuilder) {
257
- ret.push(
258
- this.alias(
259
- `(${this.wrap(queryOrIdentifier)})`,
260
- this.wrapAsIdentifier(alias)
261
- )
262
- );
263
- } else {
264
- ret.push(
265
- this.alias(this.wrap(queryOrIdentifier), this.wrapAsIdentifier(alias))
266
- );
267
- }
268
- }
269
- return ret.join(', ');
270
- }
271
-
272
- // Coerce to string to prevent strange errors when it's not a string.
273
- wrapString(value) {
274
- const asIndex = value.toLowerCase().indexOf(' as ');
275
- if (asIndex !== -1) {
276
- const first = value.slice(0, asIndex);
277
- const second = value.slice(asIndex + 4);
278
- return this.alias(this.wrap(first), this.wrapAsIdentifier(second));
279
- }
280
- const wrapped = [];
281
- let i = -1;
282
- const segments = value.split('.');
283
- while (++i < segments.length) {
284
- value = segments[i];
285
- if (i === 0 && segments.length > 1) {
286
- wrapped.push(this.wrap((value || '').trim()));
287
- } else {
288
- wrapped.push(this.wrapAsIdentifier(value));
289
- }
290
- }
291
- return wrapped.join('.');
292
- }
293
- }
294
-
295
- module.exports = Formatter;
1
+ const QueryBuilder = require('./query/builder');
2
+ const Raw = require('./raw');
3
+ const transform = require('lodash/transform');
4
+
5
+ // Valid values for the `order by` clause generation.
6
+ const orderBys = ['asc', 'desc'];
7
+
8
+ // Turn this into a lookup map
9
+ const operators = transform(
10
+ [
11
+ '=',
12
+ '<',
13
+ '>',
14
+ '<=',
15
+ '>=',
16
+ '<>',
17
+ '!=',
18
+ 'like',
19
+ 'not like',
20
+ 'between',
21
+ 'not between',
22
+ 'ilike',
23
+ 'not ilike',
24
+ 'exists',
25
+ 'not exist',
26
+ 'rlike',
27
+ 'not rlike',
28
+ 'regexp',
29
+ 'not regexp',
30
+ '&',
31
+ '|',
32
+ '^',
33
+ '<<',
34
+ '>>',
35
+ '~',
36
+ '~*',
37
+ '!~',
38
+ '!~*',
39
+ '#',
40
+ '&&',
41
+ '@>',
42
+ '<@',
43
+ '||',
44
+ '&<',
45
+ '&>',
46
+ '-|-',
47
+ '@@',
48
+ '!!',
49
+ ['?', '\\?'],
50
+ ['?|', '\\?|'],
51
+ ['?&', '\\?&'],
52
+ ],
53
+ (result, key) => {
54
+ if (Array.isArray(key)) {
55
+ result[key[0]] = key[1];
56
+ } else {
57
+ result[key] = key;
58
+ }
59
+ },
60
+ {}
61
+ );
62
+
63
+ class Formatter {
64
+ constructor(client, builder) {
65
+ this.client = client;
66
+ this.builder = builder;
67
+ this.bindings = [];
68
+ }
69
+
70
+ // Accepts a string or array of columns to wrap as appropriate.
71
+ columnize(target) {
72
+ const columns = Array.isArray(target) ? target : [target];
73
+ let str = '',
74
+ i = -1;
75
+ while (++i < columns.length) {
76
+ if (i > 0) str += ', ';
77
+ str += this.wrap(columns[i]);
78
+ }
79
+ return str;
80
+ }
81
+
82
+ // Turns a list of values into a list of ?'s, joining them with commas unless
83
+ // a "joining" value is specified (e.g. ' and ')
84
+ parameterize(values, notSetValue) {
85
+ if (typeof values === 'function') return this.parameter(values);
86
+ values = Array.isArray(values) ? values : [values];
87
+ let str = '',
88
+ i = -1;
89
+ while (++i < values.length) {
90
+ if (i > 0) str += ', ';
91
+ str += this.parameter(values[i] === undefined ? notSetValue : values[i]);
92
+ }
93
+ return str;
94
+ }
95
+
96
+ // Formats `values` into a parenthesized list of parameters for a `VALUES`
97
+ // clause.
98
+ //
99
+ // [1, 2] -> '(?, ?)'
100
+ // [[1, 2], [3, 4]] -> '((?, ?), (?, ?))'
101
+ // knex('table') -> '(select * from "table")'
102
+ // knex.raw('select ?', 1) -> '(select ?)'
103
+ //
104
+ values(values) {
105
+ if (Array.isArray(values)) {
106
+ if (Array.isArray(values[0])) {
107
+ return `(${values
108
+ .map((value) => `(${this.parameterize(value)})`)
109
+ .join(', ')})`;
110
+ }
111
+ return `(${this.parameterize(values)})`;
112
+ }
113
+
114
+ if (values instanceof Raw) {
115
+ return `(${this.parameter(values)})`;
116
+ }
117
+
118
+ return this.parameter(values);
119
+ }
120
+
121
+ // Checks whether a value is a function... if it is, we compile it
122
+ // otherwise we check whether it's a raw
123
+ parameter(value) {
124
+ if (typeof value === 'function') {
125
+ return this.outputQuery(this.compileCallback(value), true);
126
+ }
127
+ return this.unwrapRaw(value, true) || '?';
128
+ }
129
+
130
+ unwrapRaw(value, isParameter) {
131
+ let query;
132
+ if (value instanceof QueryBuilder) {
133
+ query = this.client.queryCompiler(value).toSQL();
134
+ if (query.bindings) {
135
+ this.bindings = this.bindings.concat(query.bindings);
136
+ }
137
+ return this.outputQuery(query, isParameter);
138
+ }
139
+ if (value instanceof Raw) {
140
+ value.client = this.client;
141
+ if (this.builder._queryContext) {
142
+ value.queryContext = () => {
143
+ return this.builder._queryContext;
144
+ };
145
+ }
146
+
147
+ query = value.toSQL();
148
+ if (query.bindings) {
149
+ this.bindings = this.bindings.concat(query.bindings);
150
+ }
151
+ return query.sql;
152
+ }
153
+ if (isParameter) {
154
+ this.bindings.push(value);
155
+ }
156
+ }
157
+
158
+ /**
159
+ * Creates SQL for a parameter, which might be passed to where() or .with() or
160
+ * pretty much anywhere in API.
161
+ *
162
+ * @param query Callback (for where or complete builder), Raw or QueryBuilder
163
+ * @param method Optional at least 'select' or 'update' are valid
164
+ */
165
+ rawOrFn(value, method) {
166
+ if (typeof value === 'function') {
167
+ return this.outputQuery(this.compileCallback(value, method));
168
+ }
169
+ return this.unwrapRaw(value) || '';
170
+ }
171
+
172
+ // Puts the appropriate wrapper around a value depending on the database
173
+ // engine, unless it's a knex.raw value, in which case it's left alone.
174
+ wrap(value, isParameter) {
175
+ const raw = this.unwrapRaw(value, isParameter);
176
+ if (raw) return raw;
177
+ switch (typeof value) {
178
+ case 'function':
179
+ return this.outputQuery(this.compileCallback(value), true);
180
+ case 'object':
181
+ return this.parseObject(value);
182
+ case 'number':
183
+ return value;
184
+ default:
185
+ return this.wrapString(value + '');
186
+ }
187
+ }
188
+
189
+ wrapAsIdentifier(value) {
190
+ const queryContext = this.builder.queryContext();
191
+ return this.client.wrapIdentifier((value || '').trim(), queryContext);
192
+ }
193
+
194
+ alias(first, second) {
195
+ return first + ' as ' + second;
196
+ }
197
+
198
+ operator(value) {
199
+ const raw = this.unwrapRaw(value);
200
+ if (raw) return raw;
201
+ const operator = operators[(value || '').toLowerCase()];
202
+ if (!operator) {
203
+ throw new TypeError(`The operator "${value}" is not permitted`);
204
+ }
205
+ return operator;
206
+ }
207
+
208
+ // Specify the direction of the ordering.
209
+ direction(value) {
210
+ const raw = this.unwrapRaw(value);
211
+ if (raw) return raw;
212
+ return orderBys.indexOf((value || '').toLowerCase()) !== -1 ? value : 'asc';
213
+ }
214
+
215
+ // Compiles a callback using the query builder.
216
+ compileCallback(callback, method) {
217
+ const { client } = this;
218
+
219
+ // Build the callback
220
+ const builder = client.queryBuilder();
221
+ callback.call(builder, builder);
222
+
223
+ // Compile the callback, using the current formatter (to track all bindings).
224
+ const compiler = client.queryCompiler(builder);
225
+ compiler.formatter = this;
226
+
227
+ // Return the compiled & parameterized sql.
228
+ return compiler.toSQL(method || builder._method || 'select');
229
+ }
230
+
231
+ // Ensures the query is aliased if necessary.
232
+ outputQuery(compiled, isParameter) {
233
+ let sql = compiled.sql || '';
234
+ if (sql) {
235
+ if (
236
+ (compiled.method === 'select' || compiled.method === 'first') &&
237
+ (isParameter || compiled.as)
238
+ ) {
239
+ sql = `(${sql})`;
240
+ if (compiled.as) return this.alias(sql, this.wrap(compiled.as));
241
+ }
242
+ }
243
+ return sql;
244
+ }
245
+
246
+ // Key-value notation for alias
247
+ parseObject(obj) {
248
+ const ret = [];
249
+ for (const alias in obj) {
250
+ const queryOrIdentifier = obj[alias];
251
+ // Avoids double aliasing for subqueries
252
+ if (typeof queryOrIdentifier === 'function') {
253
+ const compiled = this.compileCallback(queryOrIdentifier);
254
+ compiled.as = alias; // enforces the object's alias
255
+ ret.push(this.outputQuery(compiled, true));
256
+ } else if (queryOrIdentifier instanceof QueryBuilder) {
257
+ ret.push(
258
+ this.alias(
259
+ `(${this.wrap(queryOrIdentifier)})`,
260
+ this.wrapAsIdentifier(alias)
261
+ )
262
+ );
263
+ } else {
264
+ ret.push(
265
+ this.alias(this.wrap(queryOrIdentifier), this.wrapAsIdentifier(alias))
266
+ );
267
+ }
268
+ }
269
+ return ret.join(', ');
270
+ }
271
+
272
+ // Coerce to string to prevent strange errors when it's not a string.
273
+ wrapString(value) {
274
+ const asIndex = value.toLowerCase().indexOf(' as ');
275
+ if (asIndex !== -1) {
276
+ const first = value.slice(0, asIndex);
277
+ const second = value.slice(asIndex + 4);
278
+ return this.alias(this.wrap(first), this.wrapAsIdentifier(second));
279
+ }
280
+ const wrapped = [];
281
+ let i = -1;
282
+ const segments = value.split('.');
283
+ while (++i < segments.length) {
284
+ value = segments[i];
285
+ if (i === 0 && segments.length > 1) {
286
+ wrapped.push(this.wrap((value || '').trim()));
287
+ } else {
288
+ wrapped.push(this.wrapAsIdentifier(value));
289
+ }
290
+ }
291
+ return wrapped.join('.');
292
+ }
293
+ }
294
+
295
+ module.exports = Formatter;
@@ -1,14 +1,14 @@
1
- // FunctionHelper
2
- // -------
3
- function FunctionHelper(client) {
4
- this.client = client;
5
- }
6
-
7
- FunctionHelper.prototype.now = function (precision) {
8
- if (typeof precision === 'number') {
9
- return this.client.raw(`CURRENT_TIMESTAMP(${precision})`);
10
- }
11
- return this.client.raw('CURRENT_TIMESTAMP');
12
- };
13
-
14
- module.exports = FunctionHelper;
1
+ // FunctionHelper
2
+ // -------
3
+ function FunctionHelper(client) {
4
+ this.client = client;
5
+ }
6
+
7
+ FunctionHelper.prototype.now = function (precision) {
8
+ if (typeof precision === 'number') {
9
+ return this.client.raw(`CURRENT_TIMESTAMP(${precision})`);
10
+ }
11
+ return this.client.raw('CURRENT_TIMESTAMP');
12
+ };
13
+
14
+ module.exports = FunctionHelper;