@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.
- package/README.md +103 -67
- package/lib/index.mjs +6 -0
- package/lib/initialize.js +6 -9
- package/lib/model/AjvValidator.js +235 -282
- package/lib/model/Model.js +653 -823
- package/lib/model/ModifierNotFoundError.js +5 -7
- package/lib/model/NotFoundError.js +14 -18
- package/lib/model/RelationDoesNotExistError.js +6 -9
- package/lib/model/ValidationError.js +33 -41
- package/lib/model/Validator.js +15 -21
- package/lib/model/getModel.js +1 -3
- package/lib/model/graph/ModelGraph.js +44 -64
- package/lib/model/graph/ModelGraphBuilder.js +164 -217
- package/lib/model/graph/ModelGraphEdge.js +31 -43
- package/lib/model/graph/ModelGraphNode.js +192 -231
- package/lib/model/inheritModel.js +9 -18
- package/lib/model/modelBindKnex.js +48 -62
- package/lib/model/modelClone.js +79 -98
- package/lib/model/modelColPropMap.js +16 -26
- package/lib/model/modelConcurrency.js +24 -0
- package/lib/model/modelId.js +44 -49
- package/lib/model/modelJsonAttributes.js +56 -76
- package/lib/model/modelParseRelations.js +68 -67
- package/lib/model/modelQueryProps.js +76 -107
- package/lib/model/modelSet.js +92 -112
- package/lib/model/modelTableMetadata.js +60 -72
- package/lib/model/modelToJson.js +120 -150
- package/lib/model/modelUtils.js +30 -37
- package/lib/model/modelValidate.js +34 -42
- package/lib/model/modelValues.js +78 -91
- package/lib/model/modelVisitor.js +22 -29
- package/lib/objection.js +42 -67
- package/lib/queryBuilder/FunctionBuilder.js +231 -279
- package/lib/queryBuilder/InternalOptions.js +19 -24
- package/lib/queryBuilder/JoinBuilder.js +116 -149
- package/lib/queryBuilder/QueryBuilder.js +1009 -1271
- package/lib/queryBuilder/QueryBuilderBase.js +647 -865
- package/lib/queryBuilder/QueryBuilderContext.js +14 -21
- package/lib/queryBuilder/QueryBuilderContextBase.js +20 -29
- package/lib/queryBuilder/QueryBuilderOperationSupport.js +462 -536
- package/lib/queryBuilder/QueryBuilderUserContext.js +19 -25
- package/lib/queryBuilder/RawBuilder.js +58 -73
- package/lib/queryBuilder/ReferenceBuilder.js +232 -270
- package/lib/queryBuilder/RelationExpression.js +322 -380
- package/lib/queryBuilder/StaticHookArguments.js +94 -106
- package/lib/queryBuilder/ValueBuilder.js +100 -112
- package/lib/queryBuilder/expressions.js +86 -116
- package/lib/queryBuilder/functions/mysql.js +642 -697
- package/lib/queryBuilder/graph/GraphAction.js +44 -59
- package/lib/queryBuilder/graph/GraphData.js +7 -9
- package/lib/queryBuilder/graph/GraphFetcher.js +115 -151
- package/lib/queryBuilder/graph/GraphNodeDbExistence.js +114 -152
- package/lib/queryBuilder/graph/GraphOperation.js +16 -22
- package/lib/queryBuilder/graph/GraphOptions.js +143 -185
- package/lib/queryBuilder/graph/GraphUpsert.js +167 -241
- package/lib/queryBuilder/graph/delete/GraphDelete.js +8 -13
- package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +44 -62
- package/lib/queryBuilder/graph/insert/GraphInsert.js +99 -128
- package/lib/queryBuilder/graph/insert/GraphInsertAction.js +151 -193
- package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +57 -79
- package/lib/queryBuilder/graph/patch/GraphPatch.js +8 -13
- package/lib/queryBuilder/graph/patch/GraphPatchAction.js +181 -234
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +16 -21
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +80 -110
- package/lib/queryBuilder/join/JoinResultColumn.js +14 -18
- package/lib/queryBuilder/join/JoinResultParser.js +88 -101
- package/lib/queryBuilder/join/RelationJoiner.js +205 -288
- package/lib/queryBuilder/join/TableNode.js +117 -144
- package/lib/queryBuilder/join/TableTree.js +60 -76
- package/lib/queryBuilder/join/utils.js +10 -16
- package/lib/queryBuilder/operations/DelegateOperation.js +93 -133
- package/lib/queryBuilder/operations/DeleteOperation.js +33 -37
- package/lib/queryBuilder/operations/FindByIdOperation.js +20 -28
- package/lib/queryBuilder/operations/FindByIdsOperation.js +17 -23
- package/lib/queryBuilder/operations/FindOperation.js +114 -131
- package/lib/queryBuilder/operations/FirstOperation.js +14 -19
- package/lib/queryBuilder/operations/FromOperation.js +69 -88
- package/lib/queryBuilder/operations/InsertAndFetchOperation.js +28 -39
- package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +25 -35
- package/lib/queryBuilder/operations/InsertGraphOperation.js +77 -98
- package/lib/queryBuilder/operations/InsertOperation.js +132 -165
- package/lib/queryBuilder/operations/InstanceDeleteOperation.js +27 -36
- package/lib/queryBuilder/operations/InstanceFindOperation.js +9 -13
- package/lib/queryBuilder/operations/InstanceInsertOperation.js +13 -17
- package/lib/queryBuilder/operations/InstanceUpdateOperation.js +32 -46
- package/lib/queryBuilder/operations/JoinRelatedOperation.js +66 -86
- package/lib/queryBuilder/operations/KnexOperation.js +4 -7
- package/lib/queryBuilder/operations/MergeOperation.js +32 -45
- package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +131 -150
- package/lib/queryBuilder/operations/OnBuildKnexOperation.js +17 -23
- package/lib/queryBuilder/operations/OnBuildOperation.js +17 -23
- package/lib/queryBuilder/operations/OnErrorOperation.js +17 -23
- package/lib/queryBuilder/operations/QueryBuilderOperation.js +231 -270
- package/lib/queryBuilder/operations/RangeOperation.js +30 -40
- package/lib/queryBuilder/operations/RelateOperation.js +14 -21
- package/lib/queryBuilder/operations/ReturningOperation.js +12 -18
- package/lib/queryBuilder/operations/RunAfterOperation.js +17 -23
- package/lib/queryBuilder/operations/RunBeforeOperation.js +17 -23
- package/lib/queryBuilder/operations/UnrelateOperation.js +12 -17
- package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +48 -65
- package/lib/queryBuilder/operations/UpdateOperation.js +104 -129
- package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +25 -35
- package/lib/queryBuilder/operations/UpsertGraphOperation.js +36 -55
- package/lib/queryBuilder/operations/WhereCompositeOperation.js +30 -39
- package/lib/queryBuilder/operations/WhereInOperation.js +34 -36
- package/lib/queryBuilder/operations/eager/EagerOperation.js +37 -52
- package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +34 -44
- package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +4 -7
- package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +193 -251
- package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +18 -25
- package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectOperation.js +25 -41
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +19 -20
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOverlapsOperation.js +23 -21
- package/lib/queryBuilder/operations/jsonApi/mysqlJsonApi.js +106 -118
- package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +79 -108
- package/lib/queryBuilder/operations/select/SelectOperation.js +37 -53
- package/lib/queryBuilder/operations/select/Selection.js +94 -105
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +68 -101
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +65 -75
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +75 -84
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +760 -859
- package/lib/queryBuilder/parsers/relationExpressionParser.js +840 -952
- package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +10 -15
- package/lib/queryBuilder/transformations/QueryTransformation.js +4 -6
- package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +35 -49
- package/lib/queryBuilder/transformations/index.js +3 -9
- package/lib/queryBuilder/whereScopes.js +158 -186
- package/lib/relations/Relation.js +279 -363
- package/lib/relations/RelationDeleteOperation.js +16 -22
- package/lib/relations/RelationFindOperation.js +100 -133
- package/lib/relations/RelationInsertOperation.js +19 -27
- package/lib/relations/RelationOwner.js +167 -204
- package/lib/relations/RelationProperty.js +177 -217
- package/lib/relations/RelationUpdateOperation.js +16 -23
- package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +11 -16
- package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +24 -35
- package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +30 -41
- package/lib/relations/belongsToOne/BelongsToOneRelation.js +33 -41
- package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +38 -53
- package/lib/relations/hasMany/HasManyInsertOperation.js +35 -51
- package/lib/relations/hasMany/HasManyRelateOperation.js +38 -54
- package/lib/relations/hasMany/HasManyRelation.js +30 -38
- package/lib/relations/hasMany/HasManyUnrelateOperation.js +19 -26
- package/lib/relations/hasOne/HasOneRelation.js +4 -7
- package/lib/relations/hasOneThrough/HasOneThroughRelation.js +4 -7
- package/lib/relations/manyToMany/ManyToManyModifyMixin.js +67 -90
- package/lib/relations/manyToMany/ManyToManyRelation.js +349 -451
- package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +19 -26
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +3 -5
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +14 -20
- package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +3 -7
- package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +71 -94
- package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +31 -43
- package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +50 -67
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +4 -7
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +20 -27
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +5 -10
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +3 -5
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +55 -72
- package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +3 -7
- package/lib/transaction.js +53 -83
- package/lib/utils/assert.js +15 -21
- package/lib/utils/buildUtils.js +17 -19
- package/lib/utils/classUtils.js +7 -11
- package/lib/utils/cloneDeep.js +365 -0
- package/lib/utils/createModifier.js +36 -41
- package/lib/utils/deprecate.js +8 -12
- package/lib/utils/identifierMapping.js +153 -192
- package/lib/utils/internalPropUtils.js +4 -8
- package/lib/utils/knexUtils.js +61 -56
- package/lib/utils/mixin.js +16 -23
- package/lib/utils/mysqlJsonPath.js +55 -40
- package/lib/utils/normalizeIds.js +110 -127
- package/lib/utils/objectUtils.js +236 -338
- package/lib/utils/parseFieldExpression.js +66 -44
- package/lib/utils/postgresJsonPath.js +48 -62
- package/lib/utils/promiseUtils/after.js +10 -13
- package/lib/utils/promiseUtils/afterReturn.js +10 -13
- package/lib/utils/promiseUtils/index.js +15 -17
- package/lib/utils/promiseUtils/isPromise.js +6 -9
- package/lib/utils/promiseUtils/map.js +75 -89
- package/lib/utils/promiseUtils/mapAfterAllReturn.js +17 -23
- package/lib/utils/promiseUtils/try.js +15 -18
- package/lib/utils/resolveModel.js +91 -94
- package/lib/utils/tmpColumnUtils.js +4 -13
- package/package.json +50 -23
- package/typings/objection/index.d.mts +362 -0
- package/typings/objection/index.d.ts +36 -9
- package/lib/.eslintrc.json +0 -8
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.pegjs +0 -97
- package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +0 -141
- 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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
74
|
+
isObjectionSelection: {
|
|
75
|
+
enumerable: false,
|
|
76
|
+
writable: false,
|
|
77
|
+
value: true,
|
|
78
|
+
},
|
|
83
79
|
});
|
|
84
|
-
|
|
85
80
|
function createSelectionFromReference(ref) {
|
|
86
|
-
|
|
81
|
+
return new Selection(ref.tableName, ref.column, ref.alias);
|
|
87
82
|
}
|
|
88
|
-
|
|
89
83
|
function createSelectionFromRaw(raw) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
34
|
+
return Array.isArray(columns) && columns.length > 1;
|
|
40
35
|
}
|
|
41
|
-
|
|
42
36
|
function buildCompositeArgs(knex, columns, values) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
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
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
|
|
89
|
+
WhereInCompositeOperation,
|
|
100
90
|
};
|