@@ -106,7 +106,7 @@ declare namespace Objection {
106
106
json : Pojo ;
107
107
options : ModelOptions ;
108
108
}
109
-
109
+
110
110
export class Validator {
111
111
beforeValidate ( args : ValidatorArgs ) : void ;
112
112
validate ( args : ValidatorArgs ) : Pojo ;
@@ -128,7 +128,7 @@ declare namespace Objection {
128
128
data : any ;
129
129
message : string ;
130
130
}
131
-
131
+
132
132
export interface ErrorHash {
133
133
[ columnName : string ] : ValidationError [ ] ;
134
134
}
@@ -446,17 +446,28 @@ declare namespace Objection {
446
446
$query ( trxOrKnex ?: Transaction | knex ) : QueryBuilderSingle < this> ;
447
447
448
448
/**
449
- * Users need to explicitly type these calls, as the relationName doesn't
450
- * indicate the type (and if it returned Model directly, Partial<Model>
451
- * guards are worthless)
449
+ * If you add model relations as fields, $relatedQuery works
450
+ * automatically:
451
+ */
452
+ $relatedQuery < K extends keyof this> (
453
+ relationName : K ,
454
+ trxOrKnex ?: Transaction | knex
455
+ ) : QueryBuilderSingle < this[ K ] > ;
456
+
457
+ // PLEASE NOTE: `$relatedQuery<K extends keyof this>` MUST BE DEFINED
458
+ // BEFORE `$relatedQuery<M extends Model>`!
459
+
460
+ /**
461
+ * If you don't want to add the fields to your model, you can cast the
462
+ * call to the expected Model subclass (`$relatedQuery<Animal>('pets')`).
452
463
*/
453
464
$relatedQuery < M extends Model > (
454
465
relationName : string ,
455
466
trxOrKnex ?: Transaction | knex
456
467
) : QueryBuilder < M > ;
457
468
458
469
$loadRelated < T > (
459
- expression : RelationExpression ,
470
+ expression : keyof this | RelationExpression ,
460
471
filters ?: Filters < T > ,
461
472
trxOrKnex ?: Transaction | knex
462
473
) : QueryBuilderSingle < this> ;
0 commit comments