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

Middleware + $transactions do not play well together #7584

Closed
sdnts opened this issue Jun 10, 2021 · 1 comment · Fixed by #7662
Closed

Middleware + $transactions do not play well together #7584

sdnts opened this issue Jun 10, 2021 · 1 comment · Fixed by #7662
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/regression A reported bug in functionality that used to work before. team/client Issue for team Client. topic: middleware topic: transaction
Milestone

Comments

@sdnts
Copy link
Contributor

sdnts commented Jun 10, 2021

Bug description

If I use a middleware to Client, then call a prisma.$transaction, then the middleware blocks query execution.

How to reproduce

Assume this schema:

model User {
  id Int @id
  name String
}

I have a middlware that modifies Client responses to also include some extra parameters with it:

const prisma = new PrismaClient();

prisma.$use(async (params, next) => {
  let response = await next(params);
  console.log("middleware", response);
  return {
    params,
    response,
  };
});

  await prisma.$transaction([
    prisma.user.create({
      data: {
        id: "1",
        name: "Sid",
      },
    }),
  ]);

However:

  1. The query doesn't get executed. I don't see this record in my database.
  2. Instead of logging the Client response, the variabke response seems to be an async function. If I log response.toString(), it looks like calling next returns the actual next function. If I call this returned value again, like so:
prisma.$use(async (params, next) => {
  let response = await next(params);
  response = await (response as any)(params);
  console.log("middleware", response);
  return {
    params,
    response,
  };
});

  await prisma.$transaction([
    prisma.user.create({
      data: {
        id: "1",
        name: "Sid",
        age: 0,
      },
    }),
  ])

then everything works as expected. I see the correct log and the query gets executed.

Expected behavior

No response

Prisma information

model User {
id Int @id
name String
}

Environment & setup

  • OS: macOS
  • Database: Postgres
  • Node.js version: v14.17.0

Prisma Version

This seems to only be happening on dev. Prisma 2.24.1 behaves as expected.

I'm on:

prisma               : 2.25.0-dev.31
@prisma/client       : 2.25.0-dev.31
Current platform     : darwin
Query Engine         : query-engine 89926e7ecbebb89657ae2b56ab3454364ad7b5c5 (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli 89926e7ecbebb89657ae2b56ab3454364ad7b5c5 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 89926e7ecbebb89657ae2b56ab3454364ad7b5c5 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt 89926e7ecbebb89657ae2b56ab3454364ad7b5c5 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 89926e7ecbebb89657ae2b56ab3454364ad7b5c5
Studio               : 0.399.0

Internal Slack https://prisma-company.slack.com/archives/C016KUHB1R6/p1623327462109600

@sdnts sdnts added the kind/bug A reported bug. label Jun 10, 2021
@janpio janpio added kind/regression A reported bug in functionality that used to work before. and removed kind/bug A reported bug. labels Jun 10, 2021
@janpio janpio added this to the 2.25.0 milestone Jun 10, 2021
@janpio janpio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: middleware topic: transaction labels Jun 10, 2021
@pantharshit00
Copy link
Contributor

I can confirm this regression. In 2.25.0-dev.32 it also throws response is not a function which is also odd.

❯ yarn start
yarn run v1.22.5
$ ts-node main.ts
middleware { id: 1, name: 'Sid' }
(node:24744) UnhandledPromiseRejectionWarning: TypeError: response is not a function
    at C:\Users\harshit\code\reproductions\issue-7584\main.ts:8:23
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at main (C:\Users\harshit\code\reproductions\issue-7584\main.ts:24:16)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:24744) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handl
ed with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id:
1)(node:24744) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Terminate batch job (Y/N)?
^C

It doesn't happen in 2.24.1

@pantharshit00 pantharshit00 added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Jun 11, 2021
@pantharshit00 pantharshit00 removed their assignment Jun 11, 2021
@janpio janpio added the team/client Issue for team Client. label Jun 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/regression A reported bug in functionality that used to work before. team/client Issue for team Client. topic: middleware topic: transaction
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants