Skip to content

Commit

Permalink
feat(customizer): allow users to disable sort properly on column (#904)
Browse files Browse the repository at this point in the history
Thenkei authored Jan 3, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent c21c862 commit 2dfaeac
Showing 4 changed files with 69 additions and 1 deletion.
13 changes: 13 additions & 0 deletions packages/datasource-customizer/src/collection-customizer.ts
Original file line number Diff line number Diff line change
@@ -390,6 +390,19 @@ export default class CollectionCustomizer<
});
}

/**
* Disable sorting on a specific field.
* @param name the name of the field with sorting to be disabled
* @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/fields/sort#disabling-sort Documentation Link}
* @example
* .disableFieldSorting('fullName');
*/
disableFieldSorting(name: TColumnName<S, N>): this {
return this.pushCustomization(async () => {
this.stack.sortEmulate.getCollection(this.name).disableFieldSorting(name);
});
}

/**
* Enable sorting on a specific field using emulation.
* As for all the emulation method, the field sorting will be done in-memory.
Original file line number Diff line number Diff line change
@@ -25,6 +25,12 @@ export default class SortEmulate extends CollectionDecorator {
this.replaceFieldSorting(name, null);
}

disableFieldSorting(name: string): void {
// This is not the best way to achieve the disable behavior
// but basically we replace sort with nothing to disable any sort
this.replaceFieldSorting(name, []);
}

replaceFieldSorting(name: string, equivalentSort: PlainSortClause[]): void {
FieldValidator.validate(this, name);

@@ -75,8 +81,16 @@ export default class SortEmulate extends CollectionDecorator {
const fields: Record<string, FieldSchema> = {};

for (const [name, schema] of Object.entries(childSchema.fields)) {
const fieldSort = this.sorts.get(name);

fields[name] =
this.sorts.has(name) && schema.type === 'Column' ? { ...schema, isSortable: true } : schema;
this.sorts.has(name) &&
schema.type === 'Column' &&
// In order to support disableFieldSorting with empty array (isSortable: false)
// and emulateFieldSorting with null value (isSortable: true)
(!fieldSort || (fieldSort && fieldSort?.length !== 0))
? { ...schema, isSortable: true }
: schema;
}

return { ...childSchema, fields };
16 changes: 16 additions & 0 deletions packages/datasource-customizer/test/collection-customizer.test.ts
Original file line number Diff line number Diff line change
@@ -572,6 +572,22 @@ describe('Builder > Collection', () => {
});
});

describe('disableFieldSorting', () => {
it('should emulate sort on field', async () => {
const { dsc, customizer, stack } = await setup();
const collection = stack.sortEmulate.getCollection('authors');

const spy = jest.spyOn(collection, 'disableFieldSorting');

const self = customizer.disableFieldSorting('firstName');
await dsc.getDataSource(logger);

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('firstName');
expect(self).toEqual(customizer);
});
});

describe('emulateFieldSorting', () => {
it('should emulate sort on field', async () => {
const { dsc, customizer, stack } = await setup();
Original file line number Diff line number Diff line change
@@ -242,4 +242,29 @@ describe('SortEmulationDecoratorCollection', () => {
]);
});
});

describe('when disabling sort on book.title (no relations)', () => {
beforeEach(() => {
newBooks.disableFieldSorting('title');
});

test('schema should be updated', () => {
const schema = newBooks.schema.fields.title as ColumnSchema;
expect(schema.isSortable).toBeFalsy();
});

test('should not be concerned by sorting', async () => {
const records = await newBooks.list(
factories.caller.build(),
new PaginatedFilter({ sort: new Sort({ field: 'title', ascending: true }) }),
new Projection('id', 'title'),
);

expect(records).toStrictEqual([
{ id: 1, title: 'Foundation' },
{ id: 2, title: 'Beat the dealer' },
{ id: 3, title: 'Gomorrah' },
]);
});
});
});

0 comments on commit 2dfaeac

Please sign in to comment.