Skip to content
On this page

Update

See prisma reference

Update a single record

The following query uses update to find and update a single User record by email:

js
const updateUser = await app.service('User').update({
  where: {
    email: 'viola@prisma.io',
  },
  data: {
    name: 'Viola the Magnificent',
  },
})

Result:

js
{
   "id": 43,
   "name": "Viola the Magnificent",
   "email": "viola@prisma.io",
   "profileViews": 0,
   "role": "USER",
   "coinflips": [],
}

Update multiple records

The following query uses updateMany to update all User records that contain prisma.io:

js
const updateUsers = await app.service('User').updateMany({
  where: {
    email: {
      contains: 'prisma.io',
    },
  },
  data: {
    role: 'ADMIN',
  },
})

Result:

js
{
   "count": 19
}

Update or create records

The following query uses upsert to update a User record with a specific email address, or create that User record if it does not exist:

js
const upsertUser = await app.service('User').upsert({
  where: {
    email: 'viola@prisma.io',
  },
  update: {
    name: 'Viola the Magnificent',
  },
  create: {
    email: 'viola@prisma.io',
    name: 'Viola the Magnificent',
  },
})

From version 4.6.0, Prisma carries out upserts with database native SQL commands where possible. Learn more.

Prisma does not have a findOrCreate query. You can use upsert as a workaround. To make upsert behave like a findOrCreate method, provide an empty update parameter to upsert.

A limitation to using upsert as a workaround for findOrCreate is that upsert will only accept unique model fields in the where condition. So it's not possible to use upsert to emulate findOrCreate if the where condition contains non-unique fields.

js
{
   "id": 43,
   "name": "Viola the Magnificent",
   "email": "viola@prisma.io",
   "profileViews": 0,
   "role": "ADMIN",
   "coinflips": [],
}

Update a number field

Use atomic number operations to update a number field based on its current value - for example, increment or multiply. The following query increments the views and likes fields by 1:

js
const updatePosts = await app.service('Post').updateMany({
  data: {
    views: {
      increment: 1,
    },
    likes: {
      increment: 1,
    },
  },
})