Skip to content

Commit

Permalink
feat: constrain filter to exclude where for findById
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Feb 28, 2020
1 parent f61896e commit 360d563
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
8 changes: 8 additions & 0 deletions packages/repository/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ export interface Filter<MT extends object = AnyObject> {
include?: Inclusion[];
}

/**
* Filter without `where` property
*/
export type FilterExcludingWhere<MT extends object = AnyObject> = Omit<
Filter<MT>,
'where'
>;

/**
* TypeGuard for Filter
* @param candidate
Expand Down
4 changes: 2 additions & 2 deletions packages/repository/src/repositories/legacy-juggler-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '../common-types';
import {EntityNotFoundError} from '../errors';
import {Entity, Model, PropertyType} from '../model';
import {Filter, Inclusion, Where} from '../query';
import {Filter, Inclusion, Where, FilterExcludingWhere} from '../query';
import {
BelongsToAccessor,
BelongsToDefinition,
Expand Down Expand Up @@ -396,7 +396,7 @@ export class DefaultCrudRepository<

async findById(
id: ID,
filter?: Filter<T>,
filter?: FilterExcludingWhere<T>,
options?: Options,
): Promise<T & Relations> {
const include = filter?.include;
Expand Down
17 changes: 14 additions & 3 deletions packages/repository/src/repositories/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {CrudConnector} from '../connectors';
import {DataSource} from '../datasource';
import {EntityNotFoundError} from '../errors';
import {Entity, Model, ValueObject} from '../model';
import {Filter, Where} from '../query';
import {Filter, FilterExcludingWhere, Where} from '../query';
import {InclusionResolver} from '../relations/relation.types';
import {IsolationLevel, Transaction} from '../transaction';

Expand Down Expand Up @@ -172,6 +172,13 @@ export interface EntityCrudRepository<

/**
* Find an entity by id, return a rejected promise if not found.
*
* @remarks
*
* The rationale behind findById is to find an instance by its primary key
* (id). No other search criteria than id should be used. If a client wants
* to use a `where` clause beyond id, use `find` or `findOne` instead.
*
* @param id - Value for the entity id
* @param filter - Additional query options. E.g. `filter.include` configures
* which related models to fetch as part of the database query (or queries).
Expand All @@ -180,7 +187,7 @@ export interface EntityCrudRepository<
*/
findById(
id: ID,
filter?: Filter<T>,
filter?: FilterExcludingWhere<T>,
options?: Options,
): Promise<T & Relations>;

Expand Down Expand Up @@ -303,7 +310,11 @@ export class CrudRepositoryImpl<T extends Entity, ID>
);
}

async findById(id: ID, filter?: Filter<T>, options?: Options): Promise<T> {
async findById(
id: ID,
filter?: FilterExcludingWhere<T>,
options?: Options,
): Promise<T> {
if (typeof this.connector.findById === 'function') {
return this.toModel(
this.connector.findById(this.entityClass, id, options),
Expand Down

0 comments on commit 360d563

Please sign in to comment.