objection 3.9.0 → 4.0.0-rc.1

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 (248) hide show
  1. package/lib/dbErrors/LICENSE +21 -0
  2. package/lib/dbErrors/errorCodes/mssql.js +19 -0
  3. package/lib/dbErrors/errorCodes/mysql.js +805 -0
  4. package/lib/dbErrors/errorCodes/postgres.js +241 -0
  5. package/lib/dbErrors/errors/CheckViolationError.js +10 -0
  6. package/lib/dbErrors/errors/ConstraintViolationError.js +3 -0
  7. package/lib/dbErrors/errors/DBError.js +9 -0
  8. package/lib/dbErrors/errors/DataError.js +3 -0
  9. package/lib/dbErrors/errors/ForeignKeyViolationError.js +11 -0
  10. package/lib/dbErrors/errors/NotNullViolationError.js +12 -0
  11. package/lib/dbErrors/errors/UniqueViolationError.js +12 -0
  12. package/lib/dbErrors/index.js +59 -0
  13. package/lib/dbErrors/parsers/mssql/DBError/CheckViolationError/parser.js +28 -0
  14. package/lib/dbErrors/parsers/mssql/DBError/DataError/parser.js +25 -0
  15. package/lib/dbErrors/parsers/mssql/DBError/ForeignKeyViolationError/parser.js +39 -0
  16. package/lib/dbErrors/parsers/mssql/DBError/NotNullViolationError/parser.js +30 -0
  17. package/lib/dbErrors/parsers/mssql/DBError/UniqueViolationError/parser.js +53 -0
  18. package/lib/dbErrors/parsers/mssql/DBError/parser.js +51 -0
  19. package/lib/dbErrors/parsers/mssql/DBError/util.js +16 -0
  20. package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/ForeignKeyViolationError/parser.js +43 -0
  21. package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/NotNullViolationError/parser.js +29 -0
  22. package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/UniqueViolationError/parser.js +27 -0
  23. package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/parser.js +23 -0
  24. package/lib/dbErrors/parsers/mysql/DBError/DataError/parser.js +24 -0
  25. package/lib/dbErrors/parsers/mysql/DBError/parser.js +28 -0
  26. package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/CheckViolationError/parser.js +18 -0
  27. package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/ForeignKeyViolationError/parser.js +18 -0
  28. package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/NotNullViolationError/parser.js +18 -0
  29. package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/UniqueViolationError/parser.js +40 -0
  30. package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/parser.js +25 -0
  31. package/lib/dbErrors/parsers/postgres/DBError/DataError/parser.js +16 -0
  32. package/lib/dbErrors/parsers/postgres/DBError/parser.js +27 -0
  33. package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/CheckViolationError/parser.js +20 -0
  34. package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/ForeignKeyViolationError/parser.js +20 -0
  35. package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/NotNullViolationError/parser.js +27 -0
  36. package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/UniqueViolationError/parser.js +28 -0
  37. package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/parser.js +24 -0
  38. package/lib/dbErrors/parsers/sqlite/DBError/parser.js +23 -0
  39. package/lib/dbErrors/utils/sqlState.js +3 -0
  40. package/lib/initialize.js +1 -7
  41. package/lib/model/AjvValidator.js +51 -117
  42. package/lib/model/Model.js +84 -59
  43. package/lib/model/ModifierNotFoundError.js +1 -7
  44. package/lib/model/NotFoundError.js +1 -7
  45. package/lib/model/RelationDoesNotExistError.js +1 -7
  46. package/lib/model/ValidationError.js +3 -10
  47. package/lib/model/Validator.js +1 -7
  48. package/lib/model/getModel.js +7 -6
  49. package/lib/model/inheritModel.js +1 -7
  50. package/lib/model/modelBindKnex.js +4 -10
  51. package/lib/model/modelClone.js +5 -11
  52. package/lib/model/modelColPropMap.js +3 -10
  53. package/lib/model/modelId.js +3 -10
  54. package/lib/model/modelJsonAttributes.js +35 -16
  55. package/lib/model/modelParseRelations.js +2 -8
  56. package/lib/model/modelQueryProps.js +43 -27
  57. package/lib/model/modelSet.js +89 -41
  58. package/lib/model/modelTableMetadata.js +4 -11
  59. package/lib/model/modelToJson.js +8 -23
  60. package/lib/model/modelUtils.js +14 -13
  61. package/lib/model/modelValidate.js +3 -9
  62. package/lib/model/modelValues.js +5 -14
  63. package/lib/model/modelVisitor.js +1 -7
  64. package/lib/objection.js +33 -71
  65. package/lib/queryBuilder/FunctionBuilder.js +4 -11
  66. package/lib/queryBuilder/InternalOptions.js +4 -9
  67. package/lib/queryBuilder/JoinBuilder.js +5 -8
  68. package/lib/queryBuilder/QueryBuilder.js +126 -95
  69. package/lib/queryBuilder/QueryBuilderBase.js +41 -123
  70. package/lib/queryBuilder/QueryBuilderContext.js +2 -8
  71. package/lib/queryBuilder/QueryBuilderContextBase.js +2 -8
  72. package/lib/queryBuilder/QueryBuilderOperationSupport.js +7 -21
  73. package/lib/queryBuilder/QueryBuilderUserContext.js +1 -7
  74. package/lib/queryBuilder/RawBuilder.js +5 -13
  75. package/lib/queryBuilder/ReferenceBuilder.js +20 -48
  76. package/lib/queryBuilder/RelationExpression.js +18 -16
  77. package/lib/queryBuilder/StaticHookArguments.js +74 -16
  78. package/lib/queryBuilder/ValueBuilder.js +11 -13
  79. package/lib/queryBuilder/getJoinBuilder.js +10 -0
  80. package/lib/queryBuilder/graph/GraphLoader.js +304 -0
  81. package/lib/queryBuilder/graph/GraphNode.js +235 -0
  82. package/lib/queryBuilder/graph/GraphOptions.js +55 -248
  83. package/lib/queryBuilder/graph/GraphPlan.js +395 -0
  84. package/lib/queryBuilder/graph/GraphUpsert.js +466 -410
  85. package/lib/queryBuilder/graph/GraphWriter.js +1263 -0
  86. package/lib/queryBuilder/join/JoinResultColumn.js +1 -7
  87. package/lib/queryBuilder/join/JoinResultParser.js +6 -11
  88. package/lib/queryBuilder/join/RelationJoiner.js +66 -84
  89. package/lib/queryBuilder/join/TableNode.js +2 -8
  90. package/lib/queryBuilder/join/TableTree.js +3 -9
  91. package/lib/queryBuilder/join/utils.js +5 -15
  92. package/lib/queryBuilder/jsonDialects/JsonDialect.js +51 -0
  93. package/lib/queryBuilder/jsonDialects/MySqlJsonDialect.js +81 -0
  94. package/lib/queryBuilder/jsonDialects/PostgresJsonDialect.js +45 -0
  95. package/lib/queryBuilder/jsonDialects/SqliteJsonDialect.js +67 -0
  96. package/lib/queryBuilder/jsonDialects/index.js +44 -0
  97. package/lib/queryBuilder/operations/DelegateOperation.js +2 -8
  98. package/lib/queryBuilder/operations/DeleteOperation.js +13 -35
  99. package/lib/queryBuilder/operations/FindByIdOperation.js +7 -12
  100. package/lib/queryBuilder/operations/FindByIdsOperation.js +4 -8
  101. package/lib/queryBuilder/operations/FindOperation.js +14 -34
  102. package/lib/queryBuilder/operations/FirstOperation.js +6 -10
  103. package/lib/queryBuilder/operations/FromOperation.js +3 -9
  104. package/lib/queryBuilder/operations/InsertAndFetchOperation.js +13 -12
  105. package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +4 -10
  106. package/lib/queryBuilder/operations/InsertGraphOperation.js +7 -18
  107. package/lib/queryBuilder/operations/InsertOperation.js +118 -43
  108. package/lib/queryBuilder/operations/InstanceDeleteOperation.js +5 -12
  109. package/lib/queryBuilder/operations/InstanceFindOperation.js +4 -11
  110. package/lib/queryBuilder/operations/InstanceInsertOperation.js +2 -8
  111. package/lib/queryBuilder/operations/InstanceUpdateOperation.js +7 -14
  112. package/lib/queryBuilder/operations/JoinRelatedOperation.js +5 -11
  113. package/lib/queryBuilder/operations/KnexOperation.js +2 -8
  114. package/lib/queryBuilder/operations/LimitPerParentOperation.js +7 -69
  115. package/lib/queryBuilder/operations/MergeOperation.js +5 -11
  116. package/lib/queryBuilder/operations/NoneOperation.js +2 -8
  117. package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +20 -49
  118. package/lib/queryBuilder/operations/OnBuildKnexOperation.js +2 -8
  119. package/lib/queryBuilder/operations/OnBuildOperation.js +2 -8
  120. package/lib/queryBuilder/operations/OnErrorOperation.js +2 -8
  121. package/lib/queryBuilder/operations/QueryBuilderOperation.js +1 -7
  122. package/lib/queryBuilder/operations/RangeOperation.js +11 -26
  123. package/lib/queryBuilder/operations/RelateOperation.js +2 -8
  124. package/lib/queryBuilder/operations/ReturningOperation.js +3 -9
  125. package/lib/queryBuilder/operations/RunAfterOperation.js +2 -12
  126. package/lib/queryBuilder/operations/RunBeforeOperation.js +2 -8
  127. package/lib/queryBuilder/operations/UnrelateOperation.js +2 -8
  128. package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +14 -17
  129. package/lib/queryBuilder/operations/UpdateOperation.js +39 -125
  130. package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +8 -11
  131. package/lib/queryBuilder/operations/UpsertGraphOperation.js +6 -12
  132. package/lib/queryBuilder/operations/WhereCompositeOperation.js +3 -9
  133. package/lib/queryBuilder/operations/WhereGroupOperation.js +2 -8
  134. package/lib/queryBuilder/operations/eager/EagerOperation.js +3 -9
  135. package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +72 -10
  136. package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +2 -8
  137. package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +23 -44
  138. package/lib/queryBuilder/operations/fieldExpressions.js +122 -0
  139. package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +20 -0
  140. package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +44 -0
  141. package/lib/queryBuilder/operations/jsonApi/WhereJsonTypeOperation.js +14 -0
  142. package/lib/queryBuilder/operations/select/SelectOperation.js +11 -21
  143. package/lib/queryBuilder/operations/select/Selection.js +19 -17
  144. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +4 -10
  145. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +4 -10
  146. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +4 -10
  147. package/lib/queryBuilder/operations/windowOrder.js +56 -0
  148. package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +3 -7
  149. package/lib/queryBuilder/parsers/relationExpressionParser.js +42 -43
  150. package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +39 -36
  151. package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +2 -8
  152. package/lib/queryBuilder/transformations/QueryTransformation.js +1 -7
  153. package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +3 -9
  154. package/lib/queryBuilder/transformations/index.js +3 -11
  155. package/lib/queryBuilder/whereGroups.js +9 -24
  156. package/lib/queryBuilder/withoutOrder.js +4 -11
  157. package/lib/relations/Relation.js +47 -28
  158. package/lib/relations/RelationDeleteOperation.js +4 -14
  159. package/lib/relations/RelationFindOperation.js +119 -90
  160. package/lib/relations/RelationInsertOperation.js +2 -8
  161. package/lib/relations/RelationOwner.js +5 -11
  162. package/lib/relations/RelationProperty.js +4 -10
  163. package/lib/relations/RelationUpdateOperation.js +4 -14
  164. package/lib/relations/RelationWriteResultOperation.js +15 -0
  165. package/lib/relations/belongsToMany/BelongsToManyDeleteOperation.js +23 -0
  166. package/lib/relations/belongsToMany/BelongsToManyFindOperation.js +35 -0
  167. package/lib/relations/belongsToMany/BelongsToManyInsertOperation.js +28 -0
  168. package/lib/relations/belongsToMany/BelongsToManyRelateOperation.js +16 -0
  169. package/lib/relations/belongsToMany/BelongsToManyRelation.js +276 -0
  170. package/lib/relations/belongsToMany/BelongsToManyUnrelateOperation.js +11 -0
  171. package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +2 -8
  172. package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +6 -11
  173. package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +3 -9
  174. package/lib/relations/belongsToOne/BelongsToOneRelation.js +8 -19
  175. package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +53 -27
  176. package/lib/relations/hasMany/HasManyInsertOperation.js +8 -9
  177. package/lib/relations/hasMany/HasManyRelateOperation.js +5 -11
  178. package/lib/relations/hasMany/HasManyRelation.js +9 -11
  179. package/lib/relations/hasMany/HasManyUnrelateOperation.js +3 -9
  180. package/lib/relations/hasOne/HasOneRelation.js +2 -8
  181. package/lib/relations/hasOneThrough/HasOneThroughRelation.js +2 -8
  182. package/lib/relations/manyToMany/ManyToManyModifyMixin.js +10 -15
  183. package/lib/relations/manyToMany/ManyToManyMySqlModifyMixin.js +2 -8
  184. package/lib/relations/manyToMany/ManyToManyRelation.js +82 -50
  185. package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +2 -8
  186. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +5 -9
  187. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +2 -8
  188. package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +3 -9
  189. package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +20 -59
  190. package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +6 -11
  191. package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +4 -10
  192. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateMySqlOperation.js +3 -9
  193. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +5 -9
  194. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +3 -9
  195. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +3 -9
  196. package/lib/relations/manyToMany/update/ManyToManyUpdateMySqlOperation.js +3 -9
  197. package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +5 -9
  198. package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +79 -64
  199. package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +3 -9
  200. package/lib/transaction.js +3 -9
  201. package/lib/utils/assert.js +3 -11
  202. package/lib/utils/buildUtils.js +2 -8
  203. package/lib/utils/classUtils.js +1 -7
  204. package/lib/utils/createModifier.js +2 -8
  205. package/lib/utils/identifierMapping.js +23 -27
  206. package/lib/utils/internalPropUtils.js +1 -7
  207. package/lib/utils/knexUtils.js +84 -40
  208. package/lib/utils/mixin.js +3 -10
  209. package/lib/utils/normalizeIds.js +2 -8
  210. package/lib/utils/objectUtils.js +41 -60
  211. package/lib/utils/parseFieldExpression.js +29 -14
  212. package/lib/utils/promiseUtils/after.js +2 -8
  213. package/lib/utils/promiseUtils/afterReturn.js +2 -8
  214. package/lib/utils/promiseUtils/index.js +7 -16
  215. package/lib/utils/promiseUtils/isPromise.js +2 -8
  216. package/lib/utils/promiseUtils/map.js +25 -14
  217. package/lib/utils/promiseUtils/mapAfterAllReturn.js +2 -8
  218. package/lib/utils/promiseUtils/try.js +2 -8
  219. package/lib/utils/resolveModel.js +10 -17
  220. package/lib/utils/tmpColumnUtils.js +2 -9
  221. package/lib/utils/warnings.js +2 -14
  222. package/package.json +25 -17
  223. package/typings/objection/index.d.ts +3217 -2179
  224. package/lib/.eslintrc.json +0 -8
  225. package/lib/model/graph/ModelGraph.js +0 -80
  226. package/lib/model/graph/ModelGraphBuilder.js +0 -241
  227. package/lib/model/graph/ModelGraphEdge.js +0 -52
  228. package/lib/model/graph/ModelGraphNode.js +0 -253
  229. package/lib/queryBuilder/graph/GraphAction.js +0 -69
  230. package/lib/queryBuilder/graph/GraphData.js +0 -53
  231. package/lib/queryBuilder/graph/GraphFetcher.js +0 -199
  232. package/lib/queryBuilder/graph/GraphNodeDbExistence.js +0 -184
  233. package/lib/queryBuilder/graph/GraphOperation.js +0 -27
  234. package/lib/queryBuilder/graph/delete/GraphDelete.js +0 -73
  235. package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +0 -88
  236. package/lib/queryBuilder/graph/insert/DeferredRelationGraphInsertAction.js +0 -57
  237. package/lib/queryBuilder/graph/insert/GraphInsert.js +0 -257
  238. package/lib/queryBuilder/graph/insert/GraphInsertAction.js +0 -238
  239. package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +0 -92
  240. package/lib/queryBuilder/graph/patch/GraphPatch.js +0 -20
  241. package/lib/queryBuilder/graph/patch/GraphPatchAction.js +0 -277
  242. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +0 -31
  243. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +0 -126
  244. package/lib/queryBuilder/operations/jsonApi/WhereJsonHasPostgresOperation.js +0 -30
  245. package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectPostgresOperation.js +0 -37
  246. package/lib/queryBuilder/operations/jsonApi/WhereJsonPostgresOperation.js +0 -62
  247. package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +0 -192
  248. package/lib/utils/wrapDbError.js +0 -50
@@ -1,19 +1,37 @@
1
- {{
2
- const { warnOnce } = require('../../utils/warnings');
3
- }}
4
-
5
1
  {
6
- function assertDuplicateRelation(node, expr) {
2
+ function addChild(node, expr) {
3
+ if (isToken(expr.$relation)) {
4
+ throwMisplacedToken(expr.$relation);
5
+ }
6
+
7
7
  if (Object.prototype.hasOwnProperty.call(node, expr.$name)) {
8
- warnOnce(`Duplicate relation "${expr.$name}" in a relation expression, only the last one is used. Use "a.[b, c]" instead of "[a.b, a.c]". This will throw an error in objection 4.0.`);
8
+ const err = new Error();
9
+ err.duplicateRelationName = expr.$name;
10
+ throw err;
11
+ }
12
+
13
+ node[expr.$name] = expr;
14
+ node.$childNames.push(expr.$name);
15
+ }
16
+
17
+ // `*` and `^` end a relation path, without modifiers, aliases or relations
18
+ // after them.
19
+ function isToken(name) {
20
+ return /^(\*|\^\d*)$/.test(name);
21
+ }
9
22
 
10
- // Throws in objection 4.0, see ditojs/objection#53.
11
- // const err = new Error();
12
- // err.duplicateRelationName = expr.$name;
13
- // throw err;
23
+ function assertPlainToken(expr) {
24
+ if (expr.$childNames.length > 0 || expr.$modify.length > 0 || expr.$name !== expr.$relation) {
25
+ throwMisplacedToken(expr.$relation);
14
26
  }
15
27
  }
16
28
 
29
+ function throwMisplacedToken(token) {
30
+ const err = new Error();
31
+ err.misplacedToken = token;
32
+ throw err;
33
+ }
34
+
17
35
  function newNode() {
18
36
  return {
19
37
  $name: null,
@@ -30,12 +48,11 @@ start =
30
48
  expr:expression {
31
49
  const node = newNode()
32
50
 
33
- if (expr.$name === '*') {
51
+ if (expr.$relation === '*') {
52
+ assertPlainToken(expr);
34
53
  node.$allRecursive = true;
35
54
  } else {
36
- assertDuplicateRelation(node, expr);
37
- node[expr.$name] = expr;
38
- node.$childNames.push(expr.$name)
55
+ addChild(node, expr);
39
56
  }
40
57
 
41
58
  return node;
@@ -44,11 +61,7 @@ start =
44
61
  list:listExpression {
45
62
  const node = newNode()
46
63
 
47
- list.forEach(expr => {
48
- assertDuplicateRelation(node, expr);
49
- node[expr.$name] = expr;
50
- node.$childNames.push(expr.$name)
51
- });
64
+ list.forEach(expr => addChild(node, expr));
52
65
 
53
66
  return node;
54
67
  }
@@ -61,11 +74,7 @@ expression =
61
74
  node.$relation = name;
62
75
  node.$modify = args || [];
63
76
 
64
- list.forEach(expr => {
65
- assertDuplicateRelation(node, expr);
66
- node[expr.$name] = expr;
67
- node.$childNames.push(expr.$name)
68
- });
77
+ list.forEach(expr => addChild(node, expr));
69
78
 
70
79
  return node;
71
80
  }
@@ -78,20 +87,14 @@ expression =
78
87
  node.$modify = args || [];
79
88
 
80
89
  if (expr) {
81
- const match = /^\^(\d*)$/.exec(expr.$name);
82
-
83
- if (match) {
84
- if (match[1]) {
85
- node.$recursive = parseInt(match[1], 10);
86
- } else {
87
- node.$recursive = true;
88
- }
89
- } else if (expr.$name === '*') {
90
+ if (expr.$relation === '*') {
91
+ assertPlainToken(expr);
90
92
  node.$allRecursive = true;
93
+ } else if (isToken(expr.$relation)) {
94
+ assertPlainToken(expr);
95
+ node.$recursive = expr.$relation === '^' ? true : parseInt(expr.$relation.slice(1), 10);
91
96
  } else {
92
- assertDuplicateRelation(node, expr);
93
- node[expr.$name] = expr;
94
- node.$childNames.push(expr.$name)
97
+ addChild(node, expr);
95
98
  }
96
99
  }
97
100
 
@@ -1,8 +1,6 @@
1
- 'use strict';
1
+ import { QueryTransformation } from './QueryTransformation.js';
2
2
 
3
- const { QueryTransformation } = require('./QueryTransformation');
4
-
5
- class CompositeQueryTransformation extends QueryTransformation {
3
+ export class CompositeQueryTransformation extends QueryTransformation {
6
4
  constructor(transformations) {
7
5
  super();
8
6
  this.transformations = transformations;
@@ -16,7 +14,3 @@ class CompositeQueryTransformation extends QueryTransformation {
16
14
  return item;
17
15
  }
18
16
  }
19
-
20
- module.exports = {
21
- CompositeQueryTransformation,
22
- };
@@ -1,11 +1,5 @@
1
- 'use strict';
2
-
3
- class QueryTransformation {
1
+ export class QueryTransformation {
4
2
  onConvertQueryBuilderBase(item, builder) {
5
3
  return item;
6
4
  }
7
5
  }
8
-
9
- module.exports = {
10
- QueryTransformation,
11
- };
@@ -1,7 +1,5 @@
1
- 'use strict';
2
-
3
- const { QueryTransformation } = require('./QueryTransformation');
4
- const { isMySql } = require('../../utils/knexUtils');
1
+ import { QueryTransformation } from './QueryTransformation.js';
2
+ import { isMySql } from '../../utils/knexUtils.js';
5
3
 
6
4
  /**
7
5
  * Mysql doesn't allow queries like this:
@@ -15,7 +13,7 @@ const { isMySql } = require('../../utils/knexUtils');
15
13
  *
16
14
  * update foo set bar = 1 where id in (select * from (select id from foo))
17
15
  */
18
- class WrapMysqlModifySubqueryTransformation extends QueryTransformation {
16
+ export class WrapMysqlModifySubqueryTransformation extends QueryTransformation {
19
17
  onConvertQueryBuilderBase(query, parentQuery) {
20
18
  const knex = parentQuery.unsafeKnex();
21
19
 
@@ -66,7 +64,3 @@ function hasJoinsToTable(query, tableName) {
66
64
 
67
65
  return found;
68
66
  }
69
-
70
- module.exports = {
71
- WrapMysqlModifySubqueryTransformation,
72
- };
@@ -1,15 +1,7 @@
1
- 'use strict';
1
+ import { CompositeQueryTransformation } from './CompositeQueryTransformation.js';
2
2
 
3
- const { CompositeQueryTransformation } = require('./CompositeQueryTransformation');
3
+ import { WrapMysqlModifySubqueryTransformation } from './WrapMysqlModifySubqueryTransformation.js';
4
4
 
5
- const {
6
- WrapMysqlModifySubqueryTransformation,
7
- } = require('./WrapMysqlModifySubqueryTransformation');
8
-
9
- const transformation = new CompositeQueryTransformation([
5
+ export const transformation = new CompositeQueryTransformation([
10
6
  new WrapMysqlModifySubqueryTransformation(),
11
7
  ]);
12
-
13
- module.exports = {
14
- transformation,
15
- };
@@ -1,15 +1,13 @@
1
- 'use strict';
2
-
3
- const { WhereGroupOperation } = require('./operations/WhereGroupOperation');
4
- const { isObject, isFunction } = require('../utils/objectUtils');
5
- const { isKnexRaw } = require('../utils/knexUtils');
1
+ import { WhereGroupOperation } from './operations/WhereGroupOperation.js';
2
+ import { isObject, isFunction } from '../utils/objectUtils.js';
3
+ import { isKnexRaw } from '../utils/knexUtils.js';
6
4
 
7
5
  // Where operations of a query can come from different sources: the user, a
8
6
  // relation's owner condition, the relation's `modify`/`filter`, the id
9
- // condition of `patchAndFetchById()` and `$query()` and so on. Each source
10
- // forms its own "where group". All operations the user adds belong to the
11
- // default group, and `addWhereGroup` assigns the operations added inside its
12
- // callback to a new, separate group.
7
+ // condition of `findById()`, `findByIds()` and `$query()` and so on. Each
8
+ // source forms its own "where group". All other operations the user adds
9
+ // belong to the default group, and `addWhereGroup` assigns the operations added
10
+ // inside its callback to a new, separate group.
13
11
  //
14
12
  // Knex simply joins all where clauses with their `and` or `or` boolean. Since
15
13
  // `and` binds tighter than `or`, a user's `orWhere` would escape the relation's
@@ -22,7 +20,7 @@ const { isKnexRaw } = require('../utils/knexUtils');
22
20
  // always ANDed together: `where "ownerId" in (1) and (a or b)`. Groups without
23
21
  // an `or` are left alone so that their SQL doesn't change. The operations
24
22
  // aren't moved, so that a built query can be cloned and built again.
25
- function addWhereGroup(builder, callback) {
23
+ export function addWhereGroup(builder, callback) {
26
24
  const parentWhereGroup = builder._whereGroup;
27
25
  builder._whereGroup = {};
28
26
 
@@ -33,17 +31,10 @@ function addWhereGroup(builder, callback) {
33
31
  }
34
32
  }
35
33
 
36
- // Assign an operation that isn't added through a builder, like the result of
37
- // `toFindOperation()`, to a new, separate group.
38
- function assignWhereGroup(operation) {
39
- operation.whereGroup = {};
40
- return operation;
41
- }
42
-
43
34
  // Return a map from each where operation that needs to be wrapped in
44
35
  // parentheses to the `WhereGroupOperation` that builds its group in place of
45
36
  // the group's first operation, or to null for the group's other operations.
46
- function getWhereGroupOperations(builder) {
37
+ export function getWhereGroupOperations(builder) {
47
38
  const WhereSelector = builder.constructor.WhereSelector;
48
39
  const whereOps = new Set();
49
40
  const groups = new Map();
@@ -112,9 +103,3 @@ function mayContainTopLevelOr(op) {
112
103
  function isRaw(arg) {
113
104
  return isKnexRaw(arg) || (isObject(arg) && isFunction(arg.toKnexRaw));
114
105
  }
115
-
116
- module.exports = {
117
- addWhereGroup,
118
- assignWhereGroup,
119
- getWhereGroupOperations,
120
- };
@@ -1,19 +1,17 @@
1
- 'use strict';
2
-
3
- const { FirstOperation } = require('./operations/FirstOperation');
4
- const { isMsSql } = require('../utils/knexUtils');
1
+ import { FirstOperation } from './operations/FirstOperation.js';
2
+ import { isMsSql } from '../utils/knexUtils.js';
5
3
 
6
4
  // Removes the order of a subquery that only selects or joins rows. The order
7
5
  // can come from a relation's `modify` or an `onBuild` hook of the context, so
8
6
  // it's removed when the subquery is built.
9
- function withoutOrder(subquery) {
7
+ export function withoutOrder(subquery) {
10
8
  return subquery.onBuild(clearUnsupportedOrder);
11
9
  }
12
10
 
13
11
  // Without a limit, an order doesn't matter in subqueries and updates, and SQL
14
12
  // Server rejects it there. With a limit it decides the rows, so we keep it, and
15
13
  // SQL Server rejects an update rather than updating arbitrary rows.
16
- function clearUnsupportedOrder(builder) {
14
+ export function clearUnsupportedOrder(builder) {
17
15
  if (isMsSql(builder.knex()) && !hasLimit(builder)) {
18
16
  builder.clearOrder();
19
17
  }
@@ -26,8 +24,3 @@ function hasLimit(builder) {
26
24
  builder.findOperation(FirstOperation)?.shouldLimit(builder)
27
25
  );
28
26
  }
29
-
30
- module.exports = {
31
- withoutOrder,
32
- clearUnsupportedOrder,
33
- };
@@ -1,19 +1,17 @@
1
- 'use strict';
1
+ import { RelationProperty } from './RelationProperty.js';
2
2
 
3
- const { RelationProperty } = require('./RelationProperty');
3
+ import { RelationFindOperation } from './RelationFindOperation.js';
4
+ import { RelationUpdateOperation } from './RelationUpdateOperation.js';
5
+ import { RelationDeleteOperation } from './RelationDeleteOperation.js';
4
6
 
5
- const { RelationFindOperation } = require('./RelationFindOperation');
6
- const { RelationUpdateOperation } = require('./RelationUpdateOperation');
7
- const { RelationDeleteOperation } = require('./RelationDeleteOperation');
7
+ import { resolveModel } from '../utils/resolveModel.js';
8
+ import { get, isFunction } from '../utils/objectUtils.js';
9
+ import { mapAfterAllReturn } from '../utils/promiseUtils/index.js';
10
+ import { createModifier } from '../utils/createModifier.js';
11
+ import { addWhereGroup } from '../queryBuilder/whereGroups.js';
12
+ import { withoutOrder } from '../queryBuilder/withoutOrder.js';
8
13
 
9
- const { resolveModel } = require('../utils/resolveModel');
10
- const { get, isFunction } = require('../utils/objectUtils');
11
- const { mapAfterAllReturn } = require('../utils/promiseUtils');
12
- const { createModifier } = require('../utils/createModifier');
13
- const { addWhereGroup } = require('../queryBuilder/whereGroups');
14
- const { withoutOrder } = require('../queryBuilder/withoutOrder');
15
-
16
- class Relation {
14
+ export class Relation {
17
15
  constructor(relationName, OwnerClass) {
18
16
  this.name = relationName;
19
17
  this.ownerModelClass = OwnerClass;
@@ -84,10 +82,11 @@ class Relation {
84
82
  return false;
85
83
  }
86
84
 
87
- // Whether owner models may legitimately lack the owner join property at
88
- // `index`, so that it isn't reported as missing (#132).
89
- isOptionalOwnerProp(_index) {
90
- return false;
85
+ // Where the keys that connect owners and related models are stored: in the
86
+ // `'owner'` rows, the `'related'` rows or the rows of a `'join'` table. Owners
87
+ // of relations that aren't one-to-one hold arrays of keys.
88
+ get keySide() {
89
+ throw new Error('not implemented');
91
90
  }
92
91
 
93
92
  clone() {
@@ -151,6 +150,7 @@ class Relation {
151
150
  relatedJoinSelectQuery = this.relatedModelClass.query().childQueryOf(builder),
152
151
  relatedTable = builder.tableNameFor(this.relatedModelClass),
153
152
  ownerTable = builder.tableRefFor(this.ownerModelClass),
153
+ modifyJoin,
154
154
  } = {},
155
155
  ) {
156
156
  let relatedJoinSelect = this.applyModify(relatedJoinSelectQuery).as(relatedTableAlias);
@@ -163,18 +163,41 @@ class Relation {
163
163
  }
164
164
 
165
165
  return builder[joinOperation](relatedJoinSelect, (join) => {
166
- const relatedProp = this.relatedProp;
167
- const ownerProp = this.ownerProp;
166
+ this.joinOn(builder, join, { relatedTableAlias, ownerTable });
168
167
 
169
- relatedProp.forEach((i) => {
170
- const relatedRef = relatedProp.ref(builder, i).table(relatedTableAlias);
171
- const ownerRef = ownerProp.ref(builder, i).table(ownerTable);
168
+ if (modifyJoin) {
169
+ this._applyModifyJoin(join, modifyJoin, { owner: ownerTable, related: relatedTableAlias });
170
+ }
171
+ });
172
+ }
172
173
 
173
- join.on(relatedRef, ownerRef);
174
- });
174
+ // @overridable
175
+ joinOn(builder, join, { relatedTableAlias, ownerTable }) {
176
+ const relatedProp = this.relatedProp;
177
+ const ownerProp = this.ownerProp;
178
+
179
+ relatedProp.forEach((i) => {
180
+ const relatedRef = relatedProp.ref(builder, i).table(relatedTableAlias);
181
+ const ownerRef = ownerProp.ref(builder, i).table(ownerTable);
182
+
183
+ join.on(relatedRef, ownerRef);
175
184
  });
176
185
  }
177
186
 
187
+ // Group the conditions of `modifyJoin()`, so that an `orOn` in them can't
188
+ // escape the key conditions of the relation. Knex renders an empty group as
189
+ // a dangling `and`, so we only add it if there are conditions.
190
+ _applyModifyJoin(join, modifyJoin, aliases) {
191
+ const group = join.clone().clear(true);
192
+ modifyJoin(group, aliases);
193
+
194
+ if (group.has(true)) {
195
+ join.andOn(function () {
196
+ this.copyFrom(group, true);
197
+ });
198
+ }
199
+ }
200
+
178
201
  find(_, owner) {
179
202
  return new RelationFindOperation('find', {
180
203
  relation: this,
@@ -406,7 +429,3 @@ function aliasedTableName(tableName, alias) {
406
429
  return `${tableName} as ${alias}`;
407
430
  }
408
431
  }
409
-
410
- module.exports = {
411
- Relation,
412
- };
@@ -1,9 +1,6 @@
1
- 'use strict';
1
+ import { DeleteOperation } from '../queryBuilder/operations/DeleteOperation.js';
2
2
 
3
- const { DeleteOperation } = require('../queryBuilder/operations/DeleteOperation');
4
- const { RelationFindOperation } = require('./RelationFindOperation');
5
-
6
- class RelationDeleteOperation extends DeleteOperation {
3
+ export class RelationDeleteOperation extends DeleteOperation {
7
4
  constructor(name, opt) {
8
5
  super(name, opt);
9
6
 
@@ -16,14 +13,7 @@ class RelationDeleteOperation extends DeleteOperation {
16
13
  this.relation.findQuery(builder, this.owner);
17
14
  }
18
15
 
19
- toFindOperation() {
20
- return new RelationFindOperation('find', {
21
- relation: this.relation,
22
- owner: this.owner,
23
- });
16
+ toFindOperation(builder) {
17
+ return this.relation.find(builder, this.owner);
24
18
  }
25
19
  }
26
-
27
- module.exports = {
28
- RelationDeleteOperation,
29
- };