@abedmurrar/objection 4.0.0-beta.3 → 4.0.0-beta.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/lib/queryBuilder/FunctionBuilder.js +35 -31
- package/lib/queryBuilder/JoinBuilder.js +46 -45
- package/lib/queryBuilder/RawBuilder.js +15 -13
- package/lib/queryBuilder/ReferenceBuilder.js +25 -23
- package/lib/queryBuilder/RelationExpression.js +59 -19
- package/lib/queryBuilder/ValueBuilder.js +20 -19
- package/lib/queryBuilder/expressions.js +8 -18
- package/lib/queryBuilder/functions/mysql.js +18 -42
- package/lib/queryBuilder/join/JoinResultColumn.js +6 -4
- package/lib/queryBuilder/join/JoinResultParser.js +10 -8
- package/lib/queryBuilder/join/RelationJoiner.js +40 -35
- package/lib/queryBuilder/join/TableNode.js +7 -5
- package/lib/queryBuilder/join/TableTree.js +13 -9
- package/lib/queryBuilder/join/utils.js +8 -8
- package/package.json +1 -1
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FunctionBuilder = void 0;
|
|
4
|
+
exports.fn = fn;
|
|
5
|
+
const RawBuilder_1 = require("./RawBuilder");
|
|
6
|
+
const ReferenceBuilder_1 = require("./ReferenceBuilder");
|
|
7
|
+
const expressions_1 = require("./expressions");
|
|
8
|
+
const objectUtils_1 = require("../utils/objectUtils");
|
|
9
|
+
const knexUtils_1 = require("../utils/knexUtils");
|
|
10
|
+
const mysqlJsonPath_1 = require("../utils/mysqlJsonPath");
|
|
11
|
+
const mysql_1 = require("./functions/mysql");
|
|
12
|
+
class FunctionBuilder extends RawBuilder_1.RawBuilder {
|
|
10
13
|
constructor(sql, args) {
|
|
11
14
|
super(sql, args);
|
|
12
15
|
// The name of the `fn` helper of a MySQL only function.
|
|
@@ -46,16 +49,16 @@ class FunctionBuilder extends RawBuilder {
|
|
|
46
49
|
}
|
|
47
50
|
toKnexRaw(builder) {
|
|
48
51
|
const knex = builder.unsafeKnex();
|
|
49
|
-
if (this._mysqlFunction && knex && !isMySql(knex)) {
|
|
50
|
-
throw new Error(`fn.${this._mysqlFunction}() is a MySQL function and can't be used with ${getDialect(knex)}.`);
|
|
52
|
+
if (this._mysqlFunction && knex && !(0, knexUtils_1.isMySql)(knex)) {
|
|
53
|
+
throw new Error(`fn.${this._mysqlFunction}() is a MySQL function and can't be used with ${(0, knexUtils_1.getDialect)(knex)}.`);
|
|
51
54
|
}
|
|
52
55
|
// The SQL of the helpers is created before the database is known.
|
|
53
|
-
if (knex && isMariaDb(knex) && this._sql.includes(JSON_CAST)) {
|
|
56
|
+
if (knex && (0, knexUtils_1.isMariaDb)(knex) && this._sql.includes(mysqlJsonPath_1.JSON_CAST)) {
|
|
54
57
|
const copy = Object.assign(Object.create(Object.getPrototypeOf(this)), this);
|
|
55
|
-
copy._sql = jsonCastsForDialect(this._sql, knex);
|
|
58
|
+
copy._sql = (0, mysqlJsonPath_1.jsonCastsForDialect)(this._sql, knex);
|
|
56
59
|
return copy.toKnexRaw(builder);
|
|
57
60
|
}
|
|
58
|
-
if (this._mariaDbError && knex && isMariaDb(knex)) {
|
|
61
|
+
if (this._mariaDbError && knex && (0, knexUtils_1.isMariaDb)(knex)) {
|
|
59
62
|
throw new Error(this._mariaDbError);
|
|
60
63
|
}
|
|
61
64
|
if (this._windowFunction && !this._over) {
|
|
@@ -64,7 +67,7 @@ class FunctionBuilder extends RawBuilder {
|
|
|
64
67
|
if (this._filters.length === 0 && !this._over) {
|
|
65
68
|
return super.toKnexRaw(builder);
|
|
66
69
|
}
|
|
67
|
-
return new RawBuilder(...this._buildFilterAndOver()).as(this._as).toKnexRaw(builder);
|
|
70
|
+
return new RawBuilder_1.RawBuilder(...this._buildFilterAndOver()).as(this._as).toKnexRaw(builder);
|
|
68
71
|
}
|
|
69
72
|
_buildFilterAndOver() {
|
|
70
73
|
let sql = this._sql;
|
|
@@ -72,7 +75,7 @@ class FunctionBuilder extends RawBuilder {
|
|
|
72
75
|
if (this._filters.length !== 0) {
|
|
73
76
|
const condition = this._filters.length === 1
|
|
74
77
|
? this._filters[0]
|
|
75
|
-
: expression(`(${this._filters.map(() => '?').join(' AND ')})`, this._filters);
|
|
78
|
+
: (0, expressions_1.expression)(`(${this._filters.map(() => '?').join(' AND ')})`, this._filters);
|
|
76
79
|
if (this._aggregated === '*') {
|
|
77
80
|
sql = sql.replace('*', 'CASE WHEN ? THEN 1 END');
|
|
78
81
|
args = [condition];
|
|
@@ -82,6 +85,7 @@ class FunctionBuilder extends RawBuilder {
|
|
|
82
85
|
let i = 0;
|
|
83
86
|
sql = sql.replace(/\?/g, () => {
|
|
84
87
|
const arg = args[i++];
|
|
88
|
+
// A number: `filter()` needs an aggregate, and '*' is handled above.
|
|
85
89
|
if (i <= this._aggregated) {
|
|
86
90
|
filtered.push(condition, arg);
|
|
87
91
|
return 'CASE WHEN ? THEN ? END';
|
|
@@ -102,6 +106,7 @@ class FunctionBuilder extends RawBuilder {
|
|
|
102
106
|
return [sql, args];
|
|
103
107
|
}
|
|
104
108
|
}
|
|
109
|
+
exports.FunctionBuilder = FunctionBuilder;
|
|
105
110
|
// `fn.case()` is `CASE WHEN condition THEN result ... END` and `fn.case(value)` is
|
|
106
111
|
// `CASE value WHEN other THEN result ... END`.
|
|
107
112
|
class CaseBuilder extends FunctionBuilder {
|
|
@@ -146,7 +151,7 @@ class CaseBuilder extends FunctionBuilder {
|
|
|
146
151
|
class ExcludedBuilder extends FunctionBuilder {
|
|
147
152
|
constructor(column) {
|
|
148
153
|
super('', []);
|
|
149
|
-
if (isObject(column) && column.isObjectionReferenceBuilder) {
|
|
154
|
+
if ((0, objectUtils_1.isObject)(column) && column.isObjectionReferenceBuilder) {
|
|
150
155
|
column = column.column;
|
|
151
156
|
}
|
|
152
157
|
if (typeof column !== 'string') {
|
|
@@ -155,7 +160,7 @@ class ExcludedBuilder extends FunctionBuilder {
|
|
|
155
160
|
this._column = column;
|
|
156
161
|
}
|
|
157
162
|
toKnexRaw(builder) {
|
|
158
|
-
if (isMySql(builder.unsafeKnex())) {
|
|
163
|
+
if ((0, knexUtils_1.isMySql)(builder.unsafeKnex())) {
|
|
159
164
|
this._sql = 'VALUES(??)';
|
|
160
165
|
this._args = [this._column];
|
|
161
166
|
}
|
|
@@ -170,19 +175,19 @@ function buildOver({ partitionBy, orderBy }) {
|
|
|
170
175
|
const parts = [];
|
|
171
176
|
const args = [];
|
|
172
177
|
if (partitionBy !== undefined) {
|
|
173
|
-
const columns = asArray(partitionBy).map((it) =>
|
|
178
|
+
const columns = (0, objectUtils_1.asArray)(partitionBy).map((it) => typeof it === 'string' ? (0, ReferenceBuilder_1.ref)(it) : it);
|
|
174
179
|
parts.push(`PARTITION BY ${columns.map(() => '?').join(', ')}`);
|
|
175
180
|
args.push(...columns);
|
|
176
181
|
}
|
|
177
182
|
if (orderBy !== undefined) {
|
|
178
|
-
const [orderBySql, orderByArgs] = buildOrderBy('over()', orderBy);
|
|
183
|
+
const [orderBySql, orderByArgs] = (0, mysql_1.buildOrderBy)('over()', orderBy);
|
|
179
184
|
parts.push(`ORDER BY ${orderBySql}`);
|
|
180
185
|
args.push(...orderByArgs);
|
|
181
186
|
}
|
|
182
187
|
return [parts.join(' '), args];
|
|
183
188
|
}
|
|
184
189
|
function fn(...argsIn) {
|
|
185
|
-
const { sql, args } = normalizeRawArgs(argsIn);
|
|
190
|
+
const { sql, args } = (0, RawBuilder_1.normalizeRawArgs)(argsIn);
|
|
186
191
|
return new FunctionBuilder(`${sql}(${args.map(() => '?').join(', ')})`, args);
|
|
187
192
|
}
|
|
188
193
|
for (const func of ['coalesce', 'concat', 'upper', 'lower']) {
|
|
@@ -199,8 +204,10 @@ fn.count = (...args) => {
|
|
|
199
204
|
return aggregate(fn('COUNT', args), args.length);
|
|
200
205
|
};
|
|
201
206
|
fn.now = (precision) => {
|
|
202
|
-
|
|
203
|
-
|
|
207
|
+
// `parseInt()` converts a number to a string first.
|
|
208
|
+
precision = parseInt((0, objectUtils_1.asSingle)(precision), 10);
|
|
209
|
+
// A number, from `parseInt()`.
|
|
210
|
+
if (isNaN(precision) || !(0, objectUtils_1.isNumber)(precision)) {
|
|
204
211
|
precision = 6;
|
|
205
212
|
}
|
|
206
213
|
// We need to use a literal precision instead of a binding here
|
|
@@ -211,13 +218,14 @@ fn.now = (precision) => {
|
|
|
211
218
|
};
|
|
212
219
|
fn.case = (...args) => new CaseBuilder(args.length !== 0, args[0]);
|
|
213
220
|
fn.exists = (subquery) => {
|
|
214
|
-
if (!isObject(subquery) ||
|
|
221
|
+
if (!(0, objectUtils_1.isObject)(subquery) ||
|
|
222
|
+
!subquery.isObjectionQueryBuilderBase) {
|
|
215
223
|
throw new Error('fn.exists() takes a query.');
|
|
216
224
|
}
|
|
217
225
|
return new FunctionBuilder('(EXISTS ?)', [subquery]);
|
|
218
226
|
};
|
|
219
227
|
fn.excluded = (column) => new ExcludedBuilder(column);
|
|
220
|
-
for (const [name, sql, , , options = {}] of FUNCTIONS) {
|
|
228
|
+
for (const [name, sql, , , options = {}] of mysql_1.FUNCTIONS) {
|
|
221
229
|
// `defineProperty` since `fn.length` is a read-only property of functions.
|
|
222
230
|
Object.defineProperty(fn, name, {
|
|
223
231
|
configurable: true,
|
|
@@ -235,7 +243,7 @@ function createMySqlFunction(name, sql, options) {
|
|
|
235
243
|
if (!options.portable) {
|
|
236
244
|
builder._mysqlFunction = name;
|
|
237
245
|
}
|
|
238
|
-
const mariaDbReason = MARIADB_UNSUPPORTED[name] && MARIADB_UNSUPPORTED[name](args);
|
|
246
|
+
const mariaDbReason = mysql_1.MARIADB_UNSUPPORTED[name] && mysql_1.MARIADB_UNSUPPORTED[name](args);
|
|
239
247
|
if (mariaDbReason) {
|
|
240
248
|
builder._mariaDbError = `fn.${name}() ${mariaDbReason}.`;
|
|
241
249
|
}
|
|
@@ -276,7 +284,7 @@ function isJsonArg(json, i) {
|
|
|
276
284
|
}
|
|
277
285
|
}
|
|
278
286
|
function isJsonValue(value) {
|
|
279
|
-
return Array.isArray(value) || isPlainObject(value);
|
|
287
|
+
return Array.isArray(value) || (0, objectUtils_1.isPlainObject)(value);
|
|
280
288
|
}
|
|
281
289
|
Object.defineProperties(FunctionBuilder.prototype, {
|
|
282
290
|
isObjectionFunctionBuilder: {
|
|
@@ -285,7 +293,3 @@ Object.defineProperties(FunctionBuilder.prototype, {
|
|
|
285
293
|
value: true,
|
|
286
294
|
},
|
|
287
295
|
});
|
|
288
|
-
module.exports = {
|
|
289
|
-
FunctionBuilder,
|
|
290
|
-
fn,
|
|
291
|
-
};
|
|
@@ -1,124 +1,125 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JoinBuilder = void 0;
|
|
4
|
+
const QueryBuilderOperationSupport_1 = require("./QueryBuilderOperationSupport");
|
|
5
|
+
const KnexOperation_1 = require("./operations/KnexOperation");
|
|
6
|
+
const WhereInOperation_1 = require("./operations/WhereInOperation");
|
|
7
|
+
const QueryBuilderOperation_1 = require("./operations/QueryBuilderOperation");
|
|
6
8
|
// knex's `join.on(a, b).or.on(c, d)`: `or` makes the next condition an `or`.
|
|
7
|
-
class OrOperation extends QueryBuilderOperation {
|
|
9
|
+
class OrOperation extends QueryBuilderOperation_1.QueryBuilderOperation {
|
|
10
|
+
// knex's typings don't declare the `or` getter.
|
|
8
11
|
onBuildKnex(knexBuilder) {
|
|
9
12
|
return knexBuilder.or;
|
|
10
13
|
}
|
|
11
14
|
}
|
|
12
|
-
class JoinBuilder extends QueryBuilderOperationSupport {
|
|
15
|
+
class JoinBuilder extends QueryBuilderOperationSupport_1.QueryBuilderOperationSupport {
|
|
13
16
|
get or() {
|
|
14
17
|
return this.addOperation(new OrOperation('or'), []);
|
|
15
18
|
}
|
|
16
19
|
using(...args) {
|
|
17
|
-
return this.addOperation(new KnexOperation('using'), args);
|
|
20
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('using'), args);
|
|
18
21
|
}
|
|
19
22
|
on(...args) {
|
|
20
|
-
return this.addOperation(new KnexOperation('on'), args);
|
|
23
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('on'), args);
|
|
21
24
|
}
|
|
22
25
|
orOn(...args) {
|
|
23
|
-
return this.addOperation(new KnexOperation('orOn'), args);
|
|
26
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('orOn'), args);
|
|
24
27
|
}
|
|
25
28
|
onBetween(...args) {
|
|
26
|
-
return this.addOperation(new KnexOperation('onBetween'), args);
|
|
29
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('onBetween'), args);
|
|
27
30
|
}
|
|
28
31
|
onNotBetween(...args) {
|
|
29
|
-
return this.addOperation(new KnexOperation('onNotBetween'), args);
|
|
32
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('onNotBetween'), args);
|
|
30
33
|
}
|
|
31
34
|
orOnBetween(...args) {
|
|
32
|
-
return this.addOperation(new KnexOperation('orOnBetween'), args);
|
|
35
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('orOnBetween'), args);
|
|
33
36
|
}
|
|
34
37
|
orOnNotBetween(...args) {
|
|
35
|
-
return this.addOperation(new KnexOperation('orOnNotBetween'), args);
|
|
38
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('orOnNotBetween'), args);
|
|
36
39
|
}
|
|
37
40
|
onIn(...args) {
|
|
38
|
-
return this.addOperation(new WhereInOperation('onIn', { rawMethod: 'on' }), args);
|
|
41
|
+
return this.addOperation(new WhereInOperation_1.WhereInOperation('onIn', { rawMethod: 'on' }), args);
|
|
39
42
|
}
|
|
40
43
|
onNotIn(...args) {
|
|
41
|
-
return this.addOperation(new WhereInOperation('onNotIn', { rawMethod: 'on', not: true }), args);
|
|
44
|
+
return this.addOperation(new WhereInOperation_1.WhereInOperation('onNotIn', { rawMethod: 'on', not: true }), args);
|
|
42
45
|
}
|
|
43
46
|
orOnIn(...args) {
|
|
44
|
-
return this.addOperation(new WhereInOperation('orOnIn', { rawMethod: 'orOn' }), args);
|
|
47
|
+
return this.addOperation(new WhereInOperation_1.WhereInOperation('orOnIn', { rawMethod: 'orOn' }), args);
|
|
45
48
|
}
|
|
46
49
|
orOnNotIn(...args) {
|
|
47
|
-
return this.addOperation(new WhereInOperation('orOnNotIn', { rawMethod: 'orOn', not: true }), args);
|
|
50
|
+
return this.addOperation(new WhereInOperation_1.WhereInOperation('orOnNotIn', { rawMethod: 'orOn', not: true }), args);
|
|
48
51
|
}
|
|
49
52
|
onNull(...args) {
|
|
50
|
-
return this.addOperation(new KnexOperation('onNull'), args);
|
|
53
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('onNull'), args);
|
|
51
54
|
}
|
|
52
55
|
orOnNull(...args) {
|
|
53
|
-
return this.addOperation(new KnexOperation('orOnNull'), args);
|
|
56
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('orOnNull'), args);
|
|
54
57
|
}
|
|
55
58
|
onNotNull(...args) {
|
|
56
|
-
return this.addOperation(new KnexOperation('onNotNull'), args);
|
|
59
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('onNotNull'), args);
|
|
57
60
|
}
|
|
58
61
|
orOnNotNull(...args) {
|
|
59
|
-
return this.addOperation(new KnexOperation('orOnNotNull'), args);
|
|
62
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('orOnNotNull'), args);
|
|
60
63
|
}
|
|
61
64
|
onExists(...args) {
|
|
62
|
-
return this.addOperation(new KnexOperation('onExists'), args);
|
|
65
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('onExists'), args);
|
|
63
66
|
}
|
|
64
67
|
orOnExists(...args) {
|
|
65
|
-
return this.addOperation(new KnexOperation('orOnExists'), args);
|
|
68
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('orOnExists'), args);
|
|
66
69
|
}
|
|
67
70
|
onNotExists(...args) {
|
|
68
|
-
return this.addOperation(new KnexOperation('onNotExists'), args);
|
|
71
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('onNotExists'), args);
|
|
69
72
|
}
|
|
70
73
|
orOnNotExists(...args) {
|
|
71
|
-
return this.addOperation(new KnexOperation('orOnNotExists'), args);
|
|
74
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('orOnNotExists'), args);
|
|
72
75
|
}
|
|
73
76
|
type(...args) {
|
|
74
|
-
return this.addOperation(new KnexOperation('type'), args);
|
|
77
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('type'), args);
|
|
75
78
|
}
|
|
76
79
|
andOn(...args) {
|
|
77
|
-
return this.addOperation(new KnexOperation('andOn'), args);
|
|
80
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('andOn'), args);
|
|
78
81
|
}
|
|
79
82
|
andOnIn(...args) {
|
|
80
|
-
return this.addOperation(new WhereInOperation('andOnIn', { rawMethod: 'andOn' }), args);
|
|
83
|
+
return this.addOperation(new WhereInOperation_1.WhereInOperation('andOnIn', { rawMethod: 'andOn' }), args);
|
|
81
84
|
}
|
|
82
85
|
andOnNotIn(...args) {
|
|
83
|
-
return this.addOperation(new WhereInOperation('andOnNotIn', { rawMethod: 'andOn', not: true }), args);
|
|
86
|
+
return this.addOperation(new WhereInOperation_1.WhereInOperation('andOnNotIn', { rawMethod: 'andOn', not: true }), args);
|
|
84
87
|
}
|
|
85
88
|
andOnNull(...args) {
|
|
86
|
-
return this.addOperation(new KnexOperation('andOnNull'), args);
|
|
89
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('andOnNull'), args);
|
|
87
90
|
}
|
|
88
91
|
andOnNotNull(...args) {
|
|
89
|
-
return this.addOperation(new KnexOperation('andOnNotNull'), args);
|
|
92
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('andOnNotNull'), args);
|
|
90
93
|
}
|
|
91
94
|
andOnExists(...args) {
|
|
92
|
-
return this.addOperation(new KnexOperation('andOnExists'), args);
|
|
95
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('andOnExists'), args);
|
|
93
96
|
}
|
|
94
97
|
andOnNotExists(...args) {
|
|
95
|
-
return this.addOperation(new KnexOperation('andOnNotExists'), args);
|
|
98
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('andOnNotExists'), args);
|
|
96
99
|
}
|
|
97
100
|
andOnBetween(...args) {
|
|
98
|
-
return this.addOperation(new KnexOperation('andOnBetween'), args);
|
|
101
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('andOnBetween'), args);
|
|
99
102
|
}
|
|
100
103
|
andOnNotBetween(...args) {
|
|
101
|
-
return this.addOperation(new KnexOperation('andOnNotBetween'), args);
|
|
104
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('andOnNotBetween'), args);
|
|
102
105
|
}
|
|
103
106
|
onJsonPathEquals(...args) {
|
|
104
|
-
return this.addOperation(new KnexOperation('onJsonPathEquals'), args);
|
|
107
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('onJsonPathEquals'), args);
|
|
105
108
|
}
|
|
106
109
|
orOnJsonPathEquals(...args) {
|
|
107
|
-
return this.addOperation(new KnexOperation('orOnJsonPathEquals'), args);
|
|
110
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('orOnJsonPathEquals'), args);
|
|
108
111
|
}
|
|
109
112
|
andOnJsonPathEquals(...args) {
|
|
110
|
-
return this.addOperation(new KnexOperation('andOnJsonPathEquals'), args);
|
|
113
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('andOnJsonPathEquals'), args);
|
|
111
114
|
}
|
|
112
115
|
onVal(...args) {
|
|
113
|
-
return this.addOperation(new KnexOperation('onVal'), args);
|
|
116
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('onVal'), args);
|
|
114
117
|
}
|
|
115
118
|
andOnVal(...args) {
|
|
116
|
-
return this.addOperation(new KnexOperation('andOnVal'), args);
|
|
119
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('andOnVal'), args);
|
|
117
120
|
}
|
|
118
121
|
orOnVal(...args) {
|
|
119
|
-
return this.addOperation(new KnexOperation('orOnVal'), args);
|
|
122
|
+
return this.addOperation(new KnexOperation_1.KnexOperation('orOnVal'), args);
|
|
120
123
|
}
|
|
121
124
|
}
|
|
122
|
-
|
|
123
|
-
JoinBuilder,
|
|
124
|
-
};
|
|
125
|
+
exports.JoinBuilder = JoinBuilder;
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RawBuilder = void 0;
|
|
4
|
+
exports.normalizeRawArgs = normalizeRawArgs;
|
|
5
|
+
exports.raw = raw;
|
|
6
|
+
const objectUtils_1 = require("../utils/objectUtils");
|
|
7
|
+
const buildUtils_1 = require("../utils/buildUtils");
|
|
8
|
+
const expressions_1 = require("./expressions");
|
|
5
9
|
class RawBuilder {
|
|
6
10
|
constructor(sql, args) {
|
|
7
11
|
this._sql = `${sql}`;
|
|
@@ -18,7 +22,7 @@ class RawBuilder {
|
|
|
18
22
|
toKnexRaw(builder) {
|
|
19
23
|
let args = null;
|
|
20
24
|
let sql = this._sql;
|
|
21
|
-
if (this._args.length === 1 && isPlainObject(this._args[0])) {
|
|
25
|
+
if (this._args.length === 1 && (0, objectUtils_1.isPlainObject)(this._args[0])) {
|
|
22
26
|
args = buildObject(this._args[0], builder);
|
|
23
27
|
if (this._as) {
|
|
24
28
|
args.__alias__ = this._as;
|
|
@@ -35,6 +39,7 @@ class RawBuilder {
|
|
|
35
39
|
return builder.knex().raw(sql, args);
|
|
36
40
|
}
|
|
37
41
|
}
|
|
42
|
+
exports.RawBuilder = RawBuilder;
|
|
38
43
|
Object.defineProperties(RawBuilder.prototype, {
|
|
39
44
|
isObjectionRawBuilder: {
|
|
40
45
|
enumerable: false,
|
|
@@ -42,13 +47,15 @@ Object.defineProperties(RawBuilder.prototype, {
|
|
|
42
47
|
value: true,
|
|
43
48
|
},
|
|
44
49
|
});
|
|
45
|
-
addExpressionMethods(RawBuilder.prototype);
|
|
50
|
+
(0, expressions_1.addExpressionMethods)(RawBuilder.prototype);
|
|
51
|
+
// `buildArg()` turns objection builders into knex ones. Everything else is a
|
|
52
|
+
// value that knex binds as it is.
|
|
46
53
|
function buildArray(arr, builder) {
|
|
47
|
-
return arr.map((it) => buildArg(it, builder));
|
|
54
|
+
return arr.map((it) => (0, buildUtils_1.buildArg)(it, builder));
|
|
48
55
|
}
|
|
49
56
|
function buildObject(obj, builder) {
|
|
50
57
|
return Object.keys(obj).reduce((args, key) => {
|
|
51
|
-
args[key] = buildArg(obj[key], builder);
|
|
58
|
+
args[key] = (0, buildUtils_1.buildArg)(obj[key], builder);
|
|
52
59
|
return args;
|
|
53
60
|
}, {});
|
|
54
61
|
}
|
|
@@ -71,8 +78,3 @@ function raw(...argsIn) {
|
|
|
71
78
|
const { sql, args } = normalizeRawArgs(argsIn);
|
|
72
79
|
return new RawBuilder(sql, args);
|
|
73
80
|
}
|
|
74
|
-
module.exports = {
|
|
75
|
-
RawBuilder,
|
|
76
|
-
normalizeRawArgs,
|
|
77
|
-
raw,
|
|
78
|
-
};
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ref = exports.ReferenceBuilder = void 0;
|
|
4
|
+
const parseFieldExpression_1 = require("../utils/parseFieldExpression");
|
|
5
|
+
const objectUtils_1 = require("../utils/objectUtils");
|
|
6
|
+
const knexUtils_1 = require("../utils/knexUtils");
|
|
7
|
+
const postgresJsonPath_1 = require("../utils/postgresJsonPath");
|
|
8
|
+
const mysqlJsonPath_1 = require("../utils/mysqlJsonPath");
|
|
9
|
+
const expressions_1 = require("./expressions");
|
|
8
10
|
class ReferenceBuilder {
|
|
9
11
|
constructor(expr) {
|
|
10
12
|
this._expr = expr;
|
|
@@ -22,6 +24,8 @@ class ReferenceBuilder {
|
|
|
22
24
|
this._parseExpression(expr);
|
|
23
25
|
}
|
|
24
26
|
}
|
|
27
|
+
// `_expr`, `_parsedExpr` and `_column` are only null inside `clone()`, which
|
|
28
|
+
// sets them right after creating the clone.
|
|
25
29
|
get parsedExpr() {
|
|
26
30
|
return this._parsedExpr;
|
|
27
31
|
}
|
|
@@ -132,13 +136,13 @@ class ReferenceBuilder {
|
|
|
132
136
|
return builder.knex().raw(...this._createRawArgs(builder));
|
|
133
137
|
}
|
|
134
138
|
_parseExpression(expr) {
|
|
135
|
-
this._parsedExpr = parseFieldExpression(expr);
|
|
139
|
+
this._parsedExpr = (0, parseFieldExpression_1.parseFieldExpression)(expr);
|
|
136
140
|
this._column = this._parsedExpr.column;
|
|
137
141
|
this._table = this._parsedExpr.table;
|
|
138
142
|
}
|
|
139
143
|
_createRawArgs(builder) {
|
|
140
144
|
const knex = builder.unsafeKnex();
|
|
141
|
-
const mysql = isMySql(knex);
|
|
145
|
+
const mysql = (0, knexUtils_1.isMySql)(knex);
|
|
142
146
|
const bindings = [];
|
|
143
147
|
let sql = this._createReferenceSql(builder, bindings);
|
|
144
148
|
sql = this._maybeCast(sql, mysql, knex);
|
|
@@ -152,20 +156,20 @@ class ReferenceBuilder {
|
|
|
152
156
|
if (access.length === 0) {
|
|
153
157
|
return '??';
|
|
154
158
|
}
|
|
155
|
-
if (getJsonDialect(builder.unsafeKnex(), this._expr) === 'mysql') {
|
|
156
|
-
bindings.push(toMySqlJsonPath(access));
|
|
159
|
+
if ((0, postgresJsonPath_1.getJsonDialect)(builder.unsafeKnex(), this._expr) === 'mysql') {
|
|
160
|
+
bindings.push((0, mysqlJsonPath_1.toMySqlJsonPath)(access));
|
|
157
161
|
// Casts need the unquoted value: `abc` instead of the JSON string `"abc"`.
|
|
158
162
|
if (this._cast) {
|
|
159
163
|
return 'JSON_UNQUOTE(JSON_EXTRACT(??, ?))';
|
|
160
164
|
}
|
|
161
165
|
// MariaDB's JSON_EXTRACT() returns the JSON with spaces, so it would never
|
|
162
166
|
// be equal to a value like `val({ a: 1 })`, which is compact.
|
|
163
|
-
return isMariaDb(builder.unsafeKnex())
|
|
167
|
+
return (0, knexUtils_1.isMariaDb)(builder.unsafeKnex())
|
|
164
168
|
? 'JSON_COMPACT(JSON_EXTRACT(??, ?))'
|
|
165
169
|
: 'JSON_EXTRACT(??, ?)';
|
|
166
170
|
}
|
|
167
171
|
const extractor = this._cast ? '#>>' : '#>';
|
|
168
|
-
return `??${extractor}${toPgJsonPathLiteral(access)}`;
|
|
172
|
+
return `??${extractor}${(0, postgresJsonPath_1.toPgJsonPathLiteral)(access)}`;
|
|
169
173
|
}
|
|
170
174
|
_maybeCast(sql, mysql, knex) {
|
|
171
175
|
if (!this._cast) {
|
|
@@ -176,17 +180,17 @@ class ReferenceBuilder {
|
|
|
176
180
|
}
|
|
177
181
|
const isJsonField = this._parsedExpr.access.length > 0;
|
|
178
182
|
// MySQL can't cast to a boolean.
|
|
179
|
-
if (isBooleanType(this._cast)) {
|
|
183
|
+
if ((0, mysqlJsonPath_1.isBooleanType)(this._cast)) {
|
|
180
184
|
return isJsonField ? `(${sql} IN ('true', '1'))` : `(${sql} <> 0)`;
|
|
181
185
|
}
|
|
182
|
-
const type = toMySqlCastType(this._cast);
|
|
186
|
+
const type = (0, mysqlJsonPath_1.toMySqlCastType)(this._cast);
|
|
183
187
|
// `JSON_UNQUOTE()` already returns text. Casting it would change its collation
|
|
184
188
|
// from the case sensitive one `->>` also uses.
|
|
185
189
|
if (isJsonField && type === 'CHAR') {
|
|
186
190
|
return sql;
|
|
187
191
|
}
|
|
188
192
|
if (type === 'JSON') {
|
|
189
|
-
return toJsonSql(sql, knex);
|
|
193
|
+
return (0, mysqlJsonPath_1.toJsonSql)(sql, knex);
|
|
190
194
|
}
|
|
191
195
|
return `CAST(${sql} AS ${type})`;
|
|
192
196
|
}
|
|
@@ -195,7 +199,7 @@ class ReferenceBuilder {
|
|
|
195
199
|
return sql;
|
|
196
200
|
}
|
|
197
201
|
else if (mysql) {
|
|
198
|
-
return toJsonSql(sql, knex);
|
|
202
|
+
return (0, mysqlJsonPath_1.toJsonSql)(sql, knex);
|
|
199
203
|
}
|
|
200
204
|
else {
|
|
201
205
|
return `to_jsonb(${sql})`;
|
|
@@ -224,6 +228,7 @@ class ReferenceBuilder {
|
|
|
224
228
|
}
|
|
225
229
|
}
|
|
226
230
|
}
|
|
231
|
+
exports.ReferenceBuilder = ReferenceBuilder;
|
|
227
232
|
Object.defineProperties(ReferenceBuilder.prototype, {
|
|
228
233
|
isObjectionReferenceBuilder: {
|
|
229
234
|
enumerable: false,
|
|
@@ -231,16 +236,13 @@ Object.defineProperties(ReferenceBuilder.prototype, {
|
|
|
231
236
|
value: true,
|
|
232
237
|
},
|
|
233
238
|
});
|
|
234
|
-
addExpressionMethods(ReferenceBuilder.prototype);
|
|
239
|
+
(0, expressions_1.addExpressionMethods)(ReferenceBuilder.prototype);
|
|
235
240
|
const ref = (reference) => {
|
|
236
|
-
if (isObject(reference) && reference.isObjectionReferenceBuilder) {
|
|
241
|
+
if ((0, objectUtils_1.isObject)(reference) && reference.isObjectionReferenceBuilder) {
|
|
237
242
|
return reference;
|
|
238
243
|
}
|
|
239
244
|
else {
|
|
240
245
|
return new ReferenceBuilder(reference);
|
|
241
246
|
}
|
|
242
247
|
};
|
|
243
|
-
|
|
244
|
-
ReferenceBuilder,
|
|
245
|
-
ref,
|
|
246
|
-
};
|
|
248
|
+
exports.ref = ref;
|