forked from loopbackio/loopback-next
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// RT means related repository's type | ||
// T | ||
// ID means source model's id type | ||
import {Entity} from '../model'; | ||
import {Getter, inject} from '@loopback/core'; | ||
import {Filter} from '../'; | ||
import {AnyObject} from '..'; | ||
import {DefaultCrudRepository} from './legacy-juggler-bridge'; | ||
import {inspect} from 'util'; | ||
|
||
// TE: the target entity | ||
// TID: the ID of target entity | ||
// SID: the ID of source entity | ||
export function InclusionHandlerFactory<TE extends Entity, TID, SID>( | ||
targetRepoGetter: Getter<DefaultCrudRepository<TE, TID>>, | ||
// to support composed fk, eventually the fkName should be an array | ||
// fkNames: string[] | ||
fkName: string, | ||
) { | ||
return async function inclusionHandler( | ||
fk: SID, | ||
// There are two concerns of the Filter interface | ||
// 1st: the Filter interface doesn't provide the related model's | ||
// type for the inclusion filter, see my comment in query.ts | ||
// 2nd: when apply the fk constraint on `where`, it doesn't recognize | ||
// the fkName as a property defined in the `where` filter, | ||
// but an arbitrary string instead. As a workaround I used | ||
// a loose type: Filter<TE & AnyObject> | ||
filter?: Filter<TE & AnyObject>, | ||
): Promise<TE[]> { | ||
const targetRepo = await targetRepoGetter(); | ||
filter = filter || {}; | ||
filter.where = filter.where || {}; | ||
filter.where[fkName] = fk; | ||
// console.log(`inclusion filter: ${inspect(filter)}`); | ||
return await targetRepo.find(filter); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters