Subscribe

How to perform non-updating upserts in Prisma

✍️

Master Prisma's upsert for databases: Create or update without ID - ideal for API queries and filters.

5 Feb, 2024 · 2 min read

In our example yesterday, we were adding playlist data to our Postgres database.

If we were to add a playlist twice, however, we would notice that the URI is not unique any more and a record with such a URI already exists.

What is an upsert

Typically we could handle this with an upsert operation.

An “upsert” in Prisma is a combination of “update” and “insert”.

An upsert allows us to update an existing record if it’s found, or insert a new record if it’s not found, based on a unique identifier, such as the playlist URI in our example.

But for this example, I want to do a non-updating upsert. 👀

Non-updating upsert

The goal is to create a new record if it does not exist already, without updating any existing records if they do exist.

Essentially, it’s an “insert if not exists” operation without the update part.

For our scenario this means we check if the URI already exists and then we act as if we perform an update.

If there is not record with the URI, we perform the creation.

Important: The caveat here is that we push an empty update object, which will result in the API returning the old existing object without updating it.

How to upsert in Prisma

You can use the upsert command to perform an upsert query in Prisma.

The Prisma upsert command takes a where-query.

Where-queries should query a unique field in the DB. Here we upsert without an ID per se, because the URI is good enough as an identifier.

The Prisma upsert command comes with both the update and creates functions, like so:

const playlist = await prisma.playlist.upsert({
  where: {
    uri: uri,
  },
  update: {},
  create: playlistItem,
});

And there you go. This gives us a super good solution to create records if not already existent.

You can find the complete Prisma example based on the Spotify database on GitHub.

Thank you for reading, and let’s connect!

Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next 📖

Adding Prisma to Remix

26 Apr, 2022 · 2 min read

Adding Prisma to Remix

CRUD operations with Prisma and Fastify

19 Jan, 2022 · 6 min read

CRUD operations with Prisma and Fastify

Join 2099 devs and subscribe to my newsletter