Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sequelize): extend Inclusion interface with "required" property #10192

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions extensions/sequelize/src/sequelize/sequelize.repository.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
EntityNotFoundError,
Fields,
Filter,
FilterExcludingWhere,
Getter,
HasManyDefinition,
HasManyRepositoryFactory,
Expand Down Expand Up @@ -68,6 +67,27 @@ import {isTruelyObject} from './utils';
const debug = debugFactory('loopback:sequelize:repository');
const debugModelBuilder = debugFactory('loopback:sequelize:modelbuilder');

interface InclusionWithRequired extends Inclusion {
/**
* Setting this option to true will result in an inner join query that
* explicitly requires the specified condition for the child model.
*
* @see https://loopback.io/pages/en/lb4/readmes/loopback-next/extensions/sequelize/#inner-join
*/
required?: boolean;
}

type InclusionFilterWithRequired = string | InclusionWithRequired;

interface FilterWithRequired<T extends object> extends Filter<T> {
include?: InclusionFilterWithRequired[];
}

type FilterWithRequiredExcludingWhere<T extends object> = Omit<
FilterWithRequired<T>,
'where'
>;

/**
* Sequelize implementation of CRUD repository to be used with default loopback entities
* and SequelizeDataSource for SQL Databases
Expand Down Expand Up @@ -231,7 +251,7 @@ export class SequelizeCrudRepository<
}

async find(
filter?: Filter<T>,
filter?: FilterWithRequired<T>,
options?: AnyObject,
): Promise<(T & Relations)[]> {
const data = await this.sequelizeModel.findAll({
Expand All @@ -252,7 +272,7 @@ export class SequelizeCrudRepository<
}

async findOne(
filter?: Filter<T>,
filter?: FilterWithRequired<T>,
options?: AnyObject,
): Promise<(T & Relations) | null> {
const data = await this.sequelizeModel.findOne({
Expand All @@ -279,7 +299,7 @@ export class SequelizeCrudRepository<

async findById(
id: ID,
filter?: FilterExcludingWhere<T>,
filter?: FilterWithRequiredExcludingWhere<T>,
options?: AnyObject,
): Promise<T & Relations> {
const data = await this.sequelizeModel.findByPk(
Expand Down