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

Client Extensions #564

Open
ajhollowayvrm opened this issue Sep 5, 2023 · 4 comments
Open

Client Extensions #564

ajhollowayvrm opened this issue Sep 5, 2023 · 4 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@ajhollowayvrm
Copy link

This is more of a question. I use client extensions (https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions) pretty heavily in my code. I can mock and recreate those on my own but does Prismock support them?

@morintd
Copy link
Owner

morintd commented Sep 6, 2023

Hello @ajhollowayvrm , there is actually a dummy implementation of the $extends method in prismock, but it won't be enough if you are really using it...

I'll have a look to get a better support for $extends in the next few days/weeks. If you have a public repo with specific use-cases, let me know, I'll use it to validate my configuration.

@morintd morintd self-assigned this Sep 6, 2023
@morintd morintd added the enhancement New feature or request label Sep 6, 2023
@ajhollowayvrm
Copy link
Author

ajhollowayvrm commented Oct 13, 2023

export const DBConn = new PrismaClient({
    log: [
        {
            emit: 'stdout',
            level: 'error',
        },
        {
            emit: 'stdout',
            level: 'info',
        },
        {
            emit: 'stdout',
            level: 'warn',
        },
    ],
})
    .$extends({ name: 'TypeConversion Extensions', query: ClientExtensions })
    .$extends({
        name: 'User Attributes',
        result: {
            users: {
                injectUserAttributes: {
                    name: 'inject user attributes function',
                    needs: { id: true },
                    compute(user: any) {
                        return async (requestingUserCompanyID?: number) => {
                            // Determines if user_attributes are needed based on fields being returned
                            if (Object.keys(user).some((column) => UserAttributesFields.includes(column))) {
                                if (!user.hasOwnProperty('user_attributes')) {
                                    user.user_attributes = await DBConn.user_attributes.findMany({
                                        where: { user_id: user.id },
                                    });
                                }
                                await injectUserAttributes(user, requestingUserCompanyID);
                            }
                            return user;
                        };
                    },
                },
                updateAttributes: {
                    name: 'update user attributes function',
                    needs: { id: true, companyid: true, recognition_manager: true, recognition_admin: true, custom_manager_budget: true },
                    compute(data) {
                        return () =>
                            DBConn.user_attributes.update({
                                where: { user_id_company_id: { user_id: data.id, company_id: data.companyid } },
                                data: {
                                    manager: data.recognition_manager,
                                    admin: data.recognition_admin,
                                    budget: data.custom_manager_budget,
                                },
                                include: {
                                    users: true,
                                },
                            });
                    },
                },
                key: {
                    name: 'manger key',
                    needs: { first_name: true, last_name: true, username: true },
                    // used for manager in manager.controller.ts
                    compute(data) {
                        return data.first_name + ' ' + data.last_name + ' ' + data.username;
                    },
                },
            },
        },
    })
    .$extends({
        model: {
            users: {
                async exists(where) {
                    const count = await DBConn.users.count({ where });
                    return count > 0;
                },
            },
        },
    });

@ajhollowayvrm
Copy link
Author

@morintd here's an example of how I use it

@morintd
Copy link
Owner

morintd commented Oct 14, 2023

Thanks @ajhollowayvrm , I've been experimenting with extensions in the past few days. I don't see it getting complete support short-term, but I'll add functionalities incrementally.

I might use your example and report back about it here.

@morintd morintd added the help wanted Extra attention is needed label Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants