Skip to content

Conversation

@ymc9
Copy link
Collaborator

@ymc9 ymc9 commented Jan 26, 2024

  • Generate a function to provide value for fields using auth() in @default so we don't need to evaluate at runtime

    E.g., for model:

    model Post {
        imageUrl String @default(auth().profile.image.url)

    The following function is generated to facilitate the runtime evaluation:

    function $default$Post$imageUrl(user: any) {
        return user?.profile?.image?.url;
    }

    I think this approach will allow more flexibility in the future, e.g., we may allow calling other functions over auth() inside the @default(). The existing TypeScriptExpressionTransformer class is used to generate the function body. The file needed to be moved to the "@zenstackhq/sdk" package to be accessible. Many other updates in the PR are moving helper functions around too.

  • Correct the way of visiting nested create payload

    The NestedWriteVisistor is meant to do in-place updates to the payload. I've changed that part of logic, and also added the createMany hook.

  • Removed logic that turns direct auth() usage in @default() into stringified text

    I haven't thought about a real-world case this would be useful. Please let me know if you have one in your mind. If we would to support that, the ZModel validation part also needs to be adjusted to allow the type compatibility.

ymc9 and others added 3 commits January 26, 2024 15:08
- Generate a function to provide value for fields using `auth()` in `@default` so we don't need to evaluate at runtime
- Correct the way of visiting nested create payload
@ymc9 ymc9 merged commit 7b98938 into feature/default-auth-attributes Jan 26, 2024
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

Successfully merging this pull request may close these issues.

3 participants