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

Custom functions for @default - Accepted Proposal #5

Closed
DavidHancu opened this issue Nov 14, 2022 · 1 comment
Closed

Custom functions for @default - Accepted Proposal #5

DavidHancu opened this issue Nov 14, 2022 · 1 comment

Comments

@DavidHancu
Copy link
Owner

Scope

The aim of this proposal is to provide a sneak-peek for the new feature that will be added to Prisma Util v2.0. In this issue, I'll be outlining the process of defining a function and how the configuration will look like, so you can make preparations in advance.

Introduction

This feature will be released under the customAttributeFunctions flag and will require manual activation.

Configuration

To set a default function for a field, we can just do this:

function generateDefault(dmmf) {
    // Generate your default value based on the DMMF data of this field
    const defaultValue = "";
    return defaultValue;
}
export default {
    // Other configuration values
    defaultFunctions: {
        "path/to/file:ModelName.field": generateDefault
    }
}

The function will then be grabbed by Prisma Util and added to a function map. To copy the function over, Prisma Util will use .toString() on the provided function and wrap the code in another function, as such:

const columnMappings = {
    "ModelName.field": function() {
        const func = function generateDefault(dmmf) {
            // Generate your default value based on the DMMF data of this field
            const defaultValue = "";
            return defaultValue;
        }
        return func;
    }(),
}

Middleware

This feature makes use of Project Toolchain's Middleware API and defines a middleware called attributeFunctions. To use it, you have to import it like this and add it to your Prisma middleware chain:

import attributeFunctions from "prisma-util/toolchain/middleware/customAttributeFunctions";
prisma.$use(attributeFunctions(prisma));

And that's it! Now everything will function correctly whenever you create a new model.

Final Words

Please let us know if this is suitable for you and if you would like any changes to be made.

@DavidHancu DavidHancu changed the title [Proposal] [v2.0.0] Custom functions for @default Custom functions for @default - Accepted Proposal Dec 4, 2022
@DavidHancu
Copy link
Owner Author

Feature implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant