@abedmurrar/objection 0.0.0-stage → 4.0.0-beta.0
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/LICENSE +23 -0
- package/README.md +105 -2
- package/lib/index.mjs +6 -0
- package/lib/initialize.js +11 -0
- package/lib/model/AjvValidator.js +278 -0
- package/lib/model/Model.js +772 -0
- package/lib/model/ModifierNotFoundError.js +10 -0
- package/lib/model/NotFoundError.js +19 -0
- package/lib/model/RelationDoesNotExistError.js +11 -0
- package/lib/model/ValidationError.js +46 -0
- package/lib/model/Validator.js +20 -0
- package/lib/model/getModel.js +7 -0
- package/lib/model/graph/ModelGraph.js +60 -0
- package/lib/model/graph/ModelGraphBuilder.js +188 -0
- package/lib/model/graph/ModelGraphEdge.js +40 -0
- package/lib/model/graph/ModelGraphNode.js +201 -0
- package/lib/model/inheritModel.js +19 -0
- package/lib/model/modelBindKnex.js +69 -0
- package/lib/model/modelClone.js +102 -0
- package/lib/model/modelColPropMap.js +24 -0
- package/lib/model/modelConcurrency.js +24 -0
- package/lib/model/modelId.js +57 -0
- package/lib/model/modelJsonAttributes.js +72 -0
- package/lib/model/modelParseRelations.js +83 -0
- package/lib/model/modelQueryProps.js +116 -0
- package/lib/model/modelSet.js +116 -0
- package/lib/model/modelTableMetadata.js +71 -0
- package/lib/model/modelToJson.js +157 -0
- package/lib/model/modelUtils.js +40 -0
- package/lib/model/modelValidate.js +40 -0
- package/lib/model/modelValues.js +113 -0
- package/lib/model/modelVisitor.js +34 -0
- package/lib/objection.js +89 -0
- package/lib/queryBuilder/FunctionBuilder.js +291 -0
- package/lib/queryBuilder/InternalOptions.js +24 -0
- package/lib/queryBuilder/JoinBuilder.js +124 -0
- package/lib/queryBuilder/QueryBuilder.js +1136 -0
- package/lib/queryBuilder/QueryBuilderBase.js +666 -0
- package/lib/queryBuilder/QueryBuilderContext.js +20 -0
- package/lib/queryBuilder/QueryBuilderContextBase.js +26 -0
- package/lib/queryBuilder/QueryBuilderOperationSupport.js +484 -0
- package/lib/queryBuilder/QueryBuilderUserContext.js +25 -0
- package/lib/queryBuilder/RawBuilder.js +78 -0
- package/lib/queryBuilder/ReferenceBuilder.js +246 -0
- package/lib/queryBuilder/RelationExpression.js +373 -0
- package/lib/queryBuilder/StaticHookArguments.js +122 -0
- package/lib/queryBuilder/ValueBuilder.js +115 -0
- package/lib/queryBuilder/expressions.js +121 -0
- package/lib/queryBuilder/functions/mysql.js +762 -0
- package/lib/queryBuilder/graph/GraphAction.js +54 -0
- package/lib/queryBuilder/graph/GraphData.js +12 -0
- package/lib/queryBuilder/graph/GraphFetcher.js +155 -0
- package/lib/queryBuilder/graph/GraphNodeDbExistence.js +146 -0
- package/lib/queryBuilder/graph/GraphOperation.js +21 -0
- package/lib/queryBuilder/graph/GraphOptions.js +165 -0
- package/lib/queryBuilder/graph/GraphUpsert.js +231 -0
- package/lib/queryBuilder/graph/delete/GraphDelete.js +15 -0
- package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +56 -0
- package/lib/queryBuilder/graph/insert/GraphInsert.js +116 -0
- package/lib/queryBuilder/graph/insert/GraphInsertAction.js +183 -0
- package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +70 -0
- package/lib/queryBuilder/graph/patch/GraphPatch.js +15 -0
- package/lib/queryBuilder/graph/patch/GraphPatchAction.js +205 -0
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +26 -0
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +96 -0
- package/lib/queryBuilder/join/JoinResultColumn.js +19 -0
- package/lib/queryBuilder/join/JoinResultParser.js +106 -0
- package/lib/queryBuilder/join/RelationJoiner.js +258 -0
- package/lib/queryBuilder/join/TableNode.js +136 -0
- package/lib/queryBuilder/join/TableTree.js +69 -0
- package/lib/queryBuilder/join/utils.js +19 -0
- package/lib/queryBuilder/operations/DelegateOperation.js +101 -0
- package/lib/queryBuilder/operations/DeleteOperation.js +47 -0
- package/lib/queryBuilder/operations/FindByIdOperation.js +29 -0
- package/lib/queryBuilder/operations/FindByIdsOperation.js +23 -0
- package/lib/queryBuilder/operations/FindOperation.js +147 -0
- package/lib/queryBuilder/operations/FirstOperation.js +22 -0
- package/lib/queryBuilder/operations/FromOperation.js +91 -0
- package/lib/queryBuilder/operations/InsertAndFetchOperation.js +38 -0
- package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +35 -0
- package/lib/queryBuilder/operations/InsertGraphOperation.js +87 -0
- package/lib/queryBuilder/operations/InsertOperation.js +173 -0
- package/lib/queryBuilder/operations/InstanceDeleteOperation.js +37 -0
- package/lib/queryBuilder/operations/InstanceFindOperation.js +17 -0
- package/lib/queryBuilder/operations/InstanceInsertOperation.js +20 -0
- package/lib/queryBuilder/operations/InstanceUpdateOperation.js +45 -0
- package/lib/queryBuilder/operations/JoinRelatedOperation.js +83 -0
- package/lib/queryBuilder/operations/KnexOperation.js +11 -0
- package/lib/queryBuilder/operations/MergeOperation.js +43 -0
- package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +173 -0
- package/lib/queryBuilder/operations/OnBuildKnexOperation.js +23 -0
- package/lib/queryBuilder/operations/OnBuildOperation.js +23 -0
- package/lib/queryBuilder/operations/OnErrorOperation.js +23 -0
- package/lib/queryBuilder/operations/QueryBuilderOperation.js +258 -0
- package/lib/queryBuilder/operations/RangeOperation.js +37 -0
- package/lib/queryBuilder/operations/RelateOperation.js +20 -0
- package/lib/queryBuilder/operations/ReturningOperation.js +24 -0
- package/lib/queryBuilder/operations/RunAfterOperation.js +23 -0
- package/lib/queryBuilder/operations/RunBeforeOperation.js +23 -0
- package/lib/queryBuilder/operations/UnrelateOperation.js +18 -0
- package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +63 -0
- package/lib/queryBuilder/operations/UpdateOperation.js +130 -0
- package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +35 -0
- package/lib/queryBuilder/operations/UpsertGraphOperation.js +47 -0
- package/lib/queryBuilder/operations/WhereCompositeOperation.js +48 -0
- package/lib/queryBuilder/operations/WhereInOperation.js +46 -0
- package/lib/queryBuilder/operations/eager/EagerOperation.js +46 -0
- package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +41 -0
- package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +10 -0
- package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +227 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +29 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectOperation.js +37 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +30 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOverlapsOperation.js +33 -0
- package/lib/queryBuilder/operations/jsonApi/mysqlJsonApi.js +155 -0
- package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +135 -0
- package/lib/queryBuilder/operations/select/SelectOperation.js +50 -0
- package/lib/queryBuilder/operations/select/Selection.js +112 -0
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +84 -0
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +90 -0
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +95 -0
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +787 -0
- package/lib/queryBuilder/parsers/relationExpressionParser.js +930 -0
- package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +17 -0
- package/lib/queryBuilder/transformations/QueryTransformation.js +9 -0
- package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +57 -0
- package/lib/queryBuilder/transformations/index.js +9 -0
- package/lib/queryBuilder/whereScopes.js +199 -0
- package/lib/relations/Relation.js +317 -0
- package/lib/relations/RelationDeleteOperation.js +23 -0
- package/lib/relations/RelationFindOperation.js +116 -0
- package/lib/relations/RelationInsertOperation.js +25 -0
- package/lib/relations/RelationOwner.js +199 -0
- package/lib/relations/RelationProperty.js +207 -0
- package/lib/relations/RelationUpdateOperation.js +23 -0
- package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +18 -0
- package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +33 -0
- package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +39 -0
- package/lib/relations/belongsToOne/BelongsToOneRelation.js +45 -0
- package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +46 -0
- package/lib/relations/hasMany/HasManyInsertOperation.js +45 -0
- package/lib/relations/hasMany/HasManyRelateOperation.js +48 -0
- package/lib/relations/hasMany/HasManyRelation.js +41 -0
- package/lib/relations/hasMany/HasManyUnrelateOperation.js +26 -0
- package/lib/relations/hasOne/HasOneRelation.js +10 -0
- package/lib/relations/hasOneThrough/HasOneThroughRelation.js +10 -0
- package/lib/relations/manyToMany/ManyToManyModifyMixin.js +82 -0
- package/lib/relations/manyToMany/ManyToManyRelation.js +405 -0
- package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +29 -0
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +8 -0
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +20 -0
- package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +8 -0
- package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +82 -0
- package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +40 -0
- package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +64 -0
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +11 -0
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +27 -0
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +11 -0
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +8 -0
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +65 -0
- package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +8 -0
- package/lib/transaction.js +66 -0
- package/lib/utils/assert.js +19 -0
- package/lib/utils/buildUtils.js +19 -0
- package/lib/utils/classUtils.js +9 -0
- package/lib/utils/cloneDeep.js +365 -0
- package/lib/utils/createModifier.js +37 -0
- package/lib/utils/deprecate.js +11 -0
- package/lib/utils/identifierMapping.js +188 -0
- package/lib/utils/internalPropUtils.js +7 -0
- package/lib/utils/knexUtils.js +81 -0
- package/lib/utils/mixin.js +18 -0
- package/lib/utils/mysqlJsonPath.js +70 -0
- package/lib/utils/normalizeIds.js +127 -0
- package/lib/utils/objectUtils.js +314 -0
- package/lib/utils/parseFieldExpression.js +73 -0
- package/lib/utils/postgresJsonPath.js +96 -0
- package/lib/utils/promiseUtils/after.js +14 -0
- package/lib/utils/promiseUtils/afterReturn.js +14 -0
- package/lib/utils/promiseUtils/index.js +15 -0
- package/lib/utils/promiseUtils/isPromise.js +8 -0
- package/lib/utils/promiseUtils/map.js +96 -0
- package/lib/utils/promiseUtils/mapAfterAllReturn.js +23 -0
- package/lib/utils/promiseUtils/try.js +19 -0
- package/lib/utils/resolveModel.js +104 -0
- package/lib/utils/tmpColumnUtils.js +7 -0
- package/package.json +113 -4
- package/typings/objection/index.d.mts +362 -0
- package/typings/objection/index.d.ts +4131 -0
- package/typings/objection/relation-expression.d.ts +237 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { RawBuilder, normalizeRawArgs } = require('./RawBuilder');
|
|
3
|
+
const { ref } = require('./ReferenceBuilder');
|
|
4
|
+
const { expression } = require('./expressions');
|
|
5
|
+
const { asArray, asSingle, isNumber, isObject, isPlainObject } = require('../utils/objectUtils');
|
|
6
|
+
const { getDialect, isMySql, isMariaDb } = require('../utils/knexUtils');
|
|
7
|
+
const { jsonCastsForDialect, JSON_CAST } = require('../utils/mysqlJsonPath');
|
|
8
|
+
const { FUNCTIONS, MARIADB_UNSUPPORTED, buildOrderBy } = require('./functions/mysql');
|
|
9
|
+
class FunctionBuilder extends RawBuilder {
|
|
10
|
+
constructor(sql, args) {
|
|
11
|
+
super(sql, args);
|
|
12
|
+
// The name of the `fn` helper of a MySQL only function.
|
|
13
|
+
this._mysqlFunction = null;
|
|
14
|
+
// Why the helper can't be used on MariaDB, if it can't.
|
|
15
|
+
this._mariaDbError = null;
|
|
16
|
+
// For aggregate functions, the number of the first `?`s that are aggregated,
|
|
17
|
+
// or '*' for `COUNT(*)`.
|
|
18
|
+
this._aggregated = null;
|
|
19
|
+
// The name of the `fn` helper of a window function.
|
|
20
|
+
this._windowFunction = null;
|
|
21
|
+
this._filters = [];
|
|
22
|
+
this._over = null;
|
|
23
|
+
}
|
|
24
|
+
// Only aggregates the rows that match `condition`:
|
|
25
|
+
//
|
|
26
|
+
// fn.count().filter(Pet.ref('species').eq('dog'))
|
|
27
|
+
//
|
|
28
|
+
// is `COUNT(CASE WHEN species = 'dog' THEN 1 END)`. That works on every
|
|
29
|
+
// database, unlike `FILTER (WHERE ...)`.
|
|
30
|
+
filter(condition) {
|
|
31
|
+
if (this._aggregated === null) {
|
|
32
|
+
throw new Error('filter() only works with the aggregate functions of fn, like fn.count() and fn.sum().');
|
|
33
|
+
}
|
|
34
|
+
this._filters.push(condition);
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
// `fn.rowNumber().over({ partitionBy: 'ownerId', orderBy: ['createdAt', 'desc'] })`.
|
|
38
|
+
over(window = {}) {
|
|
39
|
+
for (const key of Object.keys(window)) {
|
|
40
|
+
if (key !== 'partitionBy' && key !== 'orderBy') {
|
|
41
|
+
throw new Error(`over(): unknown option "${key}". Use partitionBy and orderBy.`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
this._over = window;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
toKnexRaw(builder) {
|
|
48
|
+
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)}.`);
|
|
51
|
+
}
|
|
52
|
+
// The SQL of the helpers is created before the database is known.
|
|
53
|
+
if (knex && isMariaDb(knex) && this._sql.includes(JSON_CAST)) {
|
|
54
|
+
const copy = Object.assign(Object.create(Object.getPrototypeOf(this)), this);
|
|
55
|
+
copy._sql = jsonCastsForDialect(this._sql, knex);
|
|
56
|
+
return copy.toKnexRaw(builder);
|
|
57
|
+
}
|
|
58
|
+
if (this._mariaDbError && knex && isMariaDb(knex)) {
|
|
59
|
+
throw new Error(this._mariaDbError);
|
|
60
|
+
}
|
|
61
|
+
if (this._windowFunction && !this._over) {
|
|
62
|
+
throw new Error(`fn.${this._windowFunction}() is a window function and needs over().`);
|
|
63
|
+
}
|
|
64
|
+
if (this._filters.length === 0 && !this._over) {
|
|
65
|
+
return super.toKnexRaw(builder);
|
|
66
|
+
}
|
|
67
|
+
return new RawBuilder(...this._buildFilterAndOver()).as(this._as).toKnexRaw(builder);
|
|
68
|
+
}
|
|
69
|
+
_buildFilterAndOver() {
|
|
70
|
+
let sql = this._sql;
|
|
71
|
+
let args = this._args;
|
|
72
|
+
if (this._filters.length !== 0) {
|
|
73
|
+
const condition = this._filters.length === 1
|
|
74
|
+
? this._filters[0]
|
|
75
|
+
: expression(`(${this._filters.map(() => '?').join(' AND ')})`, this._filters);
|
|
76
|
+
if (this._aggregated === '*') {
|
|
77
|
+
sql = sql.replace('*', 'CASE WHEN ? THEN 1 END');
|
|
78
|
+
args = [condition];
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
const filtered = [];
|
|
82
|
+
let i = 0;
|
|
83
|
+
sql = sql.replace(/\?/g, () => {
|
|
84
|
+
const arg = args[i++];
|
|
85
|
+
if (i <= this._aggregated) {
|
|
86
|
+
filtered.push(condition, arg);
|
|
87
|
+
return 'CASE WHEN ? THEN ? END';
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
filtered.push(arg);
|
|
91
|
+
return '?';
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
args = filtered;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (this._over) {
|
|
98
|
+
const [overSql, overArgs] = buildOver(this._over);
|
|
99
|
+
sql = `${sql} OVER (${overSql})`;
|
|
100
|
+
args = [...args, ...overArgs];
|
|
101
|
+
}
|
|
102
|
+
return [sql, args];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// `fn.case()` is `CASE WHEN condition THEN result ... END` and `fn.case(value)` is
|
|
106
|
+
// `CASE value WHEN other THEN result ... END`.
|
|
107
|
+
class CaseBuilder extends FunctionBuilder {
|
|
108
|
+
constructor(hasValue, value) {
|
|
109
|
+
super('', []);
|
|
110
|
+
this._hasValue = hasValue;
|
|
111
|
+
this._value = value;
|
|
112
|
+
this._whens = [];
|
|
113
|
+
this._hasElse = false;
|
|
114
|
+
this._else = null;
|
|
115
|
+
}
|
|
116
|
+
when(condition, result) {
|
|
117
|
+
this._whens.push([condition, result]);
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
else(result) {
|
|
121
|
+
this._hasElse = true;
|
|
122
|
+
this._else = result;
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
toKnexRaw(builder) {
|
|
126
|
+
if (this._whens.length === 0) {
|
|
127
|
+
throw new Error('fn.case() needs at least one when().');
|
|
128
|
+
}
|
|
129
|
+
const args = this._hasValue ? [this._value] : [];
|
|
130
|
+
let sql = this._hasValue ? '(CASE ?' : '(CASE';
|
|
131
|
+
for (const [condition, result] of this._whens) {
|
|
132
|
+
sql += ' WHEN ? THEN ?';
|
|
133
|
+
args.push(condition, result);
|
|
134
|
+
}
|
|
135
|
+
if (this._hasElse) {
|
|
136
|
+
sql += ' ELSE ?';
|
|
137
|
+
args.push(this._else);
|
|
138
|
+
}
|
|
139
|
+
this._sql = `${sql} END)`;
|
|
140
|
+
this._args = args;
|
|
141
|
+
return super.toKnexRaw(builder);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// The value an upsert tried to insert: `VALUES(col)` on MySQL, `excluded.col` on
|
|
145
|
+
// postgres and SQLite.
|
|
146
|
+
class ExcludedBuilder extends FunctionBuilder {
|
|
147
|
+
constructor(column) {
|
|
148
|
+
super('', []);
|
|
149
|
+
if (isObject(column) && column.isObjectionReferenceBuilder) {
|
|
150
|
+
column = column.column;
|
|
151
|
+
}
|
|
152
|
+
if (typeof column !== 'string') {
|
|
153
|
+
throw new Error('fn.excluded() takes a column name.');
|
|
154
|
+
}
|
|
155
|
+
this._column = column;
|
|
156
|
+
}
|
|
157
|
+
toKnexRaw(builder) {
|
|
158
|
+
if (isMySql(builder.unsafeKnex())) {
|
|
159
|
+
this._sql = 'VALUES(??)';
|
|
160
|
+
this._args = [this._column];
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
this._sql = '??';
|
|
164
|
+
this._args = [`excluded.${this._column}`];
|
|
165
|
+
}
|
|
166
|
+
return super.toKnexRaw(builder);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function buildOver({ partitionBy, orderBy }) {
|
|
170
|
+
const parts = [];
|
|
171
|
+
const args = [];
|
|
172
|
+
if (partitionBy !== undefined) {
|
|
173
|
+
const columns = asArray(partitionBy).map((it) => (typeof it === 'string' ? ref(it) : it));
|
|
174
|
+
parts.push(`PARTITION BY ${columns.map(() => '?').join(', ')}`);
|
|
175
|
+
args.push(...columns);
|
|
176
|
+
}
|
|
177
|
+
if (orderBy !== undefined) {
|
|
178
|
+
const [orderBySql, orderByArgs] = buildOrderBy('over()', orderBy);
|
|
179
|
+
parts.push(`ORDER BY ${orderBySql}`);
|
|
180
|
+
args.push(...orderByArgs);
|
|
181
|
+
}
|
|
182
|
+
return [parts.join(' '), args];
|
|
183
|
+
}
|
|
184
|
+
function fn(...argsIn) {
|
|
185
|
+
const { sql, args } = normalizeRawArgs(argsIn);
|
|
186
|
+
return new FunctionBuilder(`${sql}(${args.map(() => '?').join(', ')})`, args);
|
|
187
|
+
}
|
|
188
|
+
for (const func of ['coalesce', 'concat', 'upper', 'lower']) {
|
|
189
|
+
fn[func] = (...args) => fn(func.toUpperCase(), args);
|
|
190
|
+
}
|
|
191
|
+
for (const func of ['sum', 'avg', 'min', 'max']) {
|
|
192
|
+
fn[func] = (...args) => aggregate(fn(func.toUpperCase(), args), args.length);
|
|
193
|
+
}
|
|
194
|
+
// `fn.count()` and `fn.count('*')` are `COUNT(*)`.
|
|
195
|
+
fn.count = (...args) => {
|
|
196
|
+
if (args.length === 0 || (args.length === 1 && args[0] === '*')) {
|
|
197
|
+
return aggregate(new FunctionBuilder('COUNT(*)', []), '*');
|
|
198
|
+
}
|
|
199
|
+
return aggregate(fn('COUNT', args), args.length);
|
|
200
|
+
};
|
|
201
|
+
fn.now = (precision) => {
|
|
202
|
+
precision = parseInt(asSingle(precision), 10);
|
|
203
|
+
if (isNaN(precision) || !isNumber(precision)) {
|
|
204
|
+
precision = 6;
|
|
205
|
+
}
|
|
206
|
+
// We need to use a literal precision instead of a binding here
|
|
207
|
+
// for the CURRENT_TIMESTAMP to work. This is okay here since we
|
|
208
|
+
// make sure `precision` is a number. There's no chance of SQL
|
|
209
|
+
// injection here.
|
|
210
|
+
return new FunctionBuilder(`CURRENT_TIMESTAMP(${precision})`, []);
|
|
211
|
+
};
|
|
212
|
+
fn.case = (...args) => new CaseBuilder(args.length !== 0, args[0]);
|
|
213
|
+
fn.exists = (subquery) => {
|
|
214
|
+
if (!isObject(subquery) || !subquery.isObjectionQueryBuilderBase) {
|
|
215
|
+
throw new Error('fn.exists() takes a query.');
|
|
216
|
+
}
|
|
217
|
+
return new FunctionBuilder('(EXISTS ?)', [subquery]);
|
|
218
|
+
};
|
|
219
|
+
fn.excluded = (column) => new ExcludedBuilder(column);
|
|
220
|
+
for (const [name, sql, , , options = {}] of FUNCTIONS) {
|
|
221
|
+
// `defineProperty` since `fn.length` is a read-only property of functions.
|
|
222
|
+
Object.defineProperty(fn, name, {
|
|
223
|
+
configurable: true,
|
|
224
|
+
enumerable: true,
|
|
225
|
+
writable: true,
|
|
226
|
+
value: createMySqlFunction(name, sql, options),
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
function createMySqlFunction(name, sql, options) {
|
|
230
|
+
return (...args) => {
|
|
231
|
+
const [text, values] = options.build
|
|
232
|
+
? options.build(name, sql, args)
|
|
233
|
+
: [`${sql}(${args.map(() => '?').join(', ')})`, args];
|
|
234
|
+
const builder = createWithJsonArgs(text, values, options.json);
|
|
235
|
+
if (!options.portable) {
|
|
236
|
+
builder._mysqlFunction = name;
|
|
237
|
+
}
|
|
238
|
+
const mariaDbReason = MARIADB_UNSUPPORTED[name] && MARIADB_UNSUPPORTED[name](args);
|
|
239
|
+
if (mariaDbReason) {
|
|
240
|
+
builder._mariaDbError = `fn.${name}() ${mariaDbReason}.`;
|
|
241
|
+
}
|
|
242
|
+
if (options.window) {
|
|
243
|
+
builder._windowFunction = name;
|
|
244
|
+
}
|
|
245
|
+
if (options.aggregate) {
|
|
246
|
+
aggregate(builder, options.aggregate === true ? args.length : options.aggregate(args));
|
|
247
|
+
}
|
|
248
|
+
return builder;
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
function aggregate(builder, aggregated) {
|
|
252
|
+
builder._aggregated = aggregated;
|
|
253
|
+
return builder;
|
|
254
|
+
}
|
|
255
|
+
// Objects and arrays passed as JSON documents are cast to JSON. Without the cast
|
|
256
|
+
// MySQL would get a string (or knex would expand an array into a list).
|
|
257
|
+
function createWithJsonArgs(text, values, json) {
|
|
258
|
+
const casts = values.map((value, i) => isJsonArg(json, i) && isJsonValue(value));
|
|
259
|
+
let i = 0;
|
|
260
|
+
text = text.replace(/\?/g, () => (casts[i++] ? 'CAST(? AS JSON)' : '?'));
|
|
261
|
+
values = values.map((value, i) => (casts[i] ? JSON.stringify(value) : value));
|
|
262
|
+
return new FunctionBuilder(text, values);
|
|
263
|
+
}
|
|
264
|
+
function isJsonArg(json, i) {
|
|
265
|
+
if (json === 'all') {
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
else if (json === 'even') {
|
|
269
|
+
return i % 2 === 0;
|
|
270
|
+
}
|
|
271
|
+
else if (json === 'odd') {
|
|
272
|
+
return i % 2 === 1;
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
return Array.isArray(json) && json.includes(i);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
function isJsonValue(value) {
|
|
279
|
+
return Array.isArray(value) || isPlainObject(value);
|
|
280
|
+
}
|
|
281
|
+
Object.defineProperties(FunctionBuilder.prototype, {
|
|
282
|
+
isObjectionFunctionBuilder: {
|
|
283
|
+
enumerable: false,
|
|
284
|
+
writable: false,
|
|
285
|
+
value: true,
|
|
286
|
+
},
|
|
287
|
+
});
|
|
288
|
+
module.exports = {
|
|
289
|
+
FunctionBuilder,
|
|
290
|
+
fn,
|
|
291
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
class InternalOptions {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.skipUndefined = false;
|
|
5
|
+
this.keepImplicitJoinProps = false;
|
|
6
|
+
this.returnImmediatelyValue = undefined;
|
|
7
|
+
this.isInternalQuery = false;
|
|
8
|
+
this.debug = false;
|
|
9
|
+
this.schema = undefined;
|
|
10
|
+
}
|
|
11
|
+
clone() {
|
|
12
|
+
const copy = new this.constructor();
|
|
13
|
+
copy.skipUndefined = this.skipUndefined;
|
|
14
|
+
copy.keepImplicitJoinProps = this.keepImplicitJoinProps;
|
|
15
|
+
copy.returnImmediatelyValue = this.returnImmediatelyValue;
|
|
16
|
+
copy.isInternalQuery = this.isInternalQuery;
|
|
17
|
+
copy.debug = this.debug;
|
|
18
|
+
copy.schema = this.schema;
|
|
19
|
+
return copy;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
module.exports = {
|
|
23
|
+
InternalOptions,
|
|
24
|
+
};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { QueryBuilderOperationSupport } = require('./QueryBuilderOperationSupport');
|
|
3
|
+
const { KnexOperation } = require('./operations/KnexOperation');
|
|
4
|
+
const { WhereInOperation } = require('./operations/WhereInOperation');
|
|
5
|
+
const { QueryBuilderOperation } = require('./operations/QueryBuilderOperation');
|
|
6
|
+
// knex's `join.on(a, b).or.on(c, d)`: `or` makes the next condition an `or`.
|
|
7
|
+
class OrOperation extends QueryBuilderOperation {
|
|
8
|
+
onBuildKnex(knexBuilder) {
|
|
9
|
+
return knexBuilder.or;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
class JoinBuilder extends QueryBuilderOperationSupport {
|
|
13
|
+
get or() {
|
|
14
|
+
return this.addOperation(new OrOperation('or'), []);
|
|
15
|
+
}
|
|
16
|
+
using(...args) {
|
|
17
|
+
return this.addOperation(new KnexOperation('using'), args);
|
|
18
|
+
}
|
|
19
|
+
on(...args) {
|
|
20
|
+
return this.addOperation(new KnexOperation('on'), args);
|
|
21
|
+
}
|
|
22
|
+
orOn(...args) {
|
|
23
|
+
return this.addOperation(new KnexOperation('orOn'), args);
|
|
24
|
+
}
|
|
25
|
+
onBetween(...args) {
|
|
26
|
+
return this.addOperation(new KnexOperation('onBetween'), args);
|
|
27
|
+
}
|
|
28
|
+
onNotBetween(...args) {
|
|
29
|
+
return this.addOperation(new KnexOperation('onNotBetween'), args);
|
|
30
|
+
}
|
|
31
|
+
orOnBetween(...args) {
|
|
32
|
+
return this.addOperation(new KnexOperation('orOnBetween'), args);
|
|
33
|
+
}
|
|
34
|
+
orOnNotBetween(...args) {
|
|
35
|
+
return this.addOperation(new KnexOperation('orOnNotBetween'), args);
|
|
36
|
+
}
|
|
37
|
+
onIn(...args) {
|
|
38
|
+
return this.addOperation(new WhereInOperation('onIn', { rawMethod: 'on' }), args);
|
|
39
|
+
}
|
|
40
|
+
onNotIn(...args) {
|
|
41
|
+
return this.addOperation(new WhereInOperation('onNotIn', { rawMethod: 'on', not: true }), args);
|
|
42
|
+
}
|
|
43
|
+
orOnIn(...args) {
|
|
44
|
+
return this.addOperation(new WhereInOperation('orOnIn', { rawMethod: 'orOn' }), args);
|
|
45
|
+
}
|
|
46
|
+
orOnNotIn(...args) {
|
|
47
|
+
return this.addOperation(new WhereInOperation('orOnNotIn', { rawMethod: 'orOn', not: true }), args);
|
|
48
|
+
}
|
|
49
|
+
onNull(...args) {
|
|
50
|
+
return this.addOperation(new KnexOperation('onNull'), args);
|
|
51
|
+
}
|
|
52
|
+
orOnNull(...args) {
|
|
53
|
+
return this.addOperation(new KnexOperation('orOnNull'), args);
|
|
54
|
+
}
|
|
55
|
+
onNotNull(...args) {
|
|
56
|
+
return this.addOperation(new KnexOperation('onNotNull'), args);
|
|
57
|
+
}
|
|
58
|
+
orOnNotNull(...args) {
|
|
59
|
+
return this.addOperation(new KnexOperation('orOnNotNull'), args);
|
|
60
|
+
}
|
|
61
|
+
onExists(...args) {
|
|
62
|
+
return this.addOperation(new KnexOperation('onExists'), args);
|
|
63
|
+
}
|
|
64
|
+
orOnExists(...args) {
|
|
65
|
+
return this.addOperation(new KnexOperation('orOnExists'), args);
|
|
66
|
+
}
|
|
67
|
+
onNotExists(...args) {
|
|
68
|
+
return this.addOperation(new KnexOperation('onNotExists'), args);
|
|
69
|
+
}
|
|
70
|
+
orOnNotExists(...args) {
|
|
71
|
+
return this.addOperation(new KnexOperation('orOnNotExists'), args);
|
|
72
|
+
}
|
|
73
|
+
type(...args) {
|
|
74
|
+
return this.addOperation(new KnexOperation('type'), args);
|
|
75
|
+
}
|
|
76
|
+
andOn(...args) {
|
|
77
|
+
return this.addOperation(new KnexOperation('andOn'), args);
|
|
78
|
+
}
|
|
79
|
+
andOnIn(...args) {
|
|
80
|
+
return this.addOperation(new WhereInOperation('andOnIn', { rawMethod: 'andOn' }), args);
|
|
81
|
+
}
|
|
82
|
+
andOnNotIn(...args) {
|
|
83
|
+
return this.addOperation(new WhereInOperation('andOnNotIn', { rawMethod: 'andOn', not: true }), args);
|
|
84
|
+
}
|
|
85
|
+
andOnNull(...args) {
|
|
86
|
+
return this.addOperation(new KnexOperation('andOnNull'), args);
|
|
87
|
+
}
|
|
88
|
+
andOnNotNull(...args) {
|
|
89
|
+
return this.addOperation(new KnexOperation('andOnNotNull'), args);
|
|
90
|
+
}
|
|
91
|
+
andOnExists(...args) {
|
|
92
|
+
return this.addOperation(new KnexOperation('andOnExists'), args);
|
|
93
|
+
}
|
|
94
|
+
andOnNotExists(...args) {
|
|
95
|
+
return this.addOperation(new KnexOperation('andOnNotExists'), args);
|
|
96
|
+
}
|
|
97
|
+
andOnBetween(...args) {
|
|
98
|
+
return this.addOperation(new KnexOperation('andOnBetween'), args);
|
|
99
|
+
}
|
|
100
|
+
andOnNotBetween(...args) {
|
|
101
|
+
return this.addOperation(new KnexOperation('andOnNotBetween'), args);
|
|
102
|
+
}
|
|
103
|
+
onJsonPathEquals(...args) {
|
|
104
|
+
return this.addOperation(new KnexOperation('onJsonPathEquals'), args);
|
|
105
|
+
}
|
|
106
|
+
orOnJsonPathEquals(...args) {
|
|
107
|
+
return this.addOperation(new KnexOperation('orOnJsonPathEquals'), args);
|
|
108
|
+
}
|
|
109
|
+
andOnJsonPathEquals(...args) {
|
|
110
|
+
return this.addOperation(new KnexOperation('andOnJsonPathEquals'), args);
|
|
111
|
+
}
|
|
112
|
+
onVal(...args) {
|
|
113
|
+
return this.addOperation(new KnexOperation('onVal'), args);
|
|
114
|
+
}
|
|
115
|
+
andOnVal(...args) {
|
|
116
|
+
return this.addOperation(new KnexOperation('andOnVal'), args);
|
|
117
|
+
}
|
|
118
|
+
orOnVal(...args) {
|
|
119
|
+
return this.addOperation(new KnexOperation('orOnVal'), args);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
module.exports = {
|
|
123
|
+
JoinBuilder,
|
|
124
|
+
};
|