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(datasource-customizer): add helper to fetch values from selected record in single actions #891

Merged
merged 2 commits into from
Dec 4, 2023
Merged
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
@@ -1,7 +1,7 @@
import { CompositeId } from '@forestadmin/datasource-toolkit';

import ActionContext from './base';
import { TCollectionName, TFieldName, TRow, TSchema } from '../../../templates';
import { TCollectionName, TFieldName, TFieldType, TRow, TSchema } from '../../../templates';

export default class ActionContextSingle<
S extends TSchema = TSchema,
@@ -24,4 +24,14 @@ export default class ActionContextSingle<

return records[0];
}

async getField<T extends TFieldName<S, N>>(field: T): Promise<TFieldType<S, N, T>> {
let value = await this.getRecord([field]);

for (const path of field.split(':')) {
value = value?.[path];
}

return value as TFieldType<S, N, T>;
}
}
Original file line number Diff line number Diff line change
@@ -8,17 +8,37 @@ describe('ActionContext', () => {
let books: Collection;

beforeEach(() => {
books = factories.collection.build({
dataSource: factories.dataSource.build(),
name: 'books',
schema: factories.collectionSchema.build({
fields: {
id: factories.columnSchema.build({ isPrimaryKey: true }),
title: factories.columnSchema.build(),
},
const dataSource = factories.dataSource.buildWithCollections([
factories.collection.build({
dataSource: factories.dataSource.build(),
name: 'authors',
schema: factories.collectionSchema.build({
fields: {
id: factories.columnSchema.build({ isPrimaryKey: true }),
firstname: factories.columnSchema.build(),
lastname: factories.columnSchema.build(),
},
}),
}),
list: jest.fn().mockResolvedValue([{ id: 1, title: 'Foundation' }]),
});
factories.collection.build({
dataSource: factories.dataSource.build(),
name: 'books',
schema: factories.collectionSchema.build({
fields: {
id: factories.columnSchema.build({ isPrimaryKey: true }),
title: factories.columnSchema.build(),
author: factories.manyToOneSchema.build({ foreignCollection: 'authors' }),
},
}),
list: jest
.fn()
.mockResolvedValue([
{ id: 1, title: 'Foundation', author: { firstname: 'Isaac', lastname: 'Asimov' } },
]),
}),
]);

books = dataSource.getCollection('books');
});

test('should factorize calls to list made at the same time', async () => {
@@ -93,4 +113,25 @@ describe('ActionContext', () => {
await expect(promise1).rejects.toThrow('bad request');
await expect(promise2).rejects.toThrow('bad request');
});

test('should get individual fields', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a test that is accessing child properties here? I think it's a major part of your contribution, and it should be tested as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. Will do!
@Scra3 would have told me the same 😁

const caller = factories.caller.build();

const context = new ActionContextSingle(books, caller, {}, {});
const [id, title, authorFirstName] = await Promise.all([
context.getField('id'),
context.getField('title'),
context.getField('author:firstname'),
]);

expect(books.list).toHaveBeenCalledTimes(1);
expect(books.list).toHaveBeenCalledWith(caller, new Filter({}), [
'id',
'title',
'author:firstname',
]);
expect(id).toEqual(1);
expect(title).toEqual('Foundation');
expect(authorFirstName).toEqual('Isaac');
});
});