Skip to content

Commit

Permalink
fix(core): allow filter condition callbacks without arguments
Browse files Browse the repository at this point in the history
Closes #847
  • Loading branch information
B4nan committed Sep 17, 2020
1 parent 2188f62 commit 5b3401f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
17 changes: 17 additions & 0 deletions docs/docs/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ const books = await orm.em.find(Book, {}, {
});
```

### Filters without parameters

If we want to have a filter condition that do not need arguments, but we want
to access the `type` parameter, we will need to explicitly set `args: false`,
otherwise error will be raised due to missing parameters:

```ts
@Filter({
name: 'withoutParams',
cond(_, type) {
return { ... };
},
args: false,
default: true,
})
```

## Global filters

We can also register filters dynamically via `EntityManager` API. We call such filters
Expand Down
17 changes: 17 additions & 0 deletions docs/versioned_docs/version-4.0/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ const books = await orm.em.find(Book, {}, {
});
```

### Filters without parameters

If we want to have a filter condition that do not need arguments, but we want
to access the `type` parameter, we will need to explicitly set `args: false`,
otherwise error will be raised due to missing parameters:

```ts
@Filter({
name: 'withoutParams',
cond(_, type) {
return { ... };
},
args: false,
default: true,
})
```

## Global filters

We can also register filters dynamically via `EntityManager` API. We call such filters
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/EntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class EntityManager<D extends IDatabaseDriver = IDatabaseDriver> {
if (filter.cond instanceof Function) {
const args = Utils.isPlainObject(options[filter.name]) ? options[filter.name] : this.filterParams[filter.name];

if (!args && filter.cond.length > 0) {
if (!args && filter.cond.length > 0 && filter.args !== false) {
throw new Error(`No arguments provided for filter '${filter.name}'`);
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export type FilterDef<T extends AnyEntity<T>> = {
cond: FilterQuery<T> | ((args: Dictionary, type: 'read' | 'update' | 'delete') => FilterQuery<T> | Promise<FilterQuery<T>>);
default?: boolean;
entity?: string[];
args?: boolean;
};

export type ExpandProperty<T> = T extends Reference<infer U> ? NonNullable<U> : T extends Collection<infer U> ? NonNullable<U> : NonNullable<T>;
Expand Down
4 changes: 3 additions & 1 deletion tests/entities/Author.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import { BaseEntity } from './BaseEntity';
@Index({ name: 'custom_idx_1', properties: ['name', 'email'] })
@Filter({
name: 'withoutParams1',
cond() {
cond(_, type) {
expect(['read', 'update', 'delete'].includes(type)).toBe(true);
return {};
},
args: false,
default: true,
})
export class Author extends BaseEntity<Author> {
Expand Down

0 comments on commit 5b3401f

Please sign in to comment.