Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ import CollectionCustomizationContext from '../../context/collection-context';

export default class OperatorsEmulateCollectionDecorator extends CollectionDecorator {
override readonly dataSource: DataSourceDecorator<OperatorsEmulateCollectionDecorator>;
private readonly fields: Map<string, Map<Operator, OperatorDefinition>> = new Map();
private readonly fields: Map<string, Map<Operator, OperatorDefinition | null>> = new Map();

emulateFieldOperator(name: string, operator: Operator): void {
this.replaceFieldOperator(name, operator, null);
this.replaceOrEmulateFieldOperator(name, operator, null);
}

replaceFieldOperator(name: string, operator: Operator, replaceBy: OperatorDefinition): void {
if (!replaceBy) throw new Error('replaceBy handler is required');
this.replaceOrEmulateFieldOperator(name, operator, replaceBy);
}

private replaceOrEmulateFieldOperator(
name: string,
operator: Operator,
replaceBy: OperatorDefinition | null,
): void {
// Check that the collection can actually support our rewriting
const pks = SchemaUtils.getPrimaryKeys(this.childCollection.schema);
pks.forEach(pk => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ describe('OperatorsEmulateCollectionDecorator', () => {
});
});

test('replaceFieldOperator() should throw if no handler is given', () => {
expect(() => newBooks.replaceFieldOperator('title', 'StartsWith', null)).toThrow(
'replaceBy handler is required',
);
});

describe('when creating a cycle in the replacements graph', () => {
beforeEach(() => {
newBooks.replaceFieldOperator('title', 'StartsWith', async value => ({
Expand Down