Support passing custom metadata per query to middleware
See original GitHub issueProblem
Currently the middleware are essentially stateless. There is one example that uses global state but in practice this is not very common nor desirable. It would be necessary for a tracing middleware.
Suggested solution
It would be nice if all prisma calls add a context parameter that would be passed down to middlewares. It could look like:
export type TicketCreateArgs = {
select?: TicketSelect | null
include?: TicketInclude | null
data: XOR<TicketCreateInput, TicketUncheckedCreateInput>
context: Record<string, any>
}
export type MiddlewareParams = {
model?: ModelName
action: PrismaAction
args: any
dataPath: string[]
runInTransaction: boolean
context: Record<string, any>
}
Alternatives
No good alternative currently if you want to respect typing, unsure if I could pass the context in the data and remove it in the middleware, but I would prefer not too.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:30
- Comments:23 (11 by maintainers)
Top Results From Across the Web
Adding Custom Metadata for Oracle WebCenter Content Server
In the data model editor task pane, click Custom Metadata. · Oracle WebCenter Content Server stores metadata under a document profile. · Click...
Read more >API Reference: ApolloServer - Apollo GraphQL Docs
This article documents the ApolloServer class from the @apollo/server package. You can use the ApolloServer class to create an instance of Apollo Server ......
Read more >Custom Metadata Allocations and Usage Calculations
* Record size is based on the maximum field size of each field type, not the actual storage that's used in each field....
Read more >Metadata and Docs URLs - FastAPI
You can customize several metadata configurations in your FastAPI application. ... terms_of_service, str, A URL to the Terms of Service for the API....
Read more >How to extend the Express Request object in TypeScript
Prerequisites; Adding custom properties to the Request type ... query : this object contains a property for each query string parameter ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Any news about this feature? I would love it in order to cleanly implement audit trails using Prisma Middlewares 🙂
Hey folks, we started speccing this out this and we’re ready to share our proposal. Please let us know what you think! I’d also like to invite you to jump on a quick call with me to get your thoughts on the proposal.
Overview
Today Prisma users can’t selectively opt-in or out of middleware functionality.
If we allowed developers to turn on certain middleware per-query, we could make middleware more useful because the middleware would be able to make decisions based on the context the query was made.
Use Cases
Goals and Product Requirements
Out of Scope
Open Questions
Approach
Add support for a
meta
field that can to be passed through all of our query objects code. Thismeta
field would be accessible through the middleware stack:All APIs that can be modified by middleware would support this
meta
field:Type of basic meta field is defined in
Prisma
namespace as:Additionally, we define a meta type for all of the actions, for example:
Each action method signature will use it’s corresponding meta type.
This allows
meta
property to accept any data without type-checking. If users want to have a stricter type checking for the meta fields that make sense for their application, they have an option to augment provided interfaces in their codebases. For example, decrypt middleware can ensure strict type-checking fordecrypt
prop by declaring:If certain option makes sense only for specific action, users have an option to augment only this actions interface. For example, soft delete middleware could do:
References