Skip to content

Commit

Permalink
refactor: extend repository methods directly
Browse files Browse the repository at this point in the history
Signed-off-by: KalleV <[email protected]>
  • Loading branch information
KalleV authored and dhmlau committed Dec 1, 2023
1 parent 5b7e6cb commit 063648b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
27 changes: 24 additions & 3 deletions extensions/sequelize/src/sequelize/sequelize.repository.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,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<
FilterExcludingWhere<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 +252,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 +273,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 +300,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
12 changes: 0 additions & 12 deletions extensions/sequelize/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

declare module '@loopback/repository' {
interface 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;
}
}

/**
* Interface defining the component's options object
*/
Expand Down

0 comments on commit 063648b

Please sign in to comment.