@abedmurrar/objection 0.0.0-stage → 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/LICENSE +23 -0
- package/README.md +105 -2
- package/lib/index.mjs +6 -0
- package/lib/initialize.js +11 -0
- package/lib/model/AjvValidator.js +278 -0
- package/lib/model/Model.js +772 -0
- package/lib/model/ModifierNotFoundError.js +10 -0
- package/lib/model/NotFoundError.js +19 -0
- package/lib/model/RelationDoesNotExistError.js +11 -0
- package/lib/model/ValidationError.js +46 -0
- package/lib/model/Validator.js +20 -0
- package/lib/model/getModel.js +7 -0
- package/lib/model/graph/ModelGraph.js +60 -0
- package/lib/model/graph/ModelGraphBuilder.js +188 -0
- package/lib/model/graph/ModelGraphEdge.js +40 -0
- package/lib/model/graph/ModelGraphNode.js +201 -0
- package/lib/model/inheritModel.js +19 -0
- package/lib/model/modelBindKnex.js +69 -0
- package/lib/model/modelClone.js +102 -0
- package/lib/model/modelColPropMap.js +24 -0
- package/lib/model/modelConcurrency.js +24 -0
- package/lib/model/modelId.js +57 -0
- package/lib/model/modelJsonAttributes.js +72 -0
- package/lib/model/modelParseRelations.js +83 -0
- package/lib/model/modelQueryProps.js +116 -0
- package/lib/model/modelSet.js +116 -0
- package/lib/model/modelTableMetadata.js +71 -0
- package/lib/model/modelToJson.js +157 -0
- package/lib/model/modelUtils.js +40 -0
- package/lib/model/modelValidate.js +40 -0
- package/lib/model/modelValues.js +113 -0
- package/lib/model/modelVisitor.js +34 -0
- package/lib/objection.js +89 -0
- package/lib/queryBuilder/FunctionBuilder.js +291 -0
- package/lib/queryBuilder/InternalOptions.js +24 -0
- package/lib/queryBuilder/JoinBuilder.js +124 -0
- package/lib/queryBuilder/QueryBuilder.js +1136 -0
- package/lib/queryBuilder/QueryBuilderBase.js +666 -0
- package/lib/queryBuilder/QueryBuilderContext.js +20 -0
- package/lib/queryBuilder/QueryBuilderContextBase.js +26 -0
- package/lib/queryBuilder/QueryBuilderOperationSupport.js +484 -0
- package/lib/queryBuilder/QueryBuilderUserContext.js +25 -0
- package/lib/queryBuilder/RawBuilder.js +78 -0
- package/lib/queryBuilder/ReferenceBuilder.js +246 -0
- package/lib/queryBuilder/RelationExpression.js +373 -0
- package/lib/queryBuilder/StaticHookArguments.js +122 -0
- package/lib/queryBuilder/ValueBuilder.js +115 -0
- package/lib/queryBuilder/expressions.js +121 -0
- package/lib/queryBuilder/functions/mysql.js +762 -0
- package/lib/queryBuilder/graph/GraphAction.js +54 -0
- package/lib/queryBuilder/graph/GraphData.js +12 -0
- package/lib/queryBuilder/graph/GraphFetcher.js +155 -0
- package/lib/queryBuilder/graph/GraphNodeDbExistence.js +146 -0
- package/lib/queryBuilder/graph/GraphOperation.js +21 -0
- package/lib/queryBuilder/graph/GraphOptions.js +165 -0
- package/lib/queryBuilder/graph/GraphUpsert.js +231 -0
- package/lib/queryBuilder/graph/delete/GraphDelete.js +15 -0
- package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +56 -0
- package/lib/queryBuilder/graph/insert/GraphInsert.js +116 -0
- package/lib/queryBuilder/graph/insert/GraphInsertAction.js +183 -0
- package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +70 -0
- package/lib/queryBuilder/graph/patch/GraphPatch.js +15 -0
- package/lib/queryBuilder/graph/patch/GraphPatchAction.js +205 -0
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +26 -0
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +96 -0
- package/lib/queryBuilder/join/JoinResultColumn.js +19 -0
- package/lib/queryBuilder/join/JoinResultParser.js +106 -0
- package/lib/queryBuilder/join/RelationJoiner.js +258 -0
- package/lib/queryBuilder/join/TableNode.js +136 -0
- package/lib/queryBuilder/join/TableTree.js +69 -0
- package/lib/queryBuilder/join/utils.js +19 -0
- package/lib/queryBuilder/operations/DelegateOperation.js +101 -0
- package/lib/queryBuilder/operations/DeleteOperation.js +47 -0
- package/lib/queryBuilder/operations/FindByIdOperation.js +29 -0
- package/lib/queryBuilder/operations/FindByIdsOperation.js +23 -0
- package/lib/queryBuilder/operations/FindOperation.js +147 -0
- package/lib/queryBuilder/operations/FirstOperation.js +22 -0
- package/lib/queryBuilder/operations/FromOperation.js +91 -0
- package/lib/queryBuilder/operations/InsertAndFetchOperation.js +38 -0
- package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +35 -0
- package/lib/queryBuilder/operations/InsertGraphOperation.js +87 -0
- package/lib/queryBuilder/operations/InsertOperation.js +173 -0
- package/lib/queryBuilder/operations/InstanceDeleteOperation.js +37 -0
- package/lib/queryBuilder/operations/InstanceFindOperation.js +17 -0
- package/lib/queryBuilder/operations/InstanceInsertOperation.js +20 -0
- package/lib/queryBuilder/operations/InstanceUpdateOperation.js +45 -0
- package/lib/queryBuilder/operations/JoinRelatedOperation.js +83 -0
- package/lib/queryBuilder/operations/KnexOperation.js +11 -0
- package/lib/queryBuilder/operations/MergeOperation.js +43 -0
- package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +173 -0
- package/lib/queryBuilder/operations/OnBuildKnexOperation.js +23 -0
- package/lib/queryBuilder/operations/OnBuildOperation.js +23 -0
- package/lib/queryBuilder/operations/OnErrorOperation.js +23 -0
- package/lib/queryBuilder/operations/QueryBuilderOperation.js +258 -0
- package/lib/queryBuilder/operations/RangeOperation.js +37 -0
- package/lib/queryBuilder/operations/RelateOperation.js +20 -0
- package/lib/queryBuilder/operations/ReturningOperation.js +24 -0
- package/lib/queryBuilder/operations/RunAfterOperation.js +23 -0
- package/lib/queryBuilder/operations/RunBeforeOperation.js +23 -0
- package/lib/queryBuilder/operations/UnrelateOperation.js +18 -0
- package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +63 -0
- package/lib/queryBuilder/operations/UpdateOperation.js +130 -0
- package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +35 -0
- package/lib/queryBuilder/operations/UpsertGraphOperation.js +47 -0
- package/lib/queryBuilder/operations/WhereCompositeOperation.js +48 -0
- package/lib/queryBuilder/operations/WhereInOperation.js +46 -0
- package/lib/queryBuilder/operations/eager/EagerOperation.js +46 -0
- package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +41 -0
- package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +10 -0
- package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +227 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +29 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectOperation.js +37 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +30 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOverlapsOperation.js +33 -0
- package/lib/queryBuilder/operations/jsonApi/mysqlJsonApi.js +155 -0
- package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +135 -0
- package/lib/queryBuilder/operations/select/SelectOperation.js +50 -0
- package/lib/queryBuilder/operations/select/Selection.js +112 -0
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +84 -0
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +90 -0
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +95 -0
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +787 -0
- package/lib/queryBuilder/parsers/relationExpressionParser.js +930 -0
- package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +17 -0
- package/lib/queryBuilder/transformations/QueryTransformation.js +9 -0
- package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +57 -0
- package/lib/queryBuilder/transformations/index.js +9 -0
- package/lib/queryBuilder/whereScopes.js +199 -0
- package/lib/relations/Relation.js +317 -0
- package/lib/relations/RelationDeleteOperation.js +23 -0
- package/lib/relations/RelationFindOperation.js +116 -0
- package/lib/relations/RelationInsertOperation.js +25 -0
- package/lib/relations/RelationOwner.js +199 -0
- package/lib/relations/RelationProperty.js +207 -0
- package/lib/relations/RelationUpdateOperation.js +23 -0
- package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +18 -0
- package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +33 -0
- package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +39 -0
- package/lib/relations/belongsToOne/BelongsToOneRelation.js +45 -0
- package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +46 -0
- package/lib/relations/hasMany/HasManyInsertOperation.js +45 -0
- package/lib/relations/hasMany/HasManyRelateOperation.js +48 -0
- package/lib/relations/hasMany/HasManyRelation.js +41 -0
- package/lib/relations/hasMany/HasManyUnrelateOperation.js +26 -0
- package/lib/relations/hasOne/HasOneRelation.js +10 -0
- package/lib/relations/hasOneThrough/HasOneThroughRelation.js +10 -0
- package/lib/relations/manyToMany/ManyToManyModifyMixin.js +82 -0
- package/lib/relations/manyToMany/ManyToManyRelation.js +405 -0
- package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +29 -0
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +8 -0
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +20 -0
- package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +8 -0
- package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +82 -0
- package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +40 -0
- package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +64 -0
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +11 -0
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +27 -0
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +11 -0
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +8 -0
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +65 -0
- package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +8 -0
- package/lib/transaction.js +66 -0
- package/lib/utils/assert.js +19 -0
- package/lib/utils/buildUtils.js +19 -0
- package/lib/utils/classUtils.js +9 -0
- package/lib/utils/cloneDeep.js +365 -0
- package/lib/utils/createModifier.js +37 -0
- package/lib/utils/deprecate.js +11 -0
- package/lib/utils/identifierMapping.js +188 -0
- package/lib/utils/internalPropUtils.js +7 -0
- package/lib/utils/knexUtils.js +81 -0
- package/lib/utils/mixin.js +18 -0
- package/lib/utils/mysqlJsonPath.js +70 -0
- package/lib/utils/normalizeIds.js +127 -0
- package/lib/utils/objectUtils.js +314 -0
- package/lib/utils/parseFieldExpression.js +73 -0
- package/lib/utils/postgresJsonPath.js +96 -0
- package/lib/utils/promiseUtils/after.js +14 -0
- package/lib/utils/promiseUtils/afterReturn.js +14 -0
- package/lib/utils/promiseUtils/index.js +15 -0
- package/lib/utils/promiseUtils/isPromise.js +8 -0
- package/lib/utils/promiseUtils/map.js +96 -0
- package/lib/utils/promiseUtils/mapAfterAllReturn.js +23 -0
- package/lib/utils/promiseUtils/try.js +19 -0
- package/lib/utils/resolveModel.js +104 -0
- package/lib/utils/tmpColumnUtils.js +7 -0
- package/package.json +113 -4
- package/typings/objection/index.d.mts +362 -0
- package/typings/objection/index.d.ts +4131 -0
- package/typings/objection/relation-expression.d.ts +237 -0
|
@@ -0,0 +1,4131 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
|
|
3
|
+
// Type definitions for Objection.js
|
|
4
|
+
// Project: <http://vincit.github.io/objection.js/>
|
|
5
|
+
//
|
|
6
|
+
// Contributions by:
|
|
7
|
+
// * Matthew McEachen <https://github.com/mceachen>
|
|
8
|
+
// * Sami Koskimäki <https://github.com/koskimas>
|
|
9
|
+
// * Mikael Lepistö <https://github.com/elhigu>
|
|
10
|
+
// * Joseph T Lapp <https://github.com/jtlapp>
|
|
11
|
+
// * Drew R. <https://github.com/drew-r>
|
|
12
|
+
// * Karl Blomster <https://github.com/kblomster>
|
|
13
|
+
// * And many others: See <https://github.com/Vincit/objection.js/blob/main/typings/objection/index.d.ts>
|
|
14
|
+
|
|
15
|
+
import Ajv, { Options as AjvOptions } from 'ajv';
|
|
16
|
+
import * as dbErrors from 'db-errors';
|
|
17
|
+
import { Knex } from 'knex';
|
|
18
|
+
import { GraphNode, ParseError, ParseRelationExpression } from './relation-expression';
|
|
19
|
+
|
|
20
|
+
// Export the entire Objection namespace.
|
|
21
|
+
export = Objection;
|
|
22
|
+
|
|
23
|
+
declare namespace Objection {
|
|
24
|
+
const raw: RawFunction;
|
|
25
|
+
const val: ValueFunction;
|
|
26
|
+
const ref: ReferenceFunction;
|
|
27
|
+
const fn: FunctionFunction;
|
|
28
|
+
|
|
29
|
+
const compose: ComposeFunction;
|
|
30
|
+
const mixin: MixinFunction;
|
|
31
|
+
const defineRelations: DefineRelationsFunction;
|
|
32
|
+
|
|
33
|
+
const BelongsToOneRelation: RelationType<'one'>;
|
|
34
|
+
const HasOneRelation: RelationType<'one'>;
|
|
35
|
+
const HasManyRelation: RelationType<'many'>;
|
|
36
|
+
const ManyToManyRelation: RelationType<'many'>;
|
|
37
|
+
const HasOneThroughRelation: RelationType<'one'>;
|
|
38
|
+
|
|
39
|
+
const snakeCaseMappers: SnakeCaseMappersFactory;
|
|
40
|
+
const knexSnakeCaseMappers: KnexSnakeCaseMappersFactory;
|
|
41
|
+
|
|
42
|
+
const transaction: transaction;
|
|
43
|
+
const initialize: initialize;
|
|
44
|
+
|
|
45
|
+
const DBError: typeof dbErrors.DBError;
|
|
46
|
+
const DataError: typeof dbErrors.DataError;
|
|
47
|
+
const CheckViolationError: typeof dbErrors.CheckViolationError;
|
|
48
|
+
const UniqueViolationError: typeof dbErrors.UniqueViolationError;
|
|
49
|
+
const ConstraintViolationError: typeof dbErrors.ConstraintViolationError;
|
|
50
|
+
const ForeignKeyViolationError: typeof dbErrors.ForeignKeyViolationError;
|
|
51
|
+
const NotNullViolationError: typeof dbErrors.NotNullViolationError;
|
|
52
|
+
|
|
53
|
+
export interface RawBuilder extends Aliasable {}
|
|
54
|
+
/**
|
|
55
|
+
* What `raw()` returns: a `RawBuilder` with the expression methods.
|
|
56
|
+
*/
|
|
57
|
+
export interface RawExpressionBuilder extends RawBuilder, ExpressionMethods {}
|
|
58
|
+
|
|
59
|
+
export interface RawFunction extends RawInterface<RawExpressionBuilder> {}
|
|
60
|
+
export interface RawInterface<R> {
|
|
61
|
+
(sql: string, ...bindings: any[]): R;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface ValueBuilder extends Castable, ExpressionMethods {}
|
|
65
|
+
export interface ValueFunction {
|
|
66
|
+
(
|
|
67
|
+
value: PrimitiveValue | PrimitiveValue[] | PrimitiveValueObject | PrimitiveValueObject[],
|
|
68
|
+
): ValueBuilder;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A reference to a column or a field of a JSON column. `T` is the type of the value.
|
|
73
|
+
*/
|
|
74
|
+
export interface ReferenceBuilder<T = unknown> extends Aliasable, ExpressionMethods {
|
|
75
|
+
/**
|
|
76
|
+
* The type of the referenced value. Only used by the typings.
|
|
77
|
+
*/
|
|
78
|
+
'~type'?: T;
|
|
79
|
+
from(tableReference: string): this;
|
|
80
|
+
table(tableReference: string): this;
|
|
81
|
+
castText(): ReferenceBuilder<string | null>;
|
|
82
|
+
castInt(): ReferenceBuilder<number | null>;
|
|
83
|
+
castBigInt(): ReferenceBuilder<FnNumeric | null>;
|
|
84
|
+
castFloat(): ReferenceBuilder<number | null>;
|
|
85
|
+
castDecimal(): ReferenceBuilder<FnNumeric | null>;
|
|
86
|
+
castReal(): ReferenceBuilder<number | null>;
|
|
87
|
+
castBool(): ReferenceBuilder<boolean | 0 | 1 | null>;
|
|
88
|
+
castJson(): ReferenceBuilder<unknown>;
|
|
89
|
+
castTo<U = unknown>(sqlType: string): ReferenceBuilder<U>;
|
|
90
|
+
}
|
|
91
|
+
export interface ReferenceFunction {
|
|
92
|
+
(expression: string): ReferenceBuilder;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The columns of model `M`: its data properties, without relations and methods.
|
|
97
|
+
*/
|
|
98
|
+
type RefColumnName<M> = Extract<ModelProps<Extract<M, Model>>, string>;
|
|
99
|
+
|
|
100
|
+
type IsJsonType<T> = 0 extends 1 & T
|
|
101
|
+
? true
|
|
102
|
+
: unknown extends T
|
|
103
|
+
? true
|
|
104
|
+
: [Exclude<T, null | undefined>] extends [Date | Buffer]
|
|
105
|
+
? false
|
|
106
|
+
: [Exclude<T, null | undefined>] extends [object]
|
|
107
|
+
? true
|
|
108
|
+
: false;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The columns of model `M` that hold JSON objects or arrays.
|
|
112
|
+
*/
|
|
113
|
+
type JsonColumnName<M> = {
|
|
114
|
+
[K in RefColumnName<M>]: IsJsonType<M[K & keyof M]> extends true ? K : never;
|
|
115
|
+
}[RefColumnName<M>];
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The json field part of a field expression (`a.b`, `[0]`, `[key]`) for a column of
|
|
119
|
+
* type `T`. The first key is checked when `T` has known keys.
|
|
120
|
+
*/
|
|
121
|
+
type JsonFieldPath<T> = 0 extends 1 & T
|
|
122
|
+
? string
|
|
123
|
+
: unknown extends T
|
|
124
|
+
? string
|
|
125
|
+
: T extends readonly unknown[]
|
|
126
|
+
? `[${string}` | `${number}${string}`
|
|
127
|
+
: string extends keyof T
|
|
128
|
+
? string
|
|
129
|
+
: `${Extract<keyof T, string>}${'' | `.${string}` | `[${string}`}` | `[${string}`;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The expressions `Model.ref()` accepts for model `M`: a column, optionally with a
|
|
133
|
+
* table or alias (`alias.column`), and field expressions of JSON columns
|
|
134
|
+
* (`column:a.b`). Models that declare no columns accept any string.
|
|
135
|
+
*/
|
|
136
|
+
type RefExpression<M> = [RefColumnName<M>] extends [never]
|
|
137
|
+
? string
|
|
138
|
+
:
|
|
139
|
+
| RefColumnName<M>
|
|
140
|
+
| `${string}.${RefColumnName<M>}`
|
|
141
|
+
| '*'
|
|
142
|
+
| {
|
|
143
|
+
[K in JsonColumnName<M>]: `${K | `${string}.${K}`}:${JsonFieldPath<
|
|
144
|
+
Exclude<M[K & keyof M], null | undefined>
|
|
145
|
+
>}`;
|
|
146
|
+
}[JsonColumnName<M>];
|
|
147
|
+
|
|
148
|
+
type RefValue<M, E> = E extends `${string}:${string}`
|
|
149
|
+
? unknown
|
|
150
|
+
: E extends `${string}.${infer C}`
|
|
151
|
+
? M[C & keyof M]
|
|
152
|
+
: M[E & keyof M];
|
|
153
|
+
|
|
154
|
+
export interface FunctionBuilder<T = unknown> extends Castable, ExpressionMethods {
|
|
155
|
+
/**
|
|
156
|
+
* The type of the result of the function. Only used by the typings.
|
|
157
|
+
*/
|
|
158
|
+
'~type'?: T;
|
|
159
|
+
}
|
|
160
|
+
export interface SqlFunctionShortcut<T = unknown> {
|
|
161
|
+
(...args: any[]): FunctionBuilder<T>;
|
|
162
|
+
}
|
|
163
|
+
export interface AggregateFunctionShortcut<T = unknown> {
|
|
164
|
+
(...args: any[]): AggregateFunctionBuilder<T>;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* An aggregate function like `fn.count()` or `fn.sum()`.
|
|
169
|
+
*/
|
|
170
|
+
export interface AggregateFunctionBuilder<T = unknown> extends FunctionBuilder<T> {
|
|
171
|
+
/**
|
|
172
|
+
* Only aggregates the rows that match `condition`.
|
|
173
|
+
* `fn.count().filter(Pet.ref('species').eq('dog'))` is
|
|
174
|
+
* `COUNT(CASE WHEN species = 'dog' THEN 1 END)`. Calling it again adds a
|
|
175
|
+
* condition with `AND`.
|
|
176
|
+
*/
|
|
177
|
+
filter(condition: FnArg): this;
|
|
178
|
+
/**
|
|
179
|
+
* Uses the function as a window function: `fn.sum(ref('amount')).over({ partitionBy: 'ownerId' })`.
|
|
180
|
+
*/
|
|
181
|
+
over(window?: FnWindow): this;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* A window function like `fn.rowNumber()`. It can only be used after `over()`.
|
|
186
|
+
*/
|
|
187
|
+
export interface WindowFunctionBuilder<T = unknown> {
|
|
188
|
+
over(window?: FnWindow): FunctionBuilder<T>;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The window of `over()`. Strings are column names.
|
|
193
|
+
*/
|
|
194
|
+
interface FnWindow {
|
|
195
|
+
partitionBy?: FnArg | readonly FnArg[];
|
|
196
|
+
orderBy?: FnOrderBy | readonly FnOrderBy[];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* `fn.case()` and `fn.case(value)`.
|
|
201
|
+
*/
|
|
202
|
+
export interface CaseBuilder<T = never> extends FunctionBuilder<T | null> {
|
|
203
|
+
/**
|
|
204
|
+
* `WHEN condition THEN result`, or `WHEN value THEN result` for `fn.case(value)`.
|
|
205
|
+
*/
|
|
206
|
+
when<const R>(condition: FnArg, result: R): CaseBuilder<T | FnResult<R>>;
|
|
207
|
+
else<const R>(result: R): FunctionBuilder<T | FnResult<R>>;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The type of the value of an expression.
|
|
212
|
+
*/
|
|
213
|
+
type FnResult<R> =
|
|
214
|
+
R extends FunctionBuilder<infer U>
|
|
215
|
+
? U
|
|
216
|
+
: R extends ReferenceBuilder<infer U>
|
|
217
|
+
? U
|
|
218
|
+
: R extends Raw | ValueBuilder | AnyQueryBuilder
|
|
219
|
+
? unknown
|
|
220
|
+
: R;
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* The value of a condition: a boolean on postgres, 0 or 1 on MySQL and SQLite, and
|
|
224
|
+
* NULL when a compared value is NULL.
|
|
225
|
+
*/
|
|
226
|
+
type SqlBoolean = boolean | 0 | 1 | null;
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* An expression with a known value type.
|
|
230
|
+
*/
|
|
231
|
+
interface Typed {
|
|
232
|
+
'~type'?: unknown;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* A value to compare with a value of type `T`. Dates can also be compared with
|
|
237
|
+
* strings.
|
|
238
|
+
*/
|
|
239
|
+
type CompareValue<T> = unknown extends T
|
|
240
|
+
? FnArg
|
|
241
|
+
: Exclude<T, undefined> | (Date extends T ? string : never) | null | FnExpression;
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Comparison, logic and arithmetic methods of `ref()`, `val()`, `fn()` and `raw()`:
|
|
245
|
+
* `Person.ref('age').gte(18)`, `Pet.ref('order').plus(1)`. The sql of each one is
|
|
246
|
+
* wrapped in parentheses. Values are checked against the type of the expression.
|
|
247
|
+
*/
|
|
248
|
+
interface ExpressionMethods {
|
|
249
|
+
/**
|
|
250
|
+
* The type of the value. Only used by the typings.
|
|
251
|
+
*/
|
|
252
|
+
'~type'?: unknown;
|
|
253
|
+
/**
|
|
254
|
+
* `= value`. `eq(null)` is `IS NULL`.
|
|
255
|
+
*/
|
|
256
|
+
eq<S extends Typed>(this: S, value: CompareValue<S['~type']>): FunctionBuilder<SqlBoolean>;
|
|
257
|
+
/**
|
|
258
|
+
* `<> value`. `ne(null)` is `IS NOT NULL`.
|
|
259
|
+
*/
|
|
260
|
+
ne<S extends Typed>(this: S, value: CompareValue<S['~type']>): FunctionBuilder<SqlBoolean>;
|
|
261
|
+
gt<S extends Typed>(this: S, value: CompareValue<S['~type']>): FunctionBuilder<SqlBoolean>;
|
|
262
|
+
gte<S extends Typed>(this: S, value: CompareValue<S['~type']>): FunctionBuilder<SqlBoolean>;
|
|
263
|
+
lt<S extends Typed>(this: S, value: CompareValue<S['~type']>): FunctionBuilder<SqlBoolean>;
|
|
264
|
+
lte<S extends Typed>(this: S, value: CompareValue<S['~type']>): FunctionBuilder<SqlBoolean>;
|
|
265
|
+
in<S extends Typed>(
|
|
266
|
+
this: S,
|
|
267
|
+
values: readonly CompareValue<S['~type']>[] | AnyQueryBuilder,
|
|
268
|
+
): FunctionBuilder<SqlBoolean>;
|
|
269
|
+
notIn<S extends Typed>(
|
|
270
|
+
this: S,
|
|
271
|
+
values: readonly CompareValue<S['~type']>[] | AnyQueryBuilder,
|
|
272
|
+
): FunctionBuilder<SqlBoolean>;
|
|
273
|
+
like(pattern: string | FnExpression): FunctionBuilder<SqlBoolean>;
|
|
274
|
+
between<S extends Typed>(
|
|
275
|
+
this: S,
|
|
276
|
+
min: CompareValue<S['~type']>,
|
|
277
|
+
max: CompareValue<S['~type']>,
|
|
278
|
+
): FunctionBuilder<SqlBoolean>;
|
|
279
|
+
isNull(): FunctionBuilder<SqlBoolean>;
|
|
280
|
+
isNotNull(): FunctionBuilder<SqlBoolean>;
|
|
281
|
+
/**
|
|
282
|
+
* NULL, an empty string or a string of only spaces.
|
|
283
|
+
*/
|
|
284
|
+
isBlank(): FunctionBuilder<SqlBoolean>;
|
|
285
|
+
isNotBlank(): FunctionBuilder<SqlBoolean>;
|
|
286
|
+
and(...conditions: FnArg[]): FunctionBuilder<SqlBoolean>;
|
|
287
|
+
or(...conditions: FnArg[]): FunctionBuilder<SqlBoolean>;
|
|
288
|
+
not(): FunctionBuilder<SqlBoolean>;
|
|
289
|
+
plus(value: FnArg): FunctionBuilder<FnNumeric | null>;
|
|
290
|
+
minus(value: FnArg): FunctionBuilder<FnNumeric | null>;
|
|
291
|
+
times(value: FnArg): FunctionBuilder<FnNumeric | null>;
|
|
292
|
+
div(value: FnArg): FunctionBuilder<FnNumeric | null>;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* A number that the database driver may return as a string, like `DECIMAL` and
|
|
297
|
+
* `BIGINT` values with mysql2's and pg's default settings.
|
|
298
|
+
*/
|
|
299
|
+
type FnNumeric = number | string;
|
|
300
|
+
/**
|
|
301
|
+
* An argument of an `fn` helper. Strings are values; use `ref()` for columns.
|
|
302
|
+
*/
|
|
303
|
+
type FnArg =
|
|
304
|
+
| ReferenceBuilder
|
|
305
|
+
| Raw
|
|
306
|
+
| FunctionBuilder<any>
|
|
307
|
+
| ValueBuilder
|
|
308
|
+
| AnyQueryBuilder
|
|
309
|
+
| PrimitiveValue;
|
|
310
|
+
/**
|
|
311
|
+
* A JSON document argument. Objects and arrays are cast to JSON.
|
|
312
|
+
*/
|
|
313
|
+
type FnJsonArg = FnArg | object;
|
|
314
|
+
/**
|
|
315
|
+
* An `fn` argument that isn't a plain value.
|
|
316
|
+
*/
|
|
317
|
+
type FnExpression = Exclude<FnArg, PrimitiveValue>;
|
|
318
|
+
type FnOrderBy = FnArg | [FnArg, 'asc' | 'desc' | 'ASC' | 'DESC'];
|
|
319
|
+
|
|
320
|
+
// <generated:mysql-function-types>
|
|
321
|
+
type FnIntervalUnit =
|
|
322
|
+
| 'MICROSECOND'
|
|
323
|
+
| 'microsecond'
|
|
324
|
+
| 'SECOND'
|
|
325
|
+
| 'second'
|
|
326
|
+
| 'MINUTE'
|
|
327
|
+
| 'minute'
|
|
328
|
+
| 'HOUR'
|
|
329
|
+
| 'hour'
|
|
330
|
+
| 'DAY'
|
|
331
|
+
| 'day'
|
|
332
|
+
| 'WEEK'
|
|
333
|
+
| 'week'
|
|
334
|
+
| 'MONTH'
|
|
335
|
+
| 'month'
|
|
336
|
+
| 'QUARTER'
|
|
337
|
+
| 'quarter'
|
|
338
|
+
| 'YEAR'
|
|
339
|
+
| 'year'
|
|
340
|
+
| 'SECOND_MICROSECOND'
|
|
341
|
+
| 'second_microsecond'
|
|
342
|
+
| 'MINUTE_MICROSECOND'
|
|
343
|
+
| 'minute_microsecond'
|
|
344
|
+
| 'MINUTE_SECOND'
|
|
345
|
+
| 'minute_second'
|
|
346
|
+
| 'HOUR_MICROSECOND'
|
|
347
|
+
| 'hour_microsecond'
|
|
348
|
+
| 'HOUR_SECOND'
|
|
349
|
+
| 'hour_second'
|
|
350
|
+
| 'HOUR_MINUTE'
|
|
351
|
+
| 'hour_minute'
|
|
352
|
+
| 'DAY_MICROSECOND'
|
|
353
|
+
| 'day_microsecond'
|
|
354
|
+
| 'DAY_SECOND'
|
|
355
|
+
| 'day_second'
|
|
356
|
+
| 'DAY_MINUTE'
|
|
357
|
+
| 'day_minute'
|
|
358
|
+
| 'DAY_HOUR'
|
|
359
|
+
| 'day_hour'
|
|
360
|
+
| 'YEAR_MONTH'
|
|
361
|
+
| 'year_month';
|
|
362
|
+
type FnTimestampUnit =
|
|
363
|
+
| 'MICROSECOND'
|
|
364
|
+
| 'microsecond'
|
|
365
|
+
| 'SECOND'
|
|
366
|
+
| 'second'
|
|
367
|
+
| 'MINUTE'
|
|
368
|
+
| 'minute'
|
|
369
|
+
| 'HOUR'
|
|
370
|
+
| 'hour'
|
|
371
|
+
| 'DAY'
|
|
372
|
+
| 'day'
|
|
373
|
+
| 'WEEK'
|
|
374
|
+
| 'week'
|
|
375
|
+
| 'MONTH'
|
|
376
|
+
| 'month'
|
|
377
|
+
| 'QUARTER'
|
|
378
|
+
| 'quarter'
|
|
379
|
+
| 'YEAR'
|
|
380
|
+
| 'year';
|
|
381
|
+
// </generated:mysql-function-types>
|
|
382
|
+
|
|
383
|
+
export interface FunctionFunction {
|
|
384
|
+
(functionName: string, ...arguments: any[]): FunctionBuilder;
|
|
385
|
+
|
|
386
|
+
now(precision: number): FunctionBuilder<Date | string>;
|
|
387
|
+
now(): FunctionBuilder<Date | string>;
|
|
388
|
+
|
|
389
|
+
coalesce: SqlFunctionShortcut;
|
|
390
|
+
concat: SqlFunctionShortcut<string | null>;
|
|
391
|
+
sum: AggregateFunctionShortcut<FnNumeric | null>;
|
|
392
|
+
avg: AggregateFunctionShortcut<FnNumeric | null>;
|
|
393
|
+
min: AggregateFunctionShortcut;
|
|
394
|
+
max: AggregateFunctionShortcut;
|
|
395
|
+
/**
|
|
396
|
+
* `fn.count()` and `fn.count('*')` are `COUNT(*)`.
|
|
397
|
+
*/
|
|
398
|
+
count: AggregateFunctionShortcut<FnNumeric>;
|
|
399
|
+
upper: SqlFunctionShortcut<string | null>;
|
|
400
|
+
lower: SqlFunctionShortcut<string | null>;
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* `fn.case().when(condition, result).else(result)` is `CASE WHEN ... END`.
|
|
404
|
+
*/
|
|
405
|
+
case(): CaseBuilder;
|
|
406
|
+
/**
|
|
407
|
+
* `fn.case(value).when(other, result).else(result)` is `CASE value WHEN ... END`.
|
|
408
|
+
*/
|
|
409
|
+
case(value: FnArg): CaseBuilder;
|
|
410
|
+
/**
|
|
411
|
+
* `EXISTS (subquery)`.
|
|
412
|
+
*/
|
|
413
|
+
exists(subquery: AnyQueryBuilder): FunctionBuilder<SqlBoolean>;
|
|
414
|
+
/**
|
|
415
|
+
* The value an upsert tried to insert, for `onConflict().merge({ ... })`:
|
|
416
|
+
* `VALUES(column)` on MySQL, `excluded.column` on postgres and SQLite.
|
|
417
|
+
*/
|
|
418
|
+
excluded(column: string | ReferenceBuilder): FunctionBuilder;
|
|
419
|
+
|
|
420
|
+
// The MySQL built-in functions. Generated from lib/queryBuilder/functions/mysql.js
|
|
421
|
+
// by scripts/generate-fn-typings.js.
|
|
422
|
+
// <generated:mysql-functions>
|
|
423
|
+
/** `ASCII()` (portable) */
|
|
424
|
+
ascii(str: FnArg): FunctionBuilder<number | null>;
|
|
425
|
+
/** `BIN()` (MySQL) */
|
|
426
|
+
bin(n: FnArg): FunctionBuilder<string | null>;
|
|
427
|
+
/** `BIT_LENGTH()` (portable) */
|
|
428
|
+
bitLength(str: FnArg): FunctionBuilder<number | null>;
|
|
429
|
+
/** `CHAR()` (MySQL) */
|
|
430
|
+
char(codes: FnArg | FnArg[], options?: { using?: string }): FunctionBuilder<Buffer | null>;
|
|
431
|
+
/** `CHAR_LENGTH()` (portable) */
|
|
432
|
+
charLength(str: FnArg): FunctionBuilder<number | null>;
|
|
433
|
+
/** `CHARACTER_LENGTH()` (portable) */
|
|
434
|
+
characterLength(str: FnArg): FunctionBuilder<number | null>;
|
|
435
|
+
/** `CONCAT_WS()` (portable) */
|
|
436
|
+
concatWs(separator: FnArg, ...strs: FnArg[]): FunctionBuilder<string | null>;
|
|
437
|
+
/** `ELT()` (MySQL) */
|
|
438
|
+
elt(n: FnArg, ...strs: FnArg[]): FunctionBuilder<string | null>;
|
|
439
|
+
/** `EXPORT_SET()` (MySQL) */
|
|
440
|
+
exportSet(
|
|
441
|
+
bits: FnArg,
|
|
442
|
+
on: FnArg,
|
|
443
|
+
off: FnArg,
|
|
444
|
+
separator?: FnArg,
|
|
445
|
+
numberOfBits?: FnArg,
|
|
446
|
+
): FunctionBuilder<string | null>;
|
|
447
|
+
/** `FIELD()` (MySQL) */
|
|
448
|
+
field(str: FnArg, ...strs: FnArg[]): FunctionBuilder<number | null>;
|
|
449
|
+
/** `FIND_IN_SET()` (MySQL) */
|
|
450
|
+
findInSet(str: FnArg, strList: FnArg): FunctionBuilder<number | null>;
|
|
451
|
+
/** `FORMAT()` (MySQL) */
|
|
452
|
+
format(x: FnArg, decimals: FnArg, locale?: FnArg): FunctionBuilder<string | null>;
|
|
453
|
+
/** `FROM_BASE64()` (MySQL) */
|
|
454
|
+
fromBase64(str: FnArg): FunctionBuilder<Buffer | null>;
|
|
455
|
+
/** `HEX()` (MySQL) */
|
|
456
|
+
hex(x: FnArg): FunctionBuilder<string | null>;
|
|
457
|
+
/** `INSERT()` (MySQL) */
|
|
458
|
+
insert(str: FnArg, pos: FnArg, len: FnArg, newStr: FnArg): FunctionBuilder<string | null>;
|
|
459
|
+
/** `INSTR()` (MySQL) */
|
|
460
|
+
instr(str: FnArg, substr: FnArg): FunctionBuilder<number | null>;
|
|
461
|
+
/** `LCASE()` (MySQL) */
|
|
462
|
+
lcase(str: FnArg): FunctionBuilder<string | null>;
|
|
463
|
+
/** `LEFT()` (portable) */
|
|
464
|
+
left(str: FnArg, len: FnArg): FunctionBuilder<string | null>;
|
|
465
|
+
/** `LENGTH()` (portable) */
|
|
466
|
+
length(str: FnArg): FunctionBuilder<number | null>;
|
|
467
|
+
/** `LOCATE()` (MySQL) */
|
|
468
|
+
locate(substr: FnArg, str: FnArg, pos?: FnArg): FunctionBuilder<number | null>;
|
|
469
|
+
/** `LPAD()` (portable) */
|
|
470
|
+
lpad(str: FnArg, len: FnArg, padStr: FnArg): FunctionBuilder<string | null>;
|
|
471
|
+
/** `LTRIM()` (portable) */
|
|
472
|
+
ltrim(str: FnArg): FunctionBuilder<string | null>;
|
|
473
|
+
/** `MAKE_SET()` (MySQL) */
|
|
474
|
+
makeSet(bits: FnArg, ...strs: FnArg[]): FunctionBuilder<string | null>;
|
|
475
|
+
/** `MID()` (MySQL) */
|
|
476
|
+
mid(str: FnArg, pos: FnArg, len?: FnArg): FunctionBuilder<string | null>;
|
|
477
|
+
/** `OCT()` (MySQL) */
|
|
478
|
+
oct(n: FnArg): FunctionBuilder<string | null>;
|
|
479
|
+
/** `OCTET_LENGTH()` (portable) */
|
|
480
|
+
octetLength(str: FnArg): FunctionBuilder<number | null>;
|
|
481
|
+
/** `ORD()` (MySQL) */
|
|
482
|
+
ord(str: FnArg): FunctionBuilder<number | null>;
|
|
483
|
+
/** `POSITION()` (portable) */
|
|
484
|
+
position(substr: FnArg, str: FnArg): FunctionBuilder<number | null>;
|
|
485
|
+
/** `QUOTE()` (MySQL) */
|
|
486
|
+
quote(str: FnArg): FunctionBuilder<string | null>;
|
|
487
|
+
/** `REGEXP_INSTR()` (MySQL) */
|
|
488
|
+
regexpInstr(
|
|
489
|
+
expr: FnArg,
|
|
490
|
+
pattern: FnArg,
|
|
491
|
+
pos?: FnArg,
|
|
492
|
+
occurrence?: FnArg,
|
|
493
|
+
returnOption?: FnArg,
|
|
494
|
+
matchType?: FnArg,
|
|
495
|
+
): FunctionBuilder<number | null>;
|
|
496
|
+
/** `REGEXP_LIKE()` (MySQL) */
|
|
497
|
+
regexpLike(expr: FnArg, pattern: FnArg, matchType?: FnArg): FunctionBuilder<0 | 1 | null>;
|
|
498
|
+
/** `REGEXP_REPLACE()` (MySQL) */
|
|
499
|
+
regexpReplace(
|
|
500
|
+
expr: FnArg,
|
|
501
|
+
pattern: FnArg,
|
|
502
|
+
replacement: FnArg,
|
|
503
|
+
pos?: FnArg,
|
|
504
|
+
occurrence?: FnArg,
|
|
505
|
+
matchType?: FnArg,
|
|
506
|
+
): FunctionBuilder<string | null>;
|
|
507
|
+
/** `REGEXP_SUBSTR()` (MySQL) */
|
|
508
|
+
regexpSubstr(
|
|
509
|
+
expr: FnArg,
|
|
510
|
+
pattern: FnArg,
|
|
511
|
+
pos?: FnArg,
|
|
512
|
+
occurrence?: FnArg,
|
|
513
|
+
matchType?: FnArg,
|
|
514
|
+
): FunctionBuilder<string | null>;
|
|
515
|
+
/** `REPEAT()` (portable) */
|
|
516
|
+
repeat(str: FnArg, count: FnArg): FunctionBuilder<string | null>;
|
|
517
|
+
/** `REPLACE()` (portable) */
|
|
518
|
+
replace(str: FnArg, from: FnArg, to: FnArg): FunctionBuilder<string | null>;
|
|
519
|
+
/** `REVERSE()` (portable) */
|
|
520
|
+
reverse(str: FnArg): FunctionBuilder<string | null>;
|
|
521
|
+
/** `RIGHT()` (portable) */
|
|
522
|
+
right(str: FnArg, len: FnArg): FunctionBuilder<string | null>;
|
|
523
|
+
/** `RPAD()` (portable) */
|
|
524
|
+
rpad(str: FnArg, len: FnArg, padStr: FnArg): FunctionBuilder<string | null>;
|
|
525
|
+
/** `RTRIM()` (portable) */
|
|
526
|
+
rtrim(str: FnArg): FunctionBuilder<string | null>;
|
|
527
|
+
/** `SOUNDEX()` (MySQL) */
|
|
528
|
+
soundex(str: FnArg): FunctionBuilder<string | null>;
|
|
529
|
+
/** `SPACE()` (MySQL) */
|
|
530
|
+
space(n: FnArg): FunctionBuilder<string | null>;
|
|
531
|
+
/** `STRCMP()` (MySQL) */
|
|
532
|
+
strcmp(a: FnArg, b: FnArg): FunctionBuilder<-1 | 0 | 1 | null>;
|
|
533
|
+
/** `SUBSTR()` (portable) */
|
|
534
|
+
substr(str: FnArg, pos: FnArg, len?: FnArg): FunctionBuilder<string | null>;
|
|
535
|
+
/** `SUBSTRING()` (portable) */
|
|
536
|
+
substring(str: FnArg, pos: FnArg, len?: FnArg): FunctionBuilder<string | null>;
|
|
537
|
+
/** `SUBSTRING_INDEX()` (MySQL) */
|
|
538
|
+
substringIndex(str: FnArg, delimiter: FnArg, count: FnArg): FunctionBuilder<string | null>;
|
|
539
|
+
/** `TO_BASE64()` (MySQL) */
|
|
540
|
+
toBase64(str: FnArg): FunctionBuilder<string | null>;
|
|
541
|
+
/** `TRIM()` (portable) */
|
|
542
|
+
trim(
|
|
543
|
+
str: FnArg,
|
|
544
|
+
options?: { side?: 'both' | 'leading' | 'trailing'; remove?: FnArg },
|
|
545
|
+
): FunctionBuilder<string | null>;
|
|
546
|
+
/** `UCASE()` (MySQL) */
|
|
547
|
+
ucase(str: FnArg): FunctionBuilder<string | null>;
|
|
548
|
+
/** `UNHEX()` (MySQL) */
|
|
549
|
+
unhex(str: FnArg): FunctionBuilder<Buffer | null>;
|
|
550
|
+
/** `WEIGHT_STRING()` (MySQL) */
|
|
551
|
+
weightString(str: FnArg): FunctionBuilder<Buffer | null>;
|
|
552
|
+
/** `MATCH()` (MySQL) */
|
|
553
|
+
match(
|
|
554
|
+
columns: FnArg | FnArg[],
|
|
555
|
+
search: FnArg,
|
|
556
|
+
options?: {
|
|
557
|
+
mode?: 'natural' | 'natural with query expansion' | 'boolean' | 'with query expansion';
|
|
558
|
+
},
|
|
559
|
+
): FunctionBuilder<number | null>;
|
|
560
|
+
/** `ABS()` (portable) */
|
|
561
|
+
abs(x: FnArg): FunctionBuilder<FnNumeric | null>;
|
|
562
|
+
/** `ACOS()` (portable) */
|
|
563
|
+
acos(x: FnArg): FunctionBuilder<number | null>;
|
|
564
|
+
/** `ASIN()` (portable) */
|
|
565
|
+
asin(x: FnArg): FunctionBuilder<number | null>;
|
|
566
|
+
/** `ATAN()` (MySQL) */
|
|
567
|
+
atan(x: FnArg, y?: FnArg): FunctionBuilder<number | null>;
|
|
568
|
+
/** `ATAN2()` (portable) */
|
|
569
|
+
atan2(y: FnArg, x: FnArg): FunctionBuilder<number | null>;
|
|
570
|
+
/** `CEIL()` (portable) */
|
|
571
|
+
ceil(x: FnArg): FunctionBuilder<FnNumeric | null>;
|
|
572
|
+
/** `CEILING()` (portable) */
|
|
573
|
+
ceiling(x: FnArg): FunctionBuilder<FnNumeric | null>;
|
|
574
|
+
/** `CONV()` (MySQL) */
|
|
575
|
+
conv(n: FnArg, fromBase: FnArg, toBase: FnArg): FunctionBuilder<string | null>;
|
|
576
|
+
/** `COS()` (portable) */
|
|
577
|
+
cos(x: FnArg): FunctionBuilder<number | null>;
|
|
578
|
+
/** `COT()` (portable) */
|
|
579
|
+
cot(x: FnArg): FunctionBuilder<number | null>;
|
|
580
|
+
/** `CRC32()` (MySQL) */
|
|
581
|
+
crc32(x: FnArg): FunctionBuilder<number | null>;
|
|
582
|
+
/** `DEGREES()` (portable) */
|
|
583
|
+
degrees(x: FnArg): FunctionBuilder<number | null>;
|
|
584
|
+
/** `EXP()` (portable) */
|
|
585
|
+
exp(x: FnArg): FunctionBuilder<number | null>;
|
|
586
|
+
/** `FLOOR()` (portable) */
|
|
587
|
+
floor(x: FnArg): FunctionBuilder<FnNumeric | null>;
|
|
588
|
+
/** `LN()` (portable) */
|
|
589
|
+
ln(x: FnArg): FunctionBuilder<number | null>;
|
|
590
|
+
/** `LOG()` (MySQL) */
|
|
591
|
+
log(baseOrX: FnArg, x?: FnArg): FunctionBuilder<number | null>;
|
|
592
|
+
/** `LOG10()` (MySQL) */
|
|
593
|
+
log10(x: FnArg): FunctionBuilder<number | null>;
|
|
594
|
+
/** `LOG2()` (MySQL) */
|
|
595
|
+
log2(x: FnArg): FunctionBuilder<number | null>;
|
|
596
|
+
/** `MOD()` (portable) */
|
|
597
|
+
mod(n: FnArg, m: FnArg): FunctionBuilder<FnNumeric | null>;
|
|
598
|
+
/** `PI()` (portable) */
|
|
599
|
+
pi(): FunctionBuilder<number | null>;
|
|
600
|
+
/** `POW()` (MySQL) */
|
|
601
|
+
pow(x: FnArg, y: FnArg): FunctionBuilder<number | null>;
|
|
602
|
+
/** `POWER()` (portable) */
|
|
603
|
+
power(x: FnArg, y: FnArg): FunctionBuilder<number | null>;
|
|
604
|
+
/** `RADIANS()` (portable) */
|
|
605
|
+
radians(x: FnArg): FunctionBuilder<number | null>;
|
|
606
|
+
/** `RAND()` (MySQL) */
|
|
607
|
+
rand(seed?: FnArg): FunctionBuilder<number | null>;
|
|
608
|
+
/** `ROUND()` (portable) */
|
|
609
|
+
round(x: FnArg, decimals?: FnArg): FunctionBuilder<FnNumeric | null>;
|
|
610
|
+
/** `SIGN()` (portable) */
|
|
611
|
+
sign(x: FnArg): FunctionBuilder<number | null>;
|
|
612
|
+
/** `SIN()` (portable) */
|
|
613
|
+
sin(x: FnArg): FunctionBuilder<number | null>;
|
|
614
|
+
/** `SQRT()` (portable) */
|
|
615
|
+
sqrt(x: FnArg): FunctionBuilder<number | null>;
|
|
616
|
+
/** `TAN()` (portable) */
|
|
617
|
+
tan(x: FnArg): FunctionBuilder<number | null>;
|
|
618
|
+
/** `TRUNCATE()` (MySQL) */
|
|
619
|
+
truncate(x: FnArg, decimals: FnArg): FunctionBuilder<FnNumeric | null>;
|
|
620
|
+
/** `ADDDATE()` (MySQL) */
|
|
621
|
+
addDate(
|
|
622
|
+
date: FnArg,
|
|
623
|
+
amount: FnArg,
|
|
624
|
+
unit?: FnIntervalUnit,
|
|
625
|
+
): FunctionBuilder<Date | string | null>;
|
|
626
|
+
/** `ADDTIME()` (MySQL) */
|
|
627
|
+
addTime(datetime: FnArg, time: FnArg): FunctionBuilder<Date | string | null>;
|
|
628
|
+
/** `CONVERT_TZ()` (MySQL) */
|
|
629
|
+
convertTz(datetime: FnArg, fromTz: FnArg, toTz: FnArg): FunctionBuilder<Date | string | null>;
|
|
630
|
+
/** `CURDATE()` (MySQL) */
|
|
631
|
+
curDate(): FunctionBuilder<Date | string | null>;
|
|
632
|
+
/** `CURRENT_DATE()` (MySQL) */
|
|
633
|
+
currentDate(): FunctionBuilder<Date | string | null>;
|
|
634
|
+
/** `CURRENT_TIME()` (MySQL) */
|
|
635
|
+
currentTime(fsp?: number): FunctionBuilder<string | null>;
|
|
636
|
+
/** `CURRENT_TIMESTAMP()` (MySQL) */
|
|
637
|
+
currentTimestamp(fsp?: number): FunctionBuilder<Date | string | null>;
|
|
638
|
+
/** `CURTIME()` (MySQL) */
|
|
639
|
+
curTime(fsp?: number): FunctionBuilder<string | null>;
|
|
640
|
+
/** `DATE()` (MySQL) */
|
|
641
|
+
date(x: FnArg): FunctionBuilder<Date | string | null>;
|
|
642
|
+
/** `DATE_ADD()` (MySQL) */
|
|
643
|
+
dateAdd(
|
|
644
|
+
date: FnArg,
|
|
645
|
+
amount: FnArg,
|
|
646
|
+
unit: FnIntervalUnit,
|
|
647
|
+
): FunctionBuilder<Date | string | null>;
|
|
648
|
+
/** `DATEDIFF()` (MySQL) */
|
|
649
|
+
dateDiff(a: FnArg, b: FnArg): FunctionBuilder<number | null>;
|
|
650
|
+
/** `DATE_FORMAT()` (MySQL) */
|
|
651
|
+
dateFormat(date: FnArg, format: FnArg): FunctionBuilder<string | null>;
|
|
652
|
+
/** `DATE_SUB()` (MySQL) */
|
|
653
|
+
dateSub(
|
|
654
|
+
date: FnArg,
|
|
655
|
+
amount: FnArg,
|
|
656
|
+
unit: FnIntervalUnit,
|
|
657
|
+
): FunctionBuilder<Date | string | null>;
|
|
658
|
+
/** `DAY()` (MySQL) */
|
|
659
|
+
day(date: FnArg): FunctionBuilder<number | null>;
|
|
660
|
+
/** `DAYNAME()` (MySQL) */
|
|
661
|
+
dayName(date: FnArg): FunctionBuilder<string | null>;
|
|
662
|
+
/** `DAYOFMONTH()` (MySQL) */
|
|
663
|
+
dayOfMonth(date: FnArg): FunctionBuilder<number | null>;
|
|
664
|
+
/** `DAYOFWEEK()` (MySQL) */
|
|
665
|
+
dayOfWeek(date: FnArg): FunctionBuilder<number | null>;
|
|
666
|
+
/** `DAYOFYEAR()` (MySQL) */
|
|
667
|
+
dayOfYear(date: FnArg): FunctionBuilder<number | null>;
|
|
668
|
+
/** `EXTRACT()` (MySQL) */
|
|
669
|
+
extract(unit: FnIntervalUnit, date: FnArg): FunctionBuilder<number | null>;
|
|
670
|
+
/** `FROM_DAYS()` (MySQL) */
|
|
671
|
+
fromDays(days: FnArg): FunctionBuilder<Date | string | null>;
|
|
672
|
+
/** `FROM_UNIXTIME()` (MySQL) */
|
|
673
|
+
fromUnixTime(timestamp: FnArg, format?: FnArg): FunctionBuilder<Date | string | null>;
|
|
674
|
+
/** `GET_FORMAT()` (MySQL) */
|
|
675
|
+
getFormat(
|
|
676
|
+
type: 'DATE' | 'TIME' | 'DATETIME' | 'TIMESTAMP',
|
|
677
|
+
locale: FnArg,
|
|
678
|
+
): FunctionBuilder<string | null>;
|
|
679
|
+
/** `HOUR()` (MySQL) */
|
|
680
|
+
hour(time: FnArg): FunctionBuilder<number | null>;
|
|
681
|
+
/** `LAST_DAY()` (MySQL) */
|
|
682
|
+
lastDay(date: FnArg): FunctionBuilder<Date | string | null>;
|
|
683
|
+
/** `LOCALTIME()` (MySQL) */
|
|
684
|
+
localTime(fsp?: number): FunctionBuilder<Date | string | null>;
|
|
685
|
+
/** `LOCALTIMESTAMP()` (MySQL) */
|
|
686
|
+
localTimestamp(fsp?: number): FunctionBuilder<Date | string | null>;
|
|
687
|
+
/** `MAKEDATE()` (MySQL) */
|
|
688
|
+
makeDate(year: FnArg, dayOfYear: FnArg): FunctionBuilder<Date | string | null>;
|
|
689
|
+
/** `MAKETIME()` (MySQL) */
|
|
690
|
+
makeTime(hour: FnArg, minute: FnArg, second: FnArg): FunctionBuilder<string | null>;
|
|
691
|
+
/** `MICROSECOND()` (MySQL) */
|
|
692
|
+
microsecond(time: FnArg): FunctionBuilder<number | null>;
|
|
693
|
+
/** `MINUTE()` (MySQL) */
|
|
694
|
+
minute(time: FnArg): FunctionBuilder<number | null>;
|
|
695
|
+
/** `MONTH()` (MySQL) */
|
|
696
|
+
month(date: FnArg): FunctionBuilder<number | null>;
|
|
697
|
+
/** `MONTHNAME()` (MySQL) */
|
|
698
|
+
monthName(date: FnArg): FunctionBuilder<string | null>;
|
|
699
|
+
/** `PERIOD_ADD()` (MySQL) */
|
|
700
|
+
periodAdd(period: FnArg, months: FnArg): FunctionBuilder<number | null>;
|
|
701
|
+
/** `PERIOD_DIFF()` (MySQL) */
|
|
702
|
+
periodDiff(a: FnArg, b: FnArg): FunctionBuilder<number | null>;
|
|
703
|
+
/** `QUARTER()` (MySQL) */
|
|
704
|
+
quarter(date: FnArg): FunctionBuilder<number | null>;
|
|
705
|
+
/** `SECOND()` (MySQL) */
|
|
706
|
+
second(time: FnArg): FunctionBuilder<number | null>;
|
|
707
|
+
/** `SEC_TO_TIME()` (MySQL) */
|
|
708
|
+
secToTime(seconds: FnArg): FunctionBuilder<string | null>;
|
|
709
|
+
/** `STR_TO_DATE()` (MySQL) */
|
|
710
|
+
strToDate(str: FnArg, format: FnArg): FunctionBuilder<Date | string | null>;
|
|
711
|
+
/** `SUBDATE()` (MySQL) */
|
|
712
|
+
subDate(
|
|
713
|
+
date: FnArg,
|
|
714
|
+
amount: FnArg,
|
|
715
|
+
unit?: FnIntervalUnit,
|
|
716
|
+
): FunctionBuilder<Date | string | null>;
|
|
717
|
+
/** `SUBTIME()` (MySQL) */
|
|
718
|
+
subTime(datetime: FnArg, time: FnArg): FunctionBuilder<Date | string | null>;
|
|
719
|
+
/** `SYSDATE()` (MySQL) */
|
|
720
|
+
sysdate(fsp?: number): FunctionBuilder<Date | string | null>;
|
|
721
|
+
/** `TIME()` (MySQL) */
|
|
722
|
+
time(x: FnArg): FunctionBuilder<string | null>;
|
|
723
|
+
/** `TIMEDIFF()` (MySQL) */
|
|
724
|
+
timeDiff(a: FnArg, b: FnArg): FunctionBuilder<string | null>;
|
|
725
|
+
/** `TIMESTAMP()` (MySQL) */
|
|
726
|
+
timestamp(x: FnArg, time?: FnArg): FunctionBuilder<Date | string | null>;
|
|
727
|
+
/** `TIMESTAMPADD()` (MySQL) */
|
|
728
|
+
timestampAdd(
|
|
729
|
+
unit: FnTimestampUnit,
|
|
730
|
+
amount: FnArg,
|
|
731
|
+
date: FnArg,
|
|
732
|
+
): FunctionBuilder<Date | string | null>;
|
|
733
|
+
/** `TIMESTAMPDIFF()` (MySQL) */
|
|
734
|
+
timestampDiff(unit: FnTimestampUnit, from: FnArg, to: FnArg): FunctionBuilder<number | null>;
|
|
735
|
+
/** `TIME_FORMAT()` (MySQL) */
|
|
736
|
+
timeFormat(time: FnArg, format: FnArg): FunctionBuilder<string | null>;
|
|
737
|
+
/** `TIME_TO_SEC()` (MySQL) */
|
|
738
|
+
timeToSec(time: FnArg): FunctionBuilder<number | null>;
|
|
739
|
+
/** `TO_DAYS()` (MySQL) */
|
|
740
|
+
toDays(date: FnArg): FunctionBuilder<number | null>;
|
|
741
|
+
/** `TO_SECONDS()` (MySQL) */
|
|
742
|
+
toSeconds(date: FnArg): FunctionBuilder<number | null>;
|
|
743
|
+
/** `UNIX_TIMESTAMP()` (MySQL) */
|
|
744
|
+
unixTimestamp(date?: FnArg): FunctionBuilder<FnNumeric | null>;
|
|
745
|
+
/** `UTC_DATE()` (MySQL) */
|
|
746
|
+
utcDate(): FunctionBuilder<Date | string | null>;
|
|
747
|
+
/** `UTC_TIME()` (MySQL) */
|
|
748
|
+
utcTime(fsp?: number): FunctionBuilder<string | null>;
|
|
749
|
+
/** `UTC_TIMESTAMP()` (MySQL) */
|
|
750
|
+
utcTimestamp(fsp?: number): FunctionBuilder<Date | string | null>;
|
|
751
|
+
/** `WEEK()` (MySQL) */
|
|
752
|
+
week(date: FnArg, mode?: FnArg): FunctionBuilder<number | null>;
|
|
753
|
+
/** `WEEKDAY()` (MySQL) */
|
|
754
|
+
weekday(date: FnArg): FunctionBuilder<number | null>;
|
|
755
|
+
/** `WEEKOFYEAR()` (MySQL) */
|
|
756
|
+
weekOfYear(date: FnArg): FunctionBuilder<number | null>;
|
|
757
|
+
/** `YEAR()` (MySQL) */
|
|
758
|
+
year(date: FnArg): FunctionBuilder<number | null>;
|
|
759
|
+
/** `YEARWEEK()` (MySQL) */
|
|
760
|
+
yearWeek(date: FnArg, mode?: FnArg): FunctionBuilder<number | null>;
|
|
761
|
+
/** `BIT_AND()` (MySQL, aggregate) */
|
|
762
|
+
bitAnd(x: FnArg): AggregateFunctionBuilder<FnNumeric | null>;
|
|
763
|
+
/** `BIT_OR()` (MySQL, aggregate) */
|
|
764
|
+
bitOr(x: FnArg): AggregateFunctionBuilder<FnNumeric | null>;
|
|
765
|
+
/** `BIT_XOR()` (MySQL, aggregate) */
|
|
766
|
+
bitXor(x: FnArg): AggregateFunctionBuilder<FnNumeric | null>;
|
|
767
|
+
/** `COUNT()` (portable, aggregate) */
|
|
768
|
+
countDistinct(...exprs: FnArg[]): AggregateFunctionBuilder<number>;
|
|
769
|
+
/** `GROUP_CONCAT()` (MySQL, aggregate) */
|
|
770
|
+
groupConcat(
|
|
771
|
+
exprs: FnArg | FnArg[],
|
|
772
|
+
options?: { distinct?: boolean; orderBy?: FnOrderBy | FnOrderBy[]; separator?: string },
|
|
773
|
+
): AggregateFunctionBuilder<string | null>;
|
|
774
|
+
/** `JSON_ARRAYAGG()` (MySQL, aggregate) */
|
|
775
|
+
jsonArrayAgg(x: FnArg): AggregateFunctionBuilder<unknown[] | null>;
|
|
776
|
+
/** `JSON_OBJECTAGG()` (MySQL) */
|
|
777
|
+
jsonObjectAgg(key: FnArg, value: FnArg): FunctionBuilder<Record<string, unknown> | null>;
|
|
778
|
+
/** `STD()` (MySQL, aggregate) */
|
|
779
|
+
std(x: FnArg): AggregateFunctionBuilder<number | null>;
|
|
780
|
+
/** `STDDEV()` (portable, aggregate) */
|
|
781
|
+
stddev(x: FnArg): AggregateFunctionBuilder<number | null>;
|
|
782
|
+
/** `STDDEV_POP()` (portable, aggregate) */
|
|
783
|
+
stddevPop(x: FnArg): AggregateFunctionBuilder<number | null>;
|
|
784
|
+
/** `STDDEV_SAMP()` (portable, aggregate) */
|
|
785
|
+
stddevSamp(x: FnArg): AggregateFunctionBuilder<number | null>;
|
|
786
|
+
/** `VARIANCE()` (portable, aggregate) */
|
|
787
|
+
variance(x: FnArg): AggregateFunctionBuilder<number | null>;
|
|
788
|
+
/** `VAR_POP()` (portable, aggregate) */
|
|
789
|
+
varPop(x: FnArg): AggregateFunctionBuilder<number | null>;
|
|
790
|
+
/** `VAR_SAMP()` (portable, aggregate) */
|
|
791
|
+
varSamp(x: FnArg): AggregateFunctionBuilder<number | null>;
|
|
792
|
+
/** `ROW_NUMBER()` (portable, window function) */
|
|
793
|
+
rowNumber(): WindowFunctionBuilder<number>;
|
|
794
|
+
/** `RANK()` (portable, window function) */
|
|
795
|
+
rank(): WindowFunctionBuilder<number>;
|
|
796
|
+
/** `DENSE_RANK()` (portable, window function) */
|
|
797
|
+
denseRank(): WindowFunctionBuilder<number>;
|
|
798
|
+
/** `PERCENT_RANK()` (portable, window function) */
|
|
799
|
+
percentRank(): WindowFunctionBuilder<number>;
|
|
800
|
+
/** `CUME_DIST()` (portable, window function) */
|
|
801
|
+
cumeDist(): WindowFunctionBuilder<number>;
|
|
802
|
+
/** `NTILE()` (portable, window function) */
|
|
803
|
+
ntile(buckets: number): WindowFunctionBuilder<number>;
|
|
804
|
+
/** `LAG()` (portable, window function) */
|
|
805
|
+
lag(x: FnArg, offset?: number, defaultValue?: FnArg): WindowFunctionBuilder<unknown>;
|
|
806
|
+
/** `LEAD()` (portable, window function) */
|
|
807
|
+
lead(x: FnArg, offset?: number, defaultValue?: FnArg): WindowFunctionBuilder<unknown>;
|
|
808
|
+
/** `FIRST_VALUE()` (portable, window function) */
|
|
809
|
+
firstValue(x: FnArg): WindowFunctionBuilder<unknown>;
|
|
810
|
+
/** `LAST_VALUE()` (portable, window function) */
|
|
811
|
+
lastValue(x: FnArg): WindowFunctionBuilder<unknown>;
|
|
812
|
+
/** `NTH_VALUE()` (portable, window function) */
|
|
813
|
+
nthValue(x: FnArg, n: number): WindowFunctionBuilder<unknown>;
|
|
814
|
+
/** `GREATEST()` (portable) */
|
|
815
|
+
greatest(...values: FnArg[]): FunctionBuilder<unknown>;
|
|
816
|
+
/** `IF()` (MySQL) */
|
|
817
|
+
if(condition: FnArg, then: FnArg, otherwise: FnArg): FunctionBuilder<unknown>;
|
|
818
|
+
/** `IFNULL()` (MySQL) */
|
|
819
|
+
ifNull(value: FnArg, otherwise: FnArg): FunctionBuilder<unknown>;
|
|
820
|
+
/** `INTERVAL()` (MySQL) */
|
|
821
|
+
interval(n: FnArg, ...ns: FnArg[]): FunctionBuilder<number | null>;
|
|
822
|
+
/** `ISNULL()` (MySQL) */
|
|
823
|
+
isNull(x: FnArg): FunctionBuilder<0 | 1 | null>;
|
|
824
|
+
/** `LEAST()` (portable) */
|
|
825
|
+
least(...values: FnArg[]): FunctionBuilder<unknown>;
|
|
826
|
+
/** `NULLIF()` (portable) */
|
|
827
|
+
nullIf(a: FnArg, b: FnArg): FunctionBuilder<unknown>;
|
|
828
|
+
/** `CAST()` (MySQL) */
|
|
829
|
+
cast(x: FnArg, type: string): FunctionBuilder<unknown>;
|
|
830
|
+
/** `CONVERT()` (MySQL) */
|
|
831
|
+
convert(x: FnArg, type: string | { using: string }): FunctionBuilder<unknown>;
|
|
832
|
+
/** `CHARSET()` (MySQL) */
|
|
833
|
+
charset(str: FnArg): FunctionBuilder<string | null>;
|
|
834
|
+
/** `COERCIBILITY()` (MySQL) */
|
|
835
|
+
coercibility(str: FnArg): FunctionBuilder<number | null>;
|
|
836
|
+
/** `COLLATION()` (MySQL) */
|
|
837
|
+
collation(str: FnArg): FunctionBuilder<string | null>;
|
|
838
|
+
/** `CONNECTION_ID()` (MySQL) */
|
|
839
|
+
connectionId(): FunctionBuilder<number | null>;
|
|
840
|
+
/** `CURRENT_ROLE()` (MySQL) */
|
|
841
|
+
currentRole(): FunctionBuilder<string | null>;
|
|
842
|
+
/** `CURRENT_USER()` (MySQL) */
|
|
843
|
+
currentUser(): FunctionBuilder<string | null>;
|
|
844
|
+
/** `DATABASE()` (MySQL) */
|
|
845
|
+
database(): FunctionBuilder<string | null>;
|
|
846
|
+
/** `ICU_VERSION()` (MySQL) */
|
|
847
|
+
icuVersion(): FunctionBuilder<string | null>;
|
|
848
|
+
/** `LAST_INSERT_ID()` (MySQL) */
|
|
849
|
+
lastInsertId(x?: FnArg): FunctionBuilder<number | null>;
|
|
850
|
+
/** `ROLES_GRAPHML()` (MySQL) */
|
|
851
|
+
rolesGraphml(): FunctionBuilder<string | null>;
|
|
852
|
+
/** `ROW_COUNT()` (MySQL) */
|
|
853
|
+
rowCount(): FunctionBuilder<number | null>;
|
|
854
|
+
/** `SCHEMA()` (MySQL) */
|
|
855
|
+
schema(): FunctionBuilder<string | null>;
|
|
856
|
+
/** `SESSION_USER()` (MySQL) */
|
|
857
|
+
sessionUser(): FunctionBuilder<string | null>;
|
|
858
|
+
/** `SYSTEM_USER()` (MySQL) */
|
|
859
|
+
systemUser(): FunctionBuilder<string | null>;
|
|
860
|
+
/** `USER()` (MySQL) */
|
|
861
|
+
user(): FunctionBuilder<string | null>;
|
|
862
|
+
/** `VERSION()` (MySQL) */
|
|
863
|
+
version(): FunctionBuilder<string | null>;
|
|
864
|
+
/** `AES_DECRYPT()` (MySQL) */
|
|
865
|
+
aesDecrypt(crypt: FnArg, key: FnArg, ...options: FnArg[]): FunctionBuilder<Buffer | null>;
|
|
866
|
+
/** `AES_ENCRYPT()` (MySQL) */
|
|
867
|
+
aesEncrypt(str: FnArg, key: FnArg, ...options: FnArg[]): FunctionBuilder<Buffer | null>;
|
|
868
|
+
/** `COMPRESS()` (MySQL) */
|
|
869
|
+
compress(str: FnArg): FunctionBuilder<Buffer | null>;
|
|
870
|
+
/** `MD5()` (portable) */
|
|
871
|
+
md5(str: FnArg): FunctionBuilder<string | null>;
|
|
872
|
+
/** `RANDOM_BYTES()` (MySQL) */
|
|
873
|
+
randomBytes(length: FnArg): FunctionBuilder<Buffer | null>;
|
|
874
|
+
/** `SHA1()` (MySQL) */
|
|
875
|
+
sha1(str: FnArg): FunctionBuilder<string | null>;
|
|
876
|
+
/** `SHA2()` (MySQL) */
|
|
877
|
+
sha2(str: FnArg, hashLength: FnArg): FunctionBuilder<string | null>;
|
|
878
|
+
/** `STATEMENT_DIGEST()` (MySQL) */
|
|
879
|
+
statementDigest(statement: FnArg): FunctionBuilder<string | null>;
|
|
880
|
+
/** `STATEMENT_DIGEST_TEXT()` (MySQL) */
|
|
881
|
+
statementDigestText(statement: FnArg): FunctionBuilder<string | null>;
|
|
882
|
+
/** `UNCOMPRESS()` (MySQL) */
|
|
883
|
+
uncompress(str: FnArg): FunctionBuilder<Buffer | null>;
|
|
884
|
+
/** `UNCOMPRESSED_LENGTH()` (MySQL) */
|
|
885
|
+
uncompressedLength(str: FnArg): FunctionBuilder<number | null>;
|
|
886
|
+
/** `GET_LOCK()` (MySQL) */
|
|
887
|
+
getLock(name: FnArg, timeout: FnArg): FunctionBuilder<0 | 1 | null>;
|
|
888
|
+
/** `IS_FREE_LOCK()` (MySQL) */
|
|
889
|
+
isFreeLock(name: FnArg): FunctionBuilder<0 | 1 | null>;
|
|
890
|
+
/** `IS_USED_LOCK()` (MySQL) */
|
|
891
|
+
isUsedLock(name: FnArg): FunctionBuilder<number | null>;
|
|
892
|
+
/** `RELEASE_ALL_LOCKS()` (MySQL) */
|
|
893
|
+
releaseAllLocks(): FunctionBuilder<number | null>;
|
|
894
|
+
/** `RELEASE_LOCK()` (MySQL) */
|
|
895
|
+
releaseLock(name: FnArg): FunctionBuilder<0 | 1 | null>;
|
|
896
|
+
/** `ANY_VALUE()` (MySQL) */
|
|
897
|
+
anyValue(x: FnArg): FunctionBuilder<unknown>;
|
|
898
|
+
/** `BIN_TO_UUID()` (MySQL) */
|
|
899
|
+
binToUuid(bin: FnArg, swap?: FnArg): FunctionBuilder<string | null>;
|
|
900
|
+
/** `DEFAULT()` (MySQL) */
|
|
901
|
+
default(column: FnArg): FunctionBuilder<unknown>;
|
|
902
|
+
/** `GROUPING()` (MySQL) */
|
|
903
|
+
grouping(...exprs: FnArg[]): FunctionBuilder<number | null>;
|
|
904
|
+
/** `INET6_ATON()` (MySQL) */
|
|
905
|
+
inet6Aton(address: FnArg): FunctionBuilder<Buffer | null>;
|
|
906
|
+
/** `INET6_NTOA()` (MySQL) */
|
|
907
|
+
inet6Ntoa(address: FnArg): FunctionBuilder<string | null>;
|
|
908
|
+
/** `INET_ATON()` (MySQL) */
|
|
909
|
+
inetAton(address: FnArg): FunctionBuilder<number | null>;
|
|
910
|
+
/** `INET_NTOA()` (MySQL) */
|
|
911
|
+
inetNtoa(address: FnArg): FunctionBuilder<string | null>;
|
|
912
|
+
/** `IS_IPV4()` (MySQL) */
|
|
913
|
+
isIpv4(address: FnArg): FunctionBuilder<0 | 1 | null>;
|
|
914
|
+
/** `IS_IPV4_COMPAT()` (MySQL) */
|
|
915
|
+
isIpv4Compat(address: FnArg): FunctionBuilder<0 | 1 | null>;
|
|
916
|
+
/** `IS_IPV4_MAPPED()` (MySQL) */
|
|
917
|
+
isIpv4Mapped(address: FnArg): FunctionBuilder<0 | 1 | null>;
|
|
918
|
+
/** `IS_IPV6()` (MySQL) */
|
|
919
|
+
isIpv6(address: FnArg): FunctionBuilder<0 | 1 | null>;
|
|
920
|
+
/** `IS_UUID()` (MySQL) */
|
|
921
|
+
isUuid(uuid: FnArg): FunctionBuilder<0 | 1 | null>;
|
|
922
|
+
/** `SLEEP()` (MySQL) */
|
|
923
|
+
sleep(seconds: FnArg): FunctionBuilder<number | null>;
|
|
924
|
+
/** `UUID()` (MySQL) */
|
|
925
|
+
uuid(): FunctionBuilder<string>;
|
|
926
|
+
/** `UUID_SHORT()` (MySQL) */
|
|
927
|
+
uuidShort(): FunctionBuilder<FnNumeric>;
|
|
928
|
+
/** `UUID_TO_BIN()` (MySQL) */
|
|
929
|
+
uuidToBin(uuid: FnArg, swap?: FnArg): FunctionBuilder<Buffer | null>;
|
|
930
|
+
/** `BIT_COUNT()` (MySQL) */
|
|
931
|
+
bitCount(x: FnArg): FunctionBuilder<number | null>;
|
|
932
|
+
/** `FORMAT_BYTES()` (MySQL) */
|
|
933
|
+
formatBytes(bytes: FnArg): FunctionBuilder<string | null>;
|
|
934
|
+
/** `FORMAT_PICO_TIME()` (MySQL) */
|
|
935
|
+
formatPicoTime(time: FnArg): FunctionBuilder<string | null>;
|
|
936
|
+
/** `PS_CURRENT_THREAD_ID()` (MySQL) */
|
|
937
|
+
psCurrentThreadId(): FunctionBuilder<number | null>;
|
|
938
|
+
/** `PS_THREAD_ID()` (MySQL) */
|
|
939
|
+
psThreadId(connectionId: FnArg): FunctionBuilder<number | null>;
|
|
940
|
+
/** `GTID_SUBSET()` (MySQL) */
|
|
941
|
+
gtidSubset(set1: FnArg, set2: FnArg): FunctionBuilder<0 | 1 | null>;
|
|
942
|
+
/** `GTID_SUBTRACT()` (MySQL) */
|
|
943
|
+
gtidSubtract(set1: FnArg, set2: FnArg): FunctionBuilder<string | null>;
|
|
944
|
+
/** `SOURCE_POS_WAIT()` (MySQL) */
|
|
945
|
+
sourcePosWait(
|
|
946
|
+
logName: FnArg,
|
|
947
|
+
logPos: FnArg,
|
|
948
|
+
...options: FnArg[]
|
|
949
|
+
): FunctionBuilder<number | null>;
|
|
950
|
+
/** `WAIT_FOR_EXECUTED_GTID_SET()` (MySQL) */
|
|
951
|
+
waitForExecutedGtidSet(gtidSet: FnArg, timeout?: FnArg): FunctionBuilder<number | null>;
|
|
952
|
+
/** `EXTRACTVALUE()` (MySQL) */
|
|
953
|
+
extractValue(xml: FnArg, xpath: FnArg): FunctionBuilder<string | null>;
|
|
954
|
+
/** `UPDATEXML()` (MySQL) */
|
|
955
|
+
updateXml(xml: FnArg, xpath: FnArg, newXml: FnArg): FunctionBuilder<string | null>;
|
|
956
|
+
/** `JSON_ARRAY()` (MySQL) */
|
|
957
|
+
jsonArray(...values: FnJsonArg[]): FunctionBuilder<unknown[]>;
|
|
958
|
+
/** `JSON_ARRAY_APPEND()` (MySQL) */
|
|
959
|
+
jsonArrayAppend(
|
|
960
|
+
doc: FnJsonArg,
|
|
961
|
+
path: FnArg,
|
|
962
|
+
value: FnJsonArg,
|
|
963
|
+
...pathsAndValues: FnJsonArg[]
|
|
964
|
+
): FunctionBuilder<unknown>;
|
|
965
|
+
/** `JSON_ARRAY_INSERT()` (MySQL) */
|
|
966
|
+
jsonArrayInsert(
|
|
967
|
+
doc: FnJsonArg,
|
|
968
|
+
path: FnArg,
|
|
969
|
+
value: FnJsonArg,
|
|
970
|
+
...pathsAndValues: FnJsonArg[]
|
|
971
|
+
): FunctionBuilder<unknown>;
|
|
972
|
+
/** `JSON_CONTAINS()` (MySQL) */
|
|
973
|
+
jsonContains(
|
|
974
|
+
target: FnJsonArg,
|
|
975
|
+
candidate: FnJsonArg,
|
|
976
|
+
path?: FnArg,
|
|
977
|
+
): FunctionBuilder<0 | 1 | null>;
|
|
978
|
+
/** `JSON_CONTAINS_PATH()` (MySQL) */
|
|
979
|
+
jsonContainsPath(
|
|
980
|
+
doc: FnJsonArg,
|
|
981
|
+
oneOrAll: 'one' | 'all',
|
|
982
|
+
...paths: FnArg[]
|
|
983
|
+
): FunctionBuilder<0 | 1 | null>;
|
|
984
|
+
/** `JSON_DEPTH()` (MySQL) */
|
|
985
|
+
jsonDepth(doc: FnJsonArg): FunctionBuilder<number | null>;
|
|
986
|
+
/** `JSON_EXTRACT()` (MySQL) */
|
|
987
|
+
jsonExtract(doc: FnJsonArg, ...paths: FnArg[]): FunctionBuilder<unknown>;
|
|
988
|
+
/** `JSON_INSERT()` (MySQL) */
|
|
989
|
+
jsonInsert(
|
|
990
|
+
doc: FnJsonArg,
|
|
991
|
+
path: FnArg,
|
|
992
|
+
value: FnJsonArg,
|
|
993
|
+
...pathsAndValues: FnJsonArg[]
|
|
994
|
+
): FunctionBuilder<unknown>;
|
|
995
|
+
/** `JSON_KEYS()` (MySQL) */
|
|
996
|
+
jsonKeys(doc: FnJsonArg, path?: FnArg): FunctionBuilder<string[] | null>;
|
|
997
|
+
/** `JSON_LENGTH()` (MySQL) */
|
|
998
|
+
jsonLength(doc: FnJsonArg, path?: FnArg): FunctionBuilder<number | null>;
|
|
999
|
+
/** `JSON_MERGE_PATCH()` (MySQL) */
|
|
1000
|
+
jsonMergePatch(...docs: FnJsonArg[]): FunctionBuilder<unknown>;
|
|
1001
|
+
/** `JSON_MERGE_PRESERVE()` (MySQL) */
|
|
1002
|
+
jsonMergePreserve(...docs: FnJsonArg[]): FunctionBuilder<unknown>;
|
|
1003
|
+
/** `JSON_OBJECT()` (MySQL) */
|
|
1004
|
+
jsonObject(
|
|
1005
|
+
objectOrKeysAndValues?: { [key: string]: FnJsonArg } | FnJsonArg,
|
|
1006
|
+
...keysAndValues: FnJsonArg[]
|
|
1007
|
+
): FunctionBuilder<Record<string, unknown>>;
|
|
1008
|
+
/** `JSON_OVERLAPS()` (MySQL) */
|
|
1009
|
+
jsonOverlaps(a: FnJsonArg, b: FnJsonArg): FunctionBuilder<0 | 1 | null>;
|
|
1010
|
+
/** `JSON_PRETTY()` (MySQL) */
|
|
1011
|
+
jsonPretty(doc: FnJsonArg): FunctionBuilder<string | null>;
|
|
1012
|
+
/** `JSON_QUOTE()` (MySQL) */
|
|
1013
|
+
jsonQuote(str: FnArg): FunctionBuilder<string | null>;
|
|
1014
|
+
/** `JSON_REMOVE()` (MySQL) */
|
|
1015
|
+
jsonRemove(doc: FnJsonArg, ...paths: FnArg[]): FunctionBuilder<unknown>;
|
|
1016
|
+
/** `JSON_REPLACE()` (MySQL) */
|
|
1017
|
+
jsonReplace(
|
|
1018
|
+
doc: FnJsonArg,
|
|
1019
|
+
path: FnArg,
|
|
1020
|
+
value: FnJsonArg,
|
|
1021
|
+
...pathsAndValues: FnJsonArg[]
|
|
1022
|
+
): FunctionBuilder<unknown>;
|
|
1023
|
+
/** `JSON_SCHEMA_VALID()` (MySQL) */
|
|
1024
|
+
jsonSchemaValid(schema: FnJsonArg, doc: FnJsonArg): FunctionBuilder<0 | 1 | null>;
|
|
1025
|
+
/** `JSON_SCHEMA_VALIDATION_REPORT()` (MySQL) */
|
|
1026
|
+
jsonSchemaValidationReport(
|
|
1027
|
+
schema: FnJsonArg,
|
|
1028
|
+
doc: FnJsonArg,
|
|
1029
|
+
): FunctionBuilder<Record<string, unknown> | null>;
|
|
1030
|
+
/** `JSON_SEARCH()` (MySQL) */
|
|
1031
|
+
jsonSearch(
|
|
1032
|
+
doc: FnJsonArg,
|
|
1033
|
+
oneOrAll: 'one' | 'all',
|
|
1034
|
+
search: FnArg,
|
|
1035
|
+
escape?: FnArg,
|
|
1036
|
+
...paths: FnArg[]
|
|
1037
|
+
): FunctionBuilder<string | string[] | null>;
|
|
1038
|
+
/** `JSON_SET()` (MySQL) */
|
|
1039
|
+
jsonSet(
|
|
1040
|
+
doc: FnJsonArg,
|
|
1041
|
+
path: FnArg,
|
|
1042
|
+
value: FnJsonArg,
|
|
1043
|
+
...pathsAndValues: FnJsonArg[]
|
|
1044
|
+
): FunctionBuilder<unknown>;
|
|
1045
|
+
/** `JSON_STORAGE_FREE()` (MySQL) */
|
|
1046
|
+
jsonStorageFree(doc: FnJsonArg): FunctionBuilder<number | null>;
|
|
1047
|
+
/** `JSON_STORAGE_SIZE()` (MySQL) */
|
|
1048
|
+
jsonStorageSize(doc: FnJsonArg): FunctionBuilder<number | null>;
|
|
1049
|
+
/** `JSON_TYPE()` (MySQL) */
|
|
1050
|
+
jsonType(value: FnJsonArg): FunctionBuilder<string | null>;
|
|
1051
|
+
/** `JSON_UNQUOTE()` (MySQL) */
|
|
1052
|
+
jsonUnquote(str: FnArg): FunctionBuilder<string | null>;
|
|
1053
|
+
/** `JSON_VALID()` (MySQL) */
|
|
1054
|
+
jsonValid(value: FnArg): FunctionBuilder<0 | 1 | null>;
|
|
1055
|
+
/** `JSON_VALUE()` (MySQL) */
|
|
1056
|
+
jsonValue(
|
|
1057
|
+
doc: FnJsonArg,
|
|
1058
|
+
path: FnArg,
|
|
1059
|
+
options?: { returning?: string },
|
|
1060
|
+
): FunctionBuilder<unknown>;
|
|
1061
|
+
/** `MEMBER OF()` (MySQL) */
|
|
1062
|
+
memberOf(value: FnJsonArg, array: FnJsonArg): FunctionBuilder<0 | 1 | null>;
|
|
1063
|
+
// </generated:mysql-functions>
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
export interface ComposeFunction {
|
|
1067
|
+
(...plugins: Plugin[]): Plugin;
|
|
1068
|
+
(plugins: Plugin[]): Plugin;
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
export interface Plugin {
|
|
1072
|
+
<M extends typeof Model>(modelClass: M): M;
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
export interface MixinFunction {
|
|
1076
|
+
<MC extends AnyModelConstructor>(modelClass: MC, ...plugins: Plugin[]): MC;
|
|
1077
|
+
<MC extends AnyModelConstructor>(modelClass: MC, plugins: Plugin[]): MC;
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
interface Aliasable {
|
|
1081
|
+
as(alias: string): this;
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
interface Castable extends Aliasable {
|
|
1085
|
+
castText(): this;
|
|
1086
|
+
castInt(): this;
|
|
1087
|
+
castBigInt(): this;
|
|
1088
|
+
castFloat(): this;
|
|
1089
|
+
castDecimal(): this;
|
|
1090
|
+
castReal(): this;
|
|
1091
|
+
castBool(): this;
|
|
1092
|
+
castJson(): this;
|
|
1093
|
+
castArray(): this;
|
|
1094
|
+
asArray(): this;
|
|
1095
|
+
castType(sqlType: string): this;
|
|
1096
|
+
castTo(sqlType: string): this;
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
type Raw = RawBuilder | Knex.Raw;
|
|
1100
|
+
type Operator = string;
|
|
1101
|
+
type ColumnRef = string | Raw | ReferenceBuilder;
|
|
1102
|
+
/**
|
|
1103
|
+
* A table: a name, a model class, a subquery, or an object that maps aliases to
|
|
1104
|
+
* any of these (`{ p: Pet }`).
|
|
1105
|
+
*/
|
|
1106
|
+
type TableRef<QB extends AnyQueryBuilder> =
|
|
1107
|
+
| ColumnRef
|
|
1108
|
+
| AnyQueryBuilder
|
|
1109
|
+
| CallbackVoid<QB>
|
|
1110
|
+
| AnyModelConstructor
|
|
1111
|
+
| { [alias: string]: string | AnyModelConstructor | AnyQueryBuilder | Raw };
|
|
1112
|
+
|
|
1113
|
+
type PrimitiveValue =
|
|
1114
|
+
| string
|
|
1115
|
+
| number
|
|
1116
|
+
| boolean
|
|
1117
|
+
| Date
|
|
1118
|
+
| Buffer
|
|
1119
|
+
| string[]
|
|
1120
|
+
| number[]
|
|
1121
|
+
| boolean[]
|
|
1122
|
+
| Date[]
|
|
1123
|
+
| Buffer[]
|
|
1124
|
+
| null;
|
|
1125
|
+
|
|
1126
|
+
type Expression<T> = T | Raw | ReferenceBuilder | ValueBuilder | AnyQueryBuilder;
|
|
1127
|
+
|
|
1128
|
+
type Id = string | number | BigInt | Buffer;
|
|
1129
|
+
type CompositeId = Id[];
|
|
1130
|
+
type MaybeCompositeId = Id | CompositeId;
|
|
1131
|
+
|
|
1132
|
+
interface ExpressionObject {
|
|
1133
|
+
[key: string]: Expression<PrimitiveValue>;
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
interface PrimitiveValueObject {
|
|
1137
|
+
[key: string]: PrimitiveValue;
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
interface CallbackVoid<T> {
|
|
1141
|
+
(this: T, arg: T): void;
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
type Identity<T> = (value: T) => T;
|
|
1145
|
+
/**
|
|
1146
|
+
* A query builder of any result kind for models of type `M`.
|
|
1147
|
+
*
|
|
1148
|
+
* Use this as the parameter type of modifiers and other functions that take
|
|
1149
|
+
* query builders so that they accept queries of every kind (`find`,
|
|
1150
|
+
* `first()`, `findById()`, `patch()`, ...).
|
|
1151
|
+
*/
|
|
1152
|
+
type AnyQueryBuilder<M extends Model = any> = QueryBuilder<M, any, any>;
|
|
1153
|
+
type AnyModelConstructor = ModelConstructor<Model>;
|
|
1154
|
+
type ModifierFunction<QB extends AnyQueryBuilder> = (qb: QB, ...args: any[]) => void;
|
|
1155
|
+
type Modifier<QB extends AnyQueryBuilder = AnyQueryBuilder> =
|
|
1156
|
+
| ModifierFunction<QB>
|
|
1157
|
+
| string
|
|
1158
|
+
| string[]
|
|
1159
|
+
| Record<string, Expression<PrimitiveValue>>;
|
|
1160
|
+
type OrderByDirection = 'asc' | 'desc' | 'ASC' | 'DESC';
|
|
1161
|
+
type OrderByNulls = 'first' | 'last';
|
|
1162
|
+
|
|
1163
|
+
interface Modifiers<QB extends AnyQueryBuilder = AnyQueryBuilder> {
|
|
1164
|
+
[key: string]: Modifier<QB>;
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
type RelationExpression<M extends Model> = string | object;
|
|
1168
|
+
|
|
1169
|
+
/**
|
|
1170
|
+
* `E` if it's a valid relation expression for model `M`, otherwise a message
|
|
1171
|
+
* that says what's wrong with it. Expressions that aren't known at compile time
|
|
1172
|
+
* (like a `string`) aren't checked.
|
|
1173
|
+
*/
|
|
1174
|
+
type CheckRelationExpression<M, E, CheckModifiers extends boolean = true> =
|
|
1175
|
+
ParseRelationExpression<E> extends infer G
|
|
1176
|
+
? G extends ParseError<infer Message>
|
|
1177
|
+
? Message
|
|
1178
|
+
: G extends GraphNode
|
|
1179
|
+
? GraphErrors<M, G['children'], '', CheckModifiers> extends infer Errors
|
|
1180
|
+
? [Errors] extends [never]
|
|
1181
|
+
? E
|
|
1182
|
+
: Errors
|
|
1183
|
+
: never
|
|
1184
|
+
: E
|
|
1185
|
+
: never;
|
|
1186
|
+
|
|
1187
|
+
// The errors of the nodes of a relation graph of model `M`. `Path` is the path
|
|
1188
|
+
// of the parent node followed by a dot, or an empty string at the root.
|
|
1189
|
+
type GraphErrors<M, Children, Path extends string, CheckModifiers extends boolean> =
|
|
1190
|
+
IsUncheckedModel<M> extends true
|
|
1191
|
+
? never
|
|
1192
|
+
: {
|
|
1193
|
+
[P in keyof Children & string]: Children[P] extends GraphNode
|
|
1194
|
+
? NodeErrors<M, Children[P], P, Path, CheckModifiers>
|
|
1195
|
+
: never;
|
|
1196
|
+
}[keyof Children & string];
|
|
1197
|
+
|
|
1198
|
+
type NodeErrors<
|
|
1199
|
+
M,
|
|
1200
|
+
N extends GraphNode,
|
|
1201
|
+
Prop extends string,
|
|
1202
|
+
Path extends string,
|
|
1203
|
+
CheckModifiers extends boolean,
|
|
1204
|
+
> =
|
|
1205
|
+
N['rel'] extends RelationNameKeys<M>
|
|
1206
|
+
?
|
|
1207
|
+
| (CheckModifiers extends true
|
|
1208
|
+
? ModifierNameErrors<RelatedModel<M, N['rel']>, N['mods'], `${Path}${Prop}`>
|
|
1209
|
+
: never)
|
|
1210
|
+
| GraphErrors<RelatedModel<M, N['rel']>, N['children'], `${Path}${Prop}.`, CheckModifiers>
|
|
1211
|
+
: Path extends `${infer Parent}.`
|
|
1212
|
+
? `Unknown relation "${N['rel']}" at "${Parent}"`
|
|
1213
|
+
: `Unknown relation "${N['rel']}"`;
|
|
1214
|
+
|
|
1215
|
+
// Named modifiers of a node must be modifiers of the related model, when its
|
|
1216
|
+
// modifiers are known.
|
|
1217
|
+
type ModifierNameErrors<RM, Mods extends readonly unknown[], Path extends string> = [
|
|
1218
|
+
KnownModifiers<RM>,
|
|
1219
|
+
] extends [never]
|
|
1220
|
+
? never
|
|
1221
|
+
: {
|
|
1222
|
+
[I in keyof Mods]: Mods[I] extends string
|
|
1223
|
+
? Mods[I] extends keyof KnownModifiers<RM>
|
|
1224
|
+
? never
|
|
1225
|
+
: `Unknown modifier "${Mods[I]}" for "${Path}"`
|
|
1226
|
+
: never;
|
|
1227
|
+
}[number];
|
|
1228
|
+
|
|
1229
|
+
// The names that are accepted as relations of `M`: its relation properties, and
|
|
1230
|
+
// the properties typed as objects, since relations are sometimes declared with
|
|
1231
|
+
// types that aren't models, like `programs?: Pick<Program, 'id' | 'name'>[]`.
|
|
1232
|
+
type RelationNameKeys<M> =
|
|
1233
|
+
| RelationKeys<M>
|
|
1234
|
+
| {
|
|
1235
|
+
[K in ModelDataKeys<M>]-?: K extends `$${string}`
|
|
1236
|
+
? never
|
|
1237
|
+
: IsAnyType<M[K]> extends true
|
|
1238
|
+
? never
|
|
1239
|
+
: NonNullable<M[K]> extends Function | Date
|
|
1240
|
+
? never
|
|
1241
|
+
: NonNullable<M[K]> extends object
|
|
1242
|
+
? K
|
|
1243
|
+
: never;
|
|
1244
|
+
}[ModelDataKeys<M>];
|
|
1245
|
+
|
|
1246
|
+
// The related model of relation `K` of model `M`.
|
|
1247
|
+
type RelatedModel<M, K extends keyof M> = ElementOrSelf<NonNullable<M[K]>>;
|
|
1248
|
+
|
|
1249
|
+
type ElementOrSelf<T> = T extends readonly (infer E)[] ? E : T;
|
|
1250
|
+
|
|
1251
|
+
// Models whose relations can't be known, like the related model of a relation
|
|
1252
|
+
// whose `modelClass` is a module path.
|
|
1253
|
+
type IsUncheckedModel<M> =
|
|
1254
|
+
IsAnyType<M> extends true
|
|
1255
|
+
? true
|
|
1256
|
+
: [Exclude<keyof M, keyof Model>] extends [never]
|
|
1257
|
+
? true
|
|
1258
|
+
: false;
|
|
1259
|
+
|
|
1260
|
+
/**
|
|
1261
|
+
* The type of model `M` with the relations of the relation expression `E`
|
|
1262
|
+
* loaded: `Loaded<Person, '[pets.toys, mom]'>`.
|
|
1263
|
+
*/
|
|
1264
|
+
export type Loaded<M, E> = WithGraph<ModelRow<M>, M, E>;
|
|
1265
|
+
|
|
1266
|
+
// Result item `I` of model `M` after loading the relations of expression `E`.
|
|
1267
|
+
type WithGraph<I, M, E> =
|
|
1268
|
+
IsAnyType<I> extends true
|
|
1269
|
+
? any
|
|
1270
|
+
: ParseRelationExpression<E> extends infer G
|
|
1271
|
+
? G extends GraphNode
|
|
1272
|
+
? G['all'] extends true
|
|
1273
|
+
? MakeOpaque<I, M>
|
|
1274
|
+
: WithChildren<I, M, G['children']>
|
|
1275
|
+
: MakeOpaque<I, M>
|
|
1276
|
+
: never;
|
|
1277
|
+
|
|
1278
|
+
type WithChildren<I, M, Children> = [keyof Children] extends [never]
|
|
1279
|
+
? I
|
|
1280
|
+
: [Extract<keyof Children, keyof I>] extends [never]
|
|
1281
|
+
? I & LoadedRelations<I, M, Children>
|
|
1282
|
+
: Replace<I, M, Extract<keyof Children, keyof I>, LoadedRelations<I, M, Children>>;
|
|
1283
|
+
|
|
1284
|
+
// `I` with the properties `K` replaced by `Props`. The rows of models that keep
|
|
1285
|
+
// their relations (see `ModelRow`) aren't copied, since the copy couldn't be
|
|
1286
|
+
// used where the model is expected. `Props` is intersected with them instead.
|
|
1287
|
+
type Replace<I, M, K, Props> = [Row<M>] extends [ModelRow<M>] ? Without<I, K> & Props : I & Props;
|
|
1288
|
+
|
|
1289
|
+
// `I` without the properties `K`.
|
|
1290
|
+
type Without<I, K> = { [P in keyof I as P extends K ? never : P]: I[P] };
|
|
1291
|
+
|
|
1292
|
+
type LoadedRelations<I, M, Children> = {
|
|
1293
|
+
-readonly [P in keyof Children as Children[P] extends { maybe: true }
|
|
1294
|
+
? never
|
|
1295
|
+
: P]: LoadedRelation<I, M, P, Children[P]>;
|
|
1296
|
+
} & MaybeLoadedRelations<I, M, Children>;
|
|
1297
|
+
|
|
1298
|
+
// Relations with a `boolean` in object notation, which may not be loaded.
|
|
1299
|
+
type MaybeLoadedRelations<I, M, Children> = [
|
|
1300
|
+
{ [P in keyof Children]: Children[P] extends { maybe: true } ? P : never }[keyof Children],
|
|
1301
|
+
] extends [never]
|
|
1302
|
+
? unknown
|
|
1303
|
+
: {
|
|
1304
|
+
-readonly [P in keyof Children as Children[P] extends { maybe: true }
|
|
1305
|
+
? P
|
|
1306
|
+
: never]?: LoadedRelation<I, M, P, Children[P]>;
|
|
1307
|
+
};
|
|
1308
|
+
|
|
1309
|
+
// The type of the relation property `P` loaded by node `N`.
|
|
1310
|
+
type LoadedRelation<I, M, P, N> = N extends GraphNode
|
|
1311
|
+
? N['rel'] extends keyof M
|
|
1312
|
+
? RelationValue<M[N['rel']], LoadedNodeRow<PreviousRow<I, P>, RelatedModel<M, N['rel']>, N>>
|
|
1313
|
+
: unknown
|
|
1314
|
+
: unknown;
|
|
1315
|
+
|
|
1316
|
+
// To-many relations are arrays. Objection sets to-one relations to `null` when
|
|
1317
|
+
// there's no related model, but rows must still be usable where the model is
|
|
1318
|
+
// expected, so the declared type decides: `Row | null` for relations declared
|
|
1319
|
+
// with `| null` (like the inferred ones), `Row | undefined` for optional ones.
|
|
1320
|
+
type RelationValue<Declared, Row> = [NonNullable<Declared>] extends [readonly any[]]
|
|
1321
|
+
? Row[]
|
|
1322
|
+
: [Extract<NonNullable<Declared>, readonly any[]>] extends [never]
|
|
1323
|
+
? null extends Declared
|
|
1324
|
+
? Row | null
|
|
1325
|
+
: undefined extends Declared
|
|
1326
|
+
? Row | undefined
|
|
1327
|
+
: Row
|
|
1328
|
+
: Row | Row[] | null;
|
|
1329
|
+
|
|
1330
|
+
// The row of a relation that an earlier call already loaded, so that calls
|
|
1331
|
+
// add to each other like they do at runtime.
|
|
1332
|
+
type PreviousRow<I, P> = P extends keyof I
|
|
1333
|
+
? {} extends Pick<I, P>
|
|
1334
|
+
? never
|
|
1335
|
+
: ElementOrSelf<NonNullable<I[P]>>
|
|
1336
|
+
: never;
|
|
1337
|
+
|
|
1338
|
+
// The row of the related model `RM` loaded by node `N`, starting from `Previous`.
|
|
1339
|
+
type LoadedNodeRow<Previous, RM, N extends GraphNode> = WithChildren<
|
|
1340
|
+
ApplyNodeModifiers<
|
|
1341
|
+
[Previous] extends [never] ? ModelRow<RM> : Previous,
|
|
1342
|
+
RM,
|
|
1343
|
+
N['recursive'] extends true ? ['~opaque'] : N['all'] extends true ? ['~opaque'] : N['mods']
|
|
1344
|
+
>,
|
|
1345
|
+
RM,
|
|
1346
|
+
N['children']
|
|
1347
|
+
>;
|
|
1348
|
+
|
|
1349
|
+
type ApplyNodeModifiers<I, RM, Mods> = Mods extends readonly [infer First, ...infer Rest]
|
|
1350
|
+
? ApplyNodeModifiers<ModifiedItem<I, RM, First>, RM, Rest>
|
|
1351
|
+
: I;
|
|
1352
|
+
|
|
1353
|
+
// The item after a modifier. A modifier function that returns the query builder
|
|
1354
|
+
// adds what it loaded. Anything else may load relations the types can't see.
|
|
1355
|
+
type ModifiedItem<I, M, Modifier> = Modifier extends (...args: any[]) => infer Ret
|
|
1356
|
+
? ReturnedItem<I, M, Ret>
|
|
1357
|
+
: [KnownModifiers<M>] extends [never]
|
|
1358
|
+
? MakeOpaque<I, M>
|
|
1359
|
+
: Modifier extends keyof KnownModifiers<M>
|
|
1360
|
+
? ModifiedItem<I, M, KnownModifiers<M>[Modifier]>
|
|
1361
|
+
: MakeOpaque<I, M>;
|
|
1362
|
+
|
|
1363
|
+
type ReturnedItem<I, M, Ret> = Ret extends { '~state': infer S extends QueryBuilderState }
|
|
1364
|
+
? IsAnyType<S['item']> extends true
|
|
1365
|
+
? MakeOpaque<I, M>
|
|
1366
|
+
: S extends { opaque: true }
|
|
1367
|
+
? MakeOpaque<MergeLoaded<I, M, S['item']>, M>
|
|
1368
|
+
: MergeLoaded<I, M, S['item']>
|
|
1369
|
+
: MakeOpaque<I, M>;
|
|
1370
|
+
|
|
1371
|
+
// Adds to `I` the relations `Other` has loaded: its required relation
|
|
1372
|
+
// properties and the properties that aren't properties of the model (aliases).
|
|
1373
|
+
type MergeLoaded<I, M, Other> = [LoadedKeys<M, Other>] extends [never]
|
|
1374
|
+
? I
|
|
1375
|
+
: [Extract<LoadedKeys<M, Other>, keyof I>] extends [never]
|
|
1376
|
+
? I & Only<Other, LoadedKeys<M, Other>>
|
|
1377
|
+
: Replace<I, M, LoadedKeys<M, Other>, Only<Other, LoadedKeys<M, Other>>>;
|
|
1378
|
+
|
|
1379
|
+
// Like `Pick`, but shown as an object type in messages.
|
|
1380
|
+
type Only<T, K> = { [P in keyof T as P extends K ? P : never]: T[P] };
|
|
1381
|
+
|
|
1382
|
+
type LoadedKeys<M, Other> =
|
|
1383
|
+
| {
|
|
1384
|
+
[K in keyof Other]-?: K extends RelationKeys<M>
|
|
1385
|
+
? {} extends Pick<Other, K>
|
|
1386
|
+
? never
|
|
1387
|
+
: K
|
|
1388
|
+
: never;
|
|
1389
|
+
}[keyof Other]
|
|
1390
|
+
| Exclude<keyof Other, keyof M>;
|
|
1391
|
+
|
|
1392
|
+
// The query builder `QB` after loading the relations of expression `E`.
|
|
1393
|
+
type WithGraphQueryBuilder<QB extends AnyQueryBuilder, E> = QB['~state'] extends {
|
|
1394
|
+
cast: true;
|
|
1395
|
+
}
|
|
1396
|
+
? OpaqueQueryBuilder<QB>
|
|
1397
|
+
: ParseRelationExpression<E> extends GraphNode
|
|
1398
|
+
? ItemQueryBuilder<QB, WithGraph<QB['~state']['item'], GraphModel<QB>, E>>
|
|
1399
|
+
: OpaqueQueryBuilder<QB>;
|
|
1400
|
+
|
|
1401
|
+
// An interface instead of a static method, so that it isn't compared every
|
|
1402
|
+
// time a model class is compared with another one.
|
|
1403
|
+
interface StaticFetchGraphMethod {
|
|
1404
|
+
<
|
|
1405
|
+
MC extends Constructor<Model>,
|
|
1406
|
+
T extends PartialModelObject<Extract<ModelInstance<MC>, Model>>,
|
|
1407
|
+
const E extends RelationExpression<Extract<ModelInstance<MC>, Model>>,
|
|
1408
|
+
>(
|
|
1409
|
+
this: MC,
|
|
1410
|
+
modelOrObject: T,
|
|
1411
|
+
expression: CheckRelationExpression<ModelInstance<MC>, E>,
|
|
1412
|
+
options?: FetchGraphOptions,
|
|
1413
|
+
): StaticFetchGraphQueryBuilder<MC, T, E, 'one'>;
|
|
1414
|
+
<
|
|
1415
|
+
MC extends Constructor<Model>,
|
|
1416
|
+
T extends PartialModelObject<Extract<ModelInstance<MC>, Model>>,
|
|
1417
|
+
const E extends RelationExpression<Extract<ModelInstance<MC>, Model>>,
|
|
1418
|
+
>(
|
|
1419
|
+
this: MC,
|
|
1420
|
+
modelOrObject: T[],
|
|
1421
|
+
expression: CheckRelationExpression<ModelInstance<MC>, E>,
|
|
1422
|
+
options?: FetchGraphOptions,
|
|
1423
|
+
): StaticFetchGraphQueryBuilder<MC, T, E, 'many'>;
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
type StaticFetchGraphQueryBuilder<MC, T, E, Kind extends 'one' | 'many'> =
|
|
1427
|
+
Extract<ModelInstance<MC>, Model> extends infer M extends Model
|
|
1428
|
+
? [M] extends [never]
|
|
1429
|
+
? any
|
|
1430
|
+
: FetchGraphQueryBuilder<QueryBuilderType<M>, M, T, E, Kind>
|
|
1431
|
+
: any;
|
|
1432
|
+
|
|
1433
|
+
// The query builder of `fetchGraph()` for objects of type `T` of model `M`.
|
|
1434
|
+
// Plain objects are turned into models.
|
|
1435
|
+
type FetchGraphQueryBuilder<QB extends AnyQueryBuilder, M, T, E, Kind extends 'one' | 'many'> =
|
|
1436
|
+
WithGraph<T extends Model ? T : M, M, E> extends infer I
|
|
1437
|
+
? RebindQueryBuilder<
|
|
1438
|
+
QB,
|
|
1439
|
+
ModelType<QB>,
|
|
1440
|
+
Kind extends 'one' ? I : I[],
|
|
1441
|
+
ParseRelationExpression<E> extends GraphNode
|
|
1442
|
+
? QueryBuilderItemState<I>
|
|
1443
|
+
: OpaqueItemState<I>
|
|
1444
|
+
>
|
|
1445
|
+
: never;
|
|
1446
|
+
|
|
1447
|
+
// The query builder `QB` with result items of type `I`.
|
|
1448
|
+
type ItemQueryBuilder<QB extends AnyQueryBuilder, I> = RebindQueryBuilder<
|
|
1449
|
+
QB,
|
|
1450
|
+
ModelType<QB>,
|
|
1451
|
+
ReplaceResultItem<ResultType<QB>, I>,
|
|
1452
|
+
QB['~state'] extends { opaque: true } ? OpaqueItemState<I> : QueryBuilderItemState<I>
|
|
1453
|
+
>;
|
|
1454
|
+
|
|
1455
|
+
// The query builder `QB` after a modifier that returned `Ret`.
|
|
1456
|
+
type ModifiedQueryBuilder<QB extends AnyQueryBuilder, Ret> = Ret extends {
|
|
1457
|
+
'~state': infer S extends QueryBuilderState;
|
|
1458
|
+
}
|
|
1459
|
+
? IsAnyType<S['item']> extends true
|
|
1460
|
+
? OpaqueQueryBuilder<QB>
|
|
1461
|
+
: S extends { opaque: true }
|
|
1462
|
+
? OpaqueQueryBuilder<
|
|
1463
|
+
ItemQueryBuilder<QB, MergeLoaded<QB['~state']['item'], GraphModel<QB>, S['item']>>
|
|
1464
|
+
>
|
|
1465
|
+
: ItemQueryBuilder<QB, MergeLoaded<QB['~state']['item'], GraphModel<QB>, S['item']>>
|
|
1466
|
+
: OpaqueQueryBuilder<QB>;
|
|
1467
|
+
|
|
1468
|
+
/**
|
|
1469
|
+
* If T is an array, returns the item type, otherwise returns T.
|
|
1470
|
+
*/
|
|
1471
|
+
type ItemType<T> = T extends Array<unknown> ? T[number] : T;
|
|
1472
|
+
|
|
1473
|
+
/**
|
|
1474
|
+
* Type for keys of non-function properties of T.
|
|
1475
|
+
*/
|
|
1476
|
+
type NonFunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T];
|
|
1477
|
+
|
|
1478
|
+
/**
|
|
1479
|
+
* Type that attempts to only select the user-defined model properties.
|
|
1480
|
+
*/
|
|
1481
|
+
type DataPropertyNames<T> = Exclude<NonFunctionPropertyNames<T>, 'QueryBuilderType'>;
|
|
1482
|
+
|
|
1483
|
+
/**
|
|
1484
|
+
* Removes `undefined` from a type.
|
|
1485
|
+
*/
|
|
1486
|
+
type Defined<T> = Exclude<T, undefined>;
|
|
1487
|
+
|
|
1488
|
+
/**
|
|
1489
|
+
* A Pojo version of model.
|
|
1490
|
+
*/
|
|
1491
|
+
type ModelObject<T extends Model> = Pick<T, DataPropertyNames<T>>;
|
|
1492
|
+
|
|
1493
|
+
/**
|
|
1494
|
+
* Any object that has some of the properties of model class T match this type.
|
|
1495
|
+
*/
|
|
1496
|
+
type PartialModelObject<T extends Model> = {
|
|
1497
|
+
[K in DataPropertyNames<T>]?: Defined<T[K]> extends Model
|
|
1498
|
+
? T[K]
|
|
1499
|
+
: Defined<T[K]> extends Array<infer I>
|
|
1500
|
+
? I extends Model
|
|
1501
|
+
? I[]
|
|
1502
|
+
: Expression<T[K]>
|
|
1503
|
+
: Expression<T[K]>;
|
|
1504
|
+
};
|
|
1505
|
+
|
|
1506
|
+
type JsonFieldKey<T> = `${JsonColumnName<T>}:${string}`;
|
|
1507
|
+
|
|
1508
|
+
/**
|
|
1509
|
+
* Checks an object for `patch()`, `update()` and `merge()` that sets fields of JSON
|
|
1510
|
+
* columns one at a time: `patch({ 'payload:courseName': 'Physics' })`. The other keys
|
|
1511
|
+
* must be properties of the model. Keys that aren't allowed get the type `never`.
|
|
1512
|
+
*/
|
|
1513
|
+
type CheckModelUpdate<M extends Model, U> = [Extract<keyof U, `${string}:${string}`>] extends [
|
|
1514
|
+
never,
|
|
1515
|
+
]
|
|
1516
|
+
? // Objects without field keys are checked by the plain overload, which gives better
|
|
1517
|
+
// errors and is cheaper for values like subqueries.
|
|
1518
|
+
never
|
|
1519
|
+
: {
|
|
1520
|
+
[K in keyof U]: K extends `${string}:${string}`
|
|
1521
|
+
? K extends JsonFieldKey<M>
|
|
1522
|
+
? U[K]
|
|
1523
|
+
: never
|
|
1524
|
+
: K extends keyof PartialModelObject<M>
|
|
1525
|
+
? PartialModelObject<M>[K]
|
|
1526
|
+
: never;
|
|
1527
|
+
};
|
|
1528
|
+
|
|
1529
|
+
/**
|
|
1530
|
+
* Additional optional parameters that may be used in graphs.
|
|
1531
|
+
*/
|
|
1532
|
+
type GraphParameters = {
|
|
1533
|
+
'#dbRef'?: MaybeCompositeId;
|
|
1534
|
+
'#ref'?: string;
|
|
1535
|
+
'#id'?: string;
|
|
1536
|
+
};
|
|
1537
|
+
|
|
1538
|
+
/**
|
|
1539
|
+
* Just like PartialModelObject but this is applied recursively to relations.
|
|
1540
|
+
*/
|
|
1541
|
+
type PartialModelGraph<M, T = M & GraphParameters> = T extends any
|
|
1542
|
+
? {
|
|
1543
|
+
[K in DataPropertyNames<T>]?: null extends T[K]
|
|
1544
|
+
? PartialModelGraphField<NonNullable<T[K]>> | null // handle nullable BelongsToOneRelations
|
|
1545
|
+
: PartialModelGraphField<T[K]>;
|
|
1546
|
+
}
|
|
1547
|
+
: never;
|
|
1548
|
+
|
|
1549
|
+
type PartialModelGraphField<F> =
|
|
1550
|
+
Defined<F> extends Model
|
|
1551
|
+
? PartialModelGraph<Defined<F>>
|
|
1552
|
+
: Defined<F> extends Array<infer I>
|
|
1553
|
+
? I extends Model
|
|
1554
|
+
? PartialModelGraph<I>[]
|
|
1555
|
+
: Expression<F>
|
|
1556
|
+
: Expression<F>;
|
|
1557
|
+
|
|
1558
|
+
/**
|
|
1559
|
+
* Extracts the property names (excluding relations) of a model class.
|
|
1560
|
+
*/
|
|
1561
|
+
type ModelProps<T extends Model> = Exclude<
|
|
1562
|
+
{
|
|
1563
|
+
[K in ModelDataKeys<T>]-?: NonNullable<T[K]> extends Model | readonly Model[]
|
|
1564
|
+
? never
|
|
1565
|
+
: NonNullable<T[K]> extends Array<infer I>
|
|
1566
|
+
? I extends Model
|
|
1567
|
+
? never
|
|
1568
|
+
: K
|
|
1569
|
+
: T[K] extends Function
|
|
1570
|
+
? never
|
|
1571
|
+
: K;
|
|
1572
|
+
}[ModelDataKeys<T>],
|
|
1573
|
+
undefined
|
|
1574
|
+
>;
|
|
1575
|
+
|
|
1576
|
+
/**
|
|
1577
|
+
* The keys of a model, without the ones objection uses internally. They are left out
|
|
1578
|
+
* before looking at the property types, since `QueryBuilderType` refers back to the
|
|
1579
|
+
* model.
|
|
1580
|
+
*/
|
|
1581
|
+
type ModelDataKeys<T> = Exclude<keyof T, 'QueryBuilderType' | `~${string}`>;
|
|
1582
|
+
|
|
1583
|
+
/**
|
|
1584
|
+
* Extracts the relation names of the a model class.
|
|
1585
|
+
*/
|
|
1586
|
+
type ModelRelations<T extends Model> = Defined<
|
|
1587
|
+
{
|
|
1588
|
+
[K in keyof T]?: NonNullable<T[K]> extends Model | readonly Model[]
|
|
1589
|
+
? K
|
|
1590
|
+
: NonNullable<T[K]> extends Array<infer I>
|
|
1591
|
+
? I extends Model
|
|
1592
|
+
? K
|
|
1593
|
+
: never
|
|
1594
|
+
: never;
|
|
1595
|
+
}[keyof T]
|
|
1596
|
+
>;
|
|
1597
|
+
|
|
1598
|
+
/**
|
|
1599
|
+
* Given a model property type, returns a query builer type of
|
|
1600
|
+
* correct kind if the property is a model or a model array.
|
|
1601
|
+
*/
|
|
1602
|
+
type RelatedQueryBuilder<T> = T extends Model
|
|
1603
|
+
? // A to-one relation query returns `undefined` if there is no related model.
|
|
1604
|
+
MaybeSingleQueryBuilder<RowQueryBuilder<QueryBuilderType<T>, T>>
|
|
1605
|
+
: T extends Array<infer I>
|
|
1606
|
+
? I extends Model
|
|
1607
|
+
? RowQueryBuilder<QueryBuilderType<I>, I>
|
|
1608
|
+
: never
|
|
1609
|
+
: never;
|
|
1610
|
+
|
|
1611
|
+
/**
|
|
1612
|
+
* Just like RelatedQueryBuilder but always returns an array
|
|
1613
|
+
* query builder even if the property type is a model and not
|
|
1614
|
+
* an array of models.
|
|
1615
|
+
*/
|
|
1616
|
+
type ArrayRelatedQueryBuilder<T> = T extends Model
|
|
1617
|
+
? RowQueryBuilder<QueryBuilderType<T>, T>
|
|
1618
|
+
: T extends Array<infer I>
|
|
1619
|
+
? I extends Model
|
|
1620
|
+
? RowQueryBuilder<QueryBuilderType<I>, I>
|
|
1621
|
+
: never
|
|
1622
|
+
: never;
|
|
1623
|
+
|
|
1624
|
+
/**
|
|
1625
|
+
* Gets the query builder type for a model type.
|
|
1626
|
+
*/
|
|
1627
|
+
type QueryBuilderType<T extends { QueryBuilderType: any }> = T['QueryBuilderType'];
|
|
1628
|
+
|
|
1629
|
+
/**
|
|
1630
|
+
* Gets the model type from a query builder type.
|
|
1631
|
+
*/
|
|
1632
|
+
type ModelType<T extends { ModelType: any }> = T['ModelType'];
|
|
1633
|
+
|
|
1634
|
+
/**
|
|
1635
|
+
* Gets the result type from a query builder type.
|
|
1636
|
+
*/
|
|
1637
|
+
type ResultType<T extends { ResultType: any }> = T['ResultType'];
|
|
1638
|
+
|
|
1639
|
+
type IsAnyType<T> = 0 extends 1 & T ? true : false;
|
|
1640
|
+
|
|
1641
|
+
/**
|
|
1642
|
+
* The type of the items of a query result `R`. For example `Person` for
|
|
1643
|
+
* `Person[]`, `Person | undefined` and `Page<Person>`. For results without
|
|
1644
|
+
* items (`number`), returns `M`.
|
|
1645
|
+
*/
|
|
1646
|
+
type ResultItemOf<R, M = unknown> =
|
|
1647
|
+
IsAnyType<R> extends true
|
|
1648
|
+
? any
|
|
1649
|
+
: [R] extends [number]
|
|
1650
|
+
? M
|
|
1651
|
+
: [R] extends [readonly (infer I)[]]
|
|
1652
|
+
? I
|
|
1653
|
+
: [R] extends [Page<infer I>]
|
|
1654
|
+
? I
|
|
1655
|
+
: Exclude<R, undefined>;
|
|
1656
|
+
|
|
1657
|
+
type IsSingleResult<R> =
|
|
1658
|
+
IsAnyType<R> extends true
|
|
1659
|
+
? false
|
|
1660
|
+
: [R] extends [number]
|
|
1661
|
+
? false
|
|
1662
|
+
: [R] extends [readonly any[]]
|
|
1663
|
+
? false
|
|
1664
|
+
: [R] extends [Page<any>]
|
|
1665
|
+
? false
|
|
1666
|
+
: true;
|
|
1667
|
+
|
|
1668
|
+
/**
|
|
1669
|
+
* The type-level state of a query builder. It's kept when the result kind
|
|
1670
|
+
* changes (for example when `first()` or `patch()` is called).
|
|
1671
|
+
*/
|
|
1672
|
+
interface QueryBuilderState {
|
|
1673
|
+
// The type of the result items, for example `Person` when the result is
|
|
1674
|
+
// `Person[]`, `Person | undefined` or `Page<Person>`.
|
|
1675
|
+
item: unknown;
|
|
1676
|
+
// `true` when something ran that may have loaded relations the types can't
|
|
1677
|
+
// see, like a modifier that doesn't return the query builder. Only opaque
|
|
1678
|
+
// states have it, so that opaque query builders can be assigned to
|
|
1679
|
+
// variables of the others.
|
|
1680
|
+
opaque?: true;
|
|
1681
|
+
// `true` after `castTo(ModelClass)`.
|
|
1682
|
+
cast?: true;
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1685
|
+
type QueryBuilderItemState<T> = { item: T };
|
|
1686
|
+
|
|
1687
|
+
type OpaqueItemState<T> = { item: T; opaque: true };
|
|
1688
|
+
|
|
1689
|
+
// The state after `castTo(ModelClass)`.
|
|
1690
|
+
type CastItemState<T> = { item: T; opaque: true; cast: true };
|
|
1691
|
+
|
|
1692
|
+
/**
|
|
1693
|
+
* A model as a query returns it: without the relations the query didn't load.
|
|
1694
|
+
* Loaded relations are added by `withGraphFetched()` and the other graph methods.
|
|
1695
|
+
*
|
|
1696
|
+
* Models whose rows couldn't be used where the model is expected keep their
|
|
1697
|
+
* relations: models that declare a relation as required (`pets!: Pet[]`
|
|
1698
|
+
* instead of `pets?: Pet[]`), and models with `private` or `protected` members.
|
|
1699
|
+
*/
|
|
1700
|
+
export type ModelRow<M> = [RequiredRelationKeys<M>] extends [never]
|
|
1701
|
+
? Row<M> extends M
|
|
1702
|
+
? Row<M>
|
|
1703
|
+
: M
|
|
1704
|
+
: M;
|
|
1705
|
+
|
|
1706
|
+
type RequiredRelationKeys<M> = {
|
|
1707
|
+
[K in RelationKeys<M>]: {} extends Pick<M, K> ? never : K;
|
|
1708
|
+
}[RelationKeys<M>];
|
|
1709
|
+
|
|
1710
|
+
type Row<M> = { [K in keyof M as K extends RelationKeys<M> ? never : K]: M[K] };
|
|
1711
|
+
|
|
1712
|
+
// The keys of the relation properties of `M`. Unlike `ModelRelations`, this
|
|
1713
|
+
// ignores properties typed as `any`.
|
|
1714
|
+
type RelationKeys<M> = {
|
|
1715
|
+
[K in keyof M]-?: IsAnyType<M[K]> extends true
|
|
1716
|
+
? never
|
|
1717
|
+
: NonNullable<M[K]> extends ModelLike | readonly ModelLike[]
|
|
1718
|
+
? K
|
|
1719
|
+
: never;
|
|
1720
|
+
}[keyof M];
|
|
1721
|
+
|
|
1722
|
+
// Checking for a few members of `Model` is much cheaper than comparing with
|
|
1723
|
+
// `Model`, which compares the types of all its methods.
|
|
1724
|
+
type ModelLike = { $modelClass: unknown; $relatedQuery: unknown; $fetchGraph: unknown };
|
|
1725
|
+
|
|
1726
|
+
// Adds back the relations of `M` that `I` doesn't have, as optional
|
|
1727
|
+
// properties, for results that may contain relations the types can't see.
|
|
1728
|
+
// A row without loaded relations becomes the model itself.
|
|
1729
|
+
type MakeOpaque<I, M> = [Exclude<RelationKeys<M>, keyof I>] extends [never]
|
|
1730
|
+
? I
|
|
1731
|
+
: [Exclude<keyof I, keyof M> | Extract<keyof I, RelationKeys<M>>] extends [never]
|
|
1732
|
+
? M
|
|
1733
|
+
: I & { [K in Exclude<RelationKeys<M>, keyof I>]?: M[K] };
|
|
1734
|
+
|
|
1735
|
+
// The query builder `QB` after something ran that may have loaded relations
|
|
1736
|
+
// the types can't see.
|
|
1737
|
+
type OpaqueQueryBuilder<QB extends AnyQueryBuilder> = QB['~state'] extends { opaque: true }
|
|
1738
|
+
? QB
|
|
1739
|
+
: RebindQueryBuilder<
|
|
1740
|
+
QB,
|
|
1741
|
+
ModelType<QB>,
|
|
1742
|
+
ReplaceResultItem<
|
|
1743
|
+
ResultType<QB>,
|
|
1744
|
+
MakeOpaque<QB['~state']['item'], RelationsModel<ModelType<QB>>>
|
|
1745
|
+
>,
|
|
1746
|
+
OpaqueItemState<MakeOpaque<QB['~state']['item'], RelationsModel<ModelType<QB>>>>
|
|
1747
|
+
>;
|
|
1748
|
+
|
|
1749
|
+
// The query builder `QB` for model `M` with rows as the result items.
|
|
1750
|
+
type RowQueryBuilder<
|
|
1751
|
+
QB extends AnyQueryBuilder,
|
|
1752
|
+
M extends Model,
|
|
1753
|
+
I = ModelRow<M>,
|
|
1754
|
+
> = RebindQueryBuilder<QB, M, I[], QueryBuilderItemState<I>>;
|
|
1755
|
+
|
|
1756
|
+
/**
|
|
1757
|
+
* The state of a query builder whose result type `R` was given explicitly,
|
|
1758
|
+
* as in `QueryBuilder<Person, Person | undefined>`.
|
|
1759
|
+
*/
|
|
1760
|
+
// This is a conditional type so that it resolves to `QueryBuilderItemState`,
|
|
1761
|
+
// whose instances only differ by the item type. This makes for example
|
|
1762
|
+
// `DefaultQueryBuilderState<M, M[]>` and `DefaultQueryBuilderState<M, M | undefined>`
|
|
1763
|
+
// the same type.
|
|
1764
|
+
type DefaultQueryBuilderState<M, R> = [R] extends [never]
|
|
1765
|
+
? QueryBuilderItemState<never>
|
|
1766
|
+
: QueryBuilderItemState<ResultItemOf<R, M>>;
|
|
1767
|
+
|
|
1768
|
+
/**
|
|
1769
|
+
* Returns the query builder type `QB` (including custom query builder
|
|
1770
|
+
* subclasses) for model `M`, result `R` and state `S`.
|
|
1771
|
+
*/
|
|
1772
|
+
type RebindQueryBuilder<
|
|
1773
|
+
QB extends AnyQueryBuilder,
|
|
1774
|
+
M extends Model,
|
|
1775
|
+
R,
|
|
1776
|
+
S extends QueryBuilderState = QB['~S'],
|
|
1777
|
+
> = (QB['~rebind'] & { '~args': [M, R, S] })['~rebind'];
|
|
1778
|
+
|
|
1779
|
+
/**
|
|
1780
|
+
* Gets the single item query builder type for a query builder.
|
|
1781
|
+
*/
|
|
1782
|
+
type SingleQueryBuilder<QB extends AnyQueryBuilder> = QB['~one'];
|
|
1783
|
+
|
|
1784
|
+
/**
|
|
1785
|
+
* Gets the single or undefined item query builder type for a query builder.
|
|
1786
|
+
*/
|
|
1787
|
+
type MaybeSingleQueryBuilder<QB extends AnyQueryBuilder> = QB['~maybe'];
|
|
1788
|
+
|
|
1789
|
+
/**
|
|
1790
|
+
* Gets the multi-item query builder type for a query builder.
|
|
1791
|
+
*/
|
|
1792
|
+
type ArrayQueryBuilder<QB extends AnyQueryBuilder> = QB['~many'];
|
|
1793
|
+
|
|
1794
|
+
/**
|
|
1795
|
+
* Gets the number query builder type for a query builder.
|
|
1796
|
+
*/
|
|
1797
|
+
type NumberQueryBuilder<QB extends AnyQueryBuilder> = QB['~count'];
|
|
1798
|
+
|
|
1799
|
+
/**
|
|
1800
|
+
* Gets the page query builder type for a query builder.
|
|
1801
|
+
*/
|
|
1802
|
+
type PageQueryBuilder<QB extends AnyQueryBuilder> = QB['~page'];
|
|
1803
|
+
|
|
1804
|
+
// `runAfter` replaces the result with whatever the callback returns, also
|
|
1805
|
+
// when it returns nothing.
|
|
1806
|
+
type RunAfterResult<T> = [Awaited<T>] extends [void] ? undefined : Awaited<T>;
|
|
1807
|
+
|
|
1808
|
+
// Replaces the item type of result `R` with `I`, keeping the result kind.
|
|
1809
|
+
type ReplaceResultItem<R, I> =
|
|
1810
|
+
IsAnyType<R> extends true ? any : [R] extends [never] ? never : ReplaceResultMember<R, I>;
|
|
1811
|
+
|
|
1812
|
+
// Each member of a union like `Person[] | Person | number | undefined`.
|
|
1813
|
+
type ReplaceResultMember<R, I> = R extends number
|
|
1814
|
+
? number
|
|
1815
|
+
: R extends readonly any[]
|
|
1816
|
+
? I[]
|
|
1817
|
+
: R extends Page<any>
|
|
1818
|
+
? Page<I>
|
|
1819
|
+
: R extends undefined | null
|
|
1820
|
+
? R
|
|
1821
|
+
: I;
|
|
1822
|
+
|
|
1823
|
+
/**
|
|
1824
|
+
* The query builder type of the static `query()` method of model class `MC`.
|
|
1825
|
+
* This is based on the static `QueryBuilder` property, which decides the
|
|
1826
|
+
* query builder class at runtime.
|
|
1827
|
+
*/
|
|
1828
|
+
// `I` is the type of the rows.
|
|
1829
|
+
type ModelClassQueryBuilder<MC, M extends Model, I = ModelRow<M>> = MC extends {
|
|
1830
|
+
QueryBuilder: { prototype: infer QB };
|
|
1831
|
+
}
|
|
1832
|
+
? keyof QueryBuilderType<M> extends keyof QB
|
|
1833
|
+
? // The static `QueryBuilder` creates query builders of the declared
|
|
1834
|
+
// `QueryBuilderType` (only the keys are compared, which is much faster).
|
|
1835
|
+
RowQueryBuilder<QueryBuilderType<M>, M, I>
|
|
1836
|
+
: // `QueryBuilderType` was declared, but the static `QueryBuilder` creates
|
|
1837
|
+
// some other query builders. Trust the class that is used at runtime.
|
|
1838
|
+
InitialQueryBuilder<QB, M, I>
|
|
1839
|
+
: RowQueryBuilder<QueryBuilderType<M>, M, I>;
|
|
1840
|
+
|
|
1841
|
+
type InitialQueryBuilder<QB, M extends Model, I> = QB extends { '~rebind': any }
|
|
1842
|
+
? (QB['~rebind'] & { '~args': [M, I[], QueryBuilderItemState<I>] })['~rebind']
|
|
1843
|
+
: QB;
|
|
1844
|
+
|
|
1845
|
+
interface ForClassMethod {
|
|
1846
|
+
<M extends Model>(modelClass: ModelConstructor<M>): RowQueryBuilder<QueryBuilderType<M>, M>;
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1849
|
+
/**
|
|
1850
|
+
* https://vincit.github.io/objection.js/api/types/#type-fieldexpression
|
|
1851
|
+
*/
|
|
1852
|
+
type FieldExpression = string;
|
|
1853
|
+
|
|
1854
|
+
type JsonObjectOrFieldExpression = object | object[] | FieldExpression;
|
|
1855
|
+
|
|
1856
|
+
type Selection<QB extends AnyQueryBuilder> = ColumnRef | AnyQueryBuilder | CallbackVoid<QB>;
|
|
1857
|
+
|
|
1858
|
+
interface SelectMethod<QB extends AnyQueryBuilder> {
|
|
1859
|
+
// These must come first so that we get autocomplete.
|
|
1860
|
+
<QBP extends QB>(...columns: ModelProps<ModelType<QBP>>[]): QB;
|
|
1861
|
+
<QBP extends QB>(columns: ModelProps<ModelType<QBP>>[]): QB;
|
|
1862
|
+
|
|
1863
|
+
<QBP extends QB>(...columns: Selection<QBP>[]): QB;
|
|
1864
|
+
<QBP extends QB>(columns: Selection<QBP>[]): QB;
|
|
1865
|
+
|
|
1866
|
+
// Allows things like `select(1)`, not sure if we should be more specific here?
|
|
1867
|
+
<QBP extends QB>(...args: any[]): QB;
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
interface AsMethod<QB extends AnyQueryBuilder> {
|
|
1871
|
+
(alias: string): QB;
|
|
1872
|
+
}
|
|
1873
|
+
|
|
1874
|
+
interface FromMethod<QB extends AnyQueryBuilder> {
|
|
1875
|
+
(table: TableRef<QB>): QB;
|
|
1876
|
+
}
|
|
1877
|
+
|
|
1878
|
+
interface FromRawMethod<QB extends AnyQueryBuilder> extends RawInterface<QB> {}
|
|
1879
|
+
|
|
1880
|
+
interface JsonExtraction {
|
|
1881
|
+
column: string | Raw | Knex.QueryBuilder;
|
|
1882
|
+
path: string;
|
|
1883
|
+
alias?: string;
|
|
1884
|
+
singleValue?: boolean;
|
|
1885
|
+
}
|
|
1886
|
+
|
|
1887
|
+
interface JsonExtract<QB extends AnyQueryBuilder> {
|
|
1888
|
+
// These must come first so that we get autocomplete.
|
|
1889
|
+
<QBP extends QB>(
|
|
1890
|
+
column: ModelProps<ModelType<QBP>>,
|
|
1891
|
+
path: string,
|
|
1892
|
+
alias?: string,
|
|
1893
|
+
singleValue?: boolean,
|
|
1894
|
+
): QB;
|
|
1895
|
+
|
|
1896
|
+
(column: ColumnRef, path: string, alias?: string, singleValue?: boolean): QB;
|
|
1897
|
+
(column: JsonExtraction[] | any[][], singleValue?: boolean): QB;
|
|
1898
|
+
}
|
|
1899
|
+
|
|
1900
|
+
interface JsonSet<QB extends AnyQueryBuilder> {
|
|
1901
|
+
// These must come first so that we get autocomplete.
|
|
1902
|
+
<QBP extends QB>(
|
|
1903
|
+
column: ModelProps<ModelType<QBP>>,
|
|
1904
|
+
path: string,
|
|
1905
|
+
value: any,
|
|
1906
|
+
alias?: string,
|
|
1907
|
+
): QB;
|
|
1908
|
+
|
|
1909
|
+
(column: ColumnRef, path: string, value: any, alias?: string): QB;
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
interface JsonInsert<QB extends AnyQueryBuilder> {
|
|
1913
|
+
// These must come first so that we get autocomplete.
|
|
1914
|
+
<QBP extends QB>(
|
|
1915
|
+
column: ModelProps<ModelType<QBP>>,
|
|
1916
|
+
path: string,
|
|
1917
|
+
value: any,
|
|
1918
|
+
alias?: string,
|
|
1919
|
+
): QB;
|
|
1920
|
+
|
|
1921
|
+
(column: ColumnRef, path: string, value: any, alias?: string): QB;
|
|
1922
|
+
}
|
|
1923
|
+
|
|
1924
|
+
interface JsonRemove<QB extends AnyQueryBuilder> {
|
|
1925
|
+
// These must come first so that we get autocomplete.
|
|
1926
|
+
<QBP extends QB>(column: ModelProps<ModelType<QBP>>, path: string, alias?: string): QB;
|
|
1927
|
+
|
|
1928
|
+
(column: ColumnRef, path: string, alias?: string): QB;
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
interface WhereMethod<QB extends AnyQueryBuilder> {
|
|
1932
|
+
// These must come first so that we get autocomplete.
|
|
1933
|
+
<QBP extends QB>(
|
|
1934
|
+
col: ModelProps<ModelType<QBP>>,
|
|
1935
|
+
op: Operator,
|
|
1936
|
+
expr: Expression<PrimitiveValue>,
|
|
1937
|
+
): QB;
|
|
1938
|
+
|
|
1939
|
+
<QBP extends QB>(col: ModelProps<ModelType<QBP>>, expr: Expression<PrimitiveValue>): QB;
|
|
1940
|
+
|
|
1941
|
+
(col: ColumnRef, op: Operator, expr: Expression<PrimitiveValue>): QB;
|
|
1942
|
+
(col: ColumnRef, expr: Expression<PrimitiveValue>): QB;
|
|
1943
|
+
|
|
1944
|
+
(condition: boolean): QB;
|
|
1945
|
+
(cb: CallbackVoid<QB>): QB;
|
|
1946
|
+
(raw: Raw): QB;
|
|
1947
|
+
<QBA extends AnyQueryBuilder>(qb: QBA): QB;
|
|
1948
|
+
|
|
1949
|
+
(obj: PartialModelObject<ModelType<QB>>): QB;
|
|
1950
|
+
// We must allow any keys in the object. The previous type
|
|
1951
|
+
// is kind of useless, but maybe one day vscode and other
|
|
1952
|
+
// tools can autocomplete using it.
|
|
1953
|
+
(obj: object): QB;
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
interface WhereRawMethod<QB extends AnyQueryBuilder> extends RawInterface<QB> {}
|
|
1957
|
+
|
|
1958
|
+
interface WhereWrappedMethod<QB extends AnyQueryBuilder> {
|
|
1959
|
+
(cb: CallbackVoid<QB>): QB;
|
|
1960
|
+
}
|
|
1961
|
+
|
|
1962
|
+
interface WhereExistsMethod<QB extends AnyQueryBuilder> {
|
|
1963
|
+
(cb: CallbackVoid<QB>): QB;
|
|
1964
|
+
(raw: Raw): QB;
|
|
1965
|
+
<QBA extends AnyQueryBuilder>(qb: QBA): QB;
|
|
1966
|
+
}
|
|
1967
|
+
|
|
1968
|
+
interface WhereInMethod<QB extends AnyQueryBuilder> {
|
|
1969
|
+
// These must come first so that we get autocomplete.
|
|
1970
|
+
<QBP extends QB>(col: ModelProps<ModelType<QBP>>, expr: Expression<PrimitiveValue>): QB;
|
|
1971
|
+
<QBP extends QB>(col: ModelProps<ModelType<QBP>>, cb: CallbackVoid<QB>): QB;
|
|
1972
|
+
<QBP extends QB>(col: ModelProps<ModelType<QBP>>, qb: AnyQueryBuilder): QB;
|
|
1973
|
+
|
|
1974
|
+
(col: ColumnRef | ColumnRef[], expr: readonly Expression<PrimitiveValue>[]): QB;
|
|
1975
|
+
(col: ColumnRef | ColumnRef[], cb: CallbackVoid<QB>): QB;
|
|
1976
|
+
(col: ColumnRef | ColumnRef[], qb: AnyQueryBuilder): QB;
|
|
1977
|
+
}
|
|
1978
|
+
|
|
1979
|
+
interface WhereBetweenMethod<QB extends AnyQueryBuilder> {
|
|
1980
|
+
(column: ColumnRef, range: [Expression<PrimitiveValue>, Expression<PrimitiveValue>]): QB;
|
|
1981
|
+
}
|
|
1982
|
+
|
|
1983
|
+
interface WhereNullMethod<QB extends AnyQueryBuilder> {
|
|
1984
|
+
(column: ColumnRef): QB;
|
|
1985
|
+
}
|
|
1986
|
+
|
|
1987
|
+
interface WhereColumnMethod<QB extends AnyQueryBuilder> {
|
|
1988
|
+
// These must come first so that we get autocomplete.
|
|
1989
|
+
<QBP extends QB>(col1: ModelProps<ModelType<QBP>>, op: Operator, col2: ColumnRef): QB;
|
|
1990
|
+
<QBP extends QB>(col1: ModelProps<ModelType<QBP>>, col2: ColumnRef): QB;
|
|
1991
|
+
|
|
1992
|
+
(col1: ColumnRef, op: Operator, col2: ColumnRef): QB;
|
|
1993
|
+
(col1: ColumnRef, col2: ColumnRef): QB;
|
|
1994
|
+
}
|
|
1995
|
+
|
|
1996
|
+
interface WhereJsonObject<QB extends AnyQueryBuilder> {
|
|
1997
|
+
// These must come first so that we get autocomplete.
|
|
1998
|
+
<QBP extends QB>(col: ModelProps<ModelType<QBP>>, value: any): QB;
|
|
1999
|
+
|
|
2000
|
+
(col: ColumnRef, value: any): QB;
|
|
2001
|
+
}
|
|
2002
|
+
|
|
2003
|
+
interface WhereJsonPath<QB extends AnyQueryBuilder> {
|
|
2004
|
+
// These must come first so that we get autocomplete.
|
|
2005
|
+
<QBP extends QB>(
|
|
2006
|
+
col: ModelProps<ModelType<QBP>>,
|
|
2007
|
+
jsonPath: string,
|
|
2008
|
+
operator: string,
|
|
2009
|
+
value: any,
|
|
2010
|
+
): QB;
|
|
2011
|
+
|
|
2012
|
+
(col: ColumnRef, jsonPath: string, operator: string, value: any): QB;
|
|
2013
|
+
}
|
|
2014
|
+
|
|
2015
|
+
interface WhereJsonMethod<QB extends AnyQueryBuilder> {
|
|
2016
|
+
(
|
|
2017
|
+
fieldExpression: FieldExpression,
|
|
2018
|
+
jsonObjectOrFieldExpression: JsonObjectOrFieldExpression,
|
|
2019
|
+
): QB;
|
|
2020
|
+
}
|
|
2021
|
+
|
|
2022
|
+
interface WhereFieldExpressionMethod<QB extends AnyQueryBuilder> {
|
|
2023
|
+
(fieldExpression: FieldExpression): QB;
|
|
2024
|
+
}
|
|
2025
|
+
|
|
2026
|
+
interface WhereJsonExpressionMethod<QB extends AnyQueryBuilder> {
|
|
2027
|
+
(fieldExpression: FieldExpression, keys: string | string[]): QB;
|
|
2028
|
+
}
|
|
2029
|
+
|
|
2030
|
+
interface WhereJsonField<QB extends AnyQueryBuilder> {
|
|
2031
|
+
(
|
|
2032
|
+
fieldExpression: FieldExpression,
|
|
2033
|
+
operator: string,
|
|
2034
|
+
value: boolean | number | string | null,
|
|
2035
|
+
): QB;
|
|
2036
|
+
}
|
|
2037
|
+
|
|
2038
|
+
interface WhereCompositeMethod<QB extends AnyQueryBuilder> {
|
|
2039
|
+
(column: ColumnRef[], op: Operator, expr: readonly Expression<PrimitiveValue>[]): QB;
|
|
2040
|
+
(column: ColumnRef, expr: Expression<PrimitiveValue>): QB;
|
|
2041
|
+
(column: ColumnRef, op: Operator, expr: Expression<PrimitiveValue>): QB;
|
|
2042
|
+
(column: ColumnRef[], expr: readonly Expression<PrimitiveValue>[]): QB;
|
|
2043
|
+
(column: ColumnRef[], qb: AnyQueryBuilder): QB;
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2046
|
+
interface WhereInCompositeMethod<QB extends AnyQueryBuilder> {
|
|
2047
|
+
(column: ColumnRef, expr: readonly Expression<PrimitiveValue>[]): QB;
|
|
2048
|
+
(column: ColumnRef, qb: AnyQueryBuilder): QB;
|
|
2049
|
+
(column: ColumnRef[], expr: readonly Expression<PrimitiveValue>[][]): QB;
|
|
2050
|
+
(column: ColumnRef[], qb: AnyQueryBuilder): QB;
|
|
2051
|
+
}
|
|
2052
|
+
|
|
2053
|
+
type QBOrCallback<QB extends AnyQueryBuilder> = AnyQueryBuilder | CallbackVoid<QB>;
|
|
2054
|
+
|
|
2055
|
+
interface BaseSetOperations<QB extends AnyQueryBuilder> {
|
|
2056
|
+
(callbackOrBuilder: QBOrCallback<QB>, wrap?: boolean): QB;
|
|
2057
|
+
(callbacksOrBuilders: QBOrCallback<QB>[], wrap?: boolean): QB;
|
|
2058
|
+
}
|
|
2059
|
+
|
|
2060
|
+
interface SetOperationsMethod<QB extends AnyQueryBuilder> extends BaseSetOperations<QB> {
|
|
2061
|
+
(...callbacksOrBuilders: QBOrCallback<QB>[]): QB;
|
|
2062
|
+
}
|
|
2063
|
+
|
|
2064
|
+
interface UnionMethod<QB extends AnyQueryBuilder> extends BaseSetOperations<QB> {
|
|
2065
|
+
(arg1: QBOrCallback<QB>, wrap?: boolean): QB;
|
|
2066
|
+
(arg1: QBOrCallback<QB>, arg2: QBOrCallback<QB>, wrap?: boolean): QB;
|
|
2067
|
+
(arg1: QBOrCallback<QB>, arg2: QBOrCallback<QB>, arg3: QBOrCallback<QB>, wrap?: boolean): QB;
|
|
2068
|
+
(
|
|
2069
|
+
arg1: QBOrCallback<QB>,
|
|
2070
|
+
arg2: QBOrCallback<QB>,
|
|
2071
|
+
arg3: QBOrCallback<QB>,
|
|
2072
|
+
arg4: QBOrCallback<QB>,
|
|
2073
|
+
wrap?: boolean,
|
|
2074
|
+
): QB;
|
|
2075
|
+
(
|
|
2076
|
+
arg1: QBOrCallback<QB>,
|
|
2077
|
+
arg2: QBOrCallback<QB>,
|
|
2078
|
+
arg3: QBOrCallback<QB>,
|
|
2079
|
+
arg4: QBOrCallback<QB>,
|
|
2080
|
+
arg5: QBOrCallback<QB>,
|
|
2081
|
+
wrap?: boolean,
|
|
2082
|
+
): QB;
|
|
2083
|
+
(
|
|
2084
|
+
arg1: QBOrCallback<QB>,
|
|
2085
|
+
arg2: QBOrCallback<QB>,
|
|
2086
|
+
arg3: QBOrCallback<QB>,
|
|
2087
|
+
arg4: QBOrCallback<QB>,
|
|
2088
|
+
arg5: QBOrCallback<QB>,
|
|
2089
|
+
arg6: QBOrCallback<QB>,
|
|
2090
|
+
wrap?: boolean,
|
|
2091
|
+
): QB;
|
|
2092
|
+
(
|
|
2093
|
+
arg1: QBOrCallback<QB>,
|
|
2094
|
+
arg2: QBOrCallback<QB>,
|
|
2095
|
+
arg3: QBOrCallback<QB>,
|
|
2096
|
+
arg4: QBOrCallback<QB>,
|
|
2097
|
+
arg5: QBOrCallback<QB>,
|
|
2098
|
+
arg6: QBOrCallback<QB>,
|
|
2099
|
+
arg7: QBOrCallback<QB>,
|
|
2100
|
+
wrap?: boolean,
|
|
2101
|
+
): QB;
|
|
2102
|
+
}
|
|
2103
|
+
|
|
2104
|
+
interface WithMethod<QB extends AnyQueryBuilder> {
|
|
2105
|
+
(alias: string, expr: CallbackVoid<QB> | AnyQueryBuilder | Raw): QB;
|
|
2106
|
+
}
|
|
2107
|
+
|
|
2108
|
+
interface JoinRelatedOptions {
|
|
2109
|
+
alias?: string | boolean;
|
|
2110
|
+
aliases?: Record<string, string>;
|
|
2111
|
+
}
|
|
2112
|
+
|
|
2113
|
+
interface JoinRelatedMethod<QB extends AnyQueryBuilder> {
|
|
2114
|
+
<const E extends RelationExpression<ModelType<QB>>>(
|
|
2115
|
+
expr: CheckRelationExpression<GraphModel<QB>, E, ChecksModifierNames<QB>>,
|
|
2116
|
+
opt?: JoinRelatedOptions,
|
|
2117
|
+
): QB;
|
|
2118
|
+
}
|
|
2119
|
+
|
|
2120
|
+
interface JoinMethod<QB extends AnyQueryBuilder> {
|
|
2121
|
+
(table: TableRef<QB>, leftCol: ColumnRef, op: Operator, rightCol: ColumnRef): QB;
|
|
2122
|
+
(table: TableRef<QB>, leftCol: ColumnRef, rightCol: ColumnRef): QB;
|
|
2123
|
+
(table: TableRef<QB>, cb: CallbackVoid<JoinBuilder>): QB;
|
|
2124
|
+
(table: TableRef<QB>, raw: Raw): QB;
|
|
2125
|
+
(raw: Raw): QB;
|
|
2126
|
+
}
|
|
2127
|
+
|
|
2128
|
+
interface CrossJoinMethod<QB extends AnyQueryBuilder> extends JoinMethod<QB> {
|
|
2129
|
+
(table: TableRef<QB>): QB;
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
type JoinValue = PrimitiveValue | Raw | ReferenceBuilder | ValueBuilder | AnyQueryBuilder;
|
|
2133
|
+
|
|
2134
|
+
interface JoinOnMethod {
|
|
2135
|
+
(column1: ColumnRef, column2: ColumnRef): JoinBuilder;
|
|
2136
|
+
(column1: ColumnRef, operator: Operator, column2: ColumnRef): JoinBuilder;
|
|
2137
|
+
(columns: { [column: string]: ColumnRef }): JoinBuilder;
|
|
2138
|
+
(callback: CallbackVoid<JoinBuilder>): JoinBuilder;
|
|
2139
|
+
(raw: Raw): JoinBuilder;
|
|
2140
|
+
}
|
|
2141
|
+
|
|
2142
|
+
interface JoinOnValMethod {
|
|
2143
|
+
(column: ColumnRef, value: JoinValue): JoinBuilder;
|
|
2144
|
+
(column: ColumnRef, operator: Operator, value: JoinValue): JoinBuilder;
|
|
2145
|
+
}
|
|
2146
|
+
|
|
2147
|
+
interface JoinOnInMethod {
|
|
2148
|
+
(column: ColumnRef, values: readonly JoinValue[] | Raw | AnyQueryBuilder): JoinBuilder;
|
|
2149
|
+
}
|
|
2150
|
+
|
|
2151
|
+
interface JoinOnNullMethod {
|
|
2152
|
+
(column: ColumnRef): JoinBuilder;
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2155
|
+
interface JoinOnExistsMethod {
|
|
2156
|
+
(query: CallbackVoid<AnyQueryBuilder> | AnyQueryBuilder): JoinBuilder;
|
|
2157
|
+
}
|
|
2158
|
+
|
|
2159
|
+
interface JoinOnBetweenMethod {
|
|
2160
|
+
(column: ColumnRef, range: readonly [JoinValue, JoinValue]): JoinBuilder;
|
|
2161
|
+
}
|
|
2162
|
+
|
|
2163
|
+
interface JoinOnJsonPathEqualsMethod {
|
|
2164
|
+
(
|
|
2165
|
+
columnFirst: ColumnRef,
|
|
2166
|
+
jsonPathFirst: string,
|
|
2167
|
+
columnSecond: ColumnRef,
|
|
2168
|
+
jsonPathSecond: string,
|
|
2169
|
+
): JoinBuilder;
|
|
2170
|
+
}
|
|
2171
|
+
|
|
2172
|
+
/**
|
|
2173
|
+
* The builder passed to join callbacks. Unlike knex's `JoinClause`, every column can
|
|
2174
|
+
* be a `ref()` or `Model.ref()`.
|
|
2175
|
+
*/
|
|
2176
|
+
export interface JoinBuilder {
|
|
2177
|
+
on: JoinOnMethod;
|
|
2178
|
+
andOn: JoinOnMethod;
|
|
2179
|
+
orOn: JoinOnMethod;
|
|
2180
|
+
|
|
2181
|
+
onVal: JoinOnValMethod;
|
|
2182
|
+
andOnVal: JoinOnValMethod;
|
|
2183
|
+
orOnVal: JoinOnValMethod;
|
|
2184
|
+
|
|
2185
|
+
onIn: JoinOnInMethod;
|
|
2186
|
+
andOnIn: JoinOnInMethod;
|
|
2187
|
+
orOnIn: JoinOnInMethod;
|
|
2188
|
+
onNotIn: JoinOnInMethod;
|
|
2189
|
+
andOnNotIn: JoinOnInMethod;
|
|
2190
|
+
orOnNotIn: JoinOnInMethod;
|
|
2191
|
+
|
|
2192
|
+
onNull: JoinOnNullMethod;
|
|
2193
|
+
andOnNull: JoinOnNullMethod;
|
|
2194
|
+
orOnNull: JoinOnNullMethod;
|
|
2195
|
+
onNotNull: JoinOnNullMethod;
|
|
2196
|
+
andOnNotNull: JoinOnNullMethod;
|
|
2197
|
+
orOnNotNull: JoinOnNullMethod;
|
|
2198
|
+
|
|
2199
|
+
onExists: JoinOnExistsMethod;
|
|
2200
|
+
andOnExists: JoinOnExistsMethod;
|
|
2201
|
+
orOnExists: JoinOnExistsMethod;
|
|
2202
|
+
onNotExists: JoinOnExistsMethod;
|
|
2203
|
+
andOnNotExists: JoinOnExistsMethod;
|
|
2204
|
+
orOnNotExists: JoinOnExistsMethod;
|
|
2205
|
+
|
|
2206
|
+
onBetween: JoinOnBetweenMethod;
|
|
2207
|
+
andOnBetween: JoinOnBetweenMethod;
|
|
2208
|
+
orOnBetween: JoinOnBetweenMethod;
|
|
2209
|
+
onNotBetween: JoinOnBetweenMethod;
|
|
2210
|
+
andOnNotBetween: JoinOnBetweenMethod;
|
|
2211
|
+
orOnNotBetween: JoinOnBetweenMethod;
|
|
2212
|
+
|
|
2213
|
+
onJsonPathEquals: JoinOnJsonPathEqualsMethod;
|
|
2214
|
+
andOnJsonPathEquals: JoinOnJsonPathEqualsMethod;
|
|
2215
|
+
orOnJsonPathEquals: JoinOnJsonPathEqualsMethod;
|
|
2216
|
+
|
|
2217
|
+
using(columns: string | readonly string[] | Raw | { [key: string]: string | Raw }): JoinBuilder;
|
|
2218
|
+
type(type: string): JoinBuilder;
|
|
2219
|
+
|
|
2220
|
+
/** Makes the next condition an `or`: `join.on(a, b).or.on(c, d)`. */
|
|
2221
|
+
readonly or: JoinBuilder;
|
|
2222
|
+
}
|
|
2223
|
+
|
|
2224
|
+
interface JoinRawMethod<QB extends AnyQueryBuilder> extends RawInterface<QB> {}
|
|
2225
|
+
|
|
2226
|
+
interface IncrementDecrementMethod<QB extends AnyQueryBuilder> {
|
|
2227
|
+
(column: string, amount?: number): QB;
|
|
2228
|
+
}
|
|
2229
|
+
|
|
2230
|
+
interface AggregateMethod<QB extends AnyQueryBuilder> {
|
|
2231
|
+
(column: ColumnRef): QB;
|
|
2232
|
+
}
|
|
2233
|
+
|
|
2234
|
+
interface CountMethod<QB extends AnyQueryBuilder> {
|
|
2235
|
+
(column?: ColumnRef, options?: { as: string }): QB;
|
|
2236
|
+
(aliasToColumnDict: { [alias: string]: string | string[] }): QB;
|
|
2237
|
+
(...columns: ColumnRef[]): QB;
|
|
2238
|
+
}
|
|
2239
|
+
|
|
2240
|
+
interface GroupByMethod<QB extends AnyQueryBuilder> {
|
|
2241
|
+
(...columns: ColumnRef[]): QB;
|
|
2242
|
+
(columns: ColumnRef[]): QB;
|
|
2243
|
+
}
|
|
2244
|
+
|
|
2245
|
+
interface OrderByDescriptor {
|
|
2246
|
+
column: ColumnRef;
|
|
2247
|
+
order?: OrderByDirection;
|
|
2248
|
+
nulls?: OrderByNulls;
|
|
2249
|
+
}
|
|
2250
|
+
|
|
2251
|
+
type ColumnRefOrOrderByDescriptor = ColumnRef | OrderByDescriptor;
|
|
2252
|
+
|
|
2253
|
+
interface OrderByMethod<QB extends AnyQueryBuilder> {
|
|
2254
|
+
(column: ColumnRef, order?: OrderByDirection, nulls?: OrderByNulls): QB;
|
|
2255
|
+
(columns: ColumnRefOrOrderByDescriptor[]): QB;
|
|
2256
|
+
}
|
|
2257
|
+
|
|
2258
|
+
interface OrderByRawMethod<QB extends AnyQueryBuilder> extends RawInterface<QB> {}
|
|
2259
|
+
|
|
2260
|
+
interface FirstMethod {
|
|
2261
|
+
<QB extends AnyQueryBuilder>(
|
|
2262
|
+
this: QB,
|
|
2263
|
+
): [ResultType<QB>] extends [readonly any[]] ? MaybeSingleQueryBuilder<QB> : QB;
|
|
2264
|
+
}
|
|
2265
|
+
|
|
2266
|
+
type ForIdValue = MaybeCompositeId | AnyQueryBuilder;
|
|
2267
|
+
|
|
2268
|
+
interface WithGraphMethod<QB extends AnyQueryBuilder> {
|
|
2269
|
+
<const E extends RelationExpression<ModelType<QB>>>(
|
|
2270
|
+
expr: CheckRelationExpression<GraphModel<QB>, E, ChecksModifierNames<QB>>,
|
|
2271
|
+
options?: GraphOptions,
|
|
2272
|
+
): WithGraphQueryBuilder<QB, E>;
|
|
2273
|
+
}
|
|
2274
|
+
|
|
2275
|
+
// The model whose relations the graph methods of `QB` use, or `any` if they're
|
|
2276
|
+
// not known.
|
|
2277
|
+
type GraphModel<QB extends AnyQueryBuilder> = QB['~state'] extends { cast: true }
|
|
2278
|
+
? any
|
|
2279
|
+
: RelationsModel<ModelType<QB>>;
|
|
2280
|
+
|
|
2281
|
+
// The model with the relations of `M`. Generic code like
|
|
2282
|
+
// `<M extends Model>(query: QueryBuilder<M, M[]>)` can infer a row type as the
|
|
2283
|
+
// model type, but rows still have the `QueryBuilderType` of their model.
|
|
2284
|
+
type RelationsModel<M> =
|
|
2285
|
+
IsAnyType<M> extends true
|
|
2286
|
+
? any
|
|
2287
|
+
: M extends { QueryBuilderType: { ModelType: infer RM } }
|
|
2288
|
+
? IsAnyType<RM> extends true
|
|
2289
|
+
? M
|
|
2290
|
+
: RM
|
|
2291
|
+
: M;
|
|
2292
|
+
|
|
2293
|
+
interface AllowGraphMethod<QB extends AnyQueryBuilder> {
|
|
2294
|
+
<const E extends RelationExpression<ModelType<QB>>>(
|
|
2295
|
+
expr: CheckRelationExpression<GraphModel<QB>, E, ChecksModifierNames<QB>>,
|
|
2296
|
+
): QB;
|
|
2297
|
+
}
|
|
2298
|
+
|
|
2299
|
+
interface IdentityMethod<QB extends AnyQueryBuilder> {
|
|
2300
|
+
(): QB;
|
|
2301
|
+
}
|
|
2302
|
+
|
|
2303
|
+
interface OneArgMethod<T, QB extends AnyQueryBuilder> {
|
|
2304
|
+
(arg: T): QB;
|
|
2305
|
+
}
|
|
2306
|
+
|
|
2307
|
+
interface StringReturningMethod {
|
|
2308
|
+
(): string;
|
|
2309
|
+
}
|
|
2310
|
+
|
|
2311
|
+
interface BooleanReturningMethod {
|
|
2312
|
+
(): boolean;
|
|
2313
|
+
}
|
|
2314
|
+
|
|
2315
|
+
interface HasMethod {
|
|
2316
|
+
(selector: string | RegExp): boolean;
|
|
2317
|
+
}
|
|
2318
|
+
|
|
2319
|
+
interface ClearMethod<QB extends AnyQueryBuilder> {
|
|
2320
|
+
(selector: string | RegExp): QB;
|
|
2321
|
+
}
|
|
2322
|
+
|
|
2323
|
+
interface ColumnInfoMethod<QB extends AnyQueryBuilder> {
|
|
2324
|
+
(): Promise<Knex.ColumnInfo>;
|
|
2325
|
+
}
|
|
2326
|
+
|
|
2327
|
+
interface TableRefForMethod {
|
|
2328
|
+
(modelClass: ModelClass<Model> | typeof Model): string;
|
|
2329
|
+
}
|
|
2330
|
+
|
|
2331
|
+
interface AliasForMethod<QB extends AnyQueryBuilder> {
|
|
2332
|
+
(modelClassOrTableName: string | AnyModelConstructor, alias: string): QB;
|
|
2333
|
+
}
|
|
2334
|
+
|
|
2335
|
+
interface ModelClassMethod<M extends Model> {
|
|
2336
|
+
(): ModelClass<M>;
|
|
2337
|
+
}
|
|
2338
|
+
|
|
2339
|
+
interface ReturningMethod {
|
|
2340
|
+
<QB extends AnyQueryBuilder>(
|
|
2341
|
+
this: QB,
|
|
2342
|
+
column: string | Raw | (string | Raw)[],
|
|
2343
|
+
): [ResultType<QB>] extends [number] ? ArrayQueryBuilder<QB> : QB;
|
|
2344
|
+
}
|
|
2345
|
+
|
|
2346
|
+
interface TimeoutOptions {
|
|
2347
|
+
cancel: boolean;
|
|
2348
|
+
}
|
|
2349
|
+
|
|
2350
|
+
interface TimeoutMethod<QB extends AnyQueryBuilder> {
|
|
2351
|
+
(ms: number, options?: TimeoutOptions): QB;
|
|
2352
|
+
}
|
|
2353
|
+
|
|
2354
|
+
export interface Page<T> {
|
|
2355
|
+
total: number;
|
|
2356
|
+
results: T[];
|
|
2357
|
+
}
|
|
2358
|
+
|
|
2359
|
+
interface RunBeforeCallback<QB extends AnyQueryBuilder> {
|
|
2360
|
+
(this: QB, result: any, query: QB): any;
|
|
2361
|
+
}
|
|
2362
|
+
|
|
2363
|
+
interface RunBeforeMethod<QB extends AnyQueryBuilder> {
|
|
2364
|
+
(cb: RunBeforeCallback<QB>): QB;
|
|
2365
|
+
}
|
|
2366
|
+
|
|
2367
|
+
interface RunAfterCallback<QB extends AnyQueryBuilder, T = unknown> {
|
|
2368
|
+
(this: QB, result: ResultType<QB>, query: QB): T;
|
|
2369
|
+
}
|
|
2370
|
+
|
|
2371
|
+
interface RunAfterMethod<QB extends AnyQueryBuilder> {
|
|
2372
|
+
// The callback's return value replaces the result of the query.
|
|
2373
|
+
<T>(
|
|
2374
|
+
cb: RunAfterCallback<QB, T>,
|
|
2375
|
+
): RebindQueryBuilder<
|
|
2376
|
+
QB,
|
|
2377
|
+
ModelType<QB>,
|
|
2378
|
+
RunAfterResult<T>,
|
|
2379
|
+
QueryBuilderItemState<ResultItemOf<RunAfterResult<T>, ModelType<QB>>>
|
|
2380
|
+
>;
|
|
2381
|
+
}
|
|
2382
|
+
|
|
2383
|
+
interface OnBuildMethod<QB extends AnyQueryBuilder> {
|
|
2384
|
+
(cb: CallbackVoid<QB>): QB;
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
interface OnBuildKnexCallback<QB extends AnyQueryBuilder> {
|
|
2388
|
+
(this: QB, knexQuery: Knex.QueryBuilder, query: QB): void;
|
|
2389
|
+
}
|
|
2390
|
+
|
|
2391
|
+
interface OnBuildKnexMethod<QB extends AnyQueryBuilder> {
|
|
2392
|
+
(cb: OnBuildKnexCallback<QB>): QB;
|
|
2393
|
+
}
|
|
2394
|
+
|
|
2395
|
+
interface OnErrorCallback<QB extends AnyQueryBuilder> {
|
|
2396
|
+
(this: QB, error: Error, query: QB): any;
|
|
2397
|
+
}
|
|
2398
|
+
|
|
2399
|
+
interface OnErrorMethod<QB extends AnyQueryBuilder> {
|
|
2400
|
+
(cb: OnErrorCallback<QB>): QB;
|
|
2401
|
+
}
|
|
2402
|
+
|
|
2403
|
+
export interface InsertGraphOptions {
|
|
2404
|
+
relate?: boolean | string[];
|
|
2405
|
+
allowRefs?: boolean;
|
|
2406
|
+
}
|
|
2407
|
+
|
|
2408
|
+
// The result of a graph insert or upsert has the relations of the input graph.
|
|
2409
|
+
interface InsertGraphMethod<M extends Model> {
|
|
2410
|
+
<QB extends AnyQueryBuilder>(
|
|
2411
|
+
this: QB,
|
|
2412
|
+
graph: PartialModelGraph<M>,
|
|
2413
|
+
options?: InsertGraphOptions,
|
|
2414
|
+
): SingleQueryBuilder<OpaqueQueryBuilder<QB>>;
|
|
2415
|
+
|
|
2416
|
+
<QB extends AnyQueryBuilder>(
|
|
2417
|
+
this: QB,
|
|
2418
|
+
graph: PartialModelGraph<M>[],
|
|
2419
|
+
options?: InsertGraphOptions,
|
|
2420
|
+
): ArrayQueryBuilder<OpaqueQueryBuilder<QB>>;
|
|
2421
|
+
}
|
|
2422
|
+
|
|
2423
|
+
export interface UpsertGraphOptions {
|
|
2424
|
+
relate?: boolean | string[];
|
|
2425
|
+
unrelate?: boolean | string[];
|
|
2426
|
+
insertMissing?: boolean | string[];
|
|
2427
|
+
update?: boolean | string[];
|
|
2428
|
+
noInsert?: boolean | string[];
|
|
2429
|
+
noUpdate?: boolean | string[];
|
|
2430
|
+
noDelete?: boolean | string[];
|
|
2431
|
+
noRelate?: boolean | string[];
|
|
2432
|
+
noUnrelate?: boolean | string[];
|
|
2433
|
+
allowRefs?: boolean;
|
|
2434
|
+
}
|
|
2435
|
+
|
|
2436
|
+
interface UpsertGraphMethod<M extends Model> {
|
|
2437
|
+
<QB extends AnyQueryBuilder>(
|
|
2438
|
+
this: QB,
|
|
2439
|
+
graph: PartialModelGraph<M>[],
|
|
2440
|
+
options?: UpsertGraphOptions,
|
|
2441
|
+
): ArrayQueryBuilder<OpaqueQueryBuilder<QB>>;
|
|
2442
|
+
|
|
2443
|
+
<QB extends AnyQueryBuilder>(
|
|
2444
|
+
this: QB,
|
|
2445
|
+
graph: PartialModelGraph<M>,
|
|
2446
|
+
options?: UpsertGraphOptions,
|
|
2447
|
+
): SingleQueryBuilder<OpaqueQueryBuilder<QB>>;
|
|
2448
|
+
}
|
|
2449
|
+
|
|
2450
|
+
interface GraphExpressionObjectMethod<QB extends AnyQueryBuilder> {
|
|
2451
|
+
(): any;
|
|
2452
|
+
}
|
|
2453
|
+
|
|
2454
|
+
export interface GraphOptions {
|
|
2455
|
+
minimize?: boolean;
|
|
2456
|
+
separator?: string;
|
|
2457
|
+
aliases?: { [key: string]: string };
|
|
2458
|
+
joinOperation?: string;
|
|
2459
|
+
maxBatchSize?: number;
|
|
2460
|
+
}
|
|
2461
|
+
|
|
2462
|
+
// The expression refers to the relations of the graph by their names, which can
|
|
2463
|
+
// be aliases. The modifier gets the query builder of the related model when the
|
|
2464
|
+
// expression is a single relation path like `'pets.toys'`.
|
|
2465
|
+
interface ModifyGraphMethod<QB extends AnyQueryBuilder> {
|
|
2466
|
+
// With the related model type given explicitly: `modifyGraph<Pet>(expr, modifier)`.
|
|
2467
|
+
<RM extends Model = never>(
|
|
2468
|
+
expr: [RM] extends [never] ? never : RelationExpression<ModelType<QB>>,
|
|
2469
|
+
modifier: Modifier<QueryBuilderType<RM>>,
|
|
2470
|
+
): QB;
|
|
2471
|
+
<const E extends RelationExpression<ModelType<QB>>>(
|
|
2472
|
+
expr: CheckGraphPath<QB['~state']['item'], GraphModel<QB>, E>,
|
|
2473
|
+
modifier: Modifier<GraphPathQueryBuilder<QB['~state']['item'], GraphModel<QB>, E>>,
|
|
2474
|
+
): QB;
|
|
2475
|
+
}
|
|
2476
|
+
|
|
2477
|
+
type CheckGraphPath<I, M, E> =
|
|
2478
|
+
ParseRelationExpression<E> extends infer G
|
|
2479
|
+
? G extends ParseError<infer Message>
|
|
2480
|
+
? Message
|
|
2481
|
+
: G extends GraphNode
|
|
2482
|
+
? GraphPathErrors<I, M, G['children'], ''> extends infer Errors
|
|
2483
|
+
? [Errors] extends [never]
|
|
2484
|
+
? E
|
|
2485
|
+
: Errors
|
|
2486
|
+
: never
|
|
2487
|
+
: E
|
|
2488
|
+
: never;
|
|
2489
|
+
|
|
2490
|
+
type GraphPathErrors<I, M, Children, Path extends string> =
|
|
2491
|
+
IsUncheckedModel<M> extends true
|
|
2492
|
+
? never
|
|
2493
|
+
: {
|
|
2494
|
+
[P in keyof Children & string]: Children[P] extends GraphNode
|
|
2495
|
+
? GraphPathStep<I, M, P, Children[P]> extends infer Step
|
|
2496
|
+
? [Step] extends [never]
|
|
2497
|
+
? Path extends `${infer Parent}.`
|
|
2498
|
+
? `Unknown relation "${P}" at "${Parent}"`
|
|
2499
|
+
: `Unknown relation "${P}"`
|
|
2500
|
+
: Step extends [infer NextI, infer RM]
|
|
2501
|
+
? GraphPathErrors<NextI, RM, Children[P]['children'], `${Path}${P}.`>
|
|
2502
|
+
: never
|
|
2503
|
+
: never
|
|
2504
|
+
: never;
|
|
2505
|
+
}[keyof Children & string];
|
|
2506
|
+
|
|
2507
|
+
// The item and the model of the relation called `P` in the graph of item `I`:
|
|
2508
|
+
// a relation of `M`, or an alias that was loaded.
|
|
2509
|
+
type GraphPathStep<I, M, P, N extends GraphNode> =
|
|
2510
|
+
N['rel'] extends RelationNameKeys<M>
|
|
2511
|
+
? [
|
|
2512
|
+
[PreviousRow<I, P>] extends [never]
|
|
2513
|
+
? ModelRow<RelatedModel<M, N['rel']>>
|
|
2514
|
+
: PreviousRow<I, P>,
|
|
2515
|
+
RelatedModel<M, N['rel']>,
|
|
2516
|
+
]
|
|
2517
|
+
: P extends keyof I
|
|
2518
|
+
? ElementOrSelf<NonNullable<I[P]>> extends infer Row
|
|
2519
|
+
? [Row, ModelOfRow<Row>]
|
|
2520
|
+
: never
|
|
2521
|
+
: never;
|
|
2522
|
+
|
|
2523
|
+
type ModelOfRow<Row> = Row extends { QueryBuilderType: { ModelType: infer RM } } ? RM : Model;
|
|
2524
|
+
|
|
2525
|
+
type GraphPathQueryBuilder<I, M, E> =
|
|
2526
|
+
ParseRelationExpression<E> extends infer G extends GraphNode
|
|
2527
|
+
? GraphPathTarget<I, M, G['children']> extends infer Target
|
|
2528
|
+
? [Target] extends [never]
|
|
2529
|
+
? AnyQueryBuilder
|
|
2530
|
+
: Target extends [any, infer RM extends Model]
|
|
2531
|
+
? RowQueryBuilder<QueryBuilderType<RM>, RM>
|
|
2532
|
+
: AnyQueryBuilder
|
|
2533
|
+
: AnyQueryBuilder
|
|
2534
|
+
: AnyQueryBuilder;
|
|
2535
|
+
|
|
2536
|
+
// The item and the model at the end of a single relation path.
|
|
2537
|
+
type GraphPathTarget<I, M, Children> = [keyof Children] extends [never]
|
|
2538
|
+
? never
|
|
2539
|
+
: IsUnion<keyof Children> extends true
|
|
2540
|
+
? never
|
|
2541
|
+
: {
|
|
2542
|
+
[P in keyof Children]: Children[P] extends GraphNode
|
|
2543
|
+
? GraphPathStep<I, M, P, Children[P]> extends [infer NextI, infer RM]
|
|
2544
|
+
? [keyof Children[P]['children']] extends [never]
|
|
2545
|
+
? [NextI, RM]
|
|
2546
|
+
: GraphPathTarget<NextI, RM, Children[P]['children']>
|
|
2547
|
+
: never
|
|
2548
|
+
: never;
|
|
2549
|
+
}[keyof Children];
|
|
2550
|
+
|
|
2551
|
+
type IsUnion<T, U = T> = T extends any ? ([U] extends [T] ? false : true) : never;
|
|
2552
|
+
|
|
2553
|
+
interface ContextMethod<QB extends AnyQueryBuilder> {
|
|
2554
|
+
(context: object): QB;
|
|
2555
|
+
(): QueryContext;
|
|
2556
|
+
}
|
|
2557
|
+
|
|
2558
|
+
interface ClearContextMethod<QB extends AnyQueryBuilder> {
|
|
2559
|
+
(): QB;
|
|
2560
|
+
}
|
|
2561
|
+
|
|
2562
|
+
type ModifierArgs<F> = F extends (qb: any, ...args: infer A) => any ? A : unknown[];
|
|
2563
|
+
|
|
2564
|
+
// A modifier function written for models of type `PM` can be used with any
|
|
2565
|
+
// query builder for models of type `PM` regardless of the result kind. Only
|
|
2566
|
+
// the model type of the query builder parameter is checked.
|
|
2567
|
+
type CheckModifierModel<QB extends AnyQueryBuilder, F> = F extends (
|
|
2568
|
+
qb: infer P,
|
|
2569
|
+
...args: any[]
|
|
2570
|
+
) => any
|
|
2571
|
+
? P extends { ModelType: infer PM }
|
|
2572
|
+
? IsAnyType<P> extends true
|
|
2573
|
+
? unknown
|
|
2574
|
+
: QB['ModelType'] extends PM
|
|
2575
|
+
? unknown
|
|
2576
|
+
: // Modifiers for a subtype of the model, like a row type with
|
|
2577
|
+
// computed columns, are allowed too.
|
|
2578
|
+
PM extends QB['ModelType']
|
|
2579
|
+
? unknown
|
|
2580
|
+
: { '~error': 'The modifier is for a different model than the query' }
|
|
2581
|
+
: unknown
|
|
2582
|
+
: unknown;
|
|
2583
|
+
|
|
2584
|
+
// Modifiers may load relations the types can't see, so the result may contain
|
|
2585
|
+
// any relation of the model.
|
|
2586
|
+
interface ModifyMethod<QB extends AnyQueryBuilder> {
|
|
2587
|
+
<Ret>(modifier: (qb: QB) => Ret): ModifiedQueryBuilder<QB, Ret>;
|
|
2588
|
+
<F extends (qb: any, ...args: any[]) => unknown>(
|
|
2589
|
+
modifier: F & CheckModifierModel<QB, F>,
|
|
2590
|
+
...args: ModifierArgs<F>
|
|
2591
|
+
): ModifiedQueryBuilder<QB, ReturnType<F>>;
|
|
2592
|
+
(modifier: object & Record<string, Expression<PrimitiveValue>>): OpaqueQueryBuilder<QB>;
|
|
2593
|
+
// The arguments are passed to every modifier of the list.
|
|
2594
|
+
<const L extends readonly (string | ((qb: any, ...args: any[]) => unknown))[]>(
|
|
2595
|
+
modifiers: L & CheckModifierList<QB, L>,
|
|
2596
|
+
...args: unknown[]
|
|
2597
|
+
): OpaqueQueryBuilder<QB>;
|
|
2598
|
+
// Named modifiers are checked when the model's static `modifiers` are known.
|
|
2599
|
+
<const N extends string>(
|
|
2600
|
+
modifier: N & CheckModifierName<QB, N>,
|
|
2601
|
+
...args: NamedModifierArgs<QB, N>
|
|
2602
|
+
): OpaqueQueryBuilder<QB>;
|
|
2603
|
+
}
|
|
2604
|
+
|
|
2605
|
+
// Names are checked unless the query builder is opaque: something the types
|
|
2606
|
+
// can't see may have added modifiers with `modifiers()`.
|
|
2607
|
+
type ChecksModifierNames<QB extends AnyQueryBuilder> = QB['~state'] extends { opaque: true }
|
|
2608
|
+
? false
|
|
2609
|
+
: true;
|
|
2610
|
+
|
|
2611
|
+
type QueryModifiers<QB extends AnyQueryBuilder> =
|
|
2612
|
+
ChecksModifierNames<QB> extends true ? KnownModifiers<ModelType<QB>> : never;
|
|
2613
|
+
|
|
2614
|
+
type ModifierNameOf<QB extends AnyQueryBuilder> = [QueryModifiers<QB>] extends [never]
|
|
2615
|
+
? string
|
|
2616
|
+
: keyof QueryModifiers<QB> & string;
|
|
2617
|
+
|
|
2618
|
+
type NamedModifierArgs<QB extends AnyQueryBuilder, N> = [QueryModifiers<QB>] extends [never]
|
|
2619
|
+
? unknown[]
|
|
2620
|
+
: N extends keyof QueryModifiers<QB>
|
|
2621
|
+
? ModifierArgs<QueryModifiers<QB>[N]>
|
|
2622
|
+
: unknown[];
|
|
2623
|
+
|
|
2624
|
+
type CheckModifierName<QB extends AnyQueryBuilder, N> =
|
|
2625
|
+
N extends ModifierNameOf<QB> ? unknown : { '~error': `Unknown modifier "${N & string}"` };
|
|
2626
|
+
|
|
2627
|
+
type CheckModifierList<QB extends AnyQueryBuilder, L> = {
|
|
2628
|
+
[I in keyof L]: L[I] extends string
|
|
2629
|
+
? L[I] extends ModifierNameOf<QB>
|
|
2630
|
+
? unknown
|
|
2631
|
+
: { '~error': `Unknown modifier "${L[I]}"` }
|
|
2632
|
+
: CheckModifierModel<QB, L[I]>;
|
|
2633
|
+
};
|
|
2634
|
+
|
|
2635
|
+
interface ModifiersMethod<QB extends AnyQueryBuilder> {
|
|
2636
|
+
// Named modifiers added here aren't known to the types, so they aren't checked
|
|
2637
|
+
// after this.
|
|
2638
|
+
(modifiers: Modifiers): OpaqueQueryBuilder<QB>;
|
|
2639
|
+
(): QB;
|
|
2640
|
+
}
|
|
2641
|
+
|
|
2642
|
+
export interface Pojo {
|
|
2643
|
+
[key: string]: any;
|
|
2644
|
+
}
|
|
2645
|
+
|
|
2646
|
+
export interface CatchablePromiseLike<R> extends PromiseLike<R> {
|
|
2647
|
+
catch<FR = never>(
|
|
2648
|
+
onrejected?: ((reason: any) => FR | PromiseLike<FR>) | undefined | null,
|
|
2649
|
+
): Promise<R | FR>;
|
|
2650
|
+
}
|
|
2651
|
+
|
|
2652
|
+
export class QueryBuilder<
|
|
2653
|
+
M extends Model,
|
|
2654
|
+
R = M[],
|
|
2655
|
+
S extends QueryBuilderState = DefaultQueryBuilderState<M, R>,
|
|
2656
|
+
> implements CatchablePromiseLike<R>
|
|
2657
|
+
{
|
|
2658
|
+
static forClass: ForClassMethod;
|
|
2659
|
+
|
|
2660
|
+
/**
|
|
2661
|
+
* Type-only members used to compute the type of the query builder after
|
|
2662
|
+
* methods that change the result type, like `first()` or `patch()`.
|
|
2663
|
+
*
|
|
2664
|
+
* A custom query builder subclass only needs to redeclare `'~rebind'`:
|
|
2665
|
+
*
|
|
2666
|
+
* ```ts
|
|
2667
|
+
* class MyQueryBuilder<
|
|
2668
|
+
* M extends Model,
|
|
2669
|
+
* R = M[],
|
|
2670
|
+
* S extends QueryBuilderState = DefaultQueryBuilderState<M, R>,
|
|
2671
|
+
* > extends QueryBuilder<M, R, S> {
|
|
2672
|
+
* declare '~rebind': MyQueryBuilder<this['~M'], this['~R'], this['~S']>;
|
|
2673
|
+
* }
|
|
2674
|
+
* ```
|
|
2675
|
+
*/
|
|
2676
|
+
'~args': unknown;
|
|
2677
|
+
'~M': this['~args'] extends [any, any, any] ? this['~args'][0] : any;
|
|
2678
|
+
'~R': this['~args'] extends [any, any, any] ? this['~args'][1] : any;
|
|
2679
|
+
'~S': this['~args'] extends [any, any, any] ? this['~args'][2] : any;
|
|
2680
|
+
'~rebind': QueryBuilder<this['~M'], this['~R'], this['~S']>;
|
|
2681
|
+
|
|
2682
|
+
// The query builder types for each result kind. These are members (instead
|
|
2683
|
+
// of type aliases) so that comparing a custom query builder to its base
|
|
2684
|
+
// class doesn't need to expand them.
|
|
2685
|
+
// The members use `this` instead of the type parameters, so that they are
|
|
2686
|
+
// the same when TypeScript compares two query builder types.
|
|
2687
|
+
'~state': S;
|
|
2688
|
+
'~many': RebindQueryBuilder<this, this['ModelType'], this['~state']['item'][], this['~state']>;
|
|
2689
|
+
'~maybe': RebindQueryBuilder<
|
|
2690
|
+
this,
|
|
2691
|
+
this['ModelType'],
|
|
2692
|
+
this['~state']['item'] | undefined,
|
|
2693
|
+
this['~state']
|
|
2694
|
+
>;
|
|
2695
|
+
'~one': RebindQueryBuilder<this, this['ModelType'], this['~state']['item'], this['~state']>;
|
|
2696
|
+
'~count': RebindQueryBuilder<this, this['ModelType'], number, this['~state']>;
|
|
2697
|
+
'~page': RebindQueryBuilder<
|
|
2698
|
+
this,
|
|
2699
|
+
this['ModelType'],
|
|
2700
|
+
Page<this['~state']['item']>,
|
|
2701
|
+
this['~state']
|
|
2702
|
+
>;
|
|
2703
|
+
|
|
2704
|
+
select: SelectMethod<this>;
|
|
2705
|
+
columns: SelectMethod<this>;
|
|
2706
|
+
column: SelectMethod<this>;
|
|
2707
|
+
distinct: SelectMethod<this>;
|
|
2708
|
+
distinctOn: SelectMethod<this>;
|
|
2709
|
+
as: AsMethod<this>;
|
|
2710
|
+
|
|
2711
|
+
from: FromMethod<this>;
|
|
2712
|
+
table: FromMethod<this>;
|
|
2713
|
+
into: FromMethod<this>;
|
|
2714
|
+
/** The table to update from (`update ... from`), postgres only. */
|
|
2715
|
+
updateFrom: FromMethod<this>;
|
|
2716
|
+
/** Tables to delete using (`delete ... using`), postgres only. */
|
|
2717
|
+
using(tables: string | readonly string[]): this;
|
|
2718
|
+
|
|
2719
|
+
/** Adds an SQL comment before the query: `/* comment *\/ select ...`. */
|
|
2720
|
+
comment(comment: string): this;
|
|
2721
|
+
/** Adds optimizer hints to the query: `select /*+ hint *\/ ...`. */
|
|
2722
|
+
hintComment(hints: string | readonly string[]): this;
|
|
2723
|
+
/** Options passed to the database driver. */
|
|
2724
|
+
options(options: Readonly<{ [key: string]: any }>): this;
|
|
2725
|
+
fromRaw: FromRawMethod<this>;
|
|
2726
|
+
|
|
2727
|
+
jsonExtract: JsonExtract<this>;
|
|
2728
|
+
jsonSet: JsonSet<this>;
|
|
2729
|
+
jsonInsert: JsonInsert<this>;
|
|
2730
|
+
jsonRemove: JsonRemove<this>;
|
|
2731
|
+
|
|
2732
|
+
where: WhereMethod<this>;
|
|
2733
|
+
andWhere: WhereMethod<this>;
|
|
2734
|
+
orWhere: WhereMethod<this>;
|
|
2735
|
+
whereNot: WhereMethod<this>;
|
|
2736
|
+
andWhereNot: WhereMethod<this>;
|
|
2737
|
+
orWhereNot: WhereMethod<this>;
|
|
2738
|
+
whereLike: WhereMethod<this>;
|
|
2739
|
+
andWhereLike: WhereMethod<this>;
|
|
2740
|
+
orWhereLike: WhereMethod<this>;
|
|
2741
|
+
whereILike: WhereMethod<this>;
|
|
2742
|
+
andWhereILike: WhereMethod<this>;
|
|
2743
|
+
orWhereILike: WhereMethod<this>;
|
|
2744
|
+
|
|
2745
|
+
/**
|
|
2746
|
+
* Makes the query match no rows: `where (1 = 0)`.
|
|
2747
|
+
*/
|
|
2748
|
+
none(): this;
|
|
2749
|
+
whereRaw: WhereRawMethod<this>;
|
|
2750
|
+
orWhereRaw: WhereRawMethod<this>;
|
|
2751
|
+
andWhereRaw: WhereRawMethod<this>;
|
|
2752
|
+
|
|
2753
|
+
whereWrapped: WhereWrappedMethod<this>;
|
|
2754
|
+
havingWrapped: WhereWrappedMethod<this>;
|
|
2755
|
+
|
|
2756
|
+
whereExists: WhereExistsMethod<this>;
|
|
2757
|
+
orWhereExists: WhereExistsMethod<this>;
|
|
2758
|
+
whereNotExists: WhereExistsMethod<this>;
|
|
2759
|
+
orWhereNotExists: WhereExistsMethod<this>;
|
|
2760
|
+
|
|
2761
|
+
whereIn: WhereInMethod<this>;
|
|
2762
|
+
orWhereIn: WhereInMethod<this>;
|
|
2763
|
+
whereNotIn: WhereInMethod<this>;
|
|
2764
|
+
orWhereNotIn: WhereInMethod<this>;
|
|
2765
|
+
|
|
2766
|
+
whereBetween: WhereBetweenMethod<this>;
|
|
2767
|
+
orWhereBetween: WhereBetweenMethod<this>;
|
|
2768
|
+
andWhereBetween: WhereBetweenMethod<this>;
|
|
2769
|
+
whereNotBetween: WhereBetweenMethod<this>;
|
|
2770
|
+
orWhereNotBetween: WhereBetweenMethod<this>;
|
|
2771
|
+
andWhereNotBetween: WhereBetweenMethod<this>;
|
|
2772
|
+
|
|
2773
|
+
whereNull: WhereNullMethod<this>;
|
|
2774
|
+
orWhereNull: WhereNullMethod<this>;
|
|
2775
|
+
whereNotNull: WhereNullMethod<this>;
|
|
2776
|
+
orWhereNotNull: WhereNullMethod<this>;
|
|
2777
|
+
|
|
2778
|
+
whereColumn: WhereColumnMethod<this>;
|
|
2779
|
+
orWhereColumn: WhereColumnMethod<this>;
|
|
2780
|
+
andWhereColumn: WhereColumnMethod<this>;
|
|
2781
|
+
whereNotColumn: WhereColumnMethod<this>;
|
|
2782
|
+
orWhereNotColumn: WhereColumnMethod<this>;
|
|
2783
|
+
andWhereNotColumn: WhereColumnMethod<this>;
|
|
2784
|
+
|
|
2785
|
+
whereJsonObject: WhereJsonObject<this>;
|
|
2786
|
+
orWhereJsonObject: WhereJsonObject<this>;
|
|
2787
|
+
andWhereJsonObject: WhereJsonObject<this>;
|
|
2788
|
+
whereNotJsonObject: WhereJsonObject<this>;
|
|
2789
|
+
orWhereNotJsonObject: WhereJsonObject<this>;
|
|
2790
|
+
andWhereNotJsonObject: WhereJsonObject<this>;
|
|
2791
|
+
|
|
2792
|
+
whereJsonPath: WhereJsonPath<this>;
|
|
2793
|
+
orWhereJsonPath: WhereJsonPath<this>;
|
|
2794
|
+
andWhereJsonPath: WhereJsonPath<this>;
|
|
2795
|
+
|
|
2796
|
+
whereJsonSupersetOf: WhereJsonMethod<this>;
|
|
2797
|
+
andWhereJsonSupersetOf: WhereJsonMethod<this>;
|
|
2798
|
+
orWhereJsonSupersetOf: WhereJsonMethod<this>;
|
|
2799
|
+
whereJsonNotSupersetOf: WhereJsonMethod<this>;
|
|
2800
|
+
andWhereJsonNotSupersetOf: WhereJsonMethod<this>;
|
|
2801
|
+
orWhereJsonNotSupersetOf: WhereJsonMethod<this>;
|
|
2802
|
+
whereJsonSubsetOf: WhereJsonMethod<this>;
|
|
2803
|
+
andWhereJsonSubsetOf: WhereJsonMethod<this>;
|
|
2804
|
+
orWhereJsonSubsetOf: WhereJsonMethod<this>;
|
|
2805
|
+
whereJsonNotSubsetOf: WhereJsonMethod<this>;
|
|
2806
|
+
andWhereJsonNotSubsetOf: WhereJsonMethod<this>;
|
|
2807
|
+
orWhereJsonNotSubsetOf: WhereJsonMethod<this>;
|
|
2808
|
+
whereJsonIsArray: WhereFieldExpressionMethod<this>;
|
|
2809
|
+
orWhereJsonIsArray: WhereFieldExpressionMethod<this>;
|
|
2810
|
+
whereJsonNotArray: WhereFieldExpressionMethod<this>;
|
|
2811
|
+
orWhereJsonNotArray: WhereFieldExpressionMethod<this>;
|
|
2812
|
+
whereJsonIsObject: WhereFieldExpressionMethod<this>;
|
|
2813
|
+
orWhereJsonIsObject: WhereFieldExpressionMethod<this>;
|
|
2814
|
+
whereJsonNotObject: WhereFieldExpressionMethod<this>;
|
|
2815
|
+
orWhereJsonNotObject: WhereFieldExpressionMethod<this>;
|
|
2816
|
+
/**
|
|
2817
|
+
* MySQL only: `JSON_OVERLAPS(field, value)`. True when two arrays share an item,
|
|
2818
|
+
* two objects share a key-value pair, or an array contains a scalar.
|
|
2819
|
+
*/
|
|
2820
|
+
whereJsonOverlaps: WhereJsonMethod<this>;
|
|
2821
|
+
andWhereJsonOverlaps: WhereJsonMethod<this>;
|
|
2822
|
+
orWhereJsonOverlaps: WhereJsonMethod<this>;
|
|
2823
|
+
/**
|
|
2824
|
+
* MySQL only: the negation of `whereJsonOverlaps`. A NULL or missing field doesn't
|
|
2825
|
+
* overlap anything, so it's found.
|
|
2826
|
+
*/
|
|
2827
|
+
whereJsonNotOverlaps: WhereJsonMethod<this>;
|
|
2828
|
+
andWhereJsonNotOverlaps: WhereJsonMethod<this>;
|
|
2829
|
+
orWhereJsonNotOverlaps: WhereJsonMethod<this>;
|
|
2830
|
+
whereJsonHasAny: WhereJsonExpressionMethod<this>;
|
|
2831
|
+
orWhereJsonHasAny: WhereJsonExpressionMethod<this>;
|
|
2832
|
+
whereJsonHasAll: WhereJsonExpressionMethod<this>;
|
|
2833
|
+
orWhereJsonHasAll: WhereJsonExpressionMethod<this>;
|
|
2834
|
+
|
|
2835
|
+
having: WhereMethod<this>;
|
|
2836
|
+
andHaving: WhereMethod<this>;
|
|
2837
|
+
orHaving: WhereMethod<this>;
|
|
2838
|
+
|
|
2839
|
+
havingRaw: WhereRawMethod<this>;
|
|
2840
|
+
orHavingRaw: WhereRawMethod<this>;
|
|
2841
|
+
|
|
2842
|
+
havingIn: WhereInMethod<this>;
|
|
2843
|
+
andHavingIn: WhereInMethod<this>;
|
|
2844
|
+
orHavingIn: WhereInMethod<this>;
|
|
2845
|
+
havingNotIn: WhereInMethod<this>;
|
|
2846
|
+
andHavingNotIn: WhereInMethod<this>;
|
|
2847
|
+
orHavingNotIn: WhereInMethod<this>;
|
|
2848
|
+
|
|
2849
|
+
havingNull: WhereNullMethod<this>;
|
|
2850
|
+
andHavingNull: WhereNullMethod<this>;
|
|
2851
|
+
orHavingNull: WhereNullMethod<this>;
|
|
2852
|
+
havingNotNull: WhereNullMethod<this>;
|
|
2853
|
+
andHavingNotNull: WhereNullMethod<this>;
|
|
2854
|
+
orHavingNotNull: WhereNullMethod<this>;
|
|
2855
|
+
|
|
2856
|
+
havingExists: WhereExistsMethod<this>;
|
|
2857
|
+
andHavingExists: WhereExistsMethod<this>;
|
|
2858
|
+
orHavingExists: WhereExistsMethod<this>;
|
|
2859
|
+
havingNotExists: WhereExistsMethod<this>;
|
|
2860
|
+
andHavingNotExists: WhereExistsMethod<this>;
|
|
2861
|
+
orHavingNotExists: WhereExistsMethod<this>;
|
|
2862
|
+
|
|
2863
|
+
havingBetween: WhereBetweenMethod<this>;
|
|
2864
|
+
andHavingBetween: WhereBetweenMethod<this>;
|
|
2865
|
+
orHavingBetween: WhereBetweenMethod<this>;
|
|
2866
|
+
havingNotBetween: WhereBetweenMethod<this>;
|
|
2867
|
+
andHavingNotBetween: WhereBetweenMethod<this>;
|
|
2868
|
+
orHavingNotBetween: WhereBetweenMethod<this>;
|
|
2869
|
+
|
|
2870
|
+
whereComposite: WhereCompositeMethod<this>;
|
|
2871
|
+
whereInComposite: WhereInCompositeMethod<this>;
|
|
2872
|
+
whereNotInComposite: WhereInCompositeMethod<this>;
|
|
2873
|
+
|
|
2874
|
+
union: UnionMethod<this>;
|
|
2875
|
+
unionAll: UnionMethod<this>;
|
|
2876
|
+
intersect: SetOperationsMethod<this>;
|
|
2877
|
+
except: SetOperationsMethod<this>;
|
|
2878
|
+
|
|
2879
|
+
with: WithMethod<this>;
|
|
2880
|
+
withRecursive: WithMethod<this>;
|
|
2881
|
+
withWrapped: WithMethod<this>;
|
|
2882
|
+
withMaterialized: WithMethod<this>;
|
|
2883
|
+
withNotMaterialized: WithMethod<this>;
|
|
2884
|
+
|
|
2885
|
+
joinRelated: JoinRelatedMethod<this>;
|
|
2886
|
+
innerJoinRelated: JoinRelatedMethod<this>;
|
|
2887
|
+
outerJoinRelated: JoinRelatedMethod<this>;
|
|
2888
|
+
leftJoinRelated: JoinRelatedMethod<this>;
|
|
2889
|
+
leftOuterJoinRelated: JoinRelatedMethod<this>;
|
|
2890
|
+
rightJoinRelated: JoinRelatedMethod<this>;
|
|
2891
|
+
rightOuterJoinRelated: JoinRelatedMethod<this>;
|
|
2892
|
+
fullOuterJoinRelated: JoinRelatedMethod<this>;
|
|
2893
|
+
|
|
2894
|
+
join: JoinMethod<this>;
|
|
2895
|
+
joinRaw: JoinRawMethod<this>;
|
|
2896
|
+
innerJoin: JoinMethod<this>;
|
|
2897
|
+
leftJoin: JoinMethod<this>;
|
|
2898
|
+
leftOuterJoin: JoinMethod<this>;
|
|
2899
|
+
rightJoin: JoinMethod<this>;
|
|
2900
|
+
rightOuterJoin: JoinMethod<this>;
|
|
2901
|
+
outerJoin: JoinMethod<this>;
|
|
2902
|
+
fullOuterJoin: JoinMethod<this>;
|
|
2903
|
+
crossJoin: CrossJoinMethod<this>;
|
|
2904
|
+
|
|
2905
|
+
count: CountMethod<this>;
|
|
2906
|
+
countDistinct: CountMethod<this>;
|
|
2907
|
+
min: AggregateMethod<this>;
|
|
2908
|
+
max: AggregateMethod<this>;
|
|
2909
|
+
sum: AggregateMethod<this>;
|
|
2910
|
+
sumDistinct: AggregateMethod<this>;
|
|
2911
|
+
avg: AggregateMethod<this>;
|
|
2912
|
+
avgDistinct: AggregateMethod<this>;
|
|
2913
|
+
increment: IncrementDecrementMethod<this>;
|
|
2914
|
+
decrement: IncrementDecrementMethod<this>;
|
|
2915
|
+
first(): IsAnyType<R> extends true ? any : [R] extends [readonly any[]] ? this['~maybe'] : this;
|
|
2916
|
+
|
|
2917
|
+
orderBy: OrderByMethod<this>;
|
|
2918
|
+
orderByRaw: OrderByRawMethod<this>;
|
|
2919
|
+
|
|
2920
|
+
groupBy: GroupByMethod<this>;
|
|
2921
|
+
groupByRaw: RawInterface<this>;
|
|
2922
|
+
|
|
2923
|
+
findById(id: MaybeCompositeId): this['~maybe'];
|
|
2924
|
+
findByIds(ids: MaybeCompositeId[]): this;
|
|
2925
|
+
findOne: WhereMethod<this['~maybe']>;
|
|
2926
|
+
|
|
2927
|
+
execute(): Promise<R>;
|
|
2928
|
+
// `castTo()` only changes the types. The relations of the graph methods are
|
|
2929
|
+
// still the relations of the original model, so they aren't checked after it.
|
|
2930
|
+
castTo<MC extends Model>(
|
|
2931
|
+
modelClass: ModelConstructor<MC>,
|
|
2932
|
+
): RebindQueryBuilder<this, MC, ReplaceResultItem<R, MC>, CastItemState<MC>>;
|
|
2933
|
+
castTo<T>(): RebindQueryBuilder<this, M, T, QueryBuilderItemState<ResultItemOf<T, M>>>;
|
|
2934
|
+
|
|
2935
|
+
// For generic code (like custom query builder methods) where the model type
|
|
2936
|
+
// isn't known: `this.update<{ deletedAt: Date }>({ deletedAt: new Date() })`.
|
|
2937
|
+
// These overloads come first so that the errors of the other overloads are
|
|
2938
|
+
// the ones that are reported.
|
|
2939
|
+
update<T extends object = never>(update: NoInfer<T>): this['~count'];
|
|
2940
|
+
update<U extends object>(update: U & CheckModelUpdate<M, U>): this['~count'];
|
|
2941
|
+
update(update: PartialModelObject<M>): this['~count'];
|
|
2942
|
+
update(): this['~count'];
|
|
2943
|
+
updateAndFetch<U extends object>(update: U & CheckModelUpdate<M, U>): this['~one'];
|
|
2944
|
+
updateAndFetch(update: PartialModelObject<M>): this['~one'];
|
|
2945
|
+
updateAndFetchById<U extends object>(
|
|
2946
|
+
id: MaybeCompositeId,
|
|
2947
|
+
update: U & CheckModelUpdate<M, U>,
|
|
2948
|
+
): this['~one'];
|
|
2949
|
+
updateAndFetchById(id: MaybeCompositeId, update: PartialModelObject<M>): this['~one'];
|
|
2950
|
+
|
|
2951
|
+
// For generic code (like custom query builder methods) where the model type
|
|
2952
|
+
// isn't known: `this.patch<{ deletedAt: Date }>({ deletedAt: new Date() })`.
|
|
2953
|
+
patch<T extends object = never>(update: NoInfer<T>): this['~count'];
|
|
2954
|
+
patch<U extends object>(update: U & CheckModelUpdate<M, U>): this['~count'];
|
|
2955
|
+
patch(update: PartialModelObject<M>): this['~count'];
|
|
2956
|
+
patch(): this['~count'];
|
|
2957
|
+
patchAndFetch<U extends object>(update: U & CheckModelUpdate<M, U>): this['~one'];
|
|
2958
|
+
patchAndFetch(update: PartialModelObject<M>): this['~one'];
|
|
2959
|
+
patchAndFetchById<U extends object>(
|
|
2960
|
+
id: MaybeCompositeId,
|
|
2961
|
+
update: U & CheckModelUpdate<M, U>,
|
|
2962
|
+
): this['~one'];
|
|
2963
|
+
patchAndFetchById(id: MaybeCompositeId, update: PartialModelObject<M>): this['~one'];
|
|
2964
|
+
|
|
2965
|
+
del(): this['~count'];
|
|
2966
|
+
delete(): this['~count'];
|
|
2967
|
+
deleteById(id: MaybeCompositeId): this['~count'];
|
|
2968
|
+
|
|
2969
|
+
// For generic code where the model type isn't known.
|
|
2970
|
+
insert<T extends object = never>(insert: NoInfer<T>): this['~one'];
|
|
2971
|
+
insert<T extends object = never>(insert: NoInfer<T>[]): this['~many'];
|
|
2972
|
+
insert(insert: PartialModelObject<M>[]): this['~many'];
|
|
2973
|
+
insert(): this['~one'];
|
|
2974
|
+
// The error of the last overload is reported, so this one is last.
|
|
2975
|
+
insert(insert: PartialModelObject<M>): this['~one'];
|
|
2976
|
+
|
|
2977
|
+
onConflict(column?: ColumnRef | ColumnRef[] | true): this;
|
|
2978
|
+
ignore(): this;
|
|
2979
|
+
merge<U extends object>(merge: U & CheckModelUpdate<M, U>): this;
|
|
2980
|
+
merge(merge?: PartialModelObject<M> | string[]): this;
|
|
2981
|
+
|
|
2982
|
+
insertAndFetch(insert: PartialModelObject<M>): this['~one'];
|
|
2983
|
+
insertAndFetch(insert: PartialModelObject<M>[]): this['~many'];
|
|
2984
|
+
insertAndFetch(): this['~one'];
|
|
2985
|
+
|
|
2986
|
+
relate(
|
|
2987
|
+
ids: MaybeCompositeId | MaybeCompositeId[] | PartialModelObject<M> | PartialModelObject<M>[],
|
|
2988
|
+
): this['~count'];
|
|
2989
|
+
|
|
2990
|
+
unrelate(): this['~count'];
|
|
2991
|
+
for(ids: ForIdValue | ForIdValue[]): this;
|
|
2992
|
+
|
|
2993
|
+
withGraphFetched: WithGraphMethod<this>;
|
|
2994
|
+
withGraphJoined: WithGraphMethod<this>;
|
|
2995
|
+
|
|
2996
|
+
truncate(): Promise<void>;
|
|
2997
|
+
allowGraph: AllowGraphMethod<this>;
|
|
2998
|
+
|
|
2999
|
+
throwIfNotFound(arg?: any): IsSingleResult<R> extends true ? this['~one'] : this;
|
|
3000
|
+
|
|
3001
|
+
returning(
|
|
3002
|
+
column: string | Raw | (string | Raw)[],
|
|
3003
|
+
): IsAnyType<R> extends true ? any : [R] extends [number] ? this['~many'] : this;
|
|
3004
|
+
forUpdate: IdentityMethod<this>;
|
|
3005
|
+
forShare: IdentityMethod<this>;
|
|
3006
|
+
forNoKeyUpdate: IdentityMethod<this>;
|
|
3007
|
+
forKeyShare: IdentityMethod<this>;
|
|
3008
|
+
skipLocked: IdentityMethod<this>;
|
|
3009
|
+
noWait: IdentityMethod<this>;
|
|
3010
|
+
skipUndefined: IdentityMethod<this>;
|
|
3011
|
+
debug: IdentityMethod<this>;
|
|
3012
|
+
alias: OneArgMethod<string, this>;
|
|
3013
|
+
aliasFor: AliasForMethod<this>;
|
|
3014
|
+
withSchema: OneArgMethod<string, this>;
|
|
3015
|
+
modelClass: ModelClassMethod<M>;
|
|
3016
|
+
tableNameFor: TableRefForMethod;
|
|
3017
|
+
tableRefFor: TableRefForMethod;
|
|
3018
|
+
reject: OneArgMethod<any, this>;
|
|
3019
|
+
resolve: OneArgMethod<any, this>;
|
|
3020
|
+
transacting: OneArgMethod<TransactionOrKnex, this>;
|
|
3021
|
+
connection: OneArgMethod<TransactionOrKnex, this>;
|
|
3022
|
+
timeout: TimeoutMethod<this>;
|
|
3023
|
+
columnInfo: ColumnInfoMethod<this>;
|
|
3024
|
+
|
|
3025
|
+
toKnexQuery<T extends {} = ModelObject<M>>(): Knex.QueryBuilder<T, T[]>;
|
|
3026
|
+
knex(knex?: Knex): Knex;
|
|
3027
|
+
clone(): this;
|
|
3028
|
+
|
|
3029
|
+
page(page: number, pageSize: number): this['~page'];
|
|
3030
|
+
range(): this['~page'];
|
|
3031
|
+
range(start: number, end: number): this['~page'];
|
|
3032
|
+
offset(offset: number): this;
|
|
3033
|
+
limit(limit: number): this;
|
|
3034
|
+
resultSize(): Promise<number>;
|
|
3035
|
+
|
|
3036
|
+
runBefore: RunBeforeMethod<this>;
|
|
3037
|
+
runAfter: RunAfterMethod<this>;
|
|
3038
|
+
|
|
3039
|
+
onBuild: OnBuildMethod<this>;
|
|
3040
|
+
onBuildKnex: OnBuildKnexMethod<this>;
|
|
3041
|
+
onError: OnErrorMethod<this>;
|
|
3042
|
+
|
|
3043
|
+
insertGraph: InsertGraphMethod<M>;
|
|
3044
|
+
insertGraphAndFetch: InsertGraphMethod<M>;
|
|
3045
|
+
|
|
3046
|
+
upsertGraph: UpsertGraphMethod<M>;
|
|
3047
|
+
upsertGraphAndFetch: UpsertGraphMethod<M>;
|
|
3048
|
+
|
|
3049
|
+
graphExpressionObject: GraphExpressionObjectMethod<this>;
|
|
3050
|
+
|
|
3051
|
+
modifyGraph: ModifyGraphMethod<this>;
|
|
3052
|
+
|
|
3053
|
+
context: ContextMethod<this>;
|
|
3054
|
+
clearContext: ClearContextMethod<this>;
|
|
3055
|
+
|
|
3056
|
+
modify: ModifyMethod<this>;
|
|
3057
|
+
modifiers: ModifiersMethod<this>;
|
|
3058
|
+
|
|
3059
|
+
isFind: BooleanReturningMethod;
|
|
3060
|
+
isExecutable: BooleanReturningMethod;
|
|
3061
|
+
isInsert: BooleanReturningMethod;
|
|
3062
|
+
isUpdate: BooleanReturningMethod;
|
|
3063
|
+
isDelete: BooleanReturningMethod;
|
|
3064
|
+
isRelate: BooleanReturningMethod;
|
|
3065
|
+
isUnrelate: BooleanReturningMethod;
|
|
3066
|
+
isInternal: BooleanReturningMethod;
|
|
3067
|
+
hasWheres: BooleanReturningMethod;
|
|
3068
|
+
hasSelects: BooleanReturningMethod;
|
|
3069
|
+
hasWithGraph: BooleanReturningMethod;
|
|
3070
|
+
|
|
3071
|
+
has: HasMethod;
|
|
3072
|
+
clear: ClearMethod<this>;
|
|
3073
|
+
|
|
3074
|
+
clearSelect: IdentityMethod<this>;
|
|
3075
|
+
clearOrder: IdentityMethod<this>;
|
|
3076
|
+
clearWhere: IdentityMethod<this>;
|
|
3077
|
+
clearGroup: IdentityMethod<this>;
|
|
3078
|
+
clearHaving: IdentityMethod<this>;
|
|
3079
|
+
clearWithGraph: IdentityMethod<this>;
|
|
3080
|
+
clearAllowGraph: IdentityMethod<this>;
|
|
3081
|
+
|
|
3082
|
+
ModelType: M;
|
|
3083
|
+
ResultType: R;
|
|
3084
|
+
|
|
3085
|
+
then<R1 = R, R2 = never>(
|
|
3086
|
+
onfulfilled?: ((value: R) => R1 | PromiseLike<R1>) | undefined | null,
|
|
3087
|
+
onrejected?: ((reason: any) => R2 | PromiseLike<R2>) | undefined | null,
|
|
3088
|
+
): Promise<R1 | R2>;
|
|
3089
|
+
|
|
3090
|
+
catch<FR = never>(
|
|
3091
|
+
onrejected?: ((reason: any) => FR | PromiseLike<FR>) | undefined | null,
|
|
3092
|
+
): Promise<R | FR>;
|
|
3093
|
+
|
|
3094
|
+
finally(onfinally?: (() => void) | undefined | null): Promise<R>;
|
|
3095
|
+
}
|
|
3096
|
+
|
|
3097
|
+
type X<T> = Promise<T>;
|
|
3098
|
+
|
|
3099
|
+
interface FetchGraphOptions {
|
|
3100
|
+
transaction?: TransactionOrKnex;
|
|
3101
|
+
skipFetched?: boolean;
|
|
3102
|
+
}
|
|
3103
|
+
|
|
3104
|
+
interface TraverserFunction {
|
|
3105
|
+
(model: Model, parentModel: Model, relationName: string): void;
|
|
3106
|
+
}
|
|
3107
|
+
|
|
3108
|
+
type ArrayQueryBuilderThunk<M extends Model> = () => ArrayQueryBuilder<QueryBuilderType<M>>;
|
|
3109
|
+
type CancelQueryThunk = (result: any) => void;
|
|
3110
|
+
|
|
3111
|
+
export interface StaticHookArguments<M extends Model, R = any> {
|
|
3112
|
+
asFindQuery: ArrayQueryBuilderThunk<M>;
|
|
3113
|
+
cancelQuery: CancelQueryThunk;
|
|
3114
|
+
context: QueryContext;
|
|
3115
|
+
transaction: TransactionOrKnex;
|
|
3116
|
+
relation?: Relation;
|
|
3117
|
+
modelOptions?: ModelOptions;
|
|
3118
|
+
items: Model[];
|
|
3119
|
+
inputItems: M[];
|
|
3120
|
+
result?: R;
|
|
3121
|
+
}
|
|
3122
|
+
|
|
3123
|
+
export type Transaction = Knex.Transaction;
|
|
3124
|
+
export type TransactionOrKnex = Transaction | Knex;
|
|
3125
|
+
|
|
3126
|
+
export interface RelationMappings {
|
|
3127
|
+
[relationName: string]: RelationMapping<any>;
|
|
3128
|
+
}
|
|
3129
|
+
|
|
3130
|
+
export type RelationMappingsThunk = () => RelationMappings;
|
|
3131
|
+
|
|
3132
|
+
type ModelClassFactory = () => AnyModelConstructor;
|
|
3133
|
+
type ModelClassSpecifier = ModelClassFactory | AnyModelConstructor | string;
|
|
3134
|
+
type RelationMappingHook<M extends Model> = (
|
|
3135
|
+
model: M,
|
|
3136
|
+
context: QueryContext,
|
|
3137
|
+
) => Promise<void> | void;
|
|
3138
|
+
type StringOrReferenceBuilder = string | ReferenceBuilder;
|
|
3139
|
+
type RelationMappingColumnRef = StringOrReferenceBuilder | readonly StringOrReferenceBuilder[];
|
|
3140
|
+
|
|
3141
|
+
export interface RelationMapping<M extends Model> {
|
|
3142
|
+
relation: RelationType;
|
|
3143
|
+
modelClass: ModelClassSpecifier;
|
|
3144
|
+
join: RelationJoin;
|
|
3145
|
+
modify?: Modifier<QueryBuilderType<M>>;
|
|
3146
|
+
filter?: Modifier<QueryBuilderType<M>>;
|
|
3147
|
+
beforeInsert?: RelationMappingHook<M>;
|
|
3148
|
+
}
|
|
3149
|
+
|
|
3150
|
+
export interface RelationJoin {
|
|
3151
|
+
from: RelationMappingColumnRef;
|
|
3152
|
+
to: RelationMappingColumnRef;
|
|
3153
|
+
through?: RelationThrough<any>;
|
|
3154
|
+
}
|
|
3155
|
+
|
|
3156
|
+
export interface RelationThrough<M extends Model> {
|
|
3157
|
+
from: RelationMappingColumnRef;
|
|
3158
|
+
to: RelationMappingColumnRef;
|
|
3159
|
+
extra?: string | readonly string[] | Readonly<Record<string, string>>;
|
|
3160
|
+
modelClass?: ModelClassSpecifier;
|
|
3161
|
+
modify?: Modifier<QueryBuilderType<M>>;
|
|
3162
|
+
filter?: Modifier<QueryBuilderType<M>>;
|
|
3163
|
+
beforeInsert?: RelationMappingHook<M>;
|
|
3164
|
+
}
|
|
3165
|
+
|
|
3166
|
+
type RelationCardinality = 'one' | 'many';
|
|
3167
|
+
|
|
3168
|
+
export interface RelationType<C extends RelationCardinality = RelationCardinality>
|
|
3169
|
+
extends Constructor<Relation> {
|
|
3170
|
+
// Type-only: whether the relation is to one model or to many models.
|
|
3171
|
+
'~cardinality'?: C;
|
|
3172
|
+
}
|
|
3173
|
+
|
|
3174
|
+
/**
|
|
3175
|
+
* The relation mappings of model class `MC`, as written in its static
|
|
3176
|
+
* `relationMappings` property (an object or a function that returns one).
|
|
3177
|
+
* `{}` when they can't be inferred, for example when the property has the
|
|
3178
|
+
* type annotation `RelationMappings` or `RelationMappingsThunk`.
|
|
3179
|
+
*/
|
|
3180
|
+
export type RelationMappingsOf<MC> = MC extends { relationMappings: infer R }
|
|
3181
|
+
? InferredRelationMappings<R extends (...args: any[]) => infer O ? O : R>
|
|
3182
|
+
: {};
|
|
3183
|
+
|
|
3184
|
+
type InferredRelationMappings<T> =
|
|
3185
|
+
IsAnyType<T> extends true ? {} : string extends keyof T ? {} : T;
|
|
3186
|
+
|
|
3187
|
+
// The model class of a relation mapping's `modelClass`: a class, or a
|
|
3188
|
+
// function that returns one. `never` for module paths, whose class can't
|
|
3189
|
+
// be known.
|
|
3190
|
+
type RelatedModelClassOf<C> =
|
|
3191
|
+
IsAnyType<C> extends true
|
|
3192
|
+
? never
|
|
3193
|
+
: C extends abstract new (
|
|
3194
|
+
...args: any[]
|
|
3195
|
+
) => Model
|
|
3196
|
+
? C
|
|
3197
|
+
: C extends () => infer K
|
|
3198
|
+
? K extends abstract new (
|
|
3199
|
+
...args: any[]
|
|
3200
|
+
) => Model
|
|
3201
|
+
? K
|
|
3202
|
+
: never
|
|
3203
|
+
: never;
|
|
3204
|
+
|
|
3205
|
+
type RelatedModelOf<C> = [RelatedModelClassOf<C>] extends [never]
|
|
3206
|
+
? Model
|
|
3207
|
+
: ModelInstance<RelatedModelClassOf<C>>;
|
|
3208
|
+
|
|
3209
|
+
// `join.through.extra` as an object of `{ property: column }`.
|
|
3210
|
+
type RelationExtraColumns<E> = E extends string
|
|
3211
|
+
? { [K in E]: K }
|
|
3212
|
+
: E extends readonly (infer N extends string)[]
|
|
3213
|
+
? { [K in N]: K }
|
|
3214
|
+
: E extends Readonly<Record<string, string>>
|
|
3215
|
+
? E
|
|
3216
|
+
: {};
|
|
3217
|
+
|
|
3218
|
+
type RelationExtraProps<Mapping> = Mapping extends {
|
|
3219
|
+
join: { through: infer Through };
|
|
3220
|
+
}
|
|
3221
|
+
? Through extends { extra: infer E }
|
|
3222
|
+
? RelationExtraColumns<E> extends infer Columns
|
|
3223
|
+
? string extends keyof Columns
|
|
3224
|
+
? // The names of the columns are not known, for example when they
|
|
3225
|
+
// were inferred as `string[]`. Use `defineRelations` to keep them.
|
|
3226
|
+
{}
|
|
3227
|
+
: {
|
|
3228
|
+
-readonly [P in keyof Columns]: Through extends { modelClass: infer C }
|
|
3229
|
+
? Columns[P] extends keyof RelatedModelOf<C>
|
|
3230
|
+
? RelatedModelOf<C>[Columns[P]]
|
|
3231
|
+
: unknown
|
|
3232
|
+
: unknown;
|
|
3233
|
+
}
|
|
3234
|
+
: {}
|
|
3235
|
+
: {}
|
|
3236
|
+
: {};
|
|
3237
|
+
|
|
3238
|
+
type WithRelationExtraProps<M, Extra> = {} extends Extra ? M : M & Extra;
|
|
3239
|
+
|
|
3240
|
+
/**
|
|
3241
|
+
* The type of the relation property of a relation mapping.
|
|
3242
|
+
*/
|
|
3243
|
+
export type RelationPropertyType<Mapping> = Mapping extends {
|
|
3244
|
+
relation: infer Type;
|
|
3245
|
+
modelClass: infer C;
|
|
3246
|
+
}
|
|
3247
|
+
? WithRelationExtraProps<RelatedModelOf<C>, RelationExtraProps<Mapping>> extends infer Related
|
|
3248
|
+
? Type extends { '~cardinality'?: infer Cardinality }
|
|
3249
|
+
? [NonNullable<Cardinality>] extends ['many']
|
|
3250
|
+
? Related[]
|
|
3251
|
+
: [NonNullable<Cardinality>] extends ['one']
|
|
3252
|
+
? Related | null
|
|
3253
|
+
: Related | Related[] | null
|
|
3254
|
+
: Related | Related[] | null
|
|
3255
|
+
: never
|
|
3256
|
+
: never;
|
|
3257
|
+
|
|
3258
|
+
/**
|
|
3259
|
+
* The relation properties of model class `MC`, inferred from its
|
|
3260
|
+
* `relationMappings`. Use it to add the relation properties to the model's
|
|
3261
|
+
* instances without declaring each of them:
|
|
3262
|
+
*
|
|
3263
|
+
* ```ts
|
|
3264
|
+
* class Person extends Model {
|
|
3265
|
+
* static relationMappings = () => ({ pets: { ... } });
|
|
3266
|
+
* }
|
|
3267
|
+
*
|
|
3268
|
+
* interface Person extends RelationsOf<typeof Person> {}
|
|
3269
|
+
* ```
|
|
3270
|
+
*/
|
|
3271
|
+
export type RelationsOf<MC> = {
|
|
3272
|
+
[K in keyof RelationMappingsOf<MC>]?: RelationPropertyType<RelationMappingsOf<MC>[K]>;
|
|
3273
|
+
};
|
|
3274
|
+
|
|
3275
|
+
type InferredRelationProps<MC, I> = {
|
|
3276
|
+
[K in Exclude<keyof RelationMappingsOf<MC>, keyof I>]?: RelationPropertyType<
|
|
3277
|
+
RelationMappingsOf<MC>[K]
|
|
3278
|
+
>;
|
|
3279
|
+
};
|
|
3280
|
+
|
|
3281
|
+
/**
|
|
3282
|
+
* The instance type of model class `MC`, including the relation properties
|
|
3283
|
+
* inferred from its `relationMappings` that the class doesn't declare.
|
|
3284
|
+
*/
|
|
3285
|
+
export type ModelInstance<MC> = MC extends abstract new (
|
|
3286
|
+
...args: any[]
|
|
3287
|
+
) => infer I
|
|
3288
|
+
? WithModelModifiers<
|
|
3289
|
+
MC,
|
|
3290
|
+
[Exclude<keyof RelationMappingsOf<MC>, keyof I>] extends [never]
|
|
3291
|
+
? I
|
|
3292
|
+
: I & InferredRelationProps<MC, I>
|
|
3293
|
+
>
|
|
3294
|
+
: never;
|
|
3295
|
+
|
|
3296
|
+
// Named modifiers are static, so the types of the instances carry them for the
|
|
3297
|
+
// query builders, which only know the instance type.
|
|
3298
|
+
type WithModelModifiers<MC, I> = [ModifiersOf<MC>] extends [never] ? I : I & ModelModifiers<MC>;
|
|
3299
|
+
|
|
3300
|
+
interface ModelModifiers<MC> {
|
|
3301
|
+
'~modifiers'?: ModifiersOf<MC>;
|
|
3302
|
+
}
|
|
3303
|
+
|
|
3304
|
+
// The named modifiers of model class `MC` from its static `modifiers`, or
|
|
3305
|
+
// `never` when their names can't be known (like with the type `Modifiers`).
|
|
3306
|
+
type ModifiersOf<MC> = MC extends { modifiers: infer Mods }
|
|
3307
|
+
? IsAnyType<Mods> extends true
|
|
3308
|
+
? never
|
|
3309
|
+
: string extends keyof Mods
|
|
3310
|
+
? never
|
|
3311
|
+
: Mods
|
|
3312
|
+
: never;
|
|
3313
|
+
|
|
3314
|
+
// The named modifiers of model `M`, or `never` when they're not known.
|
|
3315
|
+
type KnownModifiers<M> =
|
|
3316
|
+
RelationsModel<M> extends infer RM
|
|
3317
|
+
? IsAnyType<RM> extends true
|
|
3318
|
+
? never
|
|
3319
|
+
: '~modifiers' extends keyof RM
|
|
3320
|
+
? NonNullable<RM['~modifiers' & keyof RM]>
|
|
3321
|
+
: never
|
|
3322
|
+
: never;
|
|
3323
|
+
|
|
3324
|
+
/**
|
|
3325
|
+
* The names of the relations of model class `MC` inferred from its
|
|
3326
|
+
* `relationMappings`.
|
|
3327
|
+
*/
|
|
3328
|
+
type RelationNamesOf<MC> = keyof RelationMappingsOf<MC> & string;
|
|
3329
|
+
|
|
3330
|
+
// The query builder of a relation for the static `relatedQuery()`.
|
|
3331
|
+
type RelationQueryBuilder<Mapping> = Mapping extends { modelClass: infer C }
|
|
3332
|
+
? [RelatedModelClassOf<C>] extends [never]
|
|
3333
|
+
? QueryBuilder<Model>
|
|
3334
|
+
: ModelClassQueryBuilder<
|
|
3335
|
+
RelatedModelClassOf<C>,
|
|
3336
|
+
Extract<
|
|
3337
|
+
WithRelationExtraProps<
|
|
3338
|
+
ModelInstance<RelatedModelClassOf<C>>,
|
|
3339
|
+
RelationExtraProps<Mapping>
|
|
3340
|
+
>,
|
|
3341
|
+
Model
|
|
3342
|
+
>,
|
|
3343
|
+
WithRelationExtraProps<
|
|
3344
|
+
ModelRow<ModelInstance<RelatedModelClassOf<C>>>,
|
|
3345
|
+
RelationExtraProps<Mapping>
|
|
3346
|
+
>
|
|
3347
|
+
>
|
|
3348
|
+
: QueryBuilder<Model>;
|
|
3349
|
+
|
|
3350
|
+
// Shown in the error message when a relation name isn't known.
|
|
3351
|
+
type UnknownRelationName = {
|
|
3352
|
+
'~error': 'Unknown relation. Pass the related model type explicitly for relation names that can not be checked: relatedQuery<Pet>(name)';
|
|
3353
|
+
};
|
|
3354
|
+
|
|
3355
|
+
type RelationMappingsDefinition = { readonly [relationName: string]: RelationMapping<any> };
|
|
3356
|
+
|
|
3357
|
+
export interface DefineRelationsFunction {
|
|
3358
|
+
/**
|
|
3359
|
+
* Returns the relation mappings as is. Use it to define `relationMappings`
|
|
3360
|
+
* so that the types of the relations can be inferred exactly:
|
|
3361
|
+
*
|
|
3362
|
+
* ```ts
|
|
3363
|
+
* static relationMappings = defineRelations(() => ({
|
|
3364
|
+
* movies: {
|
|
3365
|
+
* relation: Model.ManyToManyRelation,
|
|
3366
|
+
* modelClass: Movie,
|
|
3367
|
+
* join: {
|
|
3368
|
+
* from: 'persons.id',
|
|
3369
|
+
* through: { from: 'persons_movies.personId', to: 'persons_movies.movieId', extra: ['role'] },
|
|
3370
|
+
* to: 'movies.id',
|
|
3371
|
+
* },
|
|
3372
|
+
* },
|
|
3373
|
+
* }));
|
|
3374
|
+
* ```
|
|
3375
|
+
*/
|
|
3376
|
+
<const T extends RelationMappingsDefinition>(relationMappings: () => T): () => T;
|
|
3377
|
+
<const T extends RelationMappingsDefinition>(relationMappings: T): T;
|
|
3378
|
+
}
|
|
3379
|
+
|
|
3380
|
+
export interface Relation {
|
|
3381
|
+
name: string;
|
|
3382
|
+
ownerModelClass: typeof Model;
|
|
3383
|
+
relatedModelClass: typeof Model;
|
|
3384
|
+
ownerProp: RelationProperty;
|
|
3385
|
+
relatedProp: RelationProperty;
|
|
3386
|
+
joinModelClass: typeof Model;
|
|
3387
|
+
joinTable: string;
|
|
3388
|
+
joinTableOwnerProp: RelationProperty;
|
|
3389
|
+
joinTableRelatedProp: RelationProperty;
|
|
3390
|
+
}
|
|
3391
|
+
|
|
3392
|
+
export interface RelationProperty {
|
|
3393
|
+
size: number;
|
|
3394
|
+
modelClass: typeof Model;
|
|
3395
|
+
props: string[];
|
|
3396
|
+
cols: string[];
|
|
3397
|
+
}
|
|
3398
|
+
|
|
3399
|
+
export interface Relations {
|
|
3400
|
+
[name: string]: Relation;
|
|
3401
|
+
}
|
|
3402
|
+
|
|
3403
|
+
export interface QueryContext {
|
|
3404
|
+
transaction: Transaction;
|
|
3405
|
+
[key: string]: any;
|
|
3406
|
+
}
|
|
3407
|
+
|
|
3408
|
+
export interface ModelOptions {
|
|
3409
|
+
patch?: boolean;
|
|
3410
|
+
skipValidation?: boolean;
|
|
3411
|
+
old?: object;
|
|
3412
|
+
}
|
|
3413
|
+
|
|
3414
|
+
export interface CloneOptions {
|
|
3415
|
+
shallow?: boolean;
|
|
3416
|
+
}
|
|
3417
|
+
|
|
3418
|
+
export interface ToJsonOptions extends CloneOptions {
|
|
3419
|
+
virtuals?: boolean | string[];
|
|
3420
|
+
}
|
|
3421
|
+
|
|
3422
|
+
export interface ValidatorContext {
|
|
3423
|
+
[key: string]: any;
|
|
3424
|
+
}
|
|
3425
|
+
|
|
3426
|
+
export interface ValidatorArgs {
|
|
3427
|
+
ctx: ValidatorContext;
|
|
3428
|
+
model: Model;
|
|
3429
|
+
json: Pojo;
|
|
3430
|
+
options: ModelOptions;
|
|
3431
|
+
}
|
|
3432
|
+
|
|
3433
|
+
export class Validator {
|
|
3434
|
+
beforeValidate(args: ValidatorArgs): void;
|
|
3435
|
+
validate(args: ValidatorArgs): Pojo;
|
|
3436
|
+
afterValidate(args: ValidatorArgs): void;
|
|
3437
|
+
}
|
|
3438
|
+
|
|
3439
|
+
export interface AjvConfig {
|
|
3440
|
+
onCreateAjv(ajv: Ajv): void;
|
|
3441
|
+
options?: AjvOptions;
|
|
3442
|
+
}
|
|
3443
|
+
|
|
3444
|
+
export class AjvValidator extends Validator {
|
|
3445
|
+
constructor(config: AjvConfig);
|
|
3446
|
+
}
|
|
3447
|
+
|
|
3448
|
+
export interface SnakeCaseMappersOptions {
|
|
3449
|
+
upperCase?: boolean;
|
|
3450
|
+
underscoreBeforeDigits?: boolean;
|
|
3451
|
+
underscoreBetweenUppercaseLetters?: boolean;
|
|
3452
|
+
}
|
|
3453
|
+
|
|
3454
|
+
export interface ColumnNameMappers {
|
|
3455
|
+
parse(json: Pojo): Pojo;
|
|
3456
|
+
format(json: Pojo): Pojo;
|
|
3457
|
+
}
|
|
3458
|
+
|
|
3459
|
+
export interface SnakeCaseMappersFactory {
|
|
3460
|
+
(options?: SnakeCaseMappersOptions): ColumnNameMappers;
|
|
3461
|
+
}
|
|
3462
|
+
|
|
3463
|
+
export interface KnexMappers {
|
|
3464
|
+
wrapIdentifier(identifier: string, origWrap: Identity<string>): string;
|
|
3465
|
+
postProcessResponse(response: any): any;
|
|
3466
|
+
}
|
|
3467
|
+
|
|
3468
|
+
export interface KnexSnakeCaseMappersFactory {
|
|
3469
|
+
(options?: SnakeCaseMappersOptions): KnexMappers;
|
|
3470
|
+
}
|
|
3471
|
+
|
|
3472
|
+
export type ValidationErrorType =
|
|
3473
|
+
| 'ModelValidation'
|
|
3474
|
+
| 'RelationExpression'
|
|
3475
|
+
| 'UnallowedRelation'
|
|
3476
|
+
| 'InvalidGraph';
|
|
3477
|
+
|
|
3478
|
+
export class ValidationError extends Error {
|
|
3479
|
+
constructor(args: CreateValidationErrorArgs & { modelClass?: ModelClass<Model> });
|
|
3480
|
+
|
|
3481
|
+
statusCode: number;
|
|
3482
|
+
message: string;
|
|
3483
|
+
data?: ErrorHash | any;
|
|
3484
|
+
type: ValidationErrorType | string;
|
|
3485
|
+
modelClass: ModelClass<Model>;
|
|
3486
|
+
}
|
|
3487
|
+
|
|
3488
|
+
export interface ValidationErrorItem {
|
|
3489
|
+
message: string;
|
|
3490
|
+
keyword: string;
|
|
3491
|
+
params: Pojo;
|
|
3492
|
+
}
|
|
3493
|
+
|
|
3494
|
+
export interface ErrorHash {
|
|
3495
|
+
[columnName: string]: ValidationErrorItem[];
|
|
3496
|
+
}
|
|
3497
|
+
|
|
3498
|
+
export interface CreateValidationErrorArgs {
|
|
3499
|
+
statusCode?: number;
|
|
3500
|
+
message?: string;
|
|
3501
|
+
data?: ErrorHash | any;
|
|
3502
|
+
// This can be any string for custom errors. ValidationErrorType is there
|
|
3503
|
+
// only to document the default values objection uses internally.
|
|
3504
|
+
type: ValidationErrorType | string;
|
|
3505
|
+
}
|
|
3506
|
+
|
|
3507
|
+
export class NotFoundError extends Error {
|
|
3508
|
+
constructor(args: CreateNotFoundErrorArgs & { modelClass?: ModelClass<Model> });
|
|
3509
|
+
|
|
3510
|
+
statusCode: number;
|
|
3511
|
+
data?: any;
|
|
3512
|
+
type: 'NotFound';
|
|
3513
|
+
modelClass: ModelClass<Model>;
|
|
3514
|
+
}
|
|
3515
|
+
|
|
3516
|
+
export interface CreateNotFoundErrorArgs {
|
|
3517
|
+
statusCode?: number;
|
|
3518
|
+
message?: string;
|
|
3519
|
+
data?: any;
|
|
3520
|
+
[key: string]: any;
|
|
3521
|
+
}
|
|
3522
|
+
|
|
3523
|
+
export interface TableMetadata {
|
|
3524
|
+
columns: Array<string>;
|
|
3525
|
+
}
|
|
3526
|
+
|
|
3527
|
+
export interface TableMetadataOptions {
|
|
3528
|
+
table: string;
|
|
3529
|
+
}
|
|
3530
|
+
|
|
3531
|
+
export interface FetchTableMetadataOptions {
|
|
3532
|
+
knex?: Knex;
|
|
3533
|
+
force?: boolean;
|
|
3534
|
+
table?: string;
|
|
3535
|
+
}
|
|
3536
|
+
|
|
3537
|
+
export interface Constructor<T> {
|
|
3538
|
+
new (): T;
|
|
3539
|
+
}
|
|
3540
|
+
|
|
3541
|
+
interface PrototypeType<T> extends Function {
|
|
3542
|
+
prototype: T;
|
|
3543
|
+
}
|
|
3544
|
+
|
|
3545
|
+
interface ConstructorFunctionType<T = any> extends PrototypeType<T> {
|
|
3546
|
+
new (...args: any[]): T;
|
|
3547
|
+
}
|
|
3548
|
+
|
|
3549
|
+
// for internal use on generic static this deduction, copied from https://github.com/microsoft/TypeScript/issues/5863#issuecomment-1483978415
|
|
3550
|
+
type ConstructorType<T = unknown, Static extends Record<string, any> = PrototypeType<T>> = (
|
|
3551
|
+
| ConstructorFunctionType<T>
|
|
3552
|
+
| PrototypeType<T>
|
|
3553
|
+
) & {
|
|
3554
|
+
[Key in keyof Static]: Static[Key];
|
|
3555
|
+
};
|
|
3556
|
+
|
|
3557
|
+
export interface ModelConstructor<M extends Model> extends Constructor<M> {}
|
|
3558
|
+
|
|
3559
|
+
export interface ModelClass<M extends Model> extends ModelConstructor<M> {
|
|
3560
|
+
QueryBuilder: typeof QueryBuilder;
|
|
3561
|
+
|
|
3562
|
+
tableName: string;
|
|
3563
|
+
idColumn: string | string[];
|
|
3564
|
+
jsonSchema: JSONSchema;
|
|
3565
|
+
relationMappings: RelationMappings | RelationMappingsThunk;
|
|
3566
|
+
modelPaths: string[];
|
|
3567
|
+
jsonAttributes: string[];
|
|
3568
|
+
virtualAttributes: string[];
|
|
3569
|
+
uidProp: string;
|
|
3570
|
+
uidRefProp: string;
|
|
3571
|
+
dbRefProp: string;
|
|
3572
|
+
propRefRegex: RegExp;
|
|
3573
|
+
pickJsonSchemaProperties: boolean;
|
|
3574
|
+
relatedFindQueryMutates: boolean;
|
|
3575
|
+
relatedInsertQueryMutates: boolean;
|
|
3576
|
+
useLimitInFirst: boolean;
|
|
3577
|
+
modifiers: Modifiers;
|
|
3578
|
+
columnNameMappers: ColumnNameMappers;
|
|
3579
|
+
|
|
3580
|
+
raw: RawFunction;
|
|
3581
|
+
ref: ReferenceFunction;
|
|
3582
|
+
fn: FunctionFunction;
|
|
3583
|
+
|
|
3584
|
+
BelongsToOneRelation: RelationType<'one'>;
|
|
3585
|
+
HasOneRelation: RelationType<'one'>;
|
|
3586
|
+
HasManyRelation: RelationType<'many'>;
|
|
3587
|
+
ManyToManyRelation: RelationType<'many'>;
|
|
3588
|
+
HasOneThroughRelation: RelationType<'one'>;
|
|
3589
|
+
|
|
3590
|
+
defaultGraphOptions?: GraphOptions;
|
|
3591
|
+
|
|
3592
|
+
query(this: Constructor<M>, trxOrKnex?: TransactionOrKnex): QueryBuilderType<M>;
|
|
3593
|
+
|
|
3594
|
+
relatedQuery<K extends keyof M>(
|
|
3595
|
+
relationName: K,
|
|
3596
|
+
trxOrKnex?: TransactionOrKnex,
|
|
3597
|
+
): NoInfer<ArrayRelatedQueryBuilder<M[K]>>;
|
|
3598
|
+
|
|
3599
|
+
// For relation names that can't be checked. The model type of the
|
|
3600
|
+
// relation must be given explicitly: `relatedQuery<Pet>(relationName)`.
|
|
3601
|
+
relatedQuery<RM extends Model = never>(
|
|
3602
|
+
relationName: [RM] extends [never] ? UnknownRelationName : string,
|
|
3603
|
+
trxOrKnex?: TransactionOrKnex,
|
|
3604
|
+
): RowQueryBuilder<QueryBuilderType<RM>, RM>;
|
|
3605
|
+
|
|
3606
|
+
fromJson(json: object, opt?: ModelOptions): M;
|
|
3607
|
+
fromDatabaseJson(json: object): M;
|
|
3608
|
+
|
|
3609
|
+
columnNameToPropertyName(columnName: string): string;
|
|
3610
|
+
propertyNameToColumnName(propertyName: string): string;
|
|
3611
|
+
|
|
3612
|
+
createValidator(): Validator;
|
|
3613
|
+
createValidationError(args: CreateValidationErrorArgs): Error;
|
|
3614
|
+
createNotFoundError(queryContext: QueryContext, args: CreateNotFoundErrorArgs): Error;
|
|
3615
|
+
|
|
3616
|
+
tableMetadata(opt?: TableMetadataOptions): TableMetadata;
|
|
3617
|
+
fetchTableMetadata(opt?: FetchTableMetadataOptions): Promise<TableMetadata>;
|
|
3618
|
+
|
|
3619
|
+
knex(knex?: Knex): Knex;
|
|
3620
|
+
knexQuery(): Knex.QueryBuilder;
|
|
3621
|
+
startTransaction(knexOrTransaction?: TransactionOrKnex): Promise<Transaction>;
|
|
3622
|
+
|
|
3623
|
+
transaction<T>(callback: (trx: Transaction) => Promise<T>): Promise<T>;
|
|
3624
|
+
transaction<T>(
|
|
3625
|
+
trxOrKnex: TransactionOrKnex,
|
|
3626
|
+
callback: (trx: Transaction) => Promise<T>,
|
|
3627
|
+
): Promise<T>;
|
|
3628
|
+
|
|
3629
|
+
bindKnex(trxOrKnex: TransactionOrKnex): this;
|
|
3630
|
+
bindTransaction(trxOrKnex: TransactionOrKnex): this;
|
|
3631
|
+
|
|
3632
|
+
fetchGraph<T extends PartialModelObject<M>, const E extends RelationExpression<M>>(
|
|
3633
|
+
modelOrObject: T,
|
|
3634
|
+
expression: CheckRelationExpression<M, E>,
|
|
3635
|
+
options?: FetchGraphOptions,
|
|
3636
|
+
): FetchGraphQueryBuilder<QueryBuilderType<M>, M, T, E, 'one'>;
|
|
3637
|
+
|
|
3638
|
+
fetchGraph<T extends PartialModelObject<M>, const E extends RelationExpression<M>>(
|
|
3639
|
+
modelOrObject: T[],
|
|
3640
|
+
expression: CheckRelationExpression<M, E>,
|
|
3641
|
+
options?: FetchGraphOptions,
|
|
3642
|
+
): FetchGraphQueryBuilder<QueryBuilderType<M>, M, T, E, 'many'>;
|
|
3643
|
+
|
|
3644
|
+
getRelations(): Relations;
|
|
3645
|
+
getRelation(name: string): Relation;
|
|
3646
|
+
|
|
3647
|
+
traverse(models: Model | Model[], traverser: TraverserFunction): void;
|
|
3648
|
+
traverse(
|
|
3649
|
+
filterConstructor: ModelConstructor<Model>,
|
|
3650
|
+
models: Model | Model[],
|
|
3651
|
+
traverser: TraverserFunction,
|
|
3652
|
+
): void;
|
|
3653
|
+
traverseAsync(models: Model | Model[], traverser: TraverserFunction): Promise<void>;
|
|
3654
|
+
traverseAsync(
|
|
3655
|
+
filterConstructor: ModelConstructor<Model>,
|
|
3656
|
+
models: Model | Model[],
|
|
3657
|
+
traverser: TraverserFunction,
|
|
3658
|
+
): Promise<void>;
|
|
3659
|
+
|
|
3660
|
+
beforeFind(args: StaticHookArguments<any>): any;
|
|
3661
|
+
afterFind(args: StaticHookArguments<any>): any;
|
|
3662
|
+
beforeInsert(args: StaticHookArguments<any>): any;
|
|
3663
|
+
afterInsert(args: StaticHookArguments<any>): any;
|
|
3664
|
+
beforeUpdate(args: StaticHookArguments<any>): any;
|
|
3665
|
+
afterUpdate(args: StaticHookArguments<any>): any;
|
|
3666
|
+
beforeDelete(args: StaticHookArguments<any>): any;
|
|
3667
|
+
afterDelete(args: StaticHookArguments<any>): any;
|
|
3668
|
+
}
|
|
3669
|
+
|
|
3670
|
+
export class Model {
|
|
3671
|
+
// This allows model classes to be used as the base class of mixins.
|
|
3672
|
+
constructor(...args: any[]);
|
|
3673
|
+
|
|
3674
|
+
static QueryBuilder: typeof QueryBuilder;
|
|
3675
|
+
|
|
3676
|
+
static tableName: string;
|
|
3677
|
+
static idColumn: string | string[];
|
|
3678
|
+
static jsonSchema: JSONSchema;
|
|
3679
|
+
static relationMappings: RelationMappings | RelationMappingsThunk;
|
|
3680
|
+
static modelPaths: string[];
|
|
3681
|
+
static jsonAttributes: string[];
|
|
3682
|
+
static virtualAttributes: string[];
|
|
3683
|
+
static uidProp: string;
|
|
3684
|
+
static uidRefProp: string;
|
|
3685
|
+
static dbRefProp: string;
|
|
3686
|
+
static propRefRegex: RegExp;
|
|
3687
|
+
static pickJsonSchemaProperties: boolean;
|
|
3688
|
+
static relatedFindQueryMutates: boolean;
|
|
3689
|
+
static relatedInsertQueryMutates: boolean;
|
|
3690
|
+
static useLimitInFirst: boolean;
|
|
3691
|
+
static modifiers: Modifiers;
|
|
3692
|
+
static columnNameMappers: ColumnNameMappers;
|
|
3693
|
+
|
|
3694
|
+
static raw: RawFunction;
|
|
3695
|
+
/**
|
|
3696
|
+
* A reference to a column of this model, qualified with the table (or the alias
|
|
3697
|
+
* of the table) in the query. The column must be a property of the model. Pass the
|
|
3698
|
+
* expression as a type argument for columns the model doesn't declare:
|
|
3699
|
+
* `Person.ref<'legacy_column'>('legacy_column')`.
|
|
3700
|
+
*/
|
|
3701
|
+
static ref<E extends string = never>(
|
|
3702
|
+
expression: [E] extends [never] ? never : NoInfer<E>,
|
|
3703
|
+
): ReferenceBuilder;
|
|
3704
|
+
static ref<MC extends AnyModelConstructor, E extends RefExpression<ModelInstance<MC>>>(
|
|
3705
|
+
this: MC,
|
|
3706
|
+
expression: E,
|
|
3707
|
+
): ReferenceBuilder<RefValue<ModelInstance<MC>, E>>;
|
|
3708
|
+
static fn: FunctionFunction;
|
|
3709
|
+
|
|
3710
|
+
static BelongsToOneRelation: RelationType<'one'>;
|
|
3711
|
+
static HasOneRelation: RelationType<'one'>;
|
|
3712
|
+
static HasManyRelation: RelationType<'many'>;
|
|
3713
|
+
static ManyToManyRelation: RelationType<'many'>;
|
|
3714
|
+
static HasOneThroughRelation: RelationType<'one'>;
|
|
3715
|
+
|
|
3716
|
+
static defaultGraphOptions?: GraphOptions;
|
|
3717
|
+
|
|
3718
|
+
static query<MC extends Constructor<Model>>(
|
|
3719
|
+
this: MC,
|
|
3720
|
+
trxOrKnex?: TransactionOrKnex,
|
|
3721
|
+
): ModelClassQueryBuilder<MC, Extract<ModelInstance<MC>, Model>>;
|
|
3722
|
+
|
|
3723
|
+
static relatedQuery<MC extends Constructor<Model>, K extends RelationNamesOf<MC>>(
|
|
3724
|
+
this: MC,
|
|
3725
|
+
relationName: K,
|
|
3726
|
+
trxOrKnex?: TransactionOrKnex,
|
|
3727
|
+
): RelationQueryBuilder<RelationMappingsOf<MC>[K]>;
|
|
3728
|
+
|
|
3729
|
+
static relatedQuery<M extends Model, K extends keyof M>(
|
|
3730
|
+
this: ConstructorType<M>,
|
|
3731
|
+
relationName: K,
|
|
3732
|
+
trxOrKnex?: TransactionOrKnex,
|
|
3733
|
+
): NoInfer<ArrayRelatedQueryBuilder<M[K]>>;
|
|
3734
|
+
|
|
3735
|
+
// For relation names that can't be checked. The model type of the
|
|
3736
|
+
// relation must be given explicitly: `relatedQuery<Pet>(relationName)`.
|
|
3737
|
+
static relatedQuery<RM extends Model = never>(
|
|
3738
|
+
relationName: [RM] extends [never] ? UnknownRelationName : string,
|
|
3739
|
+
trxOrKnex?: TransactionOrKnex,
|
|
3740
|
+
): RowQueryBuilder<QueryBuilderType<RM>, RM>;
|
|
3741
|
+
|
|
3742
|
+
static fromJson<MC extends Constructor<Model>>(
|
|
3743
|
+
this: MC,
|
|
3744
|
+
json: object,
|
|
3745
|
+
opt?: ModelOptions,
|
|
3746
|
+
): ModelInstance<MC>;
|
|
3747
|
+
static fromJson<M extends Model>(this: Constructor<M>, json: object, opt?: ModelOptions): M;
|
|
3748
|
+
static fromDatabaseJson<MC extends Constructor<Model>>(
|
|
3749
|
+
this: MC,
|
|
3750
|
+
json: object,
|
|
3751
|
+
): ModelInstance<MC>;
|
|
3752
|
+
static fromDatabaseJson<M extends Model>(this: Constructor<M>, json: object): M;
|
|
3753
|
+
|
|
3754
|
+
static columnNameToPropertyName(columnName: string): string;
|
|
3755
|
+
static propertyNameToColumnName(propertyName: string): string;
|
|
3756
|
+
|
|
3757
|
+
static createValidator(): Validator;
|
|
3758
|
+
static createValidationError(args: CreateValidationErrorArgs): Error;
|
|
3759
|
+
static createNotFoundError(queryContext: QueryContext, args: CreateNotFoundErrorArgs): Error;
|
|
3760
|
+
|
|
3761
|
+
static tableMetadata(opt?: TableMetadataOptions): TableMetadata;
|
|
3762
|
+
static fetchTableMetadata(opt?: FetchTableMetadataOptions): Promise<TableMetadata>;
|
|
3763
|
+
|
|
3764
|
+
static knex(knex?: Knex): Knex;
|
|
3765
|
+
static knexQuery(): Knex.QueryBuilder;
|
|
3766
|
+
static startTransaction(knexOrTransaction?: TransactionOrKnex): Promise<Transaction>;
|
|
3767
|
+
|
|
3768
|
+
static transaction<T>(callback: (trx: Transaction) => Promise<T>): Promise<T>;
|
|
3769
|
+
static transaction<T>(
|
|
3770
|
+
trxOrKnex: TransactionOrKnex | undefined,
|
|
3771
|
+
callback: (trx: Transaction) => Promise<T>,
|
|
3772
|
+
): Promise<T>;
|
|
3773
|
+
|
|
3774
|
+
static bindKnex<M>(this: M, trxOrKnex: TransactionOrKnex): M;
|
|
3775
|
+
static bindTransaction<M>(this: M, trxOrKnex: TransactionOrKnex): M;
|
|
3776
|
+
|
|
3777
|
+
static fetchGraph: StaticFetchGraphMethod;
|
|
3778
|
+
|
|
3779
|
+
static getRelations(): Relations;
|
|
3780
|
+
static getRelation(name: string): Relation;
|
|
3781
|
+
|
|
3782
|
+
static traverse(models: Model | Model[], traverser: TraverserFunction): void;
|
|
3783
|
+
static traverse(
|
|
3784
|
+
filterConstructor: typeof Model,
|
|
3785
|
+
models: Model | Model[],
|
|
3786
|
+
traverser: TraverserFunction,
|
|
3787
|
+
): void;
|
|
3788
|
+
static traverseAsync(models: Model | Model[], traverser: TraverserFunction): Promise<void>;
|
|
3789
|
+
static traverseAsync(
|
|
3790
|
+
filterConstructor: typeof Model,
|
|
3791
|
+
models: Model | Model[],
|
|
3792
|
+
traverser: TraverserFunction,
|
|
3793
|
+
): Promise<void>;
|
|
3794
|
+
|
|
3795
|
+
static beforeFind(args: StaticHookArguments<any>): any;
|
|
3796
|
+
static afterFind(args: StaticHookArguments<any>): any;
|
|
3797
|
+
static beforeInsert(args: StaticHookArguments<any>): any;
|
|
3798
|
+
static afterInsert(args: StaticHookArguments<any>): any;
|
|
3799
|
+
static beforeUpdate(args: StaticHookArguments<any>): any;
|
|
3800
|
+
static afterUpdate(args: StaticHookArguments<any>): any;
|
|
3801
|
+
static beforeDelete(args: StaticHookArguments<any>): any;
|
|
3802
|
+
static afterDelete(args: StaticHookArguments<any>): any;
|
|
3803
|
+
|
|
3804
|
+
$modelClass: ModelClass<this>;
|
|
3805
|
+
|
|
3806
|
+
$relatedQuery<K extends keyof this>(
|
|
3807
|
+
relationName: K,
|
|
3808
|
+
trxOrKnex?: TransactionOrKnex,
|
|
3809
|
+
): NoInfer<RelatedQueryBuilder<this[K]>>;
|
|
3810
|
+
|
|
3811
|
+
// For relation names that can't be checked. The model type of the
|
|
3812
|
+
// relation must be given explicitly: `$relatedQuery<Pet>(relationName)`.
|
|
3813
|
+
$relatedQuery<RM extends Model = never>(
|
|
3814
|
+
relationName: [RM] extends [never] ? UnknownRelationName : string,
|
|
3815
|
+
trxOrKnex?: TransactionOrKnex,
|
|
3816
|
+
): RowQueryBuilder<QueryBuilderType<RM>, RM>;
|
|
3817
|
+
|
|
3818
|
+
$query(trxOrKnex?: TransactionOrKnex): SingleQueryBuilder<QueryBuilderType<this>>;
|
|
3819
|
+
|
|
3820
|
+
$id(id: any): void;
|
|
3821
|
+
$id(): any;
|
|
3822
|
+
|
|
3823
|
+
// `this` is a type parameter so that the signature is the same for every model,
|
|
3824
|
+
// which makes comparing models much faster.
|
|
3825
|
+
$fetchGraph<T extends Model, const E extends RelationExpression<Model>>(
|
|
3826
|
+
this: T,
|
|
3827
|
+
expression: CheckRelationExpression<ModelOfRow<T>, E>,
|
|
3828
|
+
options?: FetchGraphOptions,
|
|
3829
|
+
): FetchGraphQueryBuilder<QueryBuilderType<T>, ModelOfRow<T>, T, E, 'one'>;
|
|
3830
|
+
|
|
3831
|
+
$formatDatabaseJson(json: Pojo): Pojo;
|
|
3832
|
+
$parseDatabaseJson(json: Pojo): Pojo;
|
|
3833
|
+
|
|
3834
|
+
$formatJson(json: Pojo): Pojo;
|
|
3835
|
+
$parseJson(json: Pojo, opt?: ModelOptions): Pojo;
|
|
3836
|
+
|
|
3837
|
+
$beforeValidate(jsonSchema: JSONSchema, json: Pojo, opt: ModelOptions): JSONSchema;
|
|
3838
|
+
$validate(json?: Pojo, opt?: ModelOptions): Pojo; // may throw ValidationError if validation fails
|
|
3839
|
+
$afterValidate(json: Pojo, opt: ModelOptions): void; // may throw ValidationError if validation fails
|
|
3840
|
+
|
|
3841
|
+
$beforeInsert(queryContext: QueryContext): Promise<any> | void;
|
|
3842
|
+
$afterInsert(queryContext: QueryContext): Promise<any> | void;
|
|
3843
|
+
$afterUpdate(opt: ModelOptions, queryContext: QueryContext): Promise<any> | void;
|
|
3844
|
+
$beforeUpdate(opt: ModelOptions, queryContext: QueryContext): Promise<any> | void;
|
|
3845
|
+
$afterFind(queryContext: QueryContext): Promise<any> | void;
|
|
3846
|
+
$beforeDelete(queryContext: QueryContext): Promise<any> | void;
|
|
3847
|
+
$afterDelete(queryContext: QueryContext): Promise<any> | void;
|
|
3848
|
+
|
|
3849
|
+
$toDatabaseJson(): Pojo;
|
|
3850
|
+
$toJson(opt?: ToJsonOptions): ModelObject<this>;
|
|
3851
|
+
toJSON(opt?: ToJsonOptions): ModelObject<this>;
|
|
3852
|
+
|
|
3853
|
+
$setJson(json: object, opt?: ModelOptions): this;
|
|
3854
|
+
$setDatabaseJson(json: object): this;
|
|
3855
|
+
|
|
3856
|
+
$setRelated<RM extends Model>(
|
|
3857
|
+
relation: String | Relation,
|
|
3858
|
+
related: RM | RM[] | null | undefined,
|
|
3859
|
+
): this;
|
|
3860
|
+
|
|
3861
|
+
$appendRelated<RM extends Model>(
|
|
3862
|
+
relation: String | Relation,
|
|
3863
|
+
related: RM | RM[] | null | undefined,
|
|
3864
|
+
): this;
|
|
3865
|
+
|
|
3866
|
+
$set(obj: Pojo): this;
|
|
3867
|
+
$clone(opt?: CloneOptions): this;
|
|
3868
|
+
$traverse(filterConstructor: typeof Model, traverser: TraverserFunction): this;
|
|
3869
|
+
$traverse(traverser: TraverserFunction): this;
|
|
3870
|
+
$traverseAsync(filterConstructor: typeof Model, traverser: TraverserFunction): Promise<this>;
|
|
3871
|
+
$traverseAsync(traverser: TraverserFunction): Promise<this>;
|
|
3872
|
+
$omitFromJson(keys: string | string[] | { [key: string]: boolean }): this;
|
|
3873
|
+
$omitFromDatabaseJson(keys: string | string[] | { [key: string]: boolean }): this;
|
|
3874
|
+
|
|
3875
|
+
$knex(): Knex;
|
|
3876
|
+
$transaction(): Knex;
|
|
3877
|
+
|
|
3878
|
+
QueryBuilderType: QueryBuilder<this, this[], QueryBuilderItemState<this>>;
|
|
3879
|
+
}
|
|
3880
|
+
|
|
3881
|
+
/**
|
|
3882
|
+
* Overloading is required here until the following issues (at least) are resolved:
|
|
3883
|
+
*
|
|
3884
|
+
* - https://github.com/microsoft/TypeScript/issues/1360
|
|
3885
|
+
* - https://github.com/Microsoft/TypeScript/issues/5453
|
|
3886
|
+
*
|
|
3887
|
+
* @tutorial https://vincit.github.io/objection.js/guide/transactions.html#creating-a-transaction
|
|
3888
|
+
*/
|
|
3889
|
+
export interface transaction {
|
|
3890
|
+
start(knexOrModel: Knex | AnyModelConstructor): Promise<Transaction>;
|
|
3891
|
+
|
|
3892
|
+
<MC1 extends AnyModelConstructor, ReturnValue>(
|
|
3893
|
+
modelClass1: MC1,
|
|
3894
|
+
callback: (boundModelClass: MC1, trx?: Transaction) => Promise<ReturnValue>,
|
|
3895
|
+
): Promise<ReturnValue>;
|
|
3896
|
+
|
|
3897
|
+
<MC1 extends AnyModelConstructor, MC2 extends AnyModelConstructor, ReturnValue>(
|
|
3898
|
+
modelClass1: MC1,
|
|
3899
|
+
modelClass2: MC2,
|
|
3900
|
+
callback: (
|
|
3901
|
+
boundModelClass1: MC1,
|
|
3902
|
+
boundModelClass2: MC2,
|
|
3903
|
+
trx?: Transaction,
|
|
3904
|
+
) => Promise<ReturnValue>,
|
|
3905
|
+
): Promise<ReturnValue>;
|
|
3906
|
+
|
|
3907
|
+
<
|
|
3908
|
+
MC1 extends AnyModelConstructor,
|
|
3909
|
+
MC2 extends AnyModelConstructor,
|
|
3910
|
+
MC3 extends AnyModelConstructor,
|
|
3911
|
+
ReturnValue,
|
|
3912
|
+
>(
|
|
3913
|
+
modelClass1: MC1,
|
|
3914
|
+
modelClass2: MC2,
|
|
3915
|
+
modelClass3: MC3,
|
|
3916
|
+
callback: (
|
|
3917
|
+
boundModelClass1: MC1,
|
|
3918
|
+
boundModelClass2: MC2,
|
|
3919
|
+
boundModelClass3: MC3,
|
|
3920
|
+
trx?: Transaction,
|
|
3921
|
+
) => Promise<ReturnValue>,
|
|
3922
|
+
): Promise<ReturnValue>;
|
|
3923
|
+
|
|
3924
|
+
<
|
|
3925
|
+
MC1 extends AnyModelConstructor,
|
|
3926
|
+
MC2 extends AnyModelConstructor,
|
|
3927
|
+
MC3 extends AnyModelConstructor,
|
|
3928
|
+
MC4 extends AnyModelConstructor,
|
|
3929
|
+
ReturnValue,
|
|
3930
|
+
>(
|
|
3931
|
+
modelClass1: MC1,
|
|
3932
|
+
modelClass2: MC2,
|
|
3933
|
+
modelClass3: MC3,
|
|
3934
|
+
modelClass4: MC4,
|
|
3935
|
+
callback: (
|
|
3936
|
+
boundModelClass1: MC1,
|
|
3937
|
+
boundModelClass2: MC2,
|
|
3938
|
+
boundModelClass3: MC3,
|
|
3939
|
+
boundModelClass4: MC4,
|
|
3940
|
+
trx?: Transaction,
|
|
3941
|
+
) => Promise<ReturnValue>,
|
|
3942
|
+
): Promise<ReturnValue>;
|
|
3943
|
+
|
|
3944
|
+
<
|
|
3945
|
+
MC1 extends AnyModelConstructor,
|
|
3946
|
+
MC2 extends AnyModelConstructor,
|
|
3947
|
+
MC3 extends AnyModelConstructor,
|
|
3948
|
+
MC4 extends AnyModelConstructor,
|
|
3949
|
+
MC5 extends AnyModelConstructor,
|
|
3950
|
+
ReturnValue,
|
|
3951
|
+
>(
|
|
3952
|
+
modelClass1: MC1,
|
|
3953
|
+
modelClass2: MC2,
|
|
3954
|
+
modelClass3: MC3,
|
|
3955
|
+
modelClass4: MC4,
|
|
3956
|
+
modelClass5: MC5,
|
|
3957
|
+
callback: (
|
|
3958
|
+
boundModelClass1: MC1,
|
|
3959
|
+
boundModelClass2: MC2,
|
|
3960
|
+
boundModelClass3: MC3,
|
|
3961
|
+
boundModelClass4: MC4,
|
|
3962
|
+
boundModelClass5: MC5,
|
|
3963
|
+
trx?: Transaction,
|
|
3964
|
+
) => Promise<ReturnValue>,
|
|
3965
|
+
): Promise<ReturnValue>;
|
|
3966
|
+
|
|
3967
|
+
<ReturnValue>(
|
|
3968
|
+
knex: Knex,
|
|
3969
|
+
callback: (trx: Transaction) => Promise<ReturnValue>,
|
|
3970
|
+
): Promise<ReturnValue>;
|
|
3971
|
+
}
|
|
3972
|
+
|
|
3973
|
+
interface initialize {
|
|
3974
|
+
(knex: Knex, modelClasses: AnyModelConstructor[]): Promise<void>;
|
|
3975
|
+
(modelClasses: AnyModelConstructor[]): Promise<void>;
|
|
3976
|
+
}
|
|
3977
|
+
|
|
3978
|
+
/**
|
|
3979
|
+
* JSON Schema 7
|
|
3980
|
+
* Draft 07
|
|
3981
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
3982
|
+
*
|
|
3983
|
+
* These definitions were written by
|
|
3984
|
+
*
|
|
3985
|
+
* Boris Cherny https://github.com/bcherny,
|
|
3986
|
+
* Cyrille Tuzi https://github.com/cyrilletuzi,
|
|
3987
|
+
* Lucian Buzzo https://github.com/lucianbuzzo,
|
|
3988
|
+
* Roland Groza https://github.com/rolandjitsu.
|
|
3989
|
+
*
|
|
3990
|
+
* https://www.npmjs.com/package/@types/json-schema
|
|
3991
|
+
*/
|
|
3992
|
+
|
|
3993
|
+
/**
|
|
3994
|
+
* Primitive type
|
|
3995
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
|
3996
|
+
*/
|
|
3997
|
+
export type JSONSchemaTypeName =
|
|
3998
|
+
| 'string'
|
|
3999
|
+
| 'number'
|
|
4000
|
+
| 'integer'
|
|
4001
|
+
| 'boolean'
|
|
4002
|
+
| 'object'
|
|
4003
|
+
| 'array'
|
|
4004
|
+
| 'null'
|
|
4005
|
+
| string;
|
|
4006
|
+
|
|
4007
|
+
export type JSONSchemaType = JSONSchemaArray[] | boolean | number | null | object | string;
|
|
4008
|
+
|
|
4009
|
+
// Workaround for infinite type recursion
|
|
4010
|
+
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
|
|
4011
|
+
export interface JSONSchemaArray extends Array<JSONSchemaType> {}
|
|
4012
|
+
|
|
4013
|
+
/**
|
|
4014
|
+
* Meta schema
|
|
4015
|
+
*
|
|
4016
|
+
* Recommended values:
|
|
4017
|
+
* - 'http://json-schema.org/schema#'
|
|
4018
|
+
* - 'http://json-schema.org/hyper-schema#'
|
|
4019
|
+
* - 'http://json-schema.org/draft-07/schema#'
|
|
4020
|
+
* - 'http://json-schema.org/draft-07/hyper-schema#'
|
|
4021
|
+
*
|
|
4022
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
|
|
4023
|
+
*/
|
|
4024
|
+
export type JSONSchemaVersion = string;
|
|
4025
|
+
|
|
4026
|
+
/**
|
|
4027
|
+
* JSON Schema v7
|
|
4028
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
4029
|
+
*/
|
|
4030
|
+
export type JSONSchemaDefinition = JSONSchema | boolean;
|
|
4031
|
+
export interface JSONSchema {
|
|
4032
|
+
$id?: string;
|
|
4033
|
+
$ref?: string;
|
|
4034
|
+
$schema?: JSONSchemaVersion;
|
|
4035
|
+
$comment?: string;
|
|
4036
|
+
|
|
4037
|
+
/**
|
|
4038
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
|
|
4039
|
+
*/
|
|
4040
|
+
type?: JSONSchemaTypeName | JSONSchemaTypeName[];
|
|
4041
|
+
enum?: JSONSchemaType[];
|
|
4042
|
+
const?: JSONSchemaType;
|
|
4043
|
+
|
|
4044
|
+
/**
|
|
4045
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
|
|
4046
|
+
*/
|
|
4047
|
+
multipleOf?: number;
|
|
4048
|
+
maximum?: number;
|
|
4049
|
+
exclusiveMaximum?: number;
|
|
4050
|
+
minimum?: number;
|
|
4051
|
+
exclusiveMinimum?: number;
|
|
4052
|
+
|
|
4053
|
+
/**
|
|
4054
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
|
|
4055
|
+
*/
|
|
4056
|
+
maxLength?: number;
|
|
4057
|
+
minLength?: number;
|
|
4058
|
+
pattern?: string;
|
|
4059
|
+
|
|
4060
|
+
/**
|
|
4061
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
|
|
4062
|
+
*/
|
|
4063
|
+
items?: JSONSchemaDefinition | JSONSchemaDefinition[];
|
|
4064
|
+
additionalItems?: JSONSchemaDefinition;
|
|
4065
|
+
maxItems?: number;
|
|
4066
|
+
minItems?: number;
|
|
4067
|
+
uniqueItems?: boolean;
|
|
4068
|
+
contains?: JSONSchema;
|
|
4069
|
+
|
|
4070
|
+
/**
|
|
4071
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
|
|
4072
|
+
*/
|
|
4073
|
+
maxProperties?: number;
|
|
4074
|
+
minProperties?: number;
|
|
4075
|
+
required?: string[];
|
|
4076
|
+
properties?: {
|
|
4077
|
+
[key: string]: JSONSchemaDefinition;
|
|
4078
|
+
};
|
|
4079
|
+
patternProperties?: {
|
|
4080
|
+
[key: string]: JSONSchemaDefinition;
|
|
4081
|
+
};
|
|
4082
|
+
additionalProperties?: JSONSchemaDefinition;
|
|
4083
|
+
dependencies?: {
|
|
4084
|
+
[key: string]: JSONSchemaDefinition | string[];
|
|
4085
|
+
};
|
|
4086
|
+
propertyNames?: JSONSchemaDefinition;
|
|
4087
|
+
|
|
4088
|
+
/**
|
|
4089
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
|
|
4090
|
+
*/
|
|
4091
|
+
if?: JSONSchemaDefinition;
|
|
4092
|
+
then?: JSONSchemaDefinition;
|
|
4093
|
+
else?: JSONSchemaDefinition;
|
|
4094
|
+
|
|
4095
|
+
/**
|
|
4096
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
|
|
4097
|
+
*/
|
|
4098
|
+
allOf?: JSONSchemaDefinition[];
|
|
4099
|
+
anyOf?: JSONSchemaDefinition[];
|
|
4100
|
+
oneOf?: JSONSchemaDefinition[];
|
|
4101
|
+
not?: JSONSchemaDefinition;
|
|
4102
|
+
|
|
4103
|
+
/**
|
|
4104
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
|
|
4105
|
+
*/
|
|
4106
|
+
format?: string;
|
|
4107
|
+
|
|
4108
|
+
/**
|
|
4109
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
|
|
4110
|
+
*/
|
|
4111
|
+
contentMediaType?: string;
|
|
4112
|
+
contentEncoding?: string;
|
|
4113
|
+
|
|
4114
|
+
/**
|
|
4115
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
|
|
4116
|
+
*/
|
|
4117
|
+
definitions?: {
|
|
4118
|
+
[key: string]: JSONSchemaDefinition;
|
|
4119
|
+
};
|
|
4120
|
+
|
|
4121
|
+
/**
|
|
4122
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
|
|
4123
|
+
*/
|
|
4124
|
+
title?: string;
|
|
4125
|
+
description?: string;
|
|
4126
|
+
default?: JSONSchemaType;
|
|
4127
|
+
readOnly?: boolean;
|
|
4128
|
+
writeOnly?: boolean;
|
|
4129
|
+
examples?: JSONSchemaType;
|
|
4130
|
+
}
|
|
4131
|
+
}
|