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

Add lifecycle callbacks (beforeDelete, afterUpdate, etc.) to facilitate dataProvider customization #8511

Merged
merged 12 commits into from
Dec 20, 2022

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Dec 16, 2022

Problem

Developers must put their data handling / domain logic in the dataProvider, but that isn't very practical. The dataProvider quickly becomes a mess of if(resource ==='...') that is hard to read and to maintain.

Solution

Allow developers to decorate an existing dataProvider with lifecycle callbacks.

const dataProvider = withLifecycleCallbacks(baseDataProvider, [
    {
        resource: 'posts',
        beforeDelete: async ({ id }) => {
            // delete related comments
            const { data: comments } = await baseDataProvider.getList(
                'comments',
                {
                    filter: { post_id: id },
                    pagination: { page: 1, perPage: 100 },
                    sort: { field: 'id', order: 'DESC' },
                }
            );
            await baseDataProvider.deleteMany('comments', {
                ids: comments.map(comment => comment.id),
            });
            return { id };
        },
    },
]);
  • Add code
  • Add unit tests
  • Use in simple example
  • Add e2e test
  • Add documentation

@vercel
Copy link

vercel bot commented Dec 16, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
react-admin 🔄 Building (Inspect) Dec 19, 2022 at 1:58PM (UTC)
react-admin-storybook 🔄 Building (Inspect) Dec 19, 2022 at 1:58PM (UTC)

@fzaninotto fzaninotto added RFR Ready For Review and removed WIP Work In Progress labels Dec 19, 2022
@fzaninotto
Copy link
Member Author

Switching to RFR

Copy link
Contributor

@slax57 slax57 left a comment

Choose a reason for hiding this comment

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

Great testing though 💪

fzaninotto and others added 2 commits December 19, 2022 14:40
Co-authored-by: Jean-Baptiste Kaiser <[email protected]>
Co-authored-by: Jean-Baptiste Kaiser <[email protected]>
fzaninotto and others added 2 commits December 19, 2022 14:56
@fzaninotto
Copy link
Member Author

Review applied

@slax57 slax57 added this to the 4.7.0 milestone Dec 20, 2022
@slax57 slax57 merged commit d109994 into next Dec 20, 2022
@slax57 slax57 deleted the lifecycle-callbacks branch December 20, 2022 09:16
@fzaninotto fzaninotto changed the title Add lifecycle callbacks to facilitate dataProvider customization Add lifecycle callbacks (beforeDelete, afterUpdate, etc.) to facilitate dataProvider customization Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants