Skip to content

Commit

Permalink
feat: add support of projection on get route (#1205)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-moncel authored Nov 12, 2024
1 parent 3aedf48 commit 5df3c58
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
8 changes: 2 additions & 6 deletions packages/agent/src/routes/access/get.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
ConditionTreeFactory,
PaginatedFilter,
ProjectionFactory,
} from '@forestadmin/datasource-toolkit';
import { ConditionTreeFactory, PaginatedFilter } from '@forestadmin/datasource-toolkit';
import Router from '@koa/router';
import { Context } from 'koa';

Expand Down Expand Up @@ -30,7 +26,7 @@ export default class GetRoute extends CollectionRoute {
const records = await this.collection.list(
QueryStringParser.parseCaller(context),
filter,
ProjectionFactory.all(this.collection),
QueryStringParser.parseProjectionWithPks(this.collection, context),
);

if (!records.length) {
Expand Down
40 changes: 40 additions & 0 deletions packages/agent/test/routes/access/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,46 @@ describe('GetRoute', () => {
expect(context.response.body).toEqual('test');
});

describe('with projection', () => {
test('it should handle projection', async () => {
jest
.spyOn(dataSource.getCollection('books'), 'list')
.mockResolvedValue([{ title: 'test ' }]);
services.serializer.serialize = jest.fn().mockReturnValue('test');
const get = new Get(services, options, dataSource, 'books');
const context = createMockContext({
state: { user: { email: 'john.doe@domain.com' } },
customProperties: {
query: {
timezone: 'Europe/Paris',
'fields[books]': 'name,author',
'fields[author]': 'id',
},
params: { id: '2d162303-78bf-599e-b197-93590ac3d315' },
},
});

await get.handleGet(context);

expect(dataSource.getCollection('books').list).toHaveBeenCalledWith(
{
email: 'john.doe@domain.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{
conditionTree: {
field: 'id',
operator: 'Equal',
value: '2d162303-78bf-599e-b197-93590ac3d315',
},
},
['name', 'author:id', 'id'],
);
});
});

describe('when an error happens', () => {
describe('when list returns []', () => {
test('should return an HTTP 404 response', async () => {
Expand Down

0 comments on commit 5df3c58

Please sign in to comment.