@abedmurrar/objection 4.0.0-beta.1 → 4.0.0-beta.10

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 (134) hide show
  1. package/README.md +1 -1
  2. package/lib/initialize.js +4 -5
  3. package/lib/model/AjvValidator.js +24 -21
  4. package/lib/model/Model.js +116 -114
  5. package/lib/model/ModifierNotFoundError.js +4 -4
  6. package/lib/model/NotFoundError.js +4 -4
  7. package/lib/model/RelationDoesNotExistError.js +4 -4
  8. package/lib/model/ValidationError.js +8 -8
  9. package/lib/model/Validator.js +7 -6
  10. package/lib/model/getModel.js +4 -4
  11. package/lib/model/graph/ModelGraph.js +9 -7
  12. package/lib/model/graph/ModelGraphBuilder.js +24 -24
  13. package/lib/model/graph/ModelGraphEdge.js +4 -4
  14. package/lib/model/graph/ModelGraphNode.js +17 -11
  15. package/lib/model/inheritModel.js +3 -4
  16. package/lib/model/modelBindKnex.js +12 -13
  17. package/lib/model/modelClone.js +15 -16
  18. package/lib/model/modelColPropMap.js +7 -8
  19. package/lib/model/modelConcurrency.js +6 -7
  20. package/lib/model/modelId.js +4 -5
  21. package/lib/model/modelJsonAttributes.js +10 -11
  22. package/lib/model/modelParseRelations.js +5 -6
  23. package/lib/model/modelQueryProps.js +12 -13
  24. package/lib/model/modelSet.js +17 -18
  25. package/lib/model/modelTableMetadata.js +9 -10
  26. package/lib/model/modelToJson.js +13 -14
  27. package/lib/model/modelUtils.js +7 -7
  28. package/lib/model/modelValidate.js +5 -6
  29. package/lib/model/modelValues.js +10 -10
  30. package/lib/model/modelVisitor.js +3 -4
  31. package/lib/objection.js +4 -26
  32. package/lib/queryBuilder/FunctionBuilder.js +35 -31
  33. package/lib/queryBuilder/InternalOptions.js +4 -4
  34. package/lib/queryBuilder/JoinBuilder.js +46 -45
  35. package/lib/queryBuilder/QueryBuilder.js +161 -135
  36. package/lib/queryBuilder/QueryBuilderBase.js +203 -203
  37. package/lib/queryBuilder/QueryBuilderContext.js +6 -6
  38. package/lib/queryBuilder/QueryBuilderContextBase.js +12 -8
  39. package/lib/queryBuilder/QueryBuilderOperationSupport.js +30 -25
  40. package/lib/queryBuilder/QueryBuilderUserContext.js +4 -4
  41. package/lib/queryBuilder/RawBuilder.js +15 -13
  42. package/lib/queryBuilder/ReferenceBuilder.js +25 -23
  43. package/lib/queryBuilder/RelationExpression.js +29 -20
  44. package/lib/queryBuilder/StaticHookArguments.js +11 -11
  45. package/lib/queryBuilder/ValueBuilder.js +20 -19
  46. package/lib/queryBuilder/expressions.js +8 -18
  47. package/lib/queryBuilder/functions/mysql.js +18 -42
  48. package/lib/queryBuilder/graph/GraphAction.js +7 -7
  49. package/lib/queryBuilder/graph/GraphData.js +4 -4
  50. package/lib/queryBuilder/graph/GraphFetcher.js +18 -17
  51. package/lib/queryBuilder/graph/GraphNodeDbExistence.js +45 -12
  52. package/lib/queryBuilder/graph/GraphOperation.js +4 -4
  53. package/lib/queryBuilder/graph/GraphOptions.js +5 -5
  54. package/lib/queryBuilder/graph/GraphUpsert.js +36 -36
  55. package/lib/queryBuilder/graph/delete/GraphDelete.js +8 -8
  56. package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +46 -11
  57. package/lib/queryBuilder/graph/insert/GraphInsert.js +12 -12
  58. package/lib/queryBuilder/graph/insert/GraphInsertAction.js +59 -21
  59. package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +49 -11
  60. package/lib/queryBuilder/graph/patch/GraphPatch.js +8 -8
  61. package/lib/queryBuilder/graph/patch/GraphPatchAction.js +52 -19
  62. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +8 -8
  63. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +54 -18
  64. package/lib/queryBuilder/join/JoinResultColumn.js +6 -4
  65. package/lib/queryBuilder/join/JoinResultParser.js +10 -8
  66. package/lib/queryBuilder/join/RelationJoiner.js +40 -35
  67. package/lib/queryBuilder/join/TableNode.js +7 -5
  68. package/lib/queryBuilder/join/TableTree.js +13 -9
  69. package/lib/queryBuilder/join/utils.js +8 -8
  70. package/lib/queryBuilder/operations/DelegateOperation.js +6 -6
  71. package/lib/queryBuilder/operations/DeleteOperation.js +11 -11
  72. package/lib/queryBuilder/operations/FindByIdOperation.js +8 -8
  73. package/lib/queryBuilder/operations/FindByIdsOperation.js +6 -6
  74. package/lib/queryBuilder/operations/FindOperation.js +23 -23
  75. package/lib/queryBuilder/operations/FirstOperation.js +6 -6
  76. package/lib/queryBuilder/operations/FromOperation.js +13 -13
  77. package/lib/queryBuilder/operations/InsertAndFetchOperation.js +12 -12
  78. package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +10 -10
  79. package/lib/queryBuilder/operations/InsertGraphOperation.js +11 -11
  80. package/lib/queryBuilder/operations/InsertOperation.js +23 -22
  81. package/lib/queryBuilder/operations/InstanceDeleteOperation.js +12 -12
  82. package/lib/queryBuilder/operations/InstanceFindOperation.js +10 -10
  83. package/lib/queryBuilder/operations/InstanceInsertOperation.js +6 -6
  84. package/lib/queryBuilder/operations/InstanceUpdateOperation.js +14 -14
  85. package/lib/queryBuilder/operations/JoinRelatedOperation.js +13 -13
  86. package/lib/queryBuilder/operations/KnexOperation.js +6 -6
  87. package/lib/queryBuilder/operations/MergeOperation.js +11 -11
  88. package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +26 -22
  89. package/lib/queryBuilder/operations/OnBuildKnexOperation.js +6 -6
  90. package/lib/queryBuilder/operations/OnBuildOperation.js +6 -6
  91. package/lib/queryBuilder/operations/OnErrorOperation.js +6 -6
  92. package/lib/queryBuilder/operations/QueryBuilderOperation.js +6 -4
  93. package/lib/queryBuilder/operations/RangeOperation.js +7 -6
  94. package/lib/queryBuilder/operations/RelateOperation.js +6 -6
  95. package/lib/queryBuilder/operations/ReturningOperation.js +8 -8
  96. package/lib/queryBuilder/operations/RunAfterOperation.js +6 -6
  97. package/lib/queryBuilder/operations/RunBeforeOperation.js +6 -6
  98. package/lib/queryBuilder/operations/UnrelateOperation.js +6 -6
  99. package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +12 -12
  100. package/lib/queryBuilder/operations/UpdateOperation.js +24 -24
  101. package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +10 -10
  102. package/lib/queryBuilder/operations/UpsertGraphOperation.js +10 -10
  103. package/lib/queryBuilder/operations/WhereCompositeOperation.js +8 -8
  104. package/lib/queryBuilder/operations/WhereInOperation.js +11 -9
  105. package/lib/queryBuilder/operations/eager/EagerOperation.js +11 -9
  106. package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +10 -12
  107. package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +6 -6
  108. package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +64 -28
  109. package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +45 -12
  110. package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectOperation.js +45 -12
  111. package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +45 -12
  112. package/lib/queryBuilder/operations/jsonApi/WhereJsonOverlapsOperation.js +46 -13
  113. package/lib/queryBuilder/operations/jsonApi/mysqlJsonApi.js +22 -27
  114. package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +51 -17
  115. package/lib/queryBuilder/operations/select/SelectOperation.js +12 -12
  116. package/lib/queryBuilder/operations/select/Selection.js +23 -19
  117. package/lib/queryBuilder/operations/types.js +8 -0
  118. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +13 -12
  119. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +11 -11
  120. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +10 -10
  121. package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +6 -6
  122. package/lib/queryBuilder/transformations/QueryTransformation.js +4 -4
  123. package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +11 -8
  124. package/lib/queryBuilder/transformations/index.js +8 -8
  125. package/lib/queryBuilder/whereScopes.js +15 -21
  126. package/lib/relations/RelationDeleteOperation.js +0 -2
  127. package/lib/relations/RelationFindOperation.js +0 -1
  128. package/lib/relations/RelationUpdateOperation.js +0 -1
  129. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +0 -1
  130. package/lib/transaction.js +44 -12
  131. package/lib/utils/objectUtils.js +6 -1
  132. package/lib/utils/parseFieldExpression.js +4 -35
  133. package/lib/utils/promiseUtils/index.js +19 -9
  134. package/package.json +1 -1
@@ -1,8 +1,11 @@
1
- 'use strict';
2
- const { defineNonEnumerableProperty } = require('./modelUtils');
3
- const { isPromise } = require('../utils/promiseUtils');
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchTableMetadata = fetchTableMetadata;
4
+ exports.tableMetadata = tableMetadata;
5
+ const modelUtils_1 = require("./modelUtils");
6
+ const promiseUtils_1 = require("../utils/promiseUtils");
4
7
  const TABLE_METADATA = '$$tableMetadata';
5
- function fetchTableMetadata(modelClass, { parentBuilder = null, knex = null, force = false, table = null } = {}) {
8
+ function fetchTableMetadata(modelClass, { parentBuilder = null, knex = null, force = false, table = null, } = {}) {
6
9
  // The table isn't necessarily same as `modelClass.getTableName()` for example if
7
10
  // a view is queried instead.
8
11
  if (!table) {
@@ -22,7 +25,7 @@ function fetchTableMetadata(modelClass, { parentBuilder = null, knex = null, for
22
25
  // Memoize metadata but only for modelClass. The hasOwnProperty check
23
26
  // will fail for subclasses and the value gets recreated.
24
27
  if (!Object.hasOwn(modelClass, TABLE_METADATA)) {
25
- defineNonEnumerableProperty(modelClass, TABLE_METADATA, new Map());
28
+ (0, modelUtils_1.defineNonEnumerableProperty)(modelClass, TABLE_METADATA, new Map());
26
29
  }
27
30
  // The cache needs to be checked in addition to calling tableMetadata
28
31
  // because the cache may contain a temporary promise in which case
@@ -54,7 +57,7 @@ function fetchTableMetadata(modelClass, { parentBuilder = null, knex = null, for
54
57
  function tableMetadata(modelClass, { table } = {}) {
55
58
  if (Object.hasOwn(modelClass, TABLE_METADATA)) {
56
59
  const metadata = modelClass[TABLE_METADATA].get(table || modelClass.getTableName());
57
- if (isPromise(metadata)) {
60
+ if ((0, promiseUtils_1.isPromise)(metadata)) {
58
61
  return null;
59
62
  }
60
63
  else {
@@ -65,7 +68,3 @@ function tableMetadata(modelClass, { table } = {}) {
65
68
  return null;
66
69
  }
67
70
  }
68
- module.exports = {
69
- fetchTableMetadata,
70
- tableMetadata,
71
- };
@@ -1,7 +1,10 @@
1
- 'use strict';
2
- const { isInternalProp } = require('../utils/internalPropUtils');
3
- const { mergeQueryProps } = require('./modelQueryProps');
4
- const { isObject, cloneDeep, isFunction } = require('../utils/objectUtils');
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toJson = toJson;
4
+ exports.toDatabaseJson = toDatabaseJson;
5
+ const internalPropUtils_1 = require("../utils/internalPropUtils");
6
+ const modelQueryProps_1 = require("./modelQueryProps");
7
+ const objectUtils_1 = require("../utils/objectUtils");
5
8
  const EMPTY_ARRAY = [];
6
9
  function toJson(model, optIn) {
7
10
  const modelClass = model.constructor;
@@ -29,7 +32,7 @@ function toDatabaseJson(model, builder) {
29
32
  };
30
33
  let json = toDatabaseJsonImpl(model, opt);
31
34
  json = model.$formatDatabaseJson(json);
32
- return mergeQueryProps(model, json, opt.omitFromJson, builder);
35
+ return (0, modelQueryProps_1.mergeQueryProps)(model, json, opt.omitFromJson, builder);
33
36
  }
34
37
  function getVirtuals(opt) {
35
38
  if (!opt) {
@@ -91,10 +94,10 @@ function assignJsonValue(json, key, value, opt) {
91
94
  const type = typeof value;
92
95
  if (type !== 'function' &&
93
96
  type !== 'undefined' &&
94
- !isInternalProp(key) &&
97
+ !(0, internalPropUtils_1.isInternalProp)(key) &&
95
98
  !shouldOmit(opt, key) &&
96
99
  shouldPick(opt, key)) {
97
- if (isObject(value)) {
100
+ if ((0, objectUtils_1.isObject)(value)) {
98
101
  json[key] = toJsonObject(value, opt);
99
102
  }
100
103
  else {
@@ -113,7 +116,7 @@ function assignVirtualAttributes(json, model, vAttr, opt) {
113
116
  for (let i = 0, l = vAttr.length; i < l; ++i) {
114
117
  const key = vAttr[i];
115
118
  let value = model[key];
116
- if (isFunction(value)) {
119
+ if ((0, objectUtils_1.isFunction)(value)) {
117
120
  value = value.call(model);
118
121
  }
119
122
  assignJsonValue(json, key, value, opt);
@@ -132,7 +135,7 @@ function toJsonObject(value, opt) {
132
135
  return value;
133
136
  }
134
137
  else if (opt.cloneObjects) {
135
- return cloneDeep(value);
138
+ return (0, objectUtils_1.cloneDeep)(value);
136
139
  }
137
140
  else {
138
141
  return value;
@@ -142,7 +145,7 @@ function toJsonArray(value, opt) {
142
145
  const ret = new Array(value.length);
143
146
  for (let i = 0, l = ret.length; i < l; ++i) {
144
147
  const item = value[i];
145
- if (isObject(item)) {
148
+ if ((0, objectUtils_1.isObject)(item)) {
146
149
  ret[i] = toJsonObject(item, opt);
147
150
  }
148
151
  else {
@@ -151,7 +154,3 @@ function toJsonArray(value, opt) {
151
154
  }
152
155
  return ret;
153
156
  }
154
- module.exports = {
155
- toJson,
156
- toDatabaseJson,
157
- };
@@ -1,5 +1,10 @@
1
- 'use strict';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.staticHiddenProps = exports.hiddenProps = void 0;
4
+ exports.defineNonEnumerableProperty = defineNonEnumerableProperty;
5
+ exports.keyByProps = keyByProps;
2
6
  const hiddenProps = ['$$queryProps'];
7
+ exports.hiddenProps = hiddenProps;
3
8
  const staticHiddenProps = [
4
9
  '$$knex',
5
10
  '$$validator',
@@ -16,6 +21,7 @@ const staticHiddenProps = [
16
21
  '$$idRelationProperty',
17
22
  '$$virtualAttributes',
18
23
  ];
24
+ exports.staticHiddenProps = staticHiddenProps;
19
25
  function defineNonEnumerableProperty(obj, prop, value) {
20
26
  Object.defineProperty(obj, prop, {
21
27
  enumerable: false,
@@ -32,9 +38,3 @@ function keyByProps(models, props) {
32
38
  }
33
39
  return map;
34
40
  }
35
- module.exports = {
36
- hiddenProps,
37
- staticHiddenProps,
38
- defineNonEnumerableProperty,
39
- keyByProps,
40
- };
@@ -1,5 +1,7 @@
1
- 'use strict';
2
- const { clone } = require('./modelClone');
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validate = validate;
4
+ const modelClone_1 = require("./modelClone");
3
5
  function validate(model, json, options = {}) {
4
6
  json = json || model;
5
7
  const inputJson = json;
@@ -12,7 +14,7 @@ function validate(model, json, options = {}) {
12
14
  // TODO 1: This should use `json.$toJson()` since we always validate the input representation!
13
15
  // TODO 2: This should also keep the relations in the object because some validators may
14
16
  // depend on the relations.
15
- json = clone(json, true, true);
17
+ json = (0, modelClone_1.clone)(json, true, true);
16
18
  // We can mutate `json` now that we took a copy of it.
17
19
  options = Object.assign({}, options, { mutable: true });
18
20
  }
@@ -35,6 +37,3 @@ function validate(model, json, options = {}) {
35
37
  return json;
36
38
  }
37
39
  }
38
- module.exports = {
39
- validate,
40
- };
@@ -1,8 +1,15 @@
1
- 'use strict';
2
- const { isObject } = require('../utils/objectUtils');
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PROP_KEY_PREFIX = void 0;
4
+ exports.propToStr = propToStr;
5
+ exports.values = values;
6
+ exports.hasProps = hasProps;
7
+ exports.propKey = propKey;
8
+ const objectUtils_1 = require("../utils/objectUtils");
3
9
  // Property keys needs to be prefixed with a non-numeric character so that
4
10
  // they are not considered indexes when used as object keys.
5
11
  const PROP_KEY_PREFIX = 'k_';
12
+ exports.PROP_KEY_PREFIX = PROP_KEY_PREFIX;
6
13
  function values(model, args) {
7
14
  switch (args.length) {
8
15
  case 1:
@@ -94,7 +101,7 @@ function propToStr(value) {
94
101
  else if (Buffer.isBuffer(value)) {
95
102
  return value.toString('hex');
96
103
  }
97
- else if (isObject(value)) {
104
+ else if ((0, objectUtils_1.isObject)(value)) {
98
105
  return JSON.stringify(value);
99
106
  }
100
107
  else {
@@ -104,10 +111,3 @@ function propToStr(value) {
104
111
  function isNullOrUndefined(val) {
105
112
  return val === null || val === undefined;
106
113
  }
107
- module.exports = {
108
- PROP_KEY_PREFIX,
109
- propToStr,
110
- values,
111
- hasProps,
112
- propKey,
113
- };
@@ -1,4 +1,6 @@
1
- 'use strict';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.visitModels = visitModels;
2
4
  function visitModels(models, modelClass, visitor) {
3
5
  doVisit(models, modelClass, null, null, visitor);
4
6
  }
@@ -29,6 +31,3 @@ function visitOne(model, modelClass, parent, rel, visitor) {
29
31
  }
30
32
  }
31
33
  }
32
- module.exports = {
33
- visitModels,
34
- };
package/lib/objection.js CHANGED
@@ -1,14 +1,14 @@
1
1
  'use strict';
2
2
  const { DBError, UniqueViolationError, NotNullViolationError, ForeignKeyViolationError, ConstraintViolationError, CheckViolationError, DataError, } = require('db-errors');
3
- const { Model: NativeModel } = require('./model/Model');
4
- const { QueryBuilder: NativeQueryBuilder } = require('./queryBuilder/QueryBuilder');
3
+ const { Model } = require('./model/Model');
4
+ const { QueryBuilder } = require('./queryBuilder/QueryBuilder');
5
5
  const { QueryBuilderBase } = require('./queryBuilder/QueryBuilderBase');
6
6
  const { QueryBuilderOperation } = require('./queryBuilder/operations/QueryBuilderOperation');
7
7
  const { RelationExpression } = require('./queryBuilder/RelationExpression');
8
8
  const { ValidationError } = require('./model/ValidationError');
9
9
  const { NotFoundError } = require('./model/NotFoundError');
10
- const { AjvValidator: NativeAjvValidator } = require('./model/AjvValidator');
11
- const { Validator: NativeValidator } = require('./model/Validator');
10
+ const { AjvValidator } = require('./model/AjvValidator');
11
+ const { Validator } = require('./model/Validator');
12
12
  const { Relation } = require('./relations/Relation');
13
13
  const { HasOneRelation } = require('./relations/hasOne/HasOneRelation');
14
14
  const { HasManyRelation } = require('./relations/hasMany/HasManyRelation');
@@ -23,34 +23,12 @@ const { ref } = require('./queryBuilder/ReferenceBuilder');
23
23
  const { val } = require('./queryBuilder/ValueBuilder');
24
24
  const { raw } = require('./queryBuilder/RawBuilder');
25
25
  const { fn } = require('./queryBuilder/FunctionBuilder');
26
- const { inherit } = require('./utils/classUtils');
27
- // We need to wrap the classes, that people can inherit, with ES5 classes
28
- // so that babel is able to use ES5 inheritance. sigh... Maybe people
29
- // should stop transpiling node apps to ES5 in the year 2019? Node 6
30
- // with full class support was released three years ago.
31
- function Model() {
32
- // Nothing to do here.
33
- }
34
- function QueryBuilder(...args) {
35
- NativeQueryBuilder.init(this, ...args);
36
- }
37
- function Validator(...args) {
38
- NativeValidator.init(this, ...args);
39
- }
40
- function AjvValidator(...args) {
41
- NativeAjvValidator.init(this, ...args);
42
- }
43
26
  // Returns the relation mappings as is. This only exists for typescript: it
44
27
  // preserves the exact types of the mappings (like the names of `extra`
45
28
  // columns) so that the relation types can be inferred from them.
46
29
  function defineRelations(relationMappings) {
47
30
  return relationMappings;
48
31
  }
49
- inherit(Model, NativeModel);
50
- inherit(QueryBuilder, NativeQueryBuilder);
51
- inherit(Validator, NativeValidator);
52
- inherit(AjvValidator, NativeAjvValidator);
53
- Model.QueryBuilder = QueryBuilder;
54
32
  module.exports = {
55
33
  Model,
56
34
  QueryBuilder,
@@ -1,12 +1,15 @@
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 {
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) => (typeof it === 'string' ? ref(it) : 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
- precision = parseInt(asSingle(precision), 10);
203
- if (isNaN(precision) || !isNumber(precision)) {
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) || !subquery.isObjectionQueryBuilderBase) {
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,4 +1,6 @@
1
- 'use strict';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InternalOptions = void 0;
2
4
  class InternalOptions {
3
5
  constructor() {
4
6
  this.skipUndefined = false;
@@ -19,6 +21,4 @@ class InternalOptions {
19
21
  return copy;
20
22
  }
21
23
  }
22
- module.exports = {
23
- InternalOptions,
24
- };
24
+ exports.InternalOptions = InternalOptions;
@@ -1,124 +1,125 @@
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');
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
- module.exports = {
123
- JoinBuilder,
124
- };
125
+ exports.JoinBuilder = JoinBuilder;