-
-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): allow fine-grained control over
em.upsert()
(#4669)
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
- Loading branch information
Showing
15 changed files
with
1,352 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { EntityData, EntityMetadata } from '../typings'; | ||
import type { UpsertOptions } from '../drivers/IDatabaseDriver'; | ||
|
||
/** @internal */ | ||
export function getOnConflictFields<T>(data: EntityData<T>, uniqueFields: (keyof T)[], options: UpsertOptions<T>): (keyof T)[] { | ||
if (options.onConflictMergeFields) { | ||
return options.onConflictMergeFields; | ||
} | ||
|
||
const keys = Object.keys(data).filter(f => !uniqueFields.includes(f as keyof T)) as (keyof T)[]; | ||
|
||
if (options.onConflictExcludeFields) { | ||
return keys.filter(f => !options.onConflictExcludeFields!.includes(f)); | ||
} | ||
|
||
return keys; | ||
} | ||
|
||
/** @internal */ | ||
export function getOnConflictReturningFields<T>(meta: EntityMetadata<T> | undefined, data: EntityData<T>, uniqueFields: (keyof T)[], options: UpsertOptions<T>): (keyof T)[] | '*' { | ||
if (!meta) { | ||
return '*'; | ||
} | ||
|
||
const keys = meta.comparableProps.filter(p => !p.lazy && !p.embeddable && !uniqueFields.includes(p.name)).map(p => p.name) as (keyof T)[]; | ||
|
||
if (options.onConflictAction === 'ignore') { | ||
return keys; | ||
} | ||
|
||
if (options.onConflictMergeFields) { | ||
return keys.filter(key => !options.onConflictMergeFields!.includes(key)); | ||
} | ||
|
||
if (options.onConflictExcludeFields) { | ||
return [...new Set(keys.concat(...options.onConflictExcludeFields))]; | ||
} | ||
|
||
return keys.filter(key => !(key in data)); | ||
} |
Oops, something went wrong.