Util for calling Prisma middleware for nested write operations.

Overview

Prisma Nested Middleware

Util for calling Prisma middleware for nested write operations.

Existing Prisma middleware is called once for every operation, but due to some operations containing nested writes it can become complex to ensure middleware is applied in all scenarios. See the existing issue regarding nested middleware for more information.


Build Status version MIT License semantic-release PRs Welcome

Table of Contents

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies:

npm install --save prisma-nested-middleware

Usage

Pass a middleware function to createNestedMiddleware, the returned middleware can be passed to Prisma client's $use method:

import { createNestedMiddleware } from 'prisma-nested-middleware'

client.$use(createNestedMiddleware(async (params, next) => {
  // update params here
  const result = await next(params)
  // update result here
  return result;
));

The middleware function passed to createNestedMiddleware is called for every nested write operation.

There are some differences to note when using nested middleware:

  • the list of actions that might be in params is expanded to include connectOrCreate
  • If a relation is not included using include then that middleware's next function will resolve with undefined.
  • The parent operation's params have been added to the params of nested middleware as a scope object. This is useful when the parent is relevant, for example when handling a connectOrCreate and you need to know the parent being connected to.
  • when handling a nested create action params.args does not include a data field, that must be handled manually. You can use the existence of params.scope to know when to handle a nested create.

It is helpful to walk through the lifecycle of an operation:

For the following update

client.country.update({
  where: { id: 'imagination-land' },
  data: {
    nationalDish: {
      update: {
        where: { id: 'stardust-pie' },
        data: {
          keyIngredient: {
            connectOrCreate: {
              create: { name: 'Stardust' },
              connect: { id: 'stardust' },
            },
          },
        },
      },
    },
  },
});

createNestedMiddleware calls the passed middleware function with params in the following order:

  1. { model: 'Recipe', action: 'update', args: { where: { id: 'stardust-pie' }, data: {...} } }
  2. { model: 'Food', action: 'connectOrCreate', args: { create: {...}, connect: {...} } }
  3. { model: 'Country', action: 'update', args: { where: { id: 'imagination-land', data: {...} } }

Once all of the nested middleware have passed params to next the params for the Country model are updated with any changes made; these params are then passed to the Country model's next function.

When the Country model's next resolves the next of the nested middleware are also resolved with the slice relevent to them. So the middleware called for the Recipe model resolves with the nationalDish, the middleware for the Food resolves with the keyIngredient.

Finally the return values from the nested middleware are used to modify the Country result in a similar way to params, and that modified object is the one client.country.update resolves with.

If any middleware throws an error then client.country.update will throw with that error.

LICENSE

Apache 2.0

You might also like...

Prisma 2+ generator to emit Joi schemas from your Prisma schema

Prisma 2+ generator to emit Joi schemas from your Prisma schema

Prisma Joi Generator Automatically generate Joi schemas from your Prisma Schema, and use them to validate your API endpoints or any other use you have

Dec 24, 2022

Prisma +2 generator to emit a tRPC shield from your Prisma schema

Prisma +2 generator to emit a tRPC shield from your Prisma schema

Prisma tRPC Shield Generator Automatically generate a tRPC Shield from your Prisma Schema. Updates every time npx prisma generate runs. Table of Conte

Dec 24, 2022

Prisma 2+ generator to emit Zod schemas from your Prisma schema

Prisma 2+ generator to emit Zod schemas from your Prisma schema

Prisma Zod Generator Automatically generate Zod schemas from your Prisma Schema, and use them to validate your API endpoints or any other use you have

Dec 27, 2022

This is a library to alternate and self-host the Prisma Data Proxy (cloud.prisma.io)

This is a library to alternate and self-host the Prisma Data Proxy (cloud.prisma.io)

Alternative Prisma Data Proxy This is a library to alternate and self-host the Prisma Data Proxy (cloud.prisma.io). In order to deploy your project to

Dec 28, 2022

A microservice for calling ethereum APIs.

Ether Goblin A microservice for calling ethereum APIs. Features RESTFul APIs for the Ethereum Blockchain and its ecosystem Microservice Run in Docker

Nov 15, 2022

A User Interface for calling a program's instructions

Anchor Test UI Testing Anchor programs can be lenghty and overwelming So we thought of a Cool Way of Testing Anchor program without writing an Testing

Sep 3, 2022

Reference for How to Write an Open Source JavaScript Library - https://egghead.io/series/how-to-write-an-open-source-javascript-library

Reference for How to Write an Open Source JavaScript Library The purpose of this document is to serve as a reference for: How to Write an Open Source

Dec 24, 2022

Learn GraphQL by building a blogging engine. Create resolvers, write schemas, write queries, design the database, test and also deploy.

GraphQL Blog graphqlblog.com Learn GraphQL by building a blogging engine. Create resolvers, write schemas, write queries, design the database, test an

Aug 17, 2022

This package is an open source extension for MikroORM, which enables Nested Set Tree for your needs

MikroORM nested set This package is an open source extension for MikroORM, which enables Nested Set Tree for your needs Disclaimer For now, this packa

Dec 15, 2022
Releases(v1.0.0)
Owner
Olivier Wilkinson
Olivier Wilkinson
A NestJS module that allows you use Prisma, set up multiple Prisma services, and use multi-tenancy in each Prisma service.

NestJS Prisma Module Installation To use this package, first install it: npm i @sabinthedev/nestjs-prisma Basic Usage In order to use this package, yo

Sabin Adams 39 Dec 2, 2022
A string of four operations of the library, can solve the js digital calculation accuracy of scientific notation and formatting problems, support for thousands of decimal point formatting output operations

A string of four operations of the library, can solve the js digital calculation accuracy of scientific notation and formatting problems, support for thousands of decimal point formatting output operations

null 10 Apr 6, 2022
A util for getting data and metadata for all markdown files in a given dir. Useful for building static site generators

extract-md-data A util for getting data and metadata for all markdown files in a given dir. Useful for building static site generators. Usage Given th

Claire Froelich 2 Jan 6, 2022
JS/TS lightweight value-multimethod util

fp-multik ????‍♂️ Small functional utility for control flow and conditional operator for functions. Multik is value-based multimethod for Javascript/T

Kalagin Ivan 5 Dec 25, 2022
Util for kafkajs to buffer messages and send them in batches, inspired by node-rdkafka

kafkjajs-buffer Plugin for kafkajs to buffer messages and send them in batches, inspired by node-rdkafka Overview kafkajs-buffer adds queue/buffer cap

Alberto Juan 7 Sep 7, 2022
A lightweight jQuery Ajax util library.

Just Wait Wait what? The server response. Just Wait is a lightweight jQuery utility that allows you to specify a function to be executed after a speci

Omar Muscatello 2 Jun 22, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Prisma +2 generator to emit Yup schemas from your Prisma schema

Prisma Yup Generator Automatically generate Yup schemas from your Prisma Schema, and use them to validate your API endpoints or any other use you have

Omar Dulaimi 31 Dec 24, 2022