Skip to content

Commit

Permalink
feat(repository): remove generic parameters from Inclusion type
Browse files Browse the repository at this point in the history
Ideally, `Inclusion` interface should use type information
from the source model to restrict the allowed values in the filter
for a target model. That is unfortunately rather difficult to achieve,
because an Entity does not provide type information about related models.
We could use {ModelName}WithRelations interface for first-level
inclusion, but that won't handle second-level (and deeper) inclusions.
To keep things simple, `Inclusion` allows any filters and thus does not
require any generic parameter.

BREAKING CHANGE: The type `Inclusion` is no longer generic. Please
update your code and remove any generic arguments provided for the type.

```diff
- Inclusion<MyModel>
+ Inclusion
```

Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
  • Loading branch information
bajtos committed Feb 21, 2020
1 parent e162c8c commit ed949e4
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 12 deletions.
8 changes: 3 additions & 5 deletions packages/repository/src/query.ts
Expand Up @@ -169,9 +169,7 @@ export type Fields<MT = AnyObject> = {[P in keyof MT]?: boolean};
* Example:
* `{relation: 'aRelationName', scope: {<AFilterObject>}}`
*/
export interface Inclusion<MT extends object = AnyObject> {
// ^^^ TODO(semver-major): remove the generic argument <MT>

export interface Inclusion {
relation: string;

// Technically, we should restrict the filter to target model.
Expand Down Expand Up @@ -220,7 +218,7 @@ export interface Filter<MT extends object = AnyObject> {
/**
* To include related objects
*/
include?: Inclusion<MT>[];
include?: Inclusion[];
}

/**
Expand Down Expand Up @@ -561,7 +559,7 @@ export class FilterBuilder<MT extends object = AnyObject> {
* @param i - A relation name, an array of relation names, or an `Inclusion`
* object for the relation/scope definitions
*/
include(...i: (string | string[] | Inclusion<MT>)[]): this {
include(...i: (string | string[] | Inclusion)[]): this {
if (this.filter.include == null) {
this.filter.include = [];
}
Expand Down
Expand Up @@ -42,7 +42,7 @@ export function createHasManyInclusionResolver<

return async function fetchHasManyModels(
entities: Entity[],
inclusion: Inclusion<AnyObject>,
inclusion: Inclusion,
options?: Options,
): Promise<((Target & TargetRelations)[] | undefined)[]> {
if (!entities.length) return [];
Expand Down
Expand Up @@ -39,7 +39,7 @@ export function createHasOneInclusionResolver<

return async function fetchHasOneModel(
entities: Entity[],
inclusion: Inclusion<AnyObject>,
inclusion: Inclusion,
options?: Options,
): Promise<((Target & TargetRelations) | undefined)[]> {
if (!entities.length) return [];
Expand Down
4 changes: 2 additions & 2 deletions packages/repository/src/relations/relation.helpers.ts
Expand Up @@ -11,10 +11,10 @@ import {
Entity,
EntityCrudRepository,
Filter,
FilterBuilder,
Inclusion,
Options,
Where,
FilterBuilder,
} from '..';
const debug = debugFactory('loopback:repository:relation-helpers');

Expand Down Expand Up @@ -84,7 +84,7 @@ export async function includeRelatedModels<
>(
targetRepository: EntityCrudRepository<T, unknown, Relations>,
entities: T[],
include?: Inclusion<T>[],
include?: Inclusion[],
options?: Options,
): Promise<(T & Relations)[]> {
const result = entities as (T & Relations)[];
Expand Down
4 changes: 2 additions & 2 deletions packages/repository/src/relations/relation.types.ts
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {AnyObject, Options} from '../common-types';
import {Options} from '../common-types';
import {Entity} from '../model';
import {Inclusion} from '../query';
import {TypeResolver} from '../type-resolver';
Expand Down Expand Up @@ -180,7 +180,7 @@ export type InclusionResolver<S extends Entity, T extends Entity> = (
/**
* Inclusion requested by the user (e.g. scope constraints to apply).
*/
inclusion: Inclusion<AnyObject>,
inclusion: Inclusion,
/**
* Generic options object, e.g. carrying the Transaction object.
*/
Expand Down
Expand Up @@ -530,7 +530,7 @@ export class DefaultCrudRepository<
*/
protected async includeRelatedModels(
entities: T[],
include?: Inclusion<T>[],
include?: Inclusion[],
options?: Options,
): Promise<(T & Relations)[]> {
return includeRelatedModels<T, Relations>(this, entities, include, options);
Expand Down

0 comments on commit ed949e4

Please sign in to comment.