Skip to content

Preview feature feedback: middlewares #3107

Closed

Description

@nikolasburk

Please share your feedback about the preview feature middlewares that was released in v2.3.0 in this issue.

  • If you encounter a bug, please open a bug report in this repo.
  • If the feature is working well for you, please share this in a comment below or leave a 👍 on this issue.

If you have any questions, don't hesitate to ask them in the #prisma2 channel in the Prisma Slack.

Activity

  1. sdnts commented on Jul 23, 2020

    @sdnts

    Love the API, this will be so useful to build tooling! ❤️

    Although most params in the middleware are self-explanatory, I was wondering what the dataPath & headers params stand for? (Perhaps we should add some documentation for these params?)

    For reference, this is my middleware:

    prisma.use(async (params, next) => {
      return {
        params,
        response: await next(params),
      };
    });
    

    and here are all params logged:

    params: {
        args: { include: [Object] },
        dataPath: [],
        runInTransaction: false,
        action: 'findMany',
        model: 'User',
        clientMethod: 'user.findMany',
        callsite: 'Error: \n' +
          '    at NewPrismaClient._getCallsite (/Users/siddhant/Code/Tests/experiments/node_modules/@prisma/client/src/runtime/getPrismaClient.ts:545:27)\n' +
          '    at Object.dataPath [as User] (/Users/siddhant/Code/Tests/experiments/node_modules/@prisma/client/src/runtime/getPrismaClient.ts:828:22)\n' +
          '    at Object.acc [as findMany] (/Users/siddhant/Code/Tests/experiments/node_modules/@prisma/client/src/runtime/getPrismaClient.ts:943:12)\n' +
          '    at Object.<anonymous> (/Users/siddhant/Code/Tests/experiments/index.ts:14:23)\n' +
          '    at step (/Users/siddhant/Code/Tests/experiments/index.ts:32:23)\n' +
          '    at Object.next (/Users/siddhant/Code/Tests/experiments/index.ts:13:53)\n' +
          '    at /Users/siddhant/Code/Tests/experiments/index.ts:7:71\n' +
          '    at new Promise (<anonymous>)\n' +
          '    at __awaiter (/Users/siddhant/Code/Tests/experiments/index.ts:3:12)\n' +
          '    at main (/Users/siddhant/Code/Tests/experiments/index.ts:12:14)',
        headers: undefined
      },
    
  2. sdnts commented on Jul 23, 2020

    @sdnts

    From Slack discussion:

    dataPath is for example when you do .user.findone().posts - then dataPath is ['posts']
    headers shouldn’t even be there but are there so you can change the headers for a request to the query engine - only relevant for debugging

  3. Sytten commented on Jul 23, 2020

    @Sytten

    Awesome feature. It would be very nice to have some tooling to compare the model against a type instead of a string to avoid mistakes when refactoring/renaming.

  4. added
    topic: previewFeatures
    on Jul 23, 2020
  5. julienp commented on Jul 26, 2020

    @julienp
  6. janpio commented on Jul 26, 2020

    @janpio
  7. abhiaiyer91 commented on Jul 27, 2020

    @abhiaiyer91
  8. janpio commented on Jul 27, 2020

    @janpio
  9. daniellwdb commented on Jul 31, 2020

    @daniellwdb

    The middleware is a nice feature, it would be nice if we could get the result for find queries and manipulate how data gets returned so that we can use the middleware for caching const someProp = redisClient.get(${params.args.where.id}:someProp);, if the key was found we could then prevent a database hit and return using data from the cache, otherwise we can run the query and save the result in the cache

  10. sdnts commented on Jul 31, 2020

    @sdnts

    @pandaniell You should be able to do that already if I understood you correctly. Here's an example:

    prisma.use(async (params, next) => {
      if (params.model === 'User') {
          return ' No users for you' // Or return something from your cache
      }
      const actualResponse = await next(params)
      // Cache this response
      return actualResponse
    })
    

    Essentially, requests go out only if you await the next function. Give that a shot?

  11. daniellwdb commented on Jul 31, 2020

    @daniellwdb

    @madebysid Thank you, I didn't think of trying to return something else other than next, working example using Redis

    prismaClient.use(async (params, next) => {
      if (
        params.model === "GuildSettings" &&
        params.action === "findOne" &&
        params.args.select.prefix
      ) {
        const prefix = await getAsync(`${params.args.where.id}:prefix`);
    
        if (prefix) {
          return { prefix };
        }
      }
    
      const queryResult = await next(params);
    
      redisClient.set(
        `${params.args.where.id}:prefix`,
        queryResult.prefix,
        "EX",
        10
      );
    
      return queryResult;
    });
  12. sdnts commented on Jul 31, 2020

    @sdnts

    Nice! You should wrap it up in a usable package and add it here if you'd like: https://github.com/catalinmiron/awesome-prisma :D

  13. daniellwdb commented on Aug 1, 2020

    @daniellwdb

    Would be a nice alternative to #3165 - unfortunately I lack the knowledge to make that work but maybe somebody else is interested

  14. janpio commented on Aug 1, 2020

    @janpio
    Contributor

    Open a new issue @pandaniell for "query caching middleware" and we can discuss if this might already be possible, what is missing and so on.

  15. abhiaiyer91 commented on Aug 1, 2020

    @abhiaiyer91
  16. beeme1mr commented on Aug 12, 2020

    @beeme1mr

    Is it possible to see the query Prisma generated in the middleware? I would like to use this for monitoring purposes.

  17. janpio commented on Aug 12, 2020

    @janpio
    Contributor

    What exactly do you mean @beeme1mr? The actual SQL queries are one level lower in the Prisma Engine that is executed by the Prisma Client. What you can see in the middleware is the Prisma Client query, which is sent over to the Prisma Engine for generation of SQL query(/ies).

    To see the actual SQL queries, you can use logging: https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/logging Via the event based logging, that can be used in a similar way as the middleware: https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/logging#event-based-logging

  18. beeme1mr commented on Aug 12, 2020

    @beeme1mr

    @janpio thanks for the info on the logger. That will probably work for now.

    It would be nice get the actual SQL query in the middleware though. Could I hook into the Prisma Engine to achieve this? Possibly manually construct a Document using middleware params.

  19. pantharshit00 commented on Aug 12, 2020

    @pantharshit00
    Contributor

    Could I hook into the Prisma Engine to achieve this?

    I am not sure that would be possible as engine also combines queries in a batch if they are executed in the same time frame to improve performance. Also, I don't think the client receive a document of generated query, right now when logging is enabled, the engine splits the log on the stdout.

    Do you have any specific usecase in mind which you can't do with the current query logging system?

  20. janpio commented on Aug 13, 2020

    @janpio
    Contributor

    Could I hook into the Prisma Engine to achieve this?

    This is exactly what using the logger is doing. You can do whatever you want with these events that are triggered.

  21. nikolasburk commented on Aug 18, 2020

    @nikolasburk
    ContributorAuthor

    Thanks a lot for all of your feedback, we've just promoted this future into the latest stable 2.5.0 release 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: middlewaretopic: previewFeatures

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions