Middleware

This page will provide an explanation on how to use middleware exported by Project Toolchain. These middleware are automatically generated by Prisma Util at schema generation time.

Versioning History

API Version v2.0.0 - Introduced prisma-util/toolchain/middleware export.

To use middleware generated by Prisma Util, you just have to import them like this:

Import Statement
import { desiredImports } from "prisma-util/toolchain/middleware/[FEATURE_NAME]";

The [FEATURE_NAME] section is replaced by the name of the feature that you want to import.

The list of available imports is dynamic and based on what features you've enabled in your configuration file. All features that make use of Project Toolchain's Middleware API will display a notice on their page and the name of their exports.

For standardization purposes, all features making use of Project Toolchain's Middleware API will follow these naming conventions to ensure a smoother developer experience:

  • [FEATURE_NAME] will always be lowercase and spaces will be replaced with hyphens.

  • Based on the number of middleware, the feature will export:

    • If there is only one middleware for this feature, it will be the default export for prisma-util/toolchain/middleware/[FEATURE_NAME]/index.js.

    • If there are more than two middleware for this feature, they will be exported individually and the default export for this file will be an ExportedMiddlewareBase object.

Types & Definitions

ExportedMiddlewareBase

This type represents an object with the following TypeScript definition:

ExportedMiddlewareBase
type ExportedMiddlewareBase = {
    all: (prisma: PrismaClient) => PrismaClient,
    [middlewareName: string]: (prisma: PrismaClient) => Prisma.Middleware
}

The key of this object will be the middleware name (this is set by each feature that makes use of Project Toolchain's Middleware API and documented on the specific page of said feature) and the value will be a function. This function takes your PrismaClient as a parameter, so it can initialize all the values required and will return a Middleware that you can use in PrismaClient's $use function.

This type also has a function (add) that will add all of the middleware for this feature to your PrismaClient and return the provided instance of this client.

Last updated