Skip to content

Commit 62f9d12

Browse files
committed
Change filter registration API
1 parent 1029c80 commit 62f9d12

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

packages/query-typeorm/__tests__/query/where.builder.spec.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@ describe('WhereBuilder', (): void => {
1414

1515
const customFilterRegistry = new CustomFilterRegistry();
1616
// Test for (operation) filter registration (this is valid for all fields of all entities)
17-
customFilterRegistry.setFilter('isMultipleOf', {
18-
apply(field, cmp, val: number, alias): CustomFilterResult {
19-
alias = alias ? alias : '';
20-
const pname = `param${randomString()}`;
21-
return {
22-
sql: `("${alias}"."${field}" % :${pname}) == 0`,
23-
params: { [pname]: val },
24-
};
17+
customFilterRegistry.setFilter(
18+
{ operation: 'isMultipleOf' },
19+
{
20+
apply(field, cmp, val: number, alias): CustomFilterResult {
21+
alias = alias ? alias : '';
22+
const pname = `param${randomString()}`;
23+
return {
24+
sql: `("${alias}"."${field}" % :${pname}) == 0`,
25+
params: { [pname]: val },
26+
};
27+
},
2528
},
26-
});
29+
);
2730
// Test for (class, field, operation) filter overriding the previous operation filter on a specific field
28-
customFilterRegistry.setFilter<TestEntity>(
29-
'isMultipleOf',
31+
customFilterRegistry.setFilter(
32+
{ operation: 'isMultipleOf', Entity: TestEntity, field: 'dateType' },
3033
{
3134
apply(field, cmp, val: number, alias): CustomFilterResult {
3235
alias = alias ? alias : '';
@@ -37,11 +40,10 @@ describe('WhereBuilder', (): void => {
3740
};
3841
},
3942
},
40-
{ klass: TestEntity, field: 'dateType' },
4143
);
4244
// Test for (class, field, operation) filter on a virtual property 'fakePointType' that does not really exist on the entity
43-
customFilterRegistry.setFilter<TestEntity>(
44-
'distanceFrom',
45+
customFilterRegistry.setFilter(
46+
{ operation: 'distanceFrom', Entity: TestEntity, field: 'fakePointType' },
4547
{
4648
apply(field, cmp, val: { point: { lat: number; lng: number }; radius: number }, alias): CustomFilterResult {
4749
alias = alias ? alias : '';
@@ -54,7 +56,6 @@ describe('WhereBuilder', (): void => {
5456
};
5557
},
5658
},
57-
{ klass: TestEntity, field: 'fakePointType' },
5859
);
5960

6061
const expectSQLSnapshot = (filter: Filter<TestEntity>): void => {

packages/query-typeorm/src/query/custom-filter.registry.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,23 @@ export class CustomFilterRegistry {
4040
}
4141

4242
// (operation) filter overload
43-
setFilter<Entity>(opName: string, filter: CustomFilter, opts?: unknown): void;
44-
45-
// (class, field, operation) filter overload
43+
// setFilter<Entity>(opSpec: { operation: string }, filter: CustomFilter<Entity>): void;
44+
// Implementation
4645
setFilter<Entity>(
47-
opName: string,
46+
opSpec: { operation: string; Entity?: Class<Entity>; field?: keyof Entity | string },
4847
filter: CustomFilter<Entity>,
49-
opts?: { klass: Class<Entity>; field?: keyof Entity | string },
5048
): void {
51-
if (opts && opts.klass && opts.field) {
52-
const { klass, field } = opts;
53-
if (!this.cfoRegistry.has(klass)) {
54-
this.cfoRegistry.set(klass, {});
49+
if (opSpec.Entity && opSpec.field) {
50+
// (entity/class, field, operation) filter
51+
const { Entity, field } = opSpec;
52+
if (!this.cfoRegistry.has(Entity)) {
53+
this.cfoRegistry.set(Entity, {});
5554
}
56-
const klassFilters = this.cfoRegistry.get(klass) as EntityCustomFilters;
57-
klassFilters[field] = merge(klassFilters[field], { [opName]: filter });
58-
} else {
59-
this.oRegistry[opName] = filter;
55+
const entityFilters = this.cfoRegistry.get(Entity) as EntityCustomFilters;
56+
entityFilters[field] = merge(entityFilters[field], { [opSpec.operation]: filter });
57+
} else if (opSpec.operation) {
58+
// (operation) filter
59+
this.oRegistry[opSpec.operation] = filter;
6060
}
6161
}
6262
}

0 commit comments

Comments
 (0)