Skip to content

Query Comments #10746

@Ustice

Description

@Ustice

Problem

I need a way to trace queries to the request that spawned them, but unfortunately Prisma doesn't offer any lifecycle hooks that I can use. After looking for some workarounds, I came across SQL Commenter, which seems to offer a very simple workaround, but unfortunately, there does not currently seem to be a way to add comments to a query.

Suggested solution

It should be easy enough to add SQL comments by adding a comment method to the prisma client. For instance:

const comment = `Request Id: ${ requestId }`
const user = await prisma.comment(comment).user.findUnique({
  where: {
    email: 'elsa@prisma.io',
  },
})
/* Request Id: ff849408-5170-45f7-a4c9-85c844ff61f0 */
SELECT * FROM user WHERE email = "elsa@prisma.io" LIMIT 1;

Another useful feature of this interface would be that we could reuse the comment-tagged client for multiple queries.

const taggedClient = prisma.comment(`For request: ${ requestId }`)

// Yes, this example could be a join, but humor me, please.
const user = await taggedClient.user.findUnique({ where: { email: 'elsa@prisma.io' } })
const stores = await taggedClient.store.find({ where: { city: user.city } })
/* Request Id: ff849408-5170-45f7-a4c9-85c844ff61f0 */
SELECT * FROM user WHERE email = "elsa@prisma.io" LIMIT 1;

/* Request Id: ff849408-5170-45f7-a4c9-85c844ff61f0 */
SELECT * FROM store WHERE city = "Berlin";

Alternatives

An alternative solution, and one that I was originally looking for was to be able to add metadata to queries. These could be using in the event-based logging to identify the source of query. Unfortunately, this doesn't exist either.

This could be expanded to allow for SQL block comments and the like, but even just a query-level comment would be very powerful.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions