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

possible collision with prisma extensions #302

Open
2 tasks done
imsherrill opened this issue Apr 15, 2024 · 4 comments
Open
2 tasks done

possible collision with prisma extensions #302

imsherrill opened this issue Apr 15, 2024 · 4 comments
Assignees
Labels
invalid This doesn't seem right question Further information is requested

Comments

@imsherrill
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Versions

"prisma": "^4.12.0",
"prisma-json-types-generator": "3.0.4"

A minimal reproducible example

none

Description

My understanding is that the generator is supposed to go into the generated .d.ts file and find the field and replace the field's Prisma.JsonValue type with what I've provided and added to global names (in this case let's say it's CustomJsonType)

and it works in some cases for example:

  export type ProposalGroupByOutputType = {
    id: string
    ...
    jfield: PrismaJson.CustomJsonType
    ...
  }

but seems like in other cases its missing the default types and I think its because its already overloaded the type (instead of proposal its given Extension.GetResults)

export type ProposalPayload<ExtArgs extends $Extensions.Args = $Extensions.DefaultArgs> = {
  name: "Proposal"
  objects: {
    company: CompanyPayload<ExtArgs>
    ...
  }
  scalars: $Extensions.GetResult<{
    id: string
    ...
    /**
     * [CustomJsonType]
     */
    jfield: Prisma.JsonValue
  }, ExtArgs["result"]["proposal"]>
  composites: {}
}

I know it works by using prisma's abstract syntax tree so instead of replacing the type it seems like its leaving the comment there that informs what should be swapped out for the Prisma.JsonValue

is this a bug/ collision with extensions or am I misunderstanding whats going on here?

Steps to Reproduce

setup a simple prisma extension and try to use json prisma-json-types-generator

Expected Behavior

No response

@arthurfiorette
Copy link
Owner

Can you set up a minimal reproducible example?

@snikch
Copy link

snikch commented Sep 9, 2024

We hit the same issue. Could no longer use this extension because it's overwritten by our custom RLS extension.

@arthurfiorette
Copy link
Owner

overwritten by our custom RLS extension

Im afraid I cant help with problems created by other extensions.

@snikch
Copy link

snikch commented Sep 9, 2024

Apologies I didn't make myself clear. I mean that by us using another extension, it appears the functionality is removed. The extension shouldn't be affecting your extension as far as I can see. The types are correct, but the returned value is a string not an object.

Here's the extension we're using. I see no reason it would be interfering with yours, but I also admit I don't know too much about Prisma extensions.

export interface SupabaseRowLevelSecurityOptions {
  name?: string
  claimsSetting?: string
  claimsFn?: undefined | (() => Record<string, any>)
  policyError?: Error
  logging?: boolean
}

const defaultSupabaseRowLevelSecurityOptions: SupabaseRowLevelSecurityOptions =
  {
    name: "useSupabaseRowLevelSecurity",
    claimsSetting: "request.jwt.claims",
    claimsFn: undefined,
    logging: false,
  }

export const useSupabaseRowLevelSecurity = (
  options: SupabaseRowLevelSecurityOptions = defaultSupabaseRowLevelSecurityOptions,
) => {
  const name = options.name || defaultSupabaseRowLevelSecurityOptions.name
  const claimsFn =
    options.claimsFn || defaultSupabaseRowLevelSecurityOptions.claimsFn
  const claimsSetting =
    options.claimsSetting ||
    defaultSupabaseRowLevelSecurityOptions.claimsSetting
  const policyError =
    options.policyError || defaultSupabaseRowLevelSecurityOptions.policyError

  return Prisma.defineExtension((client: PrismaClient) =>
    client.$extends({
      name: name || "useSupabaseRowLevelSecurity",
      query: {
        $allModels: {
          async $allOperations({ args, query }: any) {
            const claims = claimsFn ? JSON.stringify(claimsFn() || {}) : ""
            try {
              const [, result] = await client.$transaction([
                client.$executeRaw`SELECT set_config(${claimsSetting}, ${claims}, TRUE)`,
                query(args),
              ])

              return result
            } catch (e) {
              if (options.logging) logger.error(e)
              throw policyError || e
            }
          },
        },
      },
    }),
  )
}


...


return client.$extends(useSupabaseRowLevelSecurity({
    claimsFn: () => ({
      role: "xxx",
    }),
  }))

@arthurfiorette arthurfiorette self-assigned this Sep 30, 2024
@arthurfiorette arthurfiorette added invalid This doesn't seem right question Further information is requested labels Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants