Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow disable search on collection #1236

Merged
merged 6 commits into from
Jan 13, 2025
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
12 changes: 12 additions & 0 deletions packages/datasource-customizer/src/collection-customizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ export default class CollectionCustomizer<
});
}

/**
* Disable search on the collection
*
* * @example
* .disableSearch()
*/
disableSearch(): this {
arnaud-moncel marked this conversation as resolved.
Show resolved Hide resolved
return this.pushCustomization(async () => {
this.stack.search.getCollection(this.name).disable();
});
}

/**
* Import a field from a many to one or one to one relation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@ import { SearchDefinition } from './types';
export default class SearchCollectionDecorator extends CollectionDecorator {
override dataSource: DataSourceDecorator<SearchCollectionDecorator>;
replacer: SearchDefinition = null;
searchable = true;

replaceSearch(replacer: SearchDefinition): void {
this.searchable = true;
this.replacer = replacer;
this.markSchemaAsDirty();
}

disable() {
this.searchable = false;
this.markSchemaAsDirty();
}

override refineSchema(subSchema: CollectionSchema): CollectionSchema {
return { ...subSchema, searchable: true };
return { ...subSchema, searchable: this.searchable };
}

override async refineFilter(caller: Caller, filter?: PaginatedFilter): Promise<PaginatedFilter> {
Expand Down
14 changes: 14 additions & 0 deletions packages/datasource-customizer/test/collection-customizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ describe('Builder > Collection', () => {
});
});

describe('disableSearch', () => {
it('should edit the schema', async () => {
const { dsc, customizer, stack } = await setup();
const spy = jest.spyOn(stack.search.getCollection('authors'), 'disable');

const self = customizer.disableSearch();
await dsc.getDataSource(logger);

expect(spy).toHaveBeenCalledTimes(1);
expect(self.schema.searchable).toBeFalsy();
expect(self).toEqual(customizer);
});
});

describe('renameField', () => {
it('should throw when renaming with a name including space', async () => {
const { customizer, dsc } = await setup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,45 @@ function buildCollection(
}

describe('SearchCollectionDecorator', () => {
describe('disable', () => {
it('should set the schema to isSearchable false', async () => {
const decorator = buildCollection({});
decorator.disable();

expect(decorator.schema.searchable).toBe(false);
});
});

describe('refineSchema', () => {
it('should set the schema searchable', async () => {
const decorator = buildCollection({ searchable: false });

expect(decorator.schema.searchable).toBe(true);
});

describe('when replace and disable are both used', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Insane ♡

it('should set the schema searchable', async () => {
const decorator = buildCollection({});
decorator.disable();

expect(decorator.schema.searchable).toBe(false);

decorator.replaceSearch(value => ({ field: 'id', operator: 'Equal', value }));

expect(decorator.schema.searchable).toBe(true);
});

it('should set the schema not searchable', async () => {
const decorator = buildCollection({});
decorator.replaceSearch(value => ({ field: 'id', operator: 'Equal', value }));

expect(decorator.schema.searchable).toBe(true);

decorator.disable();

expect(decorator.schema.searchable).toBe(false);
});
});
});

describe('refineFilter', () => {
Expand Down
Loading