Prisma atomic operations encounter errors when attempting to update undefined values

According to the Prisma Typescript definition for atomic operations, we have:

export type IntFieldUpdateOperationsInput = {
  set?: number
  increment?: number
  decrement?: number
  multiply?: number
  divide?: number
}

Let's take a look at the Prisma Schema:

model SomeEntity {
  id           Int     @id @default(autoincrement())
  quantity     Int
}

We're dealing with a variable that could be either a number or undefined:

const someQuantity:number|undefined = undefined

Now, if we intend to update the 'quantity' field with this variable, our code might look like this:

const client = new PrismaClient();
client.someEntity.update({ where: { id: 1 }, data:{ quantity:{ increment: someQuantity }}})

If someQuantity===undefined, Prisma throws an Error:

[ERROR] 09:47:48 Error: Invalid `prisma.someEntity.update()` invocation:

This seems counterintuitive based on the type signature. We are using Prisma version 2.28.0 and a Postgres DB. Could there be a missing configuration or is it potentially a bug that needs reporting?

Answer №1

Prior to executing the increment operation, make sure to validate that someQuantity is of type "number". The TypeScript definition provided below clarifies the expected input:

export type IntFieldUpdateOperationsInput = {
  set?: number
  increment?: number
  decrement?: number
  multiply?: number
  divide?: number
}

In this scenario, the increment variable can only be either undefined or a number. If it is not undefined, then it must indeed have a value of number.

If the increment process is the sole action being performed, Prisma will generate an error if there are no other operations present. The error message received from

Invalid 'prisma.someEntity.update()' invocation
should provide insights into the reason behind the failure.

To resolve this issue, ensure that the update operation is carried out ONLY when increment holds a numerical value.

if (typeof someQuantity !== "number") {
  return;
}

// At this stage, `someQuantity` must be a number
const client = new PrismaClient();
client.someEntity.update({
    where: {
        id: 1
    },
    data: {
        quantity: {
            increment: someQuantity
        }
    }
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process of attaching a property to every object within an array using TypeScript?

In my search for adding a property to each object in an array, I came across a solution in AngularJs on Stack Overflow. However, I attempted the following approach which did not yield the desired outcome. Any assistance would be greatly appreciated. ex ...

Angular's HttpClient makes sure to wait for the HTTP requests to complete

Initializing arrays with the call this.Reload.All() is causing confusion and breaking the service due to multiple asynchronous calls. I am looking for a synchronous solution where each call waits for its response before proceeding to the next one. How can ...

Guide on transferring data from one table to another by choosing one or multiple rows with the help of Angular 2

https://i.sstatic.net/CF1Xo.pngHello, I'm currently facing an issue with my requirements. I'm attempting to display two tables. Data for Table One: TableOne =[ { "Id": 1, "Name": "ONLINE", "Status": false, "Track": false, }, ...

What steps should I take to resolve the issue of my Vite.ts/React website displaying as a blank white page on Github Pages?

Being new to React and Vite, I'm facing an issue where I see a blank white page when opening the link. Unlike traditional JavaScript, I am using TypeScript for this project, which could be the reason behind my problem. The project I created is a hang ...

Having trouble getting a Heroku deployment for a Node.js and PostgreSQL API to function properly

Lately, I've been focused on developing a node.js API with a PostgreSQL database for managing books. However, whenever I try to access the /books route, it leads me to the Heroku application error page. You can check out the API here. I followed this ...

Discovering a solution to extract a value from an Array of objects without explicitly referencing the key has proven to be quite challenging, as my extensive online research has failed to yield any similar or closely related problems

So I had this specific constant value const uniqueObjArr = [ { asdfgfjhjkl:"example 123" }, { qwertyuiop:"example 456" }, { zxcvbnmqwerty:"example 678" }, ] I aim to retrieve the ...

Is it possible for a function within a nodejs module to be defined but display as undefined upon access?

I am currently developing a Discord bot using NodeJS and TypeScript, and I'm facing an issue while trying to import custom modules in a loop with the following code: const eventFiles = fs.readdirSync("./src/events/").filter((file: string) =& ...

Encountering a "Module parse failed" error with type annotations in Nextjs while using Yarn Workspaces

I decided to experiment with transitioning a project from using Vite and React to Next.js and React. After reviewing the documentation on this page: https://nextjs.org/learn-pages-router/foundations/from-react-to-nextjs/getting-started-with-nextjs I made t ...

Deleting data in paranoid tables in Sequelize triggers cascading deletion

Currently, I am facing an issue with a relationship between two paranoid tables (User and Email). In the relationship configuration, I have set onDelete: 'cascade' option (Email.belongsTo(User, onDelete:'cascade')). The problem arises ...

Verify whether an object possesses all the attributes of a class in TypeScript

Within my typescript code, I have a class called abc: export class ABC{ public a : any; public b : any; public c? : any; public d? : any; } In one of my functions, I receive an input which is represented as data:any. My goal is to verify i ...

Encountering difficulties importing an NPM library into StackBlitz

Hey there, I'm currently attempting to replicate an Angular example online but am encountering issues importing my Tabulator Library in stackblitz. I keep receiving an error when trying to import it in the hello component. import Tabulator from &apo ...

Implementing TypeScript type definitions for decorator middleware strategies

Node middlewares across various frameworks are something I am currently pondering. These middlewares typically enhance a request or response object by adding properties that can be utilized by subsequent registered middlewares. However, a disadvantage of ...

What could be the reason for receiving an undefined value when trying to determine the size of the Set

Within one of my functions, I am encountering the following code: this.personService.getPersonInfo(this.personId).subscribe((res => { let response = res.body; let num = response.personList.size; ... })) Here is what the expe ...

Testing Angular components using mock HTML Document functionality is an important step in

Looking for help on testing a method in my component.ts. Here's the method: print(i) { (document.getElementById("iframe0) as any).contentWindow.print(); } I'm unsure how to mock an HTML document with an iframe in order to test this meth ...

Converting Typescript library into a standalone global JavaScript file

Currently working on developing a Typescript library that follows this structure: https://i.stack.imgur.com/YyCHk.jpg This includes the following files: restApi.class.ts import { restApiOptions } from '../models/rest.options.model'; import { ...

Enhancing Request JSON Body Validation in Next.js 14 API Handler

I'm currently in the process of developing an API for my iOS application using Next.js 14. I have managed to get it up and running successfully, however, I am facing some challenges when it comes to properly validating the body of a request. ESLint is ...

Incorporate Material Design Icons into your NPM Electron React application for sleek visuals

I am currently exploring how to incorporate Material Design Icons into an NPM Electron project with Webpack 4. The Google Github page suggests that the icons can be easily installed using npm install material-design-icons. However, based on this discussion ...

Define the state of an object using Parent and Children classes following the application of a filter

Within Angular 8, I am dealing with an Observable: let parents: Observable<Parent[]>; The classes Parent and Child are defined as follows: class Parent { id: number; name: string; children: Child[]; } class Child { id: number; name: str ...

Mastering the art of throwing and managing custom errors from the server to the client side within Next.js

I'm in the process of developing a Next.js application and I am faced with the challenge of transmitting customized error messages from the server to the client side while utilizing Next JS new server-side actions. Although my server-side code is func ...

Having trouble locating an Ionic module with your Ionic Web Builder?

Currently, I am working on building my Ionic app using their web build system. In my app.component.ts file, I have included the following import statement: import { File } from '@ionic-native/File/ngx'; Although this project compiles successful ...