question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide `upsertMany`/ `upsertFirst` operation in the client API

See original GitHub issue

Problem

I’m frustrated when I want to do an upsert of a specific row, but I don’t have fields that are in <Model>WhereUniqueInput - I only have fields that, according to my business logic, will find a unique row. Currently, I have to do a findFirst to find the ID of the row, then either a create if it doesn’t exist or an update if it does.

Suggested solution

Add a CRUD query upsertMany. If the where clause finds at least one row, then an update is performed, else, a create is performed.

export type UserUpsertManyArgs = {
  select?: UserSelect | null
  include?: UserInclude | null
  where?: UserWhereInput | null
  create: UserCreateInput
  update: UserUpdateInput
}

I’m not sure what it should return, some possibilities are:

  • the number of updated objects (probably 0 if a create is done); in which case don’t have the select or include parameters
  • a list of the updated objects, or the object itself if a create is done
  • the number of updated objects, or the object itself if a create is done

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:225
  • Comments:26

github_iconTop GitHub Comments

25reactions
MattGsoncommented, Mar 20, 2021

I would suggest that upsertMany should be reserved for a function that can bulk upsert multiple rows at once so the name of this operation should probably be something like upsertFirst.

9reactions
Lizhoohcommented, Aug 18, 2022

Currently using sql instead

$prisma.$executeRawUnsafe`
  insert into user(id, name, list) values
    ('abc1', 'xxxx', '["a","b"]'),
    ('abc2', 'abc2', '[1,2]')
  on duplicate key update
    name = values(name),
    list = values(list)
`
Read more comments on GitHub >

github_iconTop Results From Across the Web

createEntityAdapter
A function that generates a set of prebuilt reducers and selectors for performing CRUD operations on a normalized state structure containing ...
Read more >
ふむ。prismaはupsertManyみたいなのはまだないぽい
ふむ。prismaはupsertManyみたいなのはまだないぽい. Превод на туита. github.com. Provide `upsertMany`/ `upsertFirst` operation in the client API · Issue #4134 ...
Read more >
CRUD (Reference)
This page describes how to perform CRUD operations with your generated Prisma Client API. CRUD is an acronym that stands for: Create; Read;...
Read more >
Untitled
Learn how to use python api pymongo. Unordered bulk write operations are batched and sent to the server in arbitrary order where they...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found