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

[Feature Request] Custom RPC db operations #1387

Open
Ataraxy opened this issue Apr 29, 2024 · 2 comments
Open

[Feature Request] Custom RPC db operations #1387

Ataraxy opened this issue Apr 29, 2024 · 2 comments

Comments

@Ataraxy
Copy link

Ataraxy commented Apr 29, 2024

Would it be possible to allow for custom db ops so that they can be used with the RPC client? It seems that they're hard coded.

For example, If I want to create a prisma client extension that will do a findManyAndCount on a model, I would like to be able to also use it via RPC.

const findManyAndCount = {
  name: 'findManyAndCount',
  model: {
    $allModels: {
      findManyAndCount<Model, Args>(
        this: Model,
        args: Prisma.Exact<Args, Prisma.Args<Model, 'findMany'>>
      ): Promise<
        [
          Prisma.Result<Model, Args, 'findMany'>,
          number,
          Args extends { take: number } ? number : undefined
        ]
      > {
        return prisma.$transaction([
          (this as any).findMany(args),
          (this as any).count({ where: (args as any).where }),
        ]) as any;
      },
    },
  },
};

Though as an aside I'm unsure if this above extension would even work with an enhanced client but that's besides the point.

This example could of course be done with two separate queries client side or if something like a zenstack transactions api is put in such as this thread is discussing: #1203

Still such a feature could prove to be useful for other model related extensions.

Maybe it can be as simple as adding an options object that can be passed to the handler (perhaps a customOps property that is an array) and if the op exists in it then it will allow it through and not throw an error. It may also need to skip the part right after that validates the zodschema if this were the case.

switch (dbOp) {
case 'create':
case 'createMany':
case 'upsert':
if (method !== 'POST') {
return {
status: 400,
body: this.makeError('invalid request method, only POST is supported'),
};
}
if (!requestBody) {
return { status: 400, body: this.makeError('missing request body') };
}
args = requestBody;
// TODO: upsert's status code should be conditional
resCode = 201;
break;
case 'findFirst':
case 'findUnique':
case 'findMany':
case 'aggregate':
case 'groupBy':
case 'count':
if (method !== 'GET') {
return {
status: 400,
body: this.makeError('invalid request method, only GET is supported'),
};
}
try {
args = query?.q ? this.unmarshalQ(query.q as string, query.meta as string | undefined) : {};
} catch {
return { status: 400, body: this.makeError('invalid "q" query parameter') };
}
break;
case 'update':
case 'updateMany':
if (method !== 'PUT' && method !== 'PATCH') {
return {
status: 400,
body: this.makeError('invalid request method, only PUT AND PATCH are supported'),
};
}
if (!requestBody) {
return { status: 400, body: this.makeError('missing request body') };
}
args = requestBody;
break;
case 'delete':
case 'deleteMany':
if (method !== 'DELETE') {
return {
status: 400,
body: this.makeError('invalid request method, only DELETE is supported'),
};
}
try {
args = query?.q ? this.unmarshalQ(query.q as string, query.meta as string | undefined) : {};
} catch {
return { status: 400, body: this.makeError('invalid "q" query parameter') };
}
break;
default:
return { status: 400, body: this.makeError('invalid operation: ' + op) };
}
const { error, zodErrors, data: parsedArgs } = await this.processRequestPayload(args, model, dbOp, zodSchemas);

@ymc9
Copy link
Member

ymc9 commented May 2, 2024

Hi @Ataraxy , thanks for filing this and the proposal.

First of all, since Prisma client extension is not part of the schema, ZenStack doesn't really have knowledge of it. Plugins like trpc generation are solely based on the schema today.

Do you think what you need can be resolved by implementing a custom trpc route? Trpc allows flexible route merging. You can use ZenStack-enhanced prisma client inside the custom-implemented router, and merge it with ZenStack-generated ones. The benefit is that you have full control of the server-side and don't need to have multiple client-side queries. Trpc is mainly a server-side framework anyway.

@juni0r
Copy link

juni0r commented Jun 7, 2024

@ymc9 I think custom operations for the RPC client would be a great addition, since using only CRUD ops just isn't sufficient for more complex apps. I have several instances where I need to add custom business logic on the server side, such as transactions or hitting an external API.

Ultimately though it's still a regular update operation as far as the client is concerned. Sure, you could write a custom API handler but then you'll lose all the benefits of the RPC client, such as query invalidation and enhanced serialization.

I implemented a proof of concept which works very well in principle. Say we want to do a purchase by obtaining a payment from an external provider and invoke a custom action /api/model/order/pay. On the server side we confirm the payment, update the order status, maybe create some other artifacts and send a confirmation email.

const { endpoint, fetch } = getHooksContext()

interface PayOrder {
  id: string
  transactionId: string
}

const { mutateAsync: payOrder } = useModelMutation<PayOrder, QueryError, Order, true>(
  'Order',
  'PUT',
  `${endpoint}/order/pay`,
  metadata,
  undefined,
  fetch,
  true,


await payOrder({ id: order.id, transactionId: payment.transactionId })

On the server side we simply create the corresponding handler which performs all necessary logic and eventually returns the updated order object.

It works like a charm except for one problem: When parsing the result on the client, it'll determine the operation by doing a path.split('/').pop() which in the example above will yield pay and this will result in an error.

The workaround involves a little trickery with subclassing string an overriding the split method. In order to make this work more easily, the operation could be carried independently of the path.

On the server side it'd be quite handy to have a utility function such as defineRPCHandler which conveniently handles input validation, errors and sending the result as Superjson.

export default defineRPCHandler(async (event) => {
  const { id, transactionId } = parseBody(PayOrderInputSchema)

  return await prisma.$transaction(async tx => {
    const receipt = await PayBuddy.confirmPayment(transactionId)

    const order = await tx.order.update({ 
      where: { id },
      data: {
        status: 'paid',
        payment: { create: { transactionId, receipt } }
      }
    })

    // ... more business logic

    return order
  })
})

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

3 participants