Upsert in CreateMany - fixes (#13598)
fixes https://github.com/twentyhq/twenty/issues/13553 and https://github.com/twentyhq/core-team-issues/issues/1300
This commit is contained in:
+12
-19
@@ -89,19 +89,12 @@ export class GraphqlQueryCreateManyResolverService extends GraphqlQueryBaseResol
|
||||
): Promise<InsertResult> {
|
||||
const { objectMetadataItemWithFieldMaps } = executionArgs.options;
|
||||
|
||||
const selectedColumns = buildColumnsToSelect({
|
||||
select: executionArgs.graphqlQuerySelectedFieldsResult.select,
|
||||
relations: executionArgs.graphqlQuerySelectedFieldsResult.relations,
|
||||
objectMetadataItemWithFieldMaps,
|
||||
});
|
||||
|
||||
const conflictingFields = this.getConflictingFields(
|
||||
objectMetadataItemWithFieldMaps,
|
||||
);
|
||||
const existingRecords = await this.findExistingRecords(
|
||||
executionArgs,
|
||||
conflictingFields,
|
||||
selectedColumns,
|
||||
);
|
||||
|
||||
const { recordsToUpdate, recordsToInsert } = this.categorizeRecords(
|
||||
@@ -187,7 +180,6 @@ export class GraphqlQueryCreateManyResolverService extends GraphqlQueryBaseResol
|
||||
fullPath: string;
|
||||
column: string;
|
||||
}[],
|
||||
selectedColumns: Record<string, boolean>,
|
||||
): Promise<Partial<ObjectRecord>[]> {
|
||||
const { objectMetadataItemWithFieldMaps } = executionArgs.options;
|
||||
const queryBuilder = executionArgs.repository.createQueryBuilder(
|
||||
@@ -203,12 +195,7 @@ export class GraphqlQueryCreateManyResolverService extends GraphqlQueryBaseResol
|
||||
queryBuilder.orWhere(condition);
|
||||
});
|
||||
|
||||
return await queryBuilder
|
||||
.setFindOptions({
|
||||
select: selectedColumns,
|
||||
})
|
||||
.withDeleted()
|
||||
.getMany();
|
||||
return await queryBuilder.withDeleted().getMany();
|
||||
}
|
||||
|
||||
private getValueFromPath(
|
||||
@@ -271,11 +258,17 @@ export class GraphqlQueryCreateManyResolverService extends GraphqlQueryBaseResol
|
||||
for (const field of conflictingFields) {
|
||||
const requestFieldValue = this.getValueFromPath(record, field.fullPath);
|
||||
|
||||
const existingRec = existingRecords.find(
|
||||
(existingRecord) =>
|
||||
isDefined(existingRecord[field.column]) &&
|
||||
existingRecord[field.column] === requestFieldValue,
|
||||
);
|
||||
const existingRec = existingRecords.find((existingRecord) => {
|
||||
const existingFieldValue = this.getValueFromPath(
|
||||
existingRecord,
|
||||
field.fullPath,
|
||||
);
|
||||
|
||||
return (
|
||||
isDefined(existingFieldValue) &&
|
||||
existingFieldValue === requestFieldValue
|
||||
);
|
||||
});
|
||||
|
||||
if (existingRec) {
|
||||
existingRecord = { ...record, id: existingRec.id };
|
||||
|
||||
+9
-3
@@ -5,12 +5,18 @@ import { WorkspaceQueryRunnerOptions } from 'src/engine/api/graphql/workspace-qu
|
||||
|
||||
import { UserInputError } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
|
||||
|
||||
interface PostgreSQLError extends QueryFailedError {
|
||||
detail?: string;
|
||||
}
|
||||
|
||||
export const handleDuplicateKeyError = (
|
||||
error: QueryFailedError,
|
||||
error: PostgreSQLError,
|
||||
context: WorkspaceQueryRunnerOptions,
|
||||
) => {
|
||||
const indexNameMatch = error.message.match(/"([^"]+)"/);
|
||||
|
||||
const duplicatedValues = error?.detail?.match(/=\(([^)]+)\)/)?.[1];
|
||||
|
||||
if (indexNameMatch) {
|
||||
const indexName = indexNameMatch[1];
|
||||
|
||||
@@ -42,9 +48,9 @@ export const handleDuplicateKeyError = (
|
||||
|
||||
if (affectedColumns?.length === 1) {
|
||||
throw new UserInputError(
|
||||
`Duplicate ${columnNames}. Please set a unique one.`,
|
||||
`Duplicate ${columnNames} ${duplicatedValues ? `with value ${duplicatedValues}` : ''}. Please set a unique one.`,
|
||||
{
|
||||
userFriendlyMessage: `This ${columnNames.toLowerCase()} is already taken. Please choose a different value.`,
|
||||
userFriendlyMessage: `This ${columnNames.toLowerCase()} ${duplicatedValues ? `with value ${duplicatedValues}` : ''} is already taken. Please choose a different value.`,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user