objection 4.0.0-rc.4 → 4.0.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.
@@ -80,6 +80,10 @@ export class UniqueViolationError extends ConstraintViolationError {
80
80
  schema?: string;
81
81
  }
82
82
 
83
+ // Wraps an error of the database driver in the matching database error class,
84
+ // or returns it as it is.
85
+ export function wrapError<E>(err: E): DBError | E;
86
+
83
87
  export interface RawBuilder extends Aliasable {}
84
88
 
85
89
  export interface RawFunction extends RawInterface<RawBuilder> {}
@@ -214,6 +218,7 @@ export type RelationExpression<M extends Model> = string | object;
214
218
 
215
219
  // Type-level parsing of relation expressions, used to narrow the result
216
220
  // types of `withGraphFetched()`, `withGraphJoined()` and `fetchGraph()`.
221
+ // The types that declarations of inferred query types refer to are exported.
217
222
 
218
223
  /**
219
224
  * Parses a relation expression, in string or object notation, into a tree
@@ -234,9 +239,9 @@ export type ParseRelationExpression<E> = [E] extends [never]
234
239
  ? ParseRelationString<E>
235
240
  : ParseRelationObject<E>;
236
241
 
237
- export type Whitespace = ' ' | '\n' | '\r' | '\t';
242
+ type Whitespace = ' ' | '\n' | '\r' | '\t';
238
243
 
239
- export type Trim<S extends string> = S extends `${Whitespace}${infer R}`
244
+ type Trim<S extends string> = S extends `${Whitespace}${infer R}`
240
245
  ? Trim<R>
241
246
  : S extends `${infer L}${Whitespace}`
242
247
  ? Trim<L>
@@ -245,7 +250,7 @@ export type Trim<S extends string> = S extends `${Whitespace}${infer R}`
245
250
  /**
246
251
  * Counts the occurrences of character C in S, as a tuple length.
247
252
  */
248
- export type CountChar<
253
+ type CountChar<
249
254
  S extends string,
250
255
  C extends string,
251
256
  N extends 0[] = [],
@@ -254,7 +259,7 @@ export type CountChar<
254
259
  /**
255
260
  * True if S has as many opening as closing brackets and parentheses.
256
261
  */
257
- export type IsBalanced<S extends string> =
262
+ type IsBalanced<S extends string> =
258
263
  CountChar<S, '['> extends CountChar<S, ']'>
259
264
  ? CountChar<S, '('> extends CountChar<S, ')'>
260
265
  ? true
@@ -265,7 +270,7 @@ export type IsBalanced<S extends string> =
265
270
  * Splits a list of expressions at the commas that aren't nested inside
266
271
  * brackets or parentheses: 'a, b.[c, d]' -> ['a', 'b.[c, d]'].
267
272
  */
268
- export type SplitList<
273
+ type SplitList<
269
274
  S extends string,
270
275
  Acc extends string = '',
271
276
  Out extends string[] = [],
@@ -275,7 +280,7 @@ export type SplitList<
275
280
  : SplitList<Rest, `${Acc}${Head},`, Out>
276
281
  : [...Out, `${Acc}${S}`];
277
282
 
278
- export type ParseRelationList<L extends string[], Acc = {}> = L extends [
283
+ type ParseRelationList<L extends string[], Acc = {}> = L extends [
279
284
  infer H extends string,
280
285
  ...infer T extends string[],
281
286
  ]
@@ -285,7 +290,7 @@ export type ParseRelationList<L extends string[], Acc = {}> = L extends [
285
290
  /**
286
291
  * Parses '[a, b]', 'a.b' and 'a' style expressions.
287
292
  */
288
- export type ParseRelationString<S extends string> =
293
+ type ParseRelationString<S extends string> =
289
294
  Trim<S> extends `[${infer Inner}]`
290
295
  ? ParseRelationList<SplitList<Inner>>
291
296
  : Trim<S> extends `${infer Head}.${infer Rest}`
@@ -299,7 +304,7 @@ export type ParseRelationString<S extends string> =
299
304
  * 'pets(selectName)' -> 'pets'. Returns never for nodes that can't be
300
305
  * narrowed: aliases ('pets as p'), '*' and recursion ('^', '^2').
301
306
  */
302
- export type RelationName<S extends string> =
307
+ type RelationName<S extends string> =
303
308
  Trim<S> extends `${infer Name}(${string})`
304
309
  ? RelationName<Name>
305
310
  : Trim<S> extends infer Name extends string
@@ -310,7 +315,7 @@ export type RelationName<S extends string> =
310
315
  : Name
311
316
  : never;
312
317
 
313
- export type RelationNode<Name extends string, Children> = [Name] extends [never]
318
+ type RelationNode<Name extends string, Children> = [Name] extends [never]
314
319
  ? {}
315
320
  : { [K in Name]: Children };
316
321
 
@@ -319,26 +324,27 @@ export type RelationNode<Name extends string, Children> = [Name] extends [never]
319
324
  * `$` are options, and aliased nodes (`{ p: { $relation: 'pets' } }`) are
320
325
  * skipped.
321
326
  */
322
- export type ParseRelationObject<E> = string extends keyof E
323
- ? {}
324
- : {
325
- [
326
- K in keyof E as K extends `$${string}`
327
- ? never
328
- : false extends E[K]
327
+ type ParseRelationObject<E> =
328
+ HasOnlyLiteralKeys<E> extends true
329
+ ? {
330
+ [
331
+ K in keyof E as K extends `$${string}`
329
332
  ? never
330
- : E[K] extends { $relation: any }
333
+ : false extends E[K]
331
334
  ? never
332
- : K
333
- ]: E[K] extends object ? ParseRelationObject<E[K]> : {};
334
- };
335
+ : E[K] extends { $relation: any }
336
+ ? never
337
+ : K
338
+ ]: E[K] extends object ? ParseRelationObject<E[K]> : {};
339
+ }
340
+ : {};
335
341
 
336
342
  /**
337
343
  * Marks the relations in tree T as fetched on model M: they become required
338
344
  * and are narrowed recursively. Only `undefined` is removed: declared `null`
339
345
  * is kept, as to-one relations can be null when there is no related row.
340
346
  */
341
- export type WithGraph<M, T> = [keyof T] extends [never]
347
+ type WithGraph<M, T> = [keyof T] extends [never]
342
348
  ? M
343
349
  : WithGraphRelations<M, T, FetchedRelations<M, T>>;
344
350
 
@@ -362,7 +368,7 @@ export type FetchedRelations<M, T> = {
362
368
  [K in keyof M & keyof T]: NonNullable<M[K]> extends Model | Model[] ? K : never;
363
369
  }[keyof M & keyof T];
364
370
 
365
- export type WithGraphProperty<P, T> = [keyof T] extends [never]
371
+ type WithGraphProperty<P, T> = [keyof T] extends [never]
366
372
  ? P
367
373
  : P extends Array<infer I>
368
374
  ? WithGraph<I, T>[]
@@ -466,11 +472,6 @@ type ItemOf<R, M> = R extends readonly (infer I)[]
466
472
  ? R
467
473
  : M;
468
474
 
469
- /**
470
- * If T is an array, returns the item type, otherwise returns T.
471
- */
472
- export type ItemType<T> = T extends Array<unknown> ? T[number] : T;
473
-
474
475
  /**
475
476
  * Type for keys of non-function properties of T.
476
477
  */
@@ -550,7 +551,7 @@ export type PartialModelObject<T extends Model> = {
550
551
  * builders for models with narrowed relations (see WithGraphModel) remain
551
552
  * assignable to the query builders for the plain models.
552
553
  */
553
- export type PartialModelProps<T extends Model> = {
554
+ type PartialModelProps<T extends Model> = {
554
555
  [K in DataPropertyNames<T>]?: K extends RelationPropertyNames<T> ? never : Expression<T[K]>;
555
556
  };
556
557
 
@@ -622,7 +623,7 @@ export interface GeneratedBrand {
622
623
  /**
623
624
  * Insert keys are optional for optional, nullable and generated properties.
624
625
  */
625
- export type OptionalInsertPropertyNames<M> = {
626
+ type OptionalInsertPropertyNames<M> = {
626
627
  [K in keyof M]-?: {} extends Pick<M, K>
627
628
  ? K
628
629
  : null extends M[K]
@@ -632,7 +633,7 @@ export type OptionalInsertPropertyNames<M> = {
632
633
  : never;
633
634
  }[keyof M];
634
635
 
635
- export type InsertablePropertyNames<M> = Exclude<DataPropertyNames<M>, RelationPropertyNames<M>>;
636
+ type InsertablePropertyNames<M> = Exclude<DataPropertyNames<M>, RelationPropertyNames<M>>;
636
637
 
637
638
  /**
638
639
  * The complete data of a new row of model M, to check what is passed to
@@ -656,9 +657,9 @@ export type InsertableGraph<M extends Model> = Insertable<M> &
656
657
  [K in RelationPropertyNames<M>]?: InsertableGraphField<M[K]>;
657
658
  };
658
659
 
659
- export type InsertableGraphReference = { '#dbRef': MaybeCompositeId } | { '#ref': string };
660
+ type InsertableGraphReference = { '#dbRef': MaybeCompositeId } | { '#ref': string };
660
661
 
661
- export type InsertableGraphField<F> =
662
+ type InsertableGraphField<F> =
662
663
  NonNullable<F> extends (infer I extends Model)[]
663
664
  ? (InsertableGraph<I> | InsertableGraphReference)[]
664
665
  : NonNullable<F> extends infer I extends Model
@@ -1119,7 +1120,7 @@ type ExtractedProperties<M, A extends string> = {
1119
1120
  type SelectQueryBuilder<QB extends AnyQueryBuilder, C extends readonly unknown[]> = NarrowRows<
1120
1121
  QB,
1121
1122
  SelectedRow<ModelType<QB>, C>,
1122
- true extends SelectsAll<C[number]> ? true : false
1123
+ true extends IsSelectAll<C[number]> ? true : false
1123
1124
  >;
1124
1125
 
1125
1126
  /**
@@ -1200,7 +1201,7 @@ type SelectBase<Item, M, All extends boolean> = All extends true
1200
1201
  // unknown types, under any aliases.
1201
1202
  type UnknownSelection = { [key: string]: unknown } & SelectedBrand<{ [key: string]: true }>;
1202
1203
 
1203
- type SelectsAll<X> = X extends '*' | `${string}.*` ? true : false;
1204
+ type IsSelectAll<X> = X extends '*' | `${string}.*` ? true : false;
1204
1205
 
1205
1206
  /**
1206
1207
  * Replaces the item of the result R with the row.
@@ -1312,9 +1313,9 @@ type CheckRelationExpression<M, E, QB extends AnyQueryBuilder = never> = IfStric
1312
1313
 
1313
1314
  type CheckModifierExpression<E, QB extends AnyQueryBuilder> =
1314
1315
  HasQueryClass<QB> extends true
1315
- ? [CheckedModifiers<E, QB>] extends [never]
1316
+ ? [UnknownExpressionModifiers<E, QB>] extends [never]
1316
1317
  ? unknown
1317
- : { '~unknownModifier': CheckedModifiers<E, QB> }
1318
+ : { '~unknownModifier': UnknownExpressionModifiers<E, QB> }
1318
1319
  : unknown;
1319
1320
 
1320
1321
  // Queries in generic code, where the model class can't be resolved, aren't
@@ -1327,7 +1328,7 @@ type HasQueryClass<QB extends AnyQueryBuilder> =
1327
1328
  : true;
1328
1329
 
1329
1330
  // Only expressions with modifiers are checked for them.
1330
- type CheckedModifiers<E, QB> = E extends `${string}(${string}` | object
1331
+ type UnknownExpressionModifiers<E, QB> = E extends `${string}(${string}` | object
1331
1332
  ? UnknownModifiers<
1332
1333
  ClassMappings<QueryClass<QB>>,
1333
1334
  ParseModifierExpression<E>,
@@ -1404,17 +1405,17 @@ type PropertyId<M, C extends string, F> =
1404
1405
 
1405
1406
  // Modifiers with their general type, e.g. inherited from `Model`, aren't
1406
1407
  // checked.
1407
- type ChecksModifiers<QB extends AnyQueryBuilder> = IfStrict<
1408
+ type ShouldCheckModifiers<QB extends AnyQueryBuilder> = IfStrict<
1408
1409
  false,
1409
- HasQueryClass<QB> extends true ? ChecksClassModifiers<ClassModifiers<QueryClass<QB>>> : false
1410
+ HasQueryClass<QB> extends true ? HasOnlyLiteralKeys<ClassModifiers<QueryClass<QB>>> : false
1410
1411
  >;
1411
1412
 
1412
- type ChecksClassModifiers<O> = string extends keyof O ? false : true;
1413
+ type HasOnlyLiteralKeys<O> = string extends keyof O ? false : true;
1413
1414
 
1414
1415
  // The names that `modify()` takes on query builder QB, also in lists. Names
1415
1416
  // in variables typed `string` are rejected where they are checked.
1416
1417
  type ModifierName<QB extends AnyQueryBuilder> =
1417
- ChecksModifiers<QB> extends true ? keyof KnownModifiers<QB> & string : string;
1418
+ ShouldCheckModifiers<QB> extends true ? keyof KnownModifiers<QB> & string : string;
1418
1419
 
1419
1420
  type ModifyArg<QB extends AnyQueryBuilder> =
1420
1421
  | ModifierName<QB>
@@ -1430,7 +1431,7 @@ type UnknownModifiers<Mappings, T, P> = T extends unknown
1430
1431
  ? MappedClass<Mappings, K> extends infer C
1431
1432
  ? [C] extends [never]
1432
1433
  ? never
1433
- : | (ChecksClassModifiers<ClassModifiers<C>> extends true
1434
+ : | (HasOnlyLiteralKeys<ClassModifiers<C>> extends true
1434
1435
  ? Exclude<N, keyof ClassModifiers<C> | P>
1435
1436
  : never)
1436
1437
  | UnknownModifiers<ClassMappings<C>, Children, P>
@@ -1440,15 +1441,16 @@ type UnknownModifiers<Mappings, T, P> = T extends unknown
1440
1441
  : never;
1441
1442
 
1442
1443
  // Mappings with their general type, e.g. inherited from `Model`, give no class.
1443
- type MappedClass<Mappings, K> = string extends keyof Mappings
1444
- ? never
1445
- : K extends keyof Mappings
1446
- ? Mappings[K] extends { modelClass: infer C }
1447
- ? C extends abstract new (...args: any) => any
1448
- ? C
1449
- : C extends () => infer RC
1450
- ? RC
1451
- : never
1444
+ type MappedClass<Mappings, K> =
1445
+ HasOnlyLiteralKeys<Mappings> extends true
1446
+ ? K extends keyof Mappings
1447
+ ? Mappings[K] extends { modelClass: infer C }
1448
+ ? C extends abstract new (...args: any) => any
1449
+ ? C
1450
+ : C extends () => infer RC
1451
+ ? RC
1452
+ : never
1453
+ : never
1452
1454
  : never
1453
1455
  : never;
1454
1456
 
@@ -1490,28 +1492,29 @@ type ModifierNode<S extends string, Children> = [RelationName<S>] extends [never
1490
1492
  type ModifierNames<S extends string> =
1491
1493
  Trim<S> extends `${string}(${infer Args})` ? Exclude<Trim<SplitList<Args>[number]>, ''> : never;
1492
1494
 
1493
- type ParseModifierObject<E> = string extends keyof E
1494
- ? {}
1495
- : {
1496
- [
1497
- K in keyof E as K extends `$${string}`
1498
- ? never
1499
- : false extends E[K]
1495
+ type ParseModifierObject<E> =
1496
+ HasOnlyLiteralKeys<E> extends true
1497
+ ? {
1498
+ [
1499
+ K in keyof E as K extends `$${string}`
1500
1500
  ? never
1501
- : E[K] extends { $relation: any }
1501
+ : false extends E[K]
1502
1502
  ? never
1503
- : K
1504
- ]: {
1505
- names: E[K] extends { $modify: infer N }
1506
- ? N extends readonly (infer I extends string)[]
1507
- ? I
1508
- : N extends string
1509
- ? N
1510
- : never
1511
- : never;
1512
- children: E[K] extends object ? ParseModifierObject<E[K]> : {};
1513
- };
1514
- };
1503
+ : E[K] extends { $relation: any }
1504
+ ? never
1505
+ : K
1506
+ ]: {
1507
+ names: E[K] extends { $modify: infer N }
1508
+ ? N extends readonly (infer I extends string)[]
1509
+ ? I
1510
+ : N extends string
1511
+ ? N
1512
+ : never
1513
+ : never;
1514
+ children: E[K] extends object ? ParseModifierObject<E[K]> : {};
1515
+ };
1516
+ }
1517
+ : {};
1515
1518
 
1516
1519
  export interface ForClassMethod {
1517
1520
  <M extends Model>(modelClass: ModelConstructor<M>): QueryBuilderType<M>;
@@ -1537,15 +1540,14 @@ export interface SelectMethod<out QB extends AnyQueryBuilder> {
1537
1540
  <QBP extends QB>(...columns: IfStrict<Selection<QBP>, never>[]): QB;
1538
1541
  <QBP extends QB>(columns: IfStrict<Selection<QBP>, never>[]): QB;
1539
1542
 
1540
- // Allow anything else in loose mode, like `select(1)`:
1541
- <QBP extends QB>(...args: IfStrict<{} | null | undefined, never>[]): QB;
1542
-
1543
- <QBP extends QB, const C extends readonly unknown[] = []>(
1544
- ...columns: StrictSelections<ModelType<QBP>, C>
1545
- ): SelectQueryBuilder<QBP, C>;
1546
1543
  <QBP extends QB, const C extends readonly unknown[] = []>(
1547
1544
  columns: StrictSelections<ModelType<QBP>, C>,
1548
1545
  ): SelectQueryBuilder<QBP, C>;
1546
+ // Allow anything else in loose mode, like `select(1)`. The last overload
1547
+ // is what `Parameters<>` sees, so it takes the loose arguments:
1548
+ <QBP extends QB, const C extends readonly unknown[] = []>(
1549
+ ...columns: IfStrict<({} | null | undefined)[], StrictSelections<ModelType<QBP>, C>>
1550
+ ): IfStrict<QB, SelectQueryBuilder<QBP, C>>;
1549
1551
  }
1550
1552
 
1551
1553
  export interface AsMethod<out QB extends AnyQueryBuilder> {
@@ -1655,9 +1657,10 @@ export interface WhereMethod<out QB extends AnyQueryBuilder> {
1655
1657
 
1656
1658
  <QBP extends QB>(obj: IfStrict<PartialModelProps<ModelType<QBP>>, never>): QB;
1657
1659
  // Allow any keys in loose mode. The model's properties above only serve
1658
- // autocompletion.
1659
- (obj: IfStrict<object, never>): QB;
1660
- <QBP extends QB, const O extends object = {}>(obj: O & StrictWhereObject<ModelType<QBP>, O>): QB;
1660
+ // autocompletion. The last overload is what `Parameters<>` sees:
1661
+ <QBP extends QB, const O extends object = {}>(
1662
+ obj: IfStrict<object, O & StrictWhereObject<ModelType<QBP>, O>>,
1663
+ ): QB;
1661
1664
  }
1662
1665
 
1663
1666
  export interface WhereLikeMethod<out QB extends AnyQueryBuilder> {
@@ -1702,8 +1705,10 @@ export interface WhereInMethod<out QB extends AnyQueryBuilder> {
1702
1705
  col: StrictColumn<ModelType<QBP>, C>,
1703
1706
  expr: WhereInValues<QB, QBP, C>,
1704
1707
  ): QB;
1708
+ // The last overload is what `Parameters<>` sees, so it takes the loose
1709
+ // columns in loose mode:
1705
1710
  <QBP extends QB, const C extends readonly unknown[] = []>(
1706
- col: StrictColumns<ModelType<QBP>, C>,
1711
+ col: IfStrict<RefArg<QB> | RefArg<QB>[], StrictColumns<ModelType<QBP>, C>>,
1707
1712
  expr: readonly Expression<PrimitiveValue>[] | Raw | AnyQueryBuilder | CallbackVoid<QB>,
1708
1713
  ): QB;
1709
1714
  }
@@ -2807,11 +2812,11 @@ export interface BelongsToManyRelationType extends RelationType {
2807
2812
  * Names of the properties of M that hold related models. Properties typed
2808
2813
  * `any` or `any[]` are columns.
2809
2814
  */
2810
- export type RelationPropertyNames<M> = {
2811
- [K in keyof M]-?: HoldsModels<NonNullable<M[K]>> extends true ? K : never;
2815
+ type RelationPropertyNames<M> = {
2816
+ [K in keyof M]-?: HasModels<NonNullable<M[K]>> extends true ? K : never;
2812
2817
  }[keyof M];
2813
2818
 
2814
- type HoldsModels<P> = [P] extends [readonly (infer I)[]] ? IsModelLike<I> : IsModelLike<P>;
2819
+ type HasModels<P> = [P] extends [readonly (infer I)[]] ? IsModelLike<I> : IsModelLike<P>;
2815
2820
 
2816
2821
  type IsModelLike<P> = 0 extends 1 & P ? false : [P] extends [ModelLike] ? true : false;
2817
2822
 
@@ -2820,7 +2825,7 @@ type IsModelLike<P> = 0 extends 1 & P ? false : [P] extends [ModelLike] ? true :
2820
2825
  // Spreads of instances and their JSON don't have it.
2821
2826
  type ModelLike = { $clone(opt?: CloneOptions): unknown };
2822
2827
 
2823
- export type TypedModelClassSpecifier<M extends Model> =
2828
+ type TypedModelClassSpecifier<M extends Model> =
2824
2829
  string | ModelConstructor<M> | (() => ModelConstructor<M>);
2825
2830
 
2826
2831
  /**
@@ -2834,7 +2839,7 @@ export interface TypedRelationMapping<
2834
2839
  modelClass: TypedModelClassSpecifier<M>;
2835
2840
  }
2836
2841
 
2837
- export type TypedRelationMappingFor<T> =
2842
+ type TypedRelationMappingFor<T> =
2838
2843
  NonNullable<T> extends (infer I extends Model)[]
2839
2844
  ? TypedRelationMapping<
2840
2845
  I,
@@ -2951,7 +2956,7 @@ export interface SnakeCaseMappersOptions extends SnakeCaseOptions {
2951
2956
 
2952
2957
  export interface KnexSnakeCaseMappersOptions extends SnakeCaseOptions {}
2953
2958
 
2954
- export type JsonColumnFormat = 'auto' | 'text' | 'parsed';
2959
+ export type JsonColumnFormat = 'auto' | 'json' | 'text' | 'mixed';
2955
2960
 
2956
2961
  // Passed by queries to `fromDatabaseJson()`, `$setDatabaseJson()` and
2957
2962
  // `$parseDatabaseJson()`. Overrides pass them on to `super`.
@@ -3631,13 +3636,14 @@ type ModelSchemaProperties<S> = ReadonlyJSONSchema extends S
3631
3636
 
3632
3637
  // Relations are picked by their mappings, as resolving the related models
3633
3638
  // while the model's own type is built would make it its own base type.
3634
- type ModelRelationProperties<T, R = T extends () => infer M ? M : T> = string extends keyof R
3635
- ? {}
3636
- : {
3637
- -readonly [K in keyof R as IsMappedRelation<R[K]> extends true ? K : never]?: MappedRelation<
3638
- R[K]
3639
- >;
3640
- };
3639
+ type ModelRelationProperties<T, R = T extends () => infer M ? M : T> =
3640
+ HasOnlyLiteralKeys<R> extends true
3641
+ ? {
3642
+ -readonly [
3643
+ K in keyof R as IsMappedRelation<R[K]> extends true ? K : never
3644
+ ]?: MappedRelation<R[K]>;
3645
+ }
3646
+ : {};
3641
3647
 
3642
3648
  // Relation types without a brand and module paths can't be resolved.
3643
3649
  type IsMappedRelation<M> = M extends { relation: infer T; modelClass: infer C }