objection 3.1.5 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/README.md +16 -16
  2. package/lib/model/AjvValidator.js +173 -18
  3. package/lib/model/Model.js +3 -1
  4. package/lib/model/graph/ModelGraphNode.js +12 -0
  5. package/lib/model/modelToJson.js +2 -1
  6. package/lib/queryBuilder/QueryBuilder.js +339 -47
  7. package/lib/queryBuilder/QueryBuilderOperationSupport.js +5 -0
  8. package/lib/queryBuilder/ReferenceBuilder.js +5 -3
  9. package/lib/queryBuilder/RelationExpression.js +21 -9
  10. package/lib/queryBuilder/StaticHookArguments.js +12 -1
  11. package/lib/queryBuilder/graph/GraphFetcher.js +3 -1
  12. package/lib/queryBuilder/graph/GraphOptions.js +21 -2
  13. package/lib/queryBuilder/graph/GraphUpsert.js +52 -0
  14. package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +20 -14
  15. package/lib/queryBuilder/graph/insert/DeferredRelationGraphInsertAction.js +57 -0
  16. package/lib/queryBuilder/graph/insert/GraphInsert.js +113 -8
  17. package/lib/queryBuilder/graph/insert/GraphInsertAction.js +10 -1
  18. package/lib/queryBuilder/graph/patch/GraphPatchAction.js +15 -3
  19. package/lib/queryBuilder/join/JoinResultParser.js +22 -0
  20. package/lib/queryBuilder/join/RelationJoiner.js +71 -5
  21. package/lib/queryBuilder/operations/DelegateOperation.js +4 -0
  22. package/lib/queryBuilder/operations/InsertAndFetchOperation.js +7 -1
  23. package/lib/queryBuilder/operations/InsertGraphOperation.js +2 -2
  24. package/lib/queryBuilder/operations/InsertOperation.js +121 -12
  25. package/lib/queryBuilder/operations/NoneOperation.js +37 -0
  26. package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +5 -0
  27. package/lib/queryBuilder/operations/QueryBuilderOperation.js +4 -0
  28. package/lib/queryBuilder/operations/ReturningOperation.js +28 -3
  29. package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +20 -0
  30. package/lib/queryBuilder/operations/UpdateOperation.js +3 -2
  31. package/lib/queryBuilder/operations/UpsertGraphOperation.js +5 -0
  32. package/lib/queryBuilder/operations/WhereGroupOperation.js +41 -0
  33. package/lib/queryBuilder/operations/eager/EagerOperation.js +7 -5
  34. package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +4 -2
  35. package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +9 -20
  36. package/lib/queryBuilder/operations/jsonApi/WhereJsonHasPostgresOperation.js +3 -3
  37. package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectPostgresOperation.js +1 -1
  38. package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +45 -32
  39. package/lib/queryBuilder/operations/select/Selection.js +95 -4
  40. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +6 -0
  41. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +5 -1
  42. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +9 -0
  43. package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +81 -43
  44. package/lib/queryBuilder/parsers/jsonFieldExpressionParser.pegjs +5 -4
  45. package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +9 -0
  46. package/lib/queryBuilder/whereGroups.js +125 -0
  47. package/lib/relations/Relation.js +5 -2
  48. package/lib/relations/RelationProperty.js +7 -3
  49. package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +1 -0
  50. package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +5 -0
  51. package/lib/relations/hasMany/HasManyRelateOperation.js +6 -2
  52. package/lib/relations/hasMany/HasManyUnrelateOperation.js +5 -2
  53. package/lib/relations/manyToMany/ManyToManyModifyMixin.js +26 -3
  54. package/lib/relations/manyToMany/ManyToManyMySqlModifyMixin.js +66 -0
  55. package/lib/relations/manyToMany/ManyToManyPostgresModifyMixin.js +27 -0
  56. package/lib/relations/manyToMany/ManyToManyRelation.js +70 -48
  57. package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +6 -0
  58. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateMySqlOperation.js +16 -0
  59. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +5 -1
  60. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelatePostgresOperation.js +16 -0
  61. package/lib/relations/manyToMany/update/ManyToManyUpdateMySqlOperation.js +12 -0
  62. package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +8 -2
  63. package/lib/relations/manyToMany/update/ManyToManyUpdatePostgresOperation.js +12 -0
  64. package/lib/utils/deprecate.js +6 -0
  65. package/lib/utils/identifierMapping.js +23 -3
  66. package/lib/utils/objectUtils.js +9 -5
  67. package/lib/utils/parseFieldExpression.js +28 -0
  68. package/package.json +22 -14
  69. package/typings/objection/index.d.ts +339 -68
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
 
3
3
  // Type definitions for Objection.js
4
- // Project: <http://vincit.github.io/objection.js/>
4
+ // Project: <https://ditojs.github.io/objection/>
5
5
  //
6
6
  // Contributions by:
7
7
  // * Matthew McEachen <https://github.com/mceachen>
@@ -30,17 +30,20 @@ declare namespace Objection {
30
30
 
31
31
  const snakeCaseMappers: SnakeCaseMappersFactory;
32
32
  const knexSnakeCaseMappers: KnexSnakeCaseMappersFactory;
33
+ const knexIdentifierMapping: KnexIdentifierMappingFactory;
33
34
 
34
35
  const transaction: transaction;
35
36
  const initialize: initialize;
36
37
 
37
- const DBError: typeof dbErrors.DBError;
38
- const DataError: typeof dbErrors.DataError;
39
- const CheckViolationError: typeof dbErrors.CheckViolationError;
40
- const UniqueViolationError: typeof dbErrors.UniqueViolationError;
41
- const ConstraintViolationError: typeof dbErrors.ConstraintViolationError;
42
- const ForeignKeyViolationError: typeof dbErrors.ForeignKeyViolationError;
43
- const NotNullViolationError: typeof dbErrors.NotNullViolationError;
38
+ // Import aliases re-export both the value and the type side of the classes,
39
+ // so they can be used with `instanceof` as well as in type annotations.
40
+ export import DBError = dbErrors.DBError;
41
+ export import DataError = dbErrors.DataError;
42
+ export import CheckViolationError = dbErrors.CheckViolationError;
43
+ export import UniqueViolationError = dbErrors.UniqueViolationError;
44
+ export import ConstraintViolationError = dbErrors.ConstraintViolationError;
45
+ export import ForeignKeyViolationError = dbErrors.ForeignKeyViolationError;
46
+ export import NotNullViolationError = dbErrors.NotNullViolationError;
44
47
 
45
48
  export interface RawBuilder extends Aliasable {}
46
49
 
@@ -126,11 +129,13 @@ declare namespace Objection {
126
129
  | string
127
130
  | number
128
131
  | boolean
132
+ | bigint
129
133
  | Date
130
134
  | Buffer
131
135
  | string[]
132
136
  | number[]
133
137
  | boolean[]
138
+ | bigint[]
134
139
  | Date[]
135
140
  | Buffer[]
136
141
  | null;
@@ -158,10 +163,7 @@ declare namespace Objection {
158
163
  type AnyModelConstructor = ModelConstructor<Model>;
159
164
  type ModifierFunction<QB extends AnyQueryBuilder> = (qb: QB, ...args: any[]) => void;
160
165
  type Modifier<QB extends AnyQueryBuilder = AnyQueryBuilder> =
161
- | ModifierFunction<QB>
162
- | string
163
- | string[]
164
- | Record<string, Expression<PrimitiveValue>>;
166
+ ModifierFunction<QB> | string | string[] | Record<string, Expression<PrimitiveValue>>;
165
167
  type OrderByDirection = 'asc' | 'desc' | 'ASC' | 'DESC';
166
168
  type OrderByNulls = 'first' | 'last';
167
169
 
@@ -171,6 +173,216 @@ declare namespace Objection {
171
173
 
172
174
  type RelationExpression<M extends Model> = string | object;
173
175
 
176
+ // Type-level parsing of relation expressions, used to narrow the result
177
+ // types of `withGraphFetched()`, `withGraphJoined()` and `fetchGraph()`.
178
+
179
+ /**
180
+ * Parses a relation expression, in string or object notation, into a tree
181
+ * of fetched relations: '[pets.owner, children]' and
182
+ * `{ pets: { owner: true }, children: true }` both become
183
+ * `{ pets: { owner: {} }, children: {} }`.
184
+ *
185
+ * Anything that isn't a literal expression (e.g. a `string` variable) yields
186
+ * `{}`, and so do nodes the parser doesn't understand (aliases, `*`,
187
+ * recursion with `^`), so these simply don't narrow.
188
+ */
189
+ type ParseRelationExpression<E> = string extends E
190
+ ? {}
191
+ : E extends string
192
+ ? ParseRelationString<E>
193
+ : ParseRelationObject<E>;
194
+
195
+ type Whitespace = ' ' | '\n' | '\r' | '\t';
196
+
197
+ type Trim<S extends string> = S extends `${Whitespace}${infer R}`
198
+ ? Trim<R>
199
+ : S extends `${infer L}${Whitespace}`
200
+ ? Trim<L>
201
+ : S;
202
+
203
+ /**
204
+ * Counts the occurrences of character C in S, as a tuple length.
205
+ */
206
+ type CountChar<
207
+ S extends string,
208
+ C extends string,
209
+ N extends 0[] = [],
210
+ > = S extends `${string}${C}${infer R}` ? CountChar<R, C, [...N, 0]> : N['length'];
211
+
212
+ /**
213
+ * True if S has as many opening as closing brackets and parentheses.
214
+ */
215
+ type IsBalanced<S extends string> =
216
+ CountChar<S, '['> extends CountChar<S, ']'>
217
+ ? CountChar<S, '('> extends CountChar<S, ')'>
218
+ ? true
219
+ : false
220
+ : false;
221
+
222
+ /**
223
+ * Splits a list of expressions at the commas that aren't nested inside
224
+ * brackets or parentheses: 'a, b.[c, d]' -> ['a', 'b.[c, d]'].
225
+ */
226
+ type SplitList<
227
+ S extends string,
228
+ Acc extends string = '',
229
+ Out extends string[] = [],
230
+ > = S extends `${infer Head},${infer Rest}`
231
+ ? IsBalanced<`${Acc}${Head}`> extends true
232
+ ? SplitList<Rest, '', [...Out, `${Acc}${Head}`]>
233
+ : SplitList<Rest, `${Acc}${Head},`, Out>
234
+ : [...Out, `${Acc}${S}`];
235
+
236
+ type ParseRelationList<L extends string[], Acc = {}> = L extends [
237
+ infer H extends string,
238
+ ...infer T extends string[],
239
+ ]
240
+ ? ParseRelationList<T, Acc & ParseRelationString<H>>
241
+ : Acc;
242
+
243
+ /**
244
+ * Parses '[a, b]', 'a.b' and 'a' style expressions.
245
+ */
246
+ type ParseRelationString<S extends string> =
247
+ Trim<S> extends `[${infer Inner}]`
248
+ ? ParseRelationList<SplitList<Inner>>
249
+ : Trim<S> extends `${infer Head}.${infer Rest}`
250
+ ? IsBalanced<Head> extends true
251
+ ? RelationNode<RelationName<Head>, ParseRelationString<Rest>>
252
+ : {}
253
+ : RelationNode<RelationName<S>, {}>;
254
+
255
+ /**
256
+ * Extracts the relation name from a single node, dropping modifiers:
257
+ * 'pets(selectName)' -> 'pets'. Returns never for nodes that can't be
258
+ * narrowed: aliases ('pets as p'), '*' and recursion ('^', '^2').
259
+ */
260
+ type RelationName<S extends string> =
261
+ Trim<S> extends `${infer Name}(${string})`
262
+ ? RelationName<Name>
263
+ : Trim<S> extends infer Name extends string
264
+ ? Name extends '' | '*' | `^${string}` | `${string}${Whitespace}${string}`
265
+ ? never
266
+ : string extends Name
267
+ ? never
268
+ : Name
269
+ : never;
270
+
271
+ type RelationNode<Name extends string, Children> = [Name] extends [never]
272
+ ? {}
273
+ : { [K in Name]: Children };
274
+
275
+ /**
276
+ * Parses `{ a: true, b: { c: true } }` style expressions. Keys starting with
277
+ * `$` are options, and aliased nodes (`{ p: { $relation: 'pets' } }`) are
278
+ * skipped.
279
+ */
280
+ type ParseRelationObject<E> = string extends keyof E
281
+ ? {}
282
+ : {
283
+ [
284
+ K in keyof E as K extends `$${string}`
285
+ ? never
286
+ : false extends E[K]
287
+ ? never
288
+ : E[K] extends { $relation: any }
289
+ ? never
290
+ : K
291
+ ]: E[K] extends object ? ParseRelationObject<E[K]> : {};
292
+ };
293
+
294
+ /**
295
+ * Marks the relations in tree T as fetched on model M: they become required
296
+ * and are narrowed recursively. Only `undefined` is removed: declared `null`
297
+ * is kept, as to-one relations can be null when there is no related row.
298
+ */
299
+ type WithGraph<M, T> = [keyof T] extends [never]
300
+ ? M
301
+ : WithGraphRelations<M, T, FetchedRelations<M, T>>;
302
+
303
+ type WithGraphRelations<M, T, K extends keyof M & keyof T> = [K] extends [never]
304
+ ? M
305
+ : { -readonly [P in K]-?: WithGraphProperty<Defined<M[P]>, T[P]> } & UnnarrowedMethods<M> & M;
306
+
307
+ /**
308
+ * `$query()` of the un-narrowed model M, as it doesn't fetch the relations
309
+ * that were fetched on the instance. Otherwise its polymorphic `this` would
310
+ * resolve to the narrowed model. With chained narrowing, the first `$query`
311
+ * signature of the intersection wins, which is the one of the innermost,
312
+ * un-narrowed model.
313
+ */
314
+ type UnnarrowedMethods<M> = Pick<M, Extract<'$query', keyof M>>;
315
+
316
+ /**
317
+ * The keys of tree T that are relation properties of model M.
318
+ */
319
+ type FetchedRelations<M, T> = {
320
+ [K in keyof M & keyof T]: NonNullable<M[K]> extends Model | Model[] ? K : never;
321
+ }[keyof M & keyof T];
322
+
323
+ type WithGraphProperty<P, T> = [keyof T] extends [never]
324
+ ? P
325
+ : P extends Array<infer I>
326
+ ? WithGraph<I, T>[]
327
+ : P extends Model
328
+ ? WithGraph<P, T>
329
+ : P;
330
+
331
+ /**
332
+ * The model type after fetching the relation expression E on model M.
333
+ */
334
+ type WithGraphModel<M, E> = WithGraph<M, ParseRelationExpression<E>>;
335
+
336
+ /**
337
+ * The query builder type for model M after fetching relation expression E.
338
+ * `M extends unknown` makes this distributive, so that in generic code (e.g.
339
+ * `this` in model methods), TypeScript can resolve it through the constraint
340
+ * of M.
341
+ */
342
+ type WithGraphModelQueryBuilder<M extends Model, E> = M extends unknown
343
+ ? WithGraphModel<M, E>['QueryBuilderType']
344
+ : never;
345
+
346
+ /**
347
+ * The query builder type after `withGraphFetched(E)` / `withGraphJoined(E)`
348
+ * on QB. The model type is narrowed, and the query builder for the narrowed
349
+ * model is looked up through its `QueryBuilderType`, so custom query
350
+ * builders are kept. Falls back to QB if nothing can be narrowed, or if the
351
+ * model type is `any` (`0 extends 1 & M`), e.g. for AnyQueryBuilder.
352
+ * `QB extends unknown` below serves the same purpose as in
353
+ * WithGraphModelQueryBuilder.
354
+ */
355
+ type WithGraphQueryBuilder<QB extends AnyQueryBuilder, E> = WithGraphTreeQueryBuilder<
356
+ QB,
357
+ ParseRelationExpression<E>
358
+ >;
359
+
360
+ type WithGraphTreeQueryBuilder<QB extends AnyQueryBuilder, T> = [keyof T] extends [never]
361
+ ? QB
362
+ : QB extends unknown
363
+ ? 0 extends 1 & ModelType<QB>
364
+ ? QB
365
+ : [FetchedRelations<ModelType<QB>, T>] extends [never]
366
+ ? QB
367
+ : WithResultKind<QB, WithGraph<ModelType<QB>, T>['QueryBuilderType']>
368
+ : never;
369
+
370
+ /**
371
+ * Converts the query builder NQB to the same result kind as QB:
372
+ * array, single, maybe-single or page.
373
+ */
374
+ type WithResultKind<QB extends AnyQueryBuilder, NQB extends AnyQueryBuilder> = [
375
+ ResultType<QB>,
376
+ ] extends [ModelType<QB>[]]
377
+ ? ArrayQueryBuilder<NQB>
378
+ : [ResultType<QB>] extends [ModelType<QB>]
379
+ ? SingleQueryBuilder<NQB>
380
+ : [ResultType<QB>] extends [ModelType<QB> | undefined]
381
+ ? MaybeSingleQueryBuilder<NQB>
382
+ : [ResultType<QB>] extends [Page<ModelType<QB>>]
383
+ ? PageQueryBuilder<NQB>
384
+ : QB;
385
+
174
386
  /**
175
387
  * If T is an array, returns the item type, otherwise returns T.
176
388
  */
@@ -201,14 +413,24 @@ declare namespace Objection {
201
413
  */
202
414
  type PartialModelObject<T extends Model> = {
203
415
  [K in DataPropertyNames<T>]?: Defined<T[K]> extends Model
204
- ? T[K]
416
+ ? T[K] | PartialModelObject<Defined<T[K]>>
205
417
  : Defined<T[K]> extends Array<infer I>
206
418
  ? I extends Model
207
- ? I[]
419
+ ? I[] | PartialModelObject<I>[]
208
420
  : Expression<T[K]>
209
421
  : Expression<T[K]>;
210
422
  };
211
423
 
424
+ /**
425
+ * Like PartialModelObject, but relation properties are not allowed. Unlike
426
+ * PartialModelObject, this doesn't depend on the relation types, so query
427
+ * builders for models with narrowed relations (see WithGraphModel) remain
428
+ * assignable to the query builders for the plain models.
429
+ */
430
+ type PartialModelProps<T extends Model> = {
431
+ [K in DataPropertyNames<T>]?: Defined<T[K]> extends Model | Model[] ? never : Expression<T[K]>;
432
+ };
433
+
212
434
  /**
213
435
  * Additional optional parameters that may be used in graphs.
214
436
  */
@@ -216,6 +438,8 @@ declare namespace Objection {
216
438
  '#dbRef'?: MaybeCompositeId;
217
439
  '#ref'?: string;
218
440
  '#id'?: string;
441
+ '#unrelate'?: boolean;
442
+ '#delete'?: boolean;
219
443
  };
220
444
 
221
445
  /**
@@ -341,7 +565,7 @@ declare namespace Objection {
341
565
  }
342
566
 
343
567
  /**
344
- * https://vincit.github.io/objection.js/api/types/#type-fieldexpression
568
+ * https://ditojs.github.io/objection/api/types/#type-fieldexpression
345
569
  */
346
570
  type FieldExpression = string;
347
571
 
@@ -440,7 +664,7 @@ declare namespace Objection {
440
664
  (raw: Raw): QB;
441
665
  <QBA extends AnyQueryBuilder>(qb: QBA): QB;
442
666
 
443
- (obj: PartialModelObject<ModelType<QB>>): QB;
667
+ (obj: PartialModelProps<ModelType<QB>>): QB;
444
668
  // We must allow any keys in the object. The previous type
445
669
  // is kind of useless, but maybe one day vscode and other
446
670
  // tools can autocomplete using it.
@@ -597,6 +821,7 @@ declare namespace Objection {
597
821
 
598
822
  interface WithMethod<QB extends AnyQueryBuilder> {
599
823
  (alias: string, expr: CallbackVoid<QB> | AnyQueryBuilder | Raw): QB;
824
+ (alias: string, columns: string[], expr: CallbackVoid<QB> | AnyQueryBuilder | Raw): QB;
600
825
  }
601
826
 
602
827
  interface JoinRelatedOptions {
@@ -612,6 +837,7 @@ declare namespace Objection {
612
837
  (table: TableRef<QB>, leftCol: ColumnRef, op: Operator, rightCol: ColumnRef): QB;
613
838
  (table: TableRef<QB>, leftCol: ColumnRef, rightCol: ColumnRef): QB;
614
839
  (table: TableRef<QB>, cb: CallbackVoid<Knex.JoinClause>): QB;
840
+ (table: TableRef<QB>, columns: { [leftCol: string]: ColumnRef }): QB;
615
841
  (table: TableRef<QB>, raw: Raw): QB;
616
842
  (raw: Raw): QB;
617
843
  }
@@ -624,6 +850,7 @@ declare namespace Objection {
624
850
 
625
851
  interface AggregateMethod<QB extends AnyQueryBuilder> {
626
852
  (column: ColumnRef): QB;
853
+ (aliasToColumnDict: { [alias: string]: ColumnRef }): QB;
627
854
  }
628
855
 
629
856
  interface CountMethod<QB extends AnyQueryBuilder> {
@@ -655,7 +882,7 @@ declare namespace Objection {
655
882
  interface FirstMethod {
656
883
  <QB extends AnyQueryBuilder>(
657
884
  this: QB,
658
- ): QB extends ArrayQueryBuilder<QB> ? MaybeSingleQueryBuilder<QB> : QB;
885
+ ): [ResultType<QB>] extends [any[]] ? MaybeSingleQueryBuilder<QB> : QB;
659
886
  }
660
887
 
661
888
  type ForIdValue = MaybeCompositeId | AnyQueryBuilder;
@@ -693,10 +920,11 @@ declare namespace Objection {
693
920
  }
694
921
 
695
922
  interface TableRefForMethod {
696
- (modelClass: ModelClass<Model> | typeof Model): string;
923
+ (modelClassOrTableName: string | AnyModelConstructor): string;
697
924
  }
698
925
 
699
926
  interface AliasForMethod<QB extends AnyQueryBuilder> {
927
+ (modelClassOrTableName: string | AnyModelConstructor): string | null;
700
928
  (modelClassOrTableName: string | AnyModelConstructor, alias: string): QB;
701
929
  }
702
930
 
@@ -704,10 +932,15 @@ declare namespace Objection {
704
932
  (): ModelClass<M>;
705
933
  }
706
934
 
935
+ interface ReturningOptions {
936
+ includeTriggerModifications?: boolean;
937
+ }
938
+
707
939
  interface ReturningMethod {
708
940
  <QB extends AnyQueryBuilder>(
709
941
  this: QB,
710
- column: string | Raw | (string | Raw)[],
942
+ column: string | Raw | (string | Raw)[] | readonly (string | Raw)[],
943
+ options?: ReturningOptions,
711
944
  ): QB extends NumberQueryBuilder<QB> ? ArrayQueryBuilder<QB> : QB;
712
945
  }
713
946
 
@@ -818,6 +1051,10 @@ declare namespace Objection {
818
1051
  maxBatchSize?: number;
819
1052
  }
820
1053
 
1054
+ export interface WithGraphOptions extends GraphOptions {
1055
+ algorithm?: 'fetch' | 'join';
1056
+ }
1057
+
821
1058
  interface ModifyGraphMethod<QB extends AnyQueryBuilder> {
822
1059
  <M extends Model>(
823
1060
  expr: RelationExpression<ModelType<QB>>,
@@ -856,6 +1093,8 @@ declare namespace Objection {
856
1093
  export class QueryBuilder<M extends Model, R = M[]> implements CatchablePromiseLike<R> {
857
1094
  static forClass: ForClassMethod;
858
1095
 
1096
+ constructor(modelClass: ModelConstructor<M>);
1097
+
859
1098
  select: SelectMethod<this>;
860
1099
  columns: SelectMethod<this>;
861
1100
  column: SelectMethod<this>;
@@ -1031,6 +1270,7 @@ declare namespace Objection {
1031
1270
  increment: IncrementDecrementMethod<this>;
1032
1271
  decrement: IncrementDecrementMethod<this>;
1033
1272
  first: FirstMethod;
1273
+ none: IdentityMethod<this>;
1034
1274
 
1035
1275
  orderBy: OrderByMethod<this>;
1036
1276
  orderByRaw: OrderByRawMethod<this>;
@@ -1048,6 +1288,7 @@ declare namespace Objection {
1048
1288
 
1049
1289
  update(update: PartialModelObject<M>): NumberQueryBuilder<this>;
1050
1290
  update(): NumberQueryBuilder<this>;
1291
+ updateById(id: MaybeCompositeId, update: PartialModelObject<M>): NumberQueryBuilder<this>;
1051
1292
  updateAndFetch(update: PartialModelObject<M>): SingleQueryBuilder<this>;
1052
1293
  updateAndFetchById(
1053
1294
  id: MaybeCompositeId,
@@ -1056,6 +1297,7 @@ declare namespace Objection {
1056
1297
 
1057
1298
  patch(update: PartialModelObject<M>): NumberQueryBuilder<this>;
1058
1299
  patch(): NumberQueryBuilder<this>;
1300
+ patchById(id: MaybeCompositeId, update: PartialModelObject<M>): NumberQueryBuilder<this>;
1059
1301
  patchAndFetch(update: PartialModelObject<M>): SingleQueryBuilder<this>;
1060
1302
  patchAndFetchById(
1061
1303
  id: MaybeCompositeId,
@@ -1085,15 +1327,25 @@ declare namespace Objection {
1085
1327
  unrelate(): NumberQueryBuilder<this>;
1086
1328
  for(ids: ForIdValue | ForIdValue[]): this;
1087
1329
 
1088
- withGraphFetched(expr: RelationExpression<M>, options?: GraphOptions): this;
1089
- withGraphJoined(expr: RelationExpression<M>, options?: GraphOptions): this;
1330
+ // With literal relation expressions, the fetched relations become required
1331
+ // on the result type, see WithGraphQueryBuilder.
1332
+ withGraphFetched<const E extends RelationExpression<M>>(
1333
+ expr: E,
1334
+ options?: GraphOptions,
1335
+ ): WithGraphQueryBuilder<this, E>;
1336
+ withGraphJoined<const E extends RelationExpression<M>>(
1337
+ expr: E,
1338
+ options?: GraphOptions,
1339
+ ): WithGraphQueryBuilder<this, E>;
1340
+ withGraph<const E extends RelationExpression<M>>(
1341
+ expr: E,
1342
+ options?: WithGraphOptions,
1343
+ ): WithGraphQueryBuilder<this, E>;
1090
1344
 
1091
1345
  truncate(): Promise<void>;
1092
1346
  allowGraph: AllowGraphMethod<this>;
1093
1347
 
1094
- throwIfNotFound: (
1095
- arg?: any,
1096
- ) => R extends Model | undefined ? SingleQueryBuilder<QueryBuilder<M, M>> : this;
1348
+ throwIfNotFound: (arg?: any) => R extends Model | undefined ? SingleQueryBuilder<this> : this;
1097
1349
 
1098
1350
  returning: ReturningMethod;
1099
1351
  forUpdate: IdentityMethod<this>;
@@ -1110,6 +1362,8 @@ declare namespace Objection {
1110
1362
  modelClass: ModelClassMethod<M>;
1111
1363
  tableNameFor: TableRefForMethod;
1112
1364
  tableRefFor: TableRefForMethod;
1365
+ tableName(): string;
1366
+ tableRef(): string;
1113
1367
  reject: OneArgMethod<any, this>;
1114
1368
  resolve: OneArgMethod<any, this>;
1115
1369
  transacting: OneArgMethod<TransactionOrKnex, this>;
@@ -1120,12 +1374,13 @@ declare namespace Objection {
1120
1374
  toKnexQuery<T extends {} = ModelObject<M>>(): Knex.QueryBuilder<T, T[]>;
1121
1375
  knex(knex?: Knex): Knex;
1122
1376
  clone(): this;
1377
+ emptyInstance(): this;
1123
1378
 
1124
1379
  page(page: number, pageSize: number): PageQueryBuilder<this>;
1125
1380
  range(): PageQueryBuilder<this>;
1126
1381
  range(start: number, end: number): PageQueryBuilder<this>;
1127
- offset(offset: number): this;
1128
- limit(limit: number): this;
1382
+ offset(offset: number, options?: boolean | { skipBinding?: boolean }): this;
1383
+ limit(limit: number, options?: boolean | { skipBinding?: boolean }): this;
1129
1384
  resultSize(): Promise<number>;
1130
1385
 
1131
1386
  runBefore: RunBeforeMethod<this>;
@@ -1159,6 +1414,7 @@ declare namespace Objection {
1159
1414
  isRelate: BooleanReturningMethod;
1160
1415
  isUnrelate: BooleanReturningMethod;
1161
1416
  isInternal: BooleanReturningMethod;
1417
+ isJoinChildQuery: BooleanReturningMethod;
1162
1418
  hasWheres: BooleanReturningMethod;
1163
1419
  hasSelects: BooleanReturningMethod;
1164
1420
  hasWithGraph: BooleanReturningMethod;
@@ -1302,6 +1558,7 @@ declare namespace Objection {
1302
1558
 
1303
1559
  export interface ToJsonOptions extends CloneOptions {
1304
1560
  virtuals?: boolean | string[];
1561
+ format?: Pojo;
1305
1562
  }
1306
1563
 
1307
1564
  export interface ValidatorContext {
@@ -1334,6 +1591,13 @@ declare namespace Objection {
1334
1591
  upperCase?: boolean;
1335
1592
  underscoreBeforeDigits?: boolean;
1336
1593
  underscoreBetweenUppercaseLetters?: boolean;
1594
+ noDoubleUnderscores?: boolean;
1595
+ /**
1596
+ * Only convert the column part of field expressions like
1597
+ * `jsonColumn:someKey` and keep their JSON keys as written.
1598
+ * Only used by `snakeCaseMappers`.
1599
+ */
1600
+ preserveJsonKeys?: boolean;
1337
1601
  }
1338
1602
 
1339
1603
  export interface ColumnNameMappers {
@@ -1354,11 +1618,12 @@ declare namespace Objection {
1354
1618
  (options?: SnakeCaseMappersOptions): KnexMappers;
1355
1619
  }
1356
1620
 
1621
+ export interface KnexIdentifierMappingFactory {
1622
+ (colToProp: Record<string, string>): KnexMappers;
1623
+ }
1624
+
1357
1625
  export type ValidationErrorType =
1358
- | 'ModelValidation'
1359
- | 'RelationExpression'
1360
- | 'UnallowedRelation'
1361
- | 'InvalidGraph';
1626
+ 'ModelValidation' | 'RelationExpression' | 'UnallowedRelation' | 'InvalidGraph';
1362
1627
 
1363
1628
  export class ValidationError extends Error {
1364
1629
  constructor(args: CreateValidationErrorArgs & { modelClass?: ModelClass<Model> });
@@ -1426,15 +1691,17 @@ declare namespace Objection {
1426
1691
  interface PrototypeType<T> extends Function {
1427
1692
  prototype: T;
1428
1693
  }
1429
-
1694
+
1430
1695
  interface ConstructorFunctionType<T = any> extends PrototypeType<T> {
1431
1696
  new (...args: any[]): T;
1432
1697
  }
1433
-
1698
+
1434
1699
  // for internal use on generic static this deduction, copied from https://github.com/microsoft/TypeScript/issues/5863#issuecomment-1483978415
1435
- type ConstructorType<T = unknown, Static extends Record<string, any> = PrototypeType<T>> = (ConstructorFunctionType<T> | PrototypeType<T>) & {
1700
+ type ConstructorType<T = unknown, Static extends Record<string, any> = PrototypeType<T>> = (
1701
+ ConstructorFunctionType<T> | PrototypeType<T>
1702
+ ) & {
1436
1703
  [Key in keyof Static]: Static[Key];
1437
- };
1704
+ };
1438
1705
 
1439
1706
  export interface ModelConstructor<M extends Model> extends Constructor<M> {}
1440
1707
 
@@ -1442,16 +1709,18 @@ declare namespace Objection {
1442
1709
  QueryBuilder: typeof QueryBuilder;
1443
1710
 
1444
1711
  tableName: string;
1445
- idColumn: string | string[];
1712
+ idColumn: null | string | string[] | readonly string[];
1446
1713
  jsonSchema: JSONSchema;
1447
1714
  relationMappings: RelationMappings | RelationMappingsThunk;
1448
1715
  modelPaths: string[];
1449
- jsonAttributes: string[];
1716
+ jsonAttributes: string[] | readonly string[];
1450
1717
  virtualAttributes: string[];
1451
1718
  uidProp: string;
1452
1719
  uidRefProp: string;
1453
1720
  dbRefProp: string;
1454
1721
  propRefRegex: RegExp;
1722
+ graphUnrelateProp: string;
1723
+ graphDeleteProp: string;
1455
1724
  pickJsonSchemaProperties: boolean;
1456
1725
  relatedFindQueryMutates: boolean;
1457
1726
  relatedInsertQueryMutates: boolean;
@@ -1509,17 +1778,17 @@ declare namespace Objection {
1509
1778
  bindKnex(trxOrKnex: TransactionOrKnex): this;
1510
1779
  bindTransaction(trxOrKnex: TransactionOrKnex): this;
1511
1780
 
1512
- fetchGraph(
1781
+ fetchGraph<const E extends RelationExpression<M>>(
1513
1782
  modelOrObject: PartialModelObject<M>,
1514
- expression: RelationExpression<M>,
1783
+ expression: E,
1515
1784
  options?: FetchGraphOptions,
1516
- ): SingleQueryBuilder<QueryBuilderType<M>>;
1785
+ ): SingleQueryBuilder<WithGraphModelQueryBuilder<M, E>>;
1517
1786
 
1518
- fetchGraph(
1787
+ fetchGraph<const E extends RelationExpression<M>>(
1519
1788
  modelOrObject: PartialModelObject<M>[],
1520
- expression: RelationExpression<M>,
1789
+ expression: E,
1521
1790
  options?: FetchGraphOptions,
1522
- ): QueryBuilderType<M>;
1791
+ ): WithGraphModelQueryBuilder<M, E>;
1523
1792
 
1524
1793
  getRelations(): Relations;
1525
1794
  getRelation(name: string): Relation;
@@ -1551,16 +1820,18 @@ declare namespace Objection {
1551
1820
  static QueryBuilder: typeof QueryBuilder;
1552
1821
 
1553
1822
  static tableName: string;
1554
- static idColumn: string | string[];
1823
+ static idColumn: null | string | string[] | readonly string[];
1555
1824
  static jsonSchema: JSONSchema;
1556
1825
  static relationMappings: RelationMappings | RelationMappingsThunk;
1557
1826
  static modelPaths: string[];
1558
- static jsonAttributes: string[];
1827
+ static jsonAttributes: string[] | readonly string[];
1559
1828
  static virtualAttributes: string[];
1560
1829
  static uidProp: string;
1561
1830
  static uidRefProp: string;
1562
1831
  static dbRefProp: string;
1563
1832
  static propRefRegex: RegExp;
1833
+ static graphUnrelateProp: string;
1834
+ static graphDeleteProp: string;
1564
1835
  static pickJsonSchemaProperties: boolean;
1565
1836
  static relatedFindQueryMutates: boolean;
1566
1837
  static relatedInsertQueryMutates: boolean;
@@ -1581,7 +1852,7 @@ declare namespace Objection {
1581
1852
  static defaultGraphOptions?: GraphOptions;
1582
1853
 
1583
1854
  static query<M extends Model>(
1584
- this: Constructor<M>,
1855
+ this: ConstructorType<M>,
1585
1856
  trxOrKnex?: TransactionOrKnex,
1586
1857
  ): QueryBuilderType<M>;
1587
1858
 
@@ -1596,8 +1867,8 @@ declare namespace Objection {
1596
1867
  trxOrKnex?: TransactionOrKnex,
1597
1868
  ): QueryBuilderType<RM>;
1598
1869
 
1599
- static fromJson<M extends Model>(this: Constructor<M>, json: object, opt?: ModelOptions): M;
1600
- static fromDatabaseJson<M extends Model>(this: Constructor<M>, json: object): M;
1870
+ static fromJson<M extends Model>(this: ConstructorType<M>, json: object, opt?: ModelOptions): M;
1871
+ static fromDatabaseJson<M extends Model>(this: ConstructorType<M>, json: object): M;
1601
1872
 
1602
1873
  static columnNameToPropertyName(columnName: string): string;
1603
1874
  static propertyNameToColumnName(propertyName: string): string;
@@ -1622,19 +1893,19 @@ declare namespace Objection {
1622
1893
  static bindKnex<M>(this: M, trxOrKnex: TransactionOrKnex): M;
1623
1894
  static bindTransaction<M>(this: M, trxOrKnex: TransactionOrKnex): M;
1624
1895
 
1625
- static fetchGraph<M extends Model>(
1626
- this: Constructor<M>,
1896
+ static fetchGraph<M extends Model, const E extends RelationExpression<M>>(
1897
+ this: ConstructorType<M>,
1627
1898
  modelOrObject: PartialModelObject<M>,
1628
- expression: RelationExpression<M>,
1899
+ expression: E,
1629
1900
  options?: FetchGraphOptions,
1630
- ): SingleQueryBuilder<QueryBuilderType<M>>;
1901
+ ): SingleQueryBuilder<WithGraphModelQueryBuilder<M, E>>;
1631
1902
 
1632
- static fetchGraph<M extends Model>(
1633
- this: Constructor<M>,
1903
+ static fetchGraph<M extends Model, const E extends RelationExpression<M>>(
1904
+ this: ConstructorType<M>,
1634
1905
  modelOrObject: PartialModelObject<M>[],
1635
- expression: RelationExpression<M>,
1906
+ expression: E,
1636
1907
  options?: FetchGraphOptions,
1637
- ): QueryBuilderType<M>;
1908
+ ): WithGraphModelQueryBuilder<M, E>;
1638
1909
 
1639
1910
  static getRelations(): Relations;
1640
1911
  static getRelation(name: string): Relation;
@@ -1678,15 +1949,15 @@ declare namespace Objection {
1678
1949
  $id(id: any): void;
1679
1950
  $id(): any;
1680
1951
 
1681
- $fetchGraph(
1682
- expression: RelationExpression<this>,
1952
+ $fetchGraph<const E extends RelationExpression<this>>(
1953
+ expression: E,
1683
1954
  options?: FetchGraphOptions,
1684
- ): SingleQueryBuilder<QueryBuilderType<this>>;
1955
+ ): SingleQueryBuilder<WithGraphModelQueryBuilder<this, E>>;
1685
1956
 
1686
1957
  $formatDatabaseJson(json: Pojo): Pojo;
1687
1958
  $parseDatabaseJson(json: Pojo): Pojo;
1688
1959
 
1689
- $formatJson(json: Pojo): Pojo;
1960
+ $formatJson(json: Pojo, opt?: Pojo): Pojo;
1690
1961
  $parseJson(json: Pojo, opt?: ModelOptions): Pojo;
1691
1962
 
1692
1963
  $beforeValidate(jsonSchema: JSONSchema, json: Pojo, opt: ModelOptions): JSONSchema;
@@ -1739,7 +2010,7 @@ declare namespace Objection {
1739
2010
  * - https://github.com/microsoft/TypeScript/issues/1360
1740
2011
  * - https://github.com/Microsoft/TypeScript/issues/5453
1741
2012
  *
1742
- * @tutorial https://vincit.github.io/objection.js/guide/transactions.html#creating-a-transaction
2013
+ * @tutorial https://ditojs.github.io/objection/guide/transactions.html#creating-a-transaction
1743
2014
  */
1744
2015
  export interface transaction {
1745
2016
  start(knexOrModel: Knex | AnyModelConstructor): Promise<Transaction>;
@@ -1850,14 +2121,7 @@ declare namespace Objection {
1850
2121
  * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
1851
2122
  */
1852
2123
  export type JSONSchemaTypeName =
1853
- | 'string'
1854
- | 'number'
1855
- | 'integer'
1856
- | 'boolean'
1857
- | 'object'
1858
- | 'array'
1859
- | 'null'
1860
- | string;
2124
+ 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null' | string;
1861
2125
 
1862
2126
  export type JSONSchemaType = JSONSchemaArray[] | boolean | number | null | object | string;
1863
2127
 
@@ -1889,6 +2153,13 @@ declare namespace Objection {
1889
2153
  $schema?: JSONSchemaVersion;
1890
2154
  $comment?: string;
1891
2155
 
2156
+ /**
2157
+ * @see https://json-schema.org/draft/2019-09/release-notes
2158
+ */
2159
+ $defs?: {
2160
+ [key: string]: JSONSchemaDefinition;
2161
+ };
2162
+
1892
2163
  /**
1893
2164
  * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
1894
2165
  */