Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying which fields to exclude from update in upsert/upsertMany functions #4325

Closed
squivix opened this issue May 5, 2023 · 3 comments · Fixed by #4669
Closed
Labels
enhancement New feature or request

Comments

@squivix
Copy link

squivix commented May 5, 2023

Is your feature request related to a problem? Please describe.
Currently, when using the entity manager's em.upsert or em.upsertMany functions to upsert data, all fields other than the unique ones are included in the update. However, sometimes we need to keep some of the fields as they are if the row already exists, but insert a new value for them if it doesn't.

Describe the solution you'd like
I would like to be able to specify which fields to exclude from the on conflict update clause. Maybewe can add an excludeFieldsOnUpdate option in the NativeInsertUpdateOptions which is an array of field names that should be kept as is if the row already exists, but inserted with new values if it doesn't.

The proposed option looks like:

const author = await em.upsertMany(Author, [
  { email: 'foo@bar.com', name:"Foo Bar", age: 33 },
  { email: 'lol@lol.lol', name:"John Doe", age: 666 },
], {excludeFieldsOnUpdate:["name"]});

Describe alternatives you've considered
Some alternatives are to use the query builder or a raw SQL query instead of the ORM's em.upsert or em.upsertMany functions, but it would be nice to have the option through the EntityManager.

Additional context
This is based on discussion #4317

@squivix squivix added the enhancement New feature or request label May 5, 2023
@jsprw
Copy link
Contributor

jsprw commented May 21, 2023

Have you worked on this yet? I'm also interested in this feature and will be able to work on this.

@squivix
Copy link
Author

squivix commented May 21, 2023

Have you worked on this yet? I'm also interested in this feature and will be able to work on this.

I 'm struggling to set up the repo and get it to run as I'm not very familiar with docker. Also currently busy with some other stuff. Feel free to work on it. I will probably try again in about a week

@B4nan
Copy link
Member

B4nan commented Aug 29, 2023

FYI #4602 (comment)

B4nan added a commit that referenced this issue Sep 8, 2023
Adds 4 new options to `em.upsert()`:

- `onConflictFields?: (keyof T)[]` to control the conflict clause
- `onConflictAction?: 'ignore' | 'merge'` used ignore and merge as that
is how the QB methods are called
- `onConflictMergeFields?: (keyof T)[]` to control the merge clause
- `onConflictExcludeFields?: (keyof T)[]` to omit fields from the merge
clause

Closes #4325
Closes #4602
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
3 participants