@abedmurrar/objection 4.0.0-alpha.0 → 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.
Files changed (192) hide show
  1. package/README.md +103 -67
  2. package/lib/index.mjs +6 -0
  3. package/lib/initialize.js +6 -9
  4. package/lib/model/AjvValidator.js +235 -282
  5. package/lib/model/Model.js +653 -823
  6. package/lib/model/ModifierNotFoundError.js +5 -7
  7. package/lib/model/NotFoundError.js +14 -18
  8. package/lib/model/RelationDoesNotExistError.js +6 -9
  9. package/lib/model/ValidationError.js +33 -41
  10. package/lib/model/Validator.js +15 -21
  11. package/lib/model/getModel.js +1 -3
  12. package/lib/model/graph/ModelGraph.js +44 -64
  13. package/lib/model/graph/ModelGraphBuilder.js +164 -217
  14. package/lib/model/graph/ModelGraphEdge.js +31 -43
  15. package/lib/model/graph/ModelGraphNode.js +192 -231
  16. package/lib/model/inheritModel.js +9 -18
  17. package/lib/model/modelBindKnex.js +48 -62
  18. package/lib/model/modelClone.js +79 -98
  19. package/lib/model/modelColPropMap.js +16 -26
  20. package/lib/model/modelConcurrency.js +24 -0
  21. package/lib/model/modelId.js +44 -49
  22. package/lib/model/modelJsonAttributes.js +56 -76
  23. package/lib/model/modelParseRelations.js +68 -67
  24. package/lib/model/modelQueryProps.js +76 -107
  25. package/lib/model/modelSet.js +92 -112
  26. package/lib/model/modelTableMetadata.js +60 -72
  27. package/lib/model/modelToJson.js +120 -150
  28. package/lib/model/modelUtils.js +30 -37
  29. package/lib/model/modelValidate.js +34 -42
  30. package/lib/model/modelValues.js +78 -91
  31. package/lib/model/modelVisitor.js +22 -29
  32. package/lib/objection.js +42 -67
  33. package/lib/queryBuilder/FunctionBuilder.js +231 -279
  34. package/lib/queryBuilder/InternalOptions.js +19 -24
  35. package/lib/queryBuilder/JoinBuilder.js +116 -149
  36. package/lib/queryBuilder/QueryBuilder.js +1009 -1271
  37. package/lib/queryBuilder/QueryBuilderBase.js +647 -865
  38. package/lib/queryBuilder/QueryBuilderContext.js +14 -21
  39. package/lib/queryBuilder/QueryBuilderContextBase.js +20 -29
  40. package/lib/queryBuilder/QueryBuilderOperationSupport.js +462 -536
  41. package/lib/queryBuilder/QueryBuilderUserContext.js +19 -25
  42. package/lib/queryBuilder/RawBuilder.js +58 -73
  43. package/lib/queryBuilder/ReferenceBuilder.js +232 -270
  44. package/lib/queryBuilder/RelationExpression.js +322 -380
  45. package/lib/queryBuilder/StaticHookArguments.js +94 -106
  46. package/lib/queryBuilder/ValueBuilder.js +100 -112
  47. package/lib/queryBuilder/expressions.js +86 -116
  48. package/lib/queryBuilder/functions/mysql.js +642 -697
  49. package/lib/queryBuilder/graph/GraphAction.js +44 -59
  50. package/lib/queryBuilder/graph/GraphData.js +7 -9
  51. package/lib/queryBuilder/graph/GraphFetcher.js +115 -151
  52. package/lib/queryBuilder/graph/GraphNodeDbExistence.js +114 -152
  53. package/lib/queryBuilder/graph/GraphOperation.js +16 -22
  54. package/lib/queryBuilder/graph/GraphOptions.js +143 -185
  55. package/lib/queryBuilder/graph/GraphUpsert.js +167 -241
  56. package/lib/queryBuilder/graph/delete/GraphDelete.js +8 -13
  57. package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +44 -62
  58. package/lib/queryBuilder/graph/insert/GraphInsert.js +99 -128
  59. package/lib/queryBuilder/graph/insert/GraphInsertAction.js +151 -193
  60. package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +57 -79
  61. package/lib/queryBuilder/graph/patch/GraphPatch.js +8 -13
  62. package/lib/queryBuilder/graph/patch/GraphPatchAction.js +181 -234
  63. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +16 -21
  64. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +80 -110
  65. package/lib/queryBuilder/join/JoinResultColumn.js +14 -18
  66. package/lib/queryBuilder/join/JoinResultParser.js +88 -101
  67. package/lib/queryBuilder/join/RelationJoiner.js +205 -288
  68. package/lib/queryBuilder/join/TableNode.js +117 -144
  69. package/lib/queryBuilder/join/TableTree.js +60 -76
  70. package/lib/queryBuilder/join/utils.js +10 -16
  71. package/lib/queryBuilder/operations/DelegateOperation.js +93 -133
  72. package/lib/queryBuilder/operations/DeleteOperation.js +33 -37
  73. package/lib/queryBuilder/operations/FindByIdOperation.js +20 -28
  74. package/lib/queryBuilder/operations/FindByIdsOperation.js +17 -23
  75. package/lib/queryBuilder/operations/FindOperation.js +114 -131
  76. package/lib/queryBuilder/operations/FirstOperation.js +14 -19
  77. package/lib/queryBuilder/operations/FromOperation.js +69 -88
  78. package/lib/queryBuilder/operations/InsertAndFetchOperation.js +28 -39
  79. package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +25 -35
  80. package/lib/queryBuilder/operations/InsertGraphOperation.js +77 -98
  81. package/lib/queryBuilder/operations/InsertOperation.js +132 -165
  82. package/lib/queryBuilder/operations/InstanceDeleteOperation.js +27 -36
  83. package/lib/queryBuilder/operations/InstanceFindOperation.js +9 -13
  84. package/lib/queryBuilder/operations/InstanceInsertOperation.js +13 -17
  85. package/lib/queryBuilder/operations/InstanceUpdateOperation.js +32 -46
  86. package/lib/queryBuilder/operations/JoinRelatedOperation.js +66 -86
  87. package/lib/queryBuilder/operations/KnexOperation.js +4 -7
  88. package/lib/queryBuilder/operations/MergeOperation.js +32 -45
  89. package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +131 -150
  90. package/lib/queryBuilder/operations/OnBuildKnexOperation.js +17 -23
  91. package/lib/queryBuilder/operations/OnBuildOperation.js +17 -23
  92. package/lib/queryBuilder/operations/OnErrorOperation.js +17 -23
  93. package/lib/queryBuilder/operations/QueryBuilderOperation.js +231 -270
  94. package/lib/queryBuilder/operations/RangeOperation.js +30 -40
  95. package/lib/queryBuilder/operations/RelateOperation.js +14 -21
  96. package/lib/queryBuilder/operations/ReturningOperation.js +12 -18
  97. package/lib/queryBuilder/operations/RunAfterOperation.js +17 -23
  98. package/lib/queryBuilder/operations/RunBeforeOperation.js +17 -23
  99. package/lib/queryBuilder/operations/UnrelateOperation.js +12 -17
  100. package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +48 -65
  101. package/lib/queryBuilder/operations/UpdateOperation.js +104 -129
  102. package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +25 -35
  103. package/lib/queryBuilder/operations/UpsertGraphOperation.js +36 -55
  104. package/lib/queryBuilder/operations/WhereCompositeOperation.js +30 -39
  105. package/lib/queryBuilder/operations/WhereInOperation.js +34 -36
  106. package/lib/queryBuilder/operations/eager/EagerOperation.js +37 -52
  107. package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +34 -44
  108. package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +4 -7
  109. package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +193 -251
  110. package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +18 -25
  111. package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectOperation.js +25 -41
  112. package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +19 -20
  113. package/lib/queryBuilder/operations/jsonApi/WhereJsonOverlapsOperation.js +23 -21
  114. package/lib/queryBuilder/operations/jsonApi/mysqlJsonApi.js +106 -118
  115. package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +79 -108
  116. package/lib/queryBuilder/operations/select/SelectOperation.js +37 -53
  117. package/lib/queryBuilder/operations/select/Selection.js +94 -105
  118. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +68 -101
  119. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +65 -75
  120. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +75 -84
  121. package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +760 -859
  122. package/lib/queryBuilder/parsers/relationExpressionParser.js +840 -952
  123. package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +10 -15
  124. package/lib/queryBuilder/transformations/QueryTransformation.js +4 -6
  125. package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +35 -49
  126. package/lib/queryBuilder/transformations/index.js +3 -9
  127. package/lib/queryBuilder/whereScopes.js +158 -186
  128. package/lib/relations/Relation.js +279 -363
  129. package/lib/relations/RelationDeleteOperation.js +16 -22
  130. package/lib/relations/RelationFindOperation.js +100 -133
  131. package/lib/relations/RelationInsertOperation.js +19 -27
  132. package/lib/relations/RelationOwner.js +167 -204
  133. package/lib/relations/RelationProperty.js +177 -217
  134. package/lib/relations/RelationUpdateOperation.js +16 -23
  135. package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +11 -16
  136. package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +24 -35
  137. package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +30 -41
  138. package/lib/relations/belongsToOne/BelongsToOneRelation.js +33 -41
  139. package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +38 -53
  140. package/lib/relations/hasMany/HasManyInsertOperation.js +35 -51
  141. package/lib/relations/hasMany/HasManyRelateOperation.js +38 -54
  142. package/lib/relations/hasMany/HasManyRelation.js +30 -38
  143. package/lib/relations/hasMany/HasManyUnrelateOperation.js +19 -26
  144. package/lib/relations/hasOne/HasOneRelation.js +4 -7
  145. package/lib/relations/hasOneThrough/HasOneThroughRelation.js +4 -7
  146. package/lib/relations/manyToMany/ManyToManyModifyMixin.js +67 -90
  147. package/lib/relations/manyToMany/ManyToManyRelation.js +349 -451
  148. package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +19 -26
  149. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +3 -5
  150. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +14 -20
  151. package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +3 -7
  152. package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +71 -94
  153. package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +31 -43
  154. package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +50 -67
  155. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +4 -7
  156. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +20 -27
  157. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +5 -10
  158. package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +3 -5
  159. package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +55 -72
  160. package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +3 -7
  161. package/lib/transaction.js +53 -83
  162. package/lib/utils/assert.js +15 -21
  163. package/lib/utils/buildUtils.js +17 -19
  164. package/lib/utils/classUtils.js +7 -11
  165. package/lib/utils/cloneDeep.js +365 -0
  166. package/lib/utils/createModifier.js +36 -41
  167. package/lib/utils/deprecate.js +8 -12
  168. package/lib/utils/identifierMapping.js +153 -192
  169. package/lib/utils/internalPropUtils.js +4 -8
  170. package/lib/utils/knexUtils.js +61 -56
  171. package/lib/utils/mixin.js +16 -23
  172. package/lib/utils/mysqlJsonPath.js +55 -40
  173. package/lib/utils/normalizeIds.js +110 -127
  174. package/lib/utils/objectUtils.js +236 -338
  175. package/lib/utils/parseFieldExpression.js +66 -44
  176. package/lib/utils/postgresJsonPath.js +48 -62
  177. package/lib/utils/promiseUtils/after.js +10 -13
  178. package/lib/utils/promiseUtils/afterReturn.js +10 -13
  179. package/lib/utils/promiseUtils/index.js +15 -17
  180. package/lib/utils/promiseUtils/isPromise.js +6 -9
  181. package/lib/utils/promiseUtils/map.js +75 -89
  182. package/lib/utils/promiseUtils/mapAfterAllReturn.js +17 -23
  183. package/lib/utils/promiseUtils/try.js +15 -18
  184. package/lib/utils/resolveModel.js +91 -94
  185. package/lib/utils/tmpColumnUtils.js +4 -13
  186. package/package.json +50 -23
  187. package/typings/objection/index.d.mts +362 -0
  188. package/typings/objection/index.d.ts +36 -9
  189. package/lib/.eslintrc.json +0 -8
  190. package/lib/queryBuilder/parsers/jsonFieldExpressionParser.pegjs +0 -97
  191. package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +0 -141
  192. package/lib/utils/clone.js +0 -2329
@@ -1,66 +1,50 @@
1
1
  'use strict';
2
-
3
2
  const { flatten } = require('../../../utils/objectUtils');
4
3
  const { Selection } = require('./Selection');
5
4
  const { ObjectionToKnexConvertingOperation } = require('../ObjectionToKnexConvertingOperation');
6
-
7
5
  const COUNT_REGEX = /count/i;
8
-
9
6
  class SelectOperation extends ObjectionToKnexConvertingOperation {
10
- constructor(name, opt) {
11
- super(name, opt);
12
- this.selections = [];
13
- }
14
-
15
- onAdd(builder, args) {
16
- const selections = flatten(args);
17
-
18
- // Don't add an empty selection. Empty list is accepted for `count`, `countDistinct`
19
- // etc. because knex apparently supports it.
20
- if (selections.length === 0 && !COUNT_REGEX.test(this.name)) {
21
- return false;
7
+ constructor(name, opt) {
8
+ super(name, opt);
9
+ this.selections = [];
22
10
  }
23
-
24
- const ret = super.onAdd(builder, selections);
25
-
26
- for (const selection of selections) {
27
- const selectionInstance = Selection.create(selection);
28
-
29
- if (selectionInstance) {
30
- this.selections.push(selectionInstance);
31
- }
11
+ onAdd(builder, args) {
12
+ const selections = flatten(args);
13
+ // Don't add an empty selection. Empty list is accepted for `count`, `countDistinct`
14
+ // etc. because knex apparently supports it.
15
+ if (selections.length === 0 && !COUNT_REGEX.test(this.name)) {
16
+ return false;
17
+ }
18
+ const ret = super.onAdd(builder, selections);
19
+ for (const selection of selections) {
20
+ const selectionInstance = Selection.create(selection);
21
+ if (selectionInstance) {
22
+ this.selections.push(selectionInstance);
23
+ }
24
+ }
25
+ return ret;
32
26
  }
33
-
34
- return ret;
35
- }
36
-
37
- onBuildKnex(knexBuilder, builder) {
38
- return knexBuilder[this.name].apply(knexBuilder, this.getKnexArgs(builder));
39
- }
40
-
41
- findSelection(builder, selectionToFind) {
42
- const selectionInstanceToFind = Selection.create(selectionToFind);
43
-
44
- if (!selectionInstanceToFind) {
45
- return null;
27
+ onBuildKnex(knexBuilder, builder) {
28
+ return knexBuilder[this.name].apply(knexBuilder, this.getKnexArgs(builder));
46
29
  }
47
-
48
- for (const selection of this.selections) {
49
- if (Selection.doesSelect(builder, selection, selectionInstanceToFind)) {
50
- return selection;
51
- }
30
+ findSelection(builder, selectionToFind) {
31
+ const selectionInstanceToFind = Selection.create(selectionToFind);
32
+ if (!selectionInstanceToFind) {
33
+ return null;
34
+ }
35
+ for (const selection of this.selections) {
36
+ if (Selection.doesSelect(builder, selection, selectionInstanceToFind)) {
37
+ return selection;
38
+ }
39
+ }
40
+ return null;
41
+ }
42
+ clone() {
43
+ const clone = super.clone();
44
+ clone.selections = this.selections.slice();
45
+ return clone;
52
46
  }
53
-
54
- return null;
55
- }
56
-
57
- clone() {
58
- const clone = super.clone();
59
- clone.selections = this.selections.slice();
60
- return clone;
61
- }
62
47
  }
63
-
64
48
  module.exports = {
65
- SelectOperation,
49
+ SelectOperation,
66
50
  };
@@ -1,123 +1,112 @@
1
1
  'use strict';
2
-
3
2
  const { isString, isObject } = require('../../../utils/objectUtils');
4
-
5
3
  const ALIAS_REGEX = /\s+as\s+/i;
6
-
7
4
  class Selection {
8
- constructor(table, column, alias) {
9
- this.table = table;
10
- this.column = column;
11
- this.alias = alias;
12
- }
13
-
14
- get name() {
15
- return this.alias || this.column;
16
- }
17
-
18
- static create(selection) {
19
- if (isObject(selection)) {
20
- if (selection.isObjectionSelection) {
21
- return selection;
22
- } else if (selection.isObjectionReferenceBuilder) {
23
- return createSelectionFromReference(selection);
24
- } else if (selection.isObjectionRawBuilder) {
25
- return createSelectionFromRaw(selection);
26
- } else {
27
- return null;
28
- }
29
- } else if (isString(selection)) {
30
- return createSelectionFromString(selection);
31
- } else {
32
- return null;
5
+ constructor(table, column, alias) {
6
+ this.table = table;
7
+ this.column = column;
8
+ this.alias = alias;
9
+ }
10
+ get name() {
11
+ return this.alias || this.column;
33
12
  }
34
- }
35
-
36
- /**
37
- * Returns true if `selectionInBuilder` causes `selectionToTest` to be selected.
38
- *
39
- * Examples that return true:
40
- *
41
- * doesSelect(Person.query(), '*', 'name')
42
- * doesSelect(Person.query(), 'Person.*', 'name')
43
- * doesSelect(Person.query(), 'name', 'name')
44
- * doesSelect(Person.query(), 'name', 'Person.name')
45
- */
46
- static doesSelect(builder, selectionInBuilder, selectionToTest) {
47
- selectionInBuilder = Selection.create(selectionInBuilder);
48
- selectionToTest = Selection.create(selectionToTest);
49
-
50
- if (selectionInBuilder.column === '*') {
51
- if (selectionInBuilder.table) {
52
- if (selectionToTest.column === '*') {
53
- return selectionToTest.table === selectionInBuilder.table;
54
- } else {
55
- return (
56
- selectionToTest.table === null || selectionToTest.table === selectionInBuilder.table
57
- );
13
+ static create(selection) {
14
+ if (isObject(selection)) {
15
+ if (selection.isObjectionSelection) {
16
+ return selection;
17
+ }
18
+ else if (selection.isObjectionReferenceBuilder) {
19
+ return createSelectionFromReference(selection);
20
+ }
21
+ else if (selection.isObjectionRawBuilder) {
22
+ return createSelectionFromRaw(selection);
23
+ }
24
+ else {
25
+ return null;
26
+ }
27
+ }
28
+ else if (isString(selection)) {
29
+ return createSelectionFromString(selection);
30
+ }
31
+ else {
32
+ return null;
33
+ }
34
+ }
35
+ /**
36
+ * Returns true if `selectionInBuilder` causes `selectionToTest` to be selected.
37
+ *
38
+ * Examples that return true:
39
+ *
40
+ * doesSelect(Person.query(), '*', 'name')
41
+ * doesSelect(Person.query(), 'Person.*', 'name')
42
+ * doesSelect(Person.query(), 'name', 'name')
43
+ * doesSelect(Person.query(), 'name', 'Person.name')
44
+ */
45
+ static doesSelect(builder, selectionInBuilder, selectionToTest) {
46
+ selectionInBuilder = Selection.create(selectionInBuilder);
47
+ selectionToTest = Selection.create(selectionToTest);
48
+ if (selectionInBuilder.column === '*') {
49
+ if (selectionInBuilder.table) {
50
+ if (selectionToTest.column === '*') {
51
+ return selectionToTest.table === selectionInBuilder.table;
52
+ }
53
+ else {
54
+ return (selectionToTest.table === null || selectionToTest.table === selectionInBuilder.table);
55
+ }
56
+ }
57
+ else {
58
+ return true;
59
+ }
60
+ }
61
+ else {
62
+ const selectionInBuilderTable = selectionInBuilder.table || builder.tableRef();
63
+ if (selectionToTest.column === '*') {
64
+ return false;
65
+ }
66
+ else {
67
+ return (selectionToTest.column === selectionInBuilder.column &&
68
+ (selectionToTest.table === null || selectionToTest.table === selectionInBuilderTable));
69
+ }
58
70
  }
59
- } else {
60
- return true;
61
- }
62
- } else {
63
- const selectionInBuilderTable = selectionInBuilder.table || builder.tableRef();
64
-
65
- if (selectionToTest.column === '*') {
66
- return false;
67
- } else {
68
- return (
69
- selectionToTest.column === selectionInBuilder.column &&
70
- (selectionToTest.table === null || selectionToTest.table === selectionInBuilderTable)
71
- );
72
- }
73
71
  }
74
- }
75
72
  }
76
-
77
73
  Object.defineProperties(Selection.prototype, {
78
- isObjectionSelection: {
79
- enumerable: false,
80
- writable: false,
81
- value: true,
82
- },
74
+ isObjectionSelection: {
75
+ enumerable: false,
76
+ writable: false,
77
+ value: true,
78
+ },
83
79
  });
84
-
85
80
  function createSelectionFromReference(ref) {
86
- return new Selection(ref.tableName, ref.column, ref.alias);
81
+ return new Selection(ref.tableName, ref.column, ref.alias);
87
82
  }
88
-
89
83
  function createSelectionFromRaw(raw) {
90
- if (raw.alias) {
91
- return new Selection(null, null, raw.alias);
92
- } else {
93
- return null;
94
- }
84
+ if (raw.alias) {
85
+ return new Selection(null, null, raw.alias);
86
+ }
87
+ else {
88
+ return null;
89
+ }
95
90
  }
96
-
97
91
  function createSelectionFromString(selection) {
98
- let table = null;
99
- let column = null;
100
- let alias = null;
101
-
102
- if (ALIAS_REGEX.test(selection)) {
103
- const parts = selection.split(ALIAS_REGEX);
104
-
105
- selection = parts[0].trim();
106
- alias = parts[1].trim();
107
- }
108
-
109
- const dotIdx = selection.lastIndexOf('.');
110
-
111
- if (dotIdx !== -1) {
112
- table = selection.substr(0, dotIdx);
113
- column = selection.substr(dotIdx + 1);
114
- } else {
115
- column = selection;
116
- }
117
-
118
- return new Selection(table, column, alias);
92
+ let table = null;
93
+ let column = null;
94
+ let alias = null;
95
+ if (ALIAS_REGEX.test(selection)) {
96
+ const parts = selection.split(ALIAS_REGEX);
97
+ selection = parts[0].trim();
98
+ alias = parts[1].trim();
99
+ }
100
+ const dotIdx = selection.lastIndexOf('.');
101
+ if (dotIdx !== -1) {
102
+ table = selection.substr(0, dotIdx);
103
+ column = selection.substr(dotIdx + 1);
104
+ }
105
+ else {
106
+ column = selection;
107
+ }
108
+ return new Selection(table, column, alias);
119
109
  }
120
-
121
110
  module.exports = {
122
- Selection,
111
+ Selection,
123
112
  };
@@ -1,117 +1,84 @@
1
1
  'use strict';
2
-
3
2
  const { ObjectionToKnexConvertingOperation } = require('../ObjectionToKnexConvertingOperation');
4
3
  const { flatten, zipObject, isString } = require('../../../utils/objectUtils');
5
4
  const { getTempColumn } = require('../../../utils/tmpColumnUtils');
6
-
7
5
  class WhereInCompositeMsSqlOperation extends ObjectionToKnexConvertingOperation {
8
- constructor(name, opt) {
9
- super(name, opt);
10
- this.prefix = this.opt.prefix || null;
11
- }
12
-
13
- onBuildKnex(knexBuilder, builder) {
14
- const args = this.getKnexArgs(builder);
15
- return this.build(builder.knex(), knexBuilder, args[0], args[1]);
16
- }
17
-
18
- build(knex, knexBuilder, columns, values) {
19
- let isCompositeKey = Array.isArray(columns) && columns.length > 1;
20
-
21
- if (isCompositeKey) {
22
- return this.buildComposite(knex, knexBuilder, columns, values);
23
- } else {
24
- return this.buildNonComposite(knexBuilder, columns, values);
6
+ constructor(name, opt) {
7
+ super(name, opt);
8
+ this.prefix = this.opt.prefix || null;
25
9
  }
26
- }
27
-
28
- buildComposite(knex, knexBuilder, columns, values) {
29
- const helperColumns = columns.map((_, index) => getTempColumn(index));
30
-
31
- if (Array.isArray(values)) {
32
- return this.buildCompositeValue(knex, knexBuilder, columns, helperColumns, values);
33
- } else {
34
- return this.buildCompositeSubquery(
35
- knex,
36
- knexBuilder,
37
- columns,
38
- helperColumns,
39
- values.as(knex.raw(`V(${helperColumns.map((_) => '??')})`, helperColumns)),
40
- );
10
+ onBuildKnex(knexBuilder, builder) {
11
+ const args = this.getKnexArgs(builder);
12
+ return this.build(builder.knex(), knexBuilder, args[0], args[1]);
41
13
  }
42
- }
43
-
44
- buildCompositeValue(knex, knexBuilder, columns, helperColumns, values) {
45
- return this.buildCompositeSubquery(
46
- knex,
47
- knexBuilder,
48
- columns,
49
- helperColumns,
50
- knex.raw(
51
- `(VALUES ${values
52
- .map((value) => `(${value.map((_) => '?').join(',')})`)
53
- .join(',')}) AS V(${helperColumns.map((_) => '??').join(',')})`,
54
- flatten(values).concat(helperColumns),
55
- ),
56
- );
57
- }
58
-
59
- buildCompositeSubquery(knex, knexBuilder, columns, helperColumns, subQuery) {
60
- const wrapperQuery = knex.from(subQuery).where(
61
- zipObject(
62
- helperColumns,
63
- columns.map((column) => knex.raw('??', column)),
64
- ),
65
- );
66
-
67
- if (this.prefix === 'not') {
68
- return knexBuilder.whereNotExists(wrapperQuery);
69
- } else {
70
- return knexBuilder.whereExists(wrapperQuery);
14
+ build(knex, knexBuilder, columns, values) {
15
+ const isCompositeKey = Array.isArray(columns) && columns.length > 1;
16
+ if (isCompositeKey) {
17
+ return this.buildComposite(knex, knexBuilder, columns, values);
18
+ }
19
+ else {
20
+ return this.buildNonComposite(knexBuilder, columns, values);
21
+ }
71
22
  }
72
- }
73
-
74
- buildNonComposite(knexBuilder, columns, values) {
75
- const col = isString(columns) ? columns : columns[0];
76
-
77
- if (Array.isArray(values)) {
78
- values = pickNonNull(values, []);
79
- } else {
80
- values = [values];
23
+ buildComposite(knex, knexBuilder, columns, values) {
24
+ const helperColumns = columns.map((_, index) => getTempColumn(index));
25
+ if (Array.isArray(values)) {
26
+ return this.buildCompositeValue(knex, knexBuilder, columns, helperColumns, values);
27
+ }
28
+ else {
29
+ return this.buildCompositeSubquery(knex, knexBuilder, columns, helperColumns, values.as(knex.raw(`V(${helperColumns.map((_) => '??')})`, helperColumns)));
30
+ }
81
31
  }
82
-
83
- return this.whereIn(knexBuilder, col, values);
84
- }
85
-
86
- whereIn(knexBuilder, col, val) {
87
- if (this.prefix === 'not') {
88
- return knexBuilder.whereNotIn(col, val);
89
- } else {
90
- return knexBuilder.whereIn(col, val);
32
+ buildCompositeValue(knex, knexBuilder, columns, helperColumns, values) {
33
+ return this.buildCompositeSubquery(knex, knexBuilder, columns, helperColumns, knex.raw(`(VALUES ${values
34
+ .map((value) => `(${value.map((_) => '?').join(',')})`)
35
+ .join(',')}) AS V(${helperColumns.map((_) => '??').join(',')})`, flatten(values).concat(helperColumns)));
36
+ }
37
+ buildCompositeSubquery(knex, knexBuilder, columns, helperColumns, subQuery) {
38
+ const wrapperQuery = knex.from(subQuery).where(zipObject(helperColumns, columns.map((column) => knex.raw('??', column))));
39
+ if (this.prefix === 'not') {
40
+ return knexBuilder.whereNotExists(wrapperQuery);
41
+ }
42
+ else {
43
+ return knexBuilder.whereExists(wrapperQuery);
44
+ }
45
+ }
46
+ buildNonComposite(knexBuilder, columns, values) {
47
+ const col = isString(columns) ? columns : columns[0];
48
+ if (Array.isArray(values)) {
49
+ values = pickNonNull(values, []);
50
+ }
51
+ else {
52
+ values = [values];
53
+ }
54
+ return this.whereIn(knexBuilder, col, values);
55
+ }
56
+ whereIn(knexBuilder, col, val) {
57
+ if (this.prefix === 'not') {
58
+ return knexBuilder.whereNotIn(col, val);
59
+ }
60
+ else {
61
+ return knexBuilder.whereIn(col, val);
62
+ }
63
+ }
64
+ clone() {
65
+ const clone = super.clone();
66
+ clone.prefix = this.prefix;
67
+ return clone;
91
68
  }
92
- }
93
-
94
- clone() {
95
- const clone = super.clone();
96
- clone.prefix = this.prefix;
97
- return clone;
98
- }
99
69
  }
100
-
101
70
  function pickNonNull(values, output) {
102
- for (let i = 0, l = values.length; i < l; ++i) {
103
- const val = values[i];
104
-
105
- if (Array.isArray(val)) {
106
- pickNonNull(val, output);
107
- } else if (val !== null && val !== undefined) {
108
- output.push(val);
71
+ for (let i = 0, l = values.length; i < l; ++i) {
72
+ const val = values[i];
73
+ if (Array.isArray(val)) {
74
+ pickNonNull(val, output);
75
+ }
76
+ else if (val !== null && val !== undefined) {
77
+ output.push(val);
78
+ }
109
79
  }
110
- }
111
-
112
- return output;
80
+ return output;
113
81
  }
114
-
115
82
  module.exports = {
116
- WhereInCompositeMsSqlOperation,
83
+ WhereInCompositeMsSqlOperation,
117
84
  };
@@ -1,100 +1,90 @@
1
1
  'use strict';
2
-
3
2
  const { ObjectionToKnexConvertingOperation } = require('../ObjectionToKnexConvertingOperation');
4
3
  const { isObject, asSingle } = require('../../../utils/objectUtils');
5
4
  const { isKnexQueryBuilder } = require('../../../utils/knexUtils');
6
-
7
5
  class WhereInCompositeOperation extends ObjectionToKnexConvertingOperation {
8
- constructor(name, opt) {
9
- super(name, opt);
10
- this.prefix = this.opt.prefix || null;
11
- }
12
-
13
- onBuildKnex(knexBuilder, builder) {
14
- const whereInArgs = buildWhereInArgs(builder.knex(), ...this.getKnexArgs(builder));
15
-
16
- if (this.prefix === 'not') {
17
- return knexBuilder.whereNotIn(...whereInArgs);
18
- } else {
19
- return knexBuilder.whereIn(...whereInArgs);
6
+ constructor(name, opt) {
7
+ super(name, opt);
8
+ this.prefix = this.opt.prefix || null;
9
+ }
10
+ onBuildKnex(knexBuilder, builder) {
11
+ const whereInArgs = buildWhereInArgs(builder.knex(), ...this.getKnexArgs(builder));
12
+ if (this.prefix === 'not') {
13
+ return knexBuilder.whereNotIn(...whereInArgs);
14
+ }
15
+ else {
16
+ return knexBuilder.whereIn(...whereInArgs);
17
+ }
18
+ }
19
+ clone() {
20
+ const clone = super.clone();
21
+ clone.prefix = this.prefix;
22
+ return clone;
20
23
  }
21
- }
22
-
23
- clone() {
24
- const clone = super.clone();
25
- clone.prefix = this.prefix;
26
- return clone;
27
- }
28
24
  }
29
-
30
25
  function buildWhereInArgs(knex, columns, values) {
31
- if (isCompositeKey(columns)) {
32
- return buildCompositeArgs(knex, columns, values);
33
- } else {
34
- return buildNonCompositeArgs(columns, values);
35
- }
26
+ if (isCompositeKey(columns)) {
27
+ return buildCompositeArgs(knex, columns, values);
28
+ }
29
+ else {
30
+ return buildNonCompositeArgs(columns, values);
31
+ }
36
32
  }
37
-
38
33
  function isCompositeKey(columns) {
39
- return Array.isArray(columns) && columns.length > 1;
34
+ return Array.isArray(columns) && columns.length > 1;
40
35
  }
41
-
42
36
  function buildCompositeArgs(knex, columns, values) {
43
- if (Array.isArray(values)) {
44
- return buildCompositeValueArgs(columns, values);
45
- } else {
46
- return buildCompositeSubqueryArgs(knex, columns, values);
47
- }
37
+ if (Array.isArray(values)) {
38
+ return buildCompositeValueArgs(columns, values);
39
+ }
40
+ else {
41
+ return buildCompositeSubqueryArgs(knex, columns, values);
42
+ }
48
43
  }
49
-
50
44
  function buildCompositeValueArgs(columns, values) {
51
- if (!Array.isArray(values[0])) {
52
- return [columns, [values]];
53
- } else {
54
- return [columns, values];
55
- }
45
+ if (!Array.isArray(values[0])) {
46
+ return [columns, [values]];
47
+ }
48
+ else {
49
+ return [columns, values];
50
+ }
56
51
  }
57
-
58
52
  function buildCompositeSubqueryArgs(knex, columns, subquery) {
59
- const sql = `(${columns
60
- .map((col) => {
61
- // On older versions of knex, raw doesn't work
62
- // with `??`. We use `?` for those.
63
- if (isObject(col)) {
64
- return '?';
65
- } else {
66
- return '??';
67
- }
53
+ const sql = `(${columns
54
+ .map((col) => {
55
+ // On older versions of knex, raw doesn't work
56
+ // with `??`. We use `?` for those.
57
+ if (isObject(col)) {
58
+ return '?';
59
+ }
60
+ else {
61
+ return '??';
62
+ }
68
63
  })
69
- .join(',')})`;
70
-
71
- return [knex.raw(sql, columns), subquery];
64
+ .join(',')})`;
65
+ return [knex.raw(sql, columns), subquery];
72
66
  }
73
-
74
67
  function buildNonCompositeArgs(columns, values) {
75
- if (Array.isArray(values)) {
76
- values = pickNonNull(values, []);
77
- } else if (!isKnexQueryBuilder(values)) {
78
- values = [values];
79
- }
80
-
81
- return [asSingle(columns), values];
68
+ if (Array.isArray(values)) {
69
+ values = pickNonNull(values, []);
70
+ }
71
+ else if (!isKnexQueryBuilder(values)) {
72
+ values = [values];
73
+ }
74
+ return [asSingle(columns), values];
82
75
  }
83
-
84
76
  function pickNonNull(values, output) {
85
- for (let i = 0, l = values.length; i < l; ++i) {
86
- const val = values[i];
87
-
88
- if (Array.isArray(val)) {
89
- pickNonNull(val, output);
90
- } else if (val !== null && val !== undefined) {
91
- output.push(val);
77
+ for (let i = 0, l = values.length; i < l; ++i) {
78
+ const val = values[i];
79
+ if (Array.isArray(val)) {
80
+ pickNonNull(val, output);
81
+ }
82
+ else if (val !== null && val !== undefined) {
83
+ output.push(val);
84
+ }
92
85
  }
93
- }
94
-
95
- return output;
86
+ return output;
96
87
  }
97
-
98
88
  module.exports = {
99
- WhereInCompositeOperation,
89
+ WhereInCompositeOperation,
100
90
  };