drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: onConflictDoUpdate() set many

Open
#1,728 36 comments 121 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe what you want

This is a follow-up of a [discussion on discord](https://discord.com/channels/1043890932593987624/1099708311030464583/1190259252288299009)

The idea is that, when inserting an array of object values, it's a bit complex to ask for an update for value which cause a conflict. I came up with something like the following

```ts

const values: CrmCompanies[] = ... ;

await db
.insert(crmCompanies)
.values(values)
.onConflictDoUpdate({
target: crmCompanies.id,
set: Object.assign(
{},
...Object.keys(values[0])
.filter((k) => k !== "id")
.map((k) => ({ [k]: sql`excluded.${k}` })),
) as Partial,
});
```

As you can see, the syntax is not very easy to come up with, and we **lose type** information. Ideally, I would rather write something such as (not necessarily that exact syntax of course):

```ts
const values: CrmCompanies[] = ... ;

await db
.insert(crmCompanies)
.values(values)
.onConflictDoUpdate({
target: crmCompanies.id,
set: values,
});
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.