@abedmurrar/objection 0.0.0-stage → 4.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +23 -0
- package/README.md +69 -2
- package/lib/.eslintrc.json +8 -0
- package/lib/initialize.js +14 -0
- package/lib/model/AjvValidator.js +325 -0
- package/lib/model/Model.js +942 -0
- package/lib/model/ModifierNotFoundError.js +12 -0
- package/lib/model/NotFoundError.js +23 -0
- package/lib/model/RelationDoesNotExistError.js +14 -0
- package/lib/model/ValidationError.js +54 -0
- package/lib/model/Validator.js +26 -0
- package/lib/model/getModel.js +9 -0
- package/lib/model/graph/ModelGraph.js +80 -0
- package/lib/model/graph/ModelGraphBuilder.js +241 -0
- package/lib/model/graph/ModelGraphEdge.js +52 -0
- package/lib/model/graph/ModelGraphNode.js +240 -0
- package/lib/model/inheritModel.js +28 -0
- package/lib/model/modelBindKnex.js +83 -0
- package/lib/model/modelClone.js +121 -0
- package/lib/model/modelColPropMap.js +34 -0
- package/lib/model/modelId.js +62 -0
- package/lib/model/modelJsonAttributes.js +92 -0
- package/lib/model/modelParseRelations.js +82 -0
- package/lib/model/modelQueryProps.js +147 -0
- package/lib/model/modelSet.js +136 -0
- package/lib/model/modelTableMetadata.js +83 -0
- package/lib/model/modelToJson.js +187 -0
- package/lib/model/modelUtils.js +47 -0
- package/lib/model/modelValidate.js +48 -0
- package/lib/model/modelValues.js +126 -0
- package/lib/model/modelVisitor.js +41 -0
- package/lib/objection.js +114 -0
- package/lib/queryBuilder/FunctionBuilder.js +339 -0
- package/lib/queryBuilder/InternalOptions.js +29 -0
- package/lib/queryBuilder/JoinBuilder.js +157 -0
- package/lib/queryBuilder/QueryBuilder.js +1398 -0
- package/lib/queryBuilder/QueryBuilderBase.js +884 -0
- package/lib/queryBuilder/QueryBuilderContext.js +27 -0
- package/lib/queryBuilder/QueryBuilderContextBase.js +35 -0
- package/lib/queryBuilder/QueryBuilderOperationSupport.js +558 -0
- package/lib/queryBuilder/QueryBuilderUserContext.js +31 -0
- package/lib/queryBuilder/RawBuilder.js +93 -0
- package/lib/queryBuilder/ReferenceBuilder.js +284 -0
- package/lib/queryBuilder/RelationExpression.js +431 -0
- package/lib/queryBuilder/StaticHookArguments.js +134 -0
- package/lib/queryBuilder/ValueBuilder.js +127 -0
- package/lib/queryBuilder/expressions.js +151 -0
- package/lib/queryBuilder/functions/mysql.js +817 -0
- package/lib/queryBuilder/graph/GraphAction.js +69 -0
- package/lib/queryBuilder/graph/GraphData.js +14 -0
- package/lib/queryBuilder/graph/GraphFetcher.js +191 -0
- package/lib/queryBuilder/graph/GraphNodeDbExistence.js +184 -0
- package/lib/queryBuilder/graph/GraphOperation.js +27 -0
- package/lib/queryBuilder/graph/GraphOptions.js +207 -0
- package/lib/queryBuilder/graph/GraphUpsert.js +305 -0
- package/lib/queryBuilder/graph/delete/GraphDelete.js +20 -0
- package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +74 -0
- package/lib/queryBuilder/graph/insert/GraphInsert.js +145 -0
- package/lib/queryBuilder/graph/insert/GraphInsertAction.js +225 -0
- package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +92 -0
- package/lib/queryBuilder/graph/patch/GraphPatch.js +20 -0
- package/lib/queryBuilder/graph/patch/GraphPatchAction.js +258 -0
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +31 -0
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +126 -0
- package/lib/queryBuilder/join/JoinResultColumn.js +23 -0
- package/lib/queryBuilder/join/JoinResultParser.js +119 -0
- package/lib/queryBuilder/join/RelationJoiner.js +341 -0
- package/lib/queryBuilder/join/TableNode.js +163 -0
- package/lib/queryBuilder/join/TableTree.js +85 -0
- package/lib/queryBuilder/join/utils.js +25 -0
- package/lib/queryBuilder/operations/DelegateOperation.js +141 -0
- package/lib/queryBuilder/operations/DeleteOperation.js +51 -0
- package/lib/queryBuilder/operations/FindByIdOperation.js +37 -0
- package/lib/queryBuilder/operations/FindByIdsOperation.js +29 -0
- package/lib/queryBuilder/operations/FindOperation.js +164 -0
- package/lib/queryBuilder/operations/FirstOperation.js +27 -0
- package/lib/queryBuilder/operations/FromOperation.js +110 -0
- package/lib/queryBuilder/operations/InsertAndFetchOperation.js +49 -0
- package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +45 -0
- package/lib/queryBuilder/operations/InsertGraphOperation.js +108 -0
- package/lib/queryBuilder/operations/InsertOperation.js +206 -0
- package/lib/queryBuilder/operations/InstanceDeleteOperation.js +46 -0
- package/lib/queryBuilder/operations/InstanceFindOperation.js +21 -0
- package/lib/queryBuilder/operations/InstanceInsertOperation.js +24 -0
- package/lib/queryBuilder/operations/InstanceUpdateOperation.js +59 -0
- package/lib/queryBuilder/operations/JoinRelatedOperation.js +103 -0
- package/lib/queryBuilder/operations/KnexOperation.js +14 -0
- package/lib/queryBuilder/operations/MergeOperation.js +56 -0
- package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +192 -0
- package/lib/queryBuilder/operations/OnBuildKnexOperation.js +29 -0
- package/lib/queryBuilder/operations/OnBuildOperation.js +29 -0
- package/lib/queryBuilder/operations/OnErrorOperation.js +29 -0
- package/lib/queryBuilder/operations/QueryBuilderOperation.js +297 -0
- package/lib/queryBuilder/operations/RangeOperation.js +47 -0
- package/lib/queryBuilder/operations/RelateOperation.js +27 -0
- package/lib/queryBuilder/operations/ReturningOperation.js +30 -0
- package/lib/queryBuilder/operations/RunAfterOperation.js +29 -0
- package/lib/queryBuilder/operations/RunBeforeOperation.js +29 -0
- package/lib/queryBuilder/operations/UnrelateOperation.js +23 -0
- package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +80 -0
- package/lib/queryBuilder/operations/UpdateOperation.js +155 -0
- package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +45 -0
- package/lib/queryBuilder/operations/UpsertGraphOperation.js +66 -0
- package/lib/queryBuilder/operations/WhereCompositeOperation.js +57 -0
- package/lib/queryBuilder/operations/WhereInOperation.js +48 -0
- package/lib/queryBuilder/operations/eager/EagerOperation.js +61 -0
- package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +51 -0
- package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +13 -0
- package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +285 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +36 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectOperation.js +53 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +31 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOverlapsOperation.js +31 -0
- package/lib/queryBuilder/operations/jsonApi/mysqlJsonApi.js +167 -0
- package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +164 -0
- package/lib/queryBuilder/operations/select/SelectOperation.js +66 -0
- package/lib/queryBuilder/operations/select/Selection.js +123 -0
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +117 -0
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +100 -0
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +104 -0
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +886 -0
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.pegjs +97 -0
- package/lib/queryBuilder/parsers/relationExpressionParser.js +1042 -0
- package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +141 -0
- package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +22 -0
- package/lib/queryBuilder/transformations/QueryTransformation.js +11 -0
- package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +71 -0
- package/lib/queryBuilder/transformations/index.js +15 -0
- package/lib/queryBuilder/whereScopes.js +227 -0
- package/lib/relations/Relation.js +401 -0
- package/lib/relations/RelationDeleteOperation.js +29 -0
- package/lib/relations/RelationFindOperation.js +149 -0
- package/lib/relations/RelationInsertOperation.js +33 -0
- package/lib/relations/RelationOwner.js +236 -0
- package/lib/relations/RelationProperty.js +247 -0
- package/lib/relations/RelationUpdateOperation.js +30 -0
- package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +23 -0
- package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +44 -0
- package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +50 -0
- package/lib/relations/belongsToOne/BelongsToOneRelation.js +53 -0
- package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +61 -0
- package/lib/relations/hasMany/HasManyInsertOperation.js +61 -0
- package/lib/relations/hasMany/HasManyRelateOperation.js +64 -0
- package/lib/relations/hasMany/HasManyRelation.js +49 -0
- package/lib/relations/hasMany/HasManyUnrelateOperation.js +33 -0
- package/lib/relations/hasOne/HasOneRelation.js +13 -0
- package/lib/relations/hasOneThrough/HasOneThroughRelation.js +13 -0
- package/lib/relations/manyToMany/ManyToManyModifyMixin.js +105 -0
- package/lib/relations/manyToMany/ManyToManyRelation.js +507 -0
- package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +36 -0
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +10 -0
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +26 -0
- package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +12 -0
- package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +105 -0
- package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +52 -0
- package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +81 -0
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +14 -0
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +34 -0
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +16 -0
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +10 -0
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +82 -0
- package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +12 -0
- package/lib/transaction.js +96 -0
- package/lib/utils/assert.js +25 -0
- package/lib/utils/buildUtils.js +21 -0
- package/lib/utils/classUtils.js +13 -0
- package/lib/utils/clone.js +2329 -0
- package/lib/utils/createModifier.js +42 -0
- package/lib/utils/deprecate.js +15 -0
- package/lib/utils/identifierMapping.js +227 -0
- package/lib/utils/internalPropUtils.js +11 -0
- package/lib/utils/knexUtils.js +76 -0
- package/lib/utils/mixin.js +25 -0
- package/lib/utils/mysqlJsonPath.js +55 -0
- package/lib/utils/normalizeIds.js +144 -0
- package/lib/utils/objectUtils.js +416 -0
- package/lib/utils/parseFieldExpression.js +51 -0
- package/lib/utils/postgresJsonPath.js +110 -0
- package/lib/utils/promiseUtils/after.js +17 -0
- package/lib/utils/promiseUtils/afterReturn.js +17 -0
- package/lib/utils/promiseUtils/index.js +17 -0
- package/lib/utils/promiseUtils/isPromise.js +11 -0
- package/lib/utils/promiseUtils/map.js +110 -0
- package/lib/utils/promiseUtils/mapAfterAllReturn.js +29 -0
- package/lib/utils/promiseUtils/try.js +22 -0
- package/lib/utils/resolveModel.js +107 -0
- package/lib/utils/tmpColumnUtils.js +16 -0
- package/package.json +86 -4
- package/typings/objection/index.d.ts +4104 -0
- package/typings/objection/relation-expression.d.ts +237 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { isObject } = require('../utils/objectUtils');
|
|
4
|
+
|
|
5
|
+
// Comparison, logic and arithmetic methods for `ref()`, `val()`, `fn()` and
|
|
6
|
+
// `raw()`, so that expressions can be written without raw sql:
|
|
7
|
+
//
|
|
8
|
+
// Person.ref('age').gte(18).as('isAdult')
|
|
9
|
+
// Pet.ref('order').plus(1)
|
|
10
|
+
// Person.ref('firstName').isNotBlank().or(Person.ref('lastName').isNotBlank())
|
|
11
|
+
//
|
|
12
|
+
// Every method returns a new `FunctionBuilder`. The sql is wrapped in parentheses,
|
|
13
|
+
// so expressions can be nested without thinking about operator precedence. The
|
|
14
|
+
// arguments are bindings like the arguments of `fn()`: strings are values and
|
|
15
|
+
// columns are passed with `ref()` or `Model.ref()`.
|
|
16
|
+
|
|
17
|
+
const COMPARISONS = {
|
|
18
|
+
eq: '=',
|
|
19
|
+
ne: '<>',
|
|
20
|
+
gt: '>',
|
|
21
|
+
gte: '>=',
|
|
22
|
+
lt: '<',
|
|
23
|
+
lte: '<=',
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const ARITHMETIC = {
|
|
27
|
+
plus: '+',
|
|
28
|
+
minus: '-',
|
|
29
|
+
times: '*',
|
|
30
|
+
div: '/',
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const methods = {
|
|
34
|
+
// `eq(null)` and `ne(null)` are `IS NULL` and `IS NOT NULL`, like `where(col, null)`.
|
|
35
|
+
eq(value) {
|
|
36
|
+
return value === null ? this.isNull() : expression('(? = ?)', [this, value]);
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
ne(value) {
|
|
40
|
+
return value === null ? this.isNotNull() : expression('(? <> ?)', [this, value]);
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
in(values) {
|
|
44
|
+
return createIn(this, values, 'IN');
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
notIn(values) {
|
|
48
|
+
return createIn(this, values, 'NOT IN');
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
like(pattern) {
|
|
52
|
+
return expression('(? LIKE ?)', [this, pattern]);
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
between(min, max) {
|
|
56
|
+
return expression('(? BETWEEN ? AND ?)', [this, min, max]);
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
isNull() {
|
|
60
|
+
return expression('(? IS NULL)', [this]);
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
isNotNull() {
|
|
64
|
+
return expression('(? IS NOT NULL)', [this]);
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
// NULL, an empty string or a string of only spaces.
|
|
68
|
+
isBlank() {
|
|
69
|
+
return expression("(COALESCE(TRIM(?), '') = '')", [this]);
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
isNotBlank() {
|
|
73
|
+
return expression("(COALESCE(TRIM(?), '') <> '')", [this]);
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
and(...conditions) {
|
|
77
|
+
return createLogic(this, conditions, 'AND');
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
or(...conditions) {
|
|
81
|
+
return createLogic(this, conditions, 'OR');
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
not() {
|
|
85
|
+
return expression('(NOT ?)', [this]);
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
for (const [name, operator] of Object.entries(COMPARISONS)) {
|
|
90
|
+
if (!methods[name]) {
|
|
91
|
+
methods[name] = function (value) {
|
|
92
|
+
return expression(`(? ${operator} ?)`, [this, value]);
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
for (const [name, operator] of Object.entries(ARITHMETIC)) {
|
|
98
|
+
methods[name] = function (value) {
|
|
99
|
+
return expression(`(? ${operator} ?)`, [this, value]);
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function createIn(left, values, operator) {
|
|
104
|
+
if (isSubquery(values)) {
|
|
105
|
+
return expression(`(? ${operator} ?)`, [left, values]);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (!Array.isArray(values)) {
|
|
109
|
+
throw new Error(`${operator.toLowerCase()}() takes an array or a subquery.`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (values.length === 0) {
|
|
113
|
+
// Like knex's `whereIn(col, [])`.
|
|
114
|
+
return expression(operator === 'IN' ? '(1 = 0)' : '(1 = 1)', []);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return expression(`(? ${operator} (${values.map(() => '?').join(', ')}))`, [left, ...values]);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function createLogic(first, rest, operator) {
|
|
121
|
+
const conditions = [first, ...rest];
|
|
122
|
+
return expression(`(${conditions.map(() => '?').join(` ${operator} `)})`, conditions);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function isSubquery(value) {
|
|
126
|
+
return (
|
|
127
|
+
isObject(value) && (value.isObjectionQueryBuilderBase || typeof value.toSQL === 'function')
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function expression(sql, args) {
|
|
132
|
+
// Required here because `FunctionBuilder` itself gets these methods.
|
|
133
|
+
const { FunctionBuilder } = require('./FunctionBuilder');
|
|
134
|
+
return new FunctionBuilder(sql, args);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function addExpressionMethods(prototype) {
|
|
138
|
+
for (const [name, method] of Object.entries(methods)) {
|
|
139
|
+
Object.defineProperty(prototype, name, {
|
|
140
|
+
configurable: true,
|
|
141
|
+
enumerable: false,
|
|
142
|
+
writable: true,
|
|
143
|
+
value: method,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
module.exports = {
|
|
149
|
+
addExpressionMethods,
|
|
150
|
+
expression,
|
|
151
|
+
};
|