feat(core): Use filters for data table upsert (no-changelog) (#19292)

This commit is contained in:
Daria
2025-09-08 14:41:09 +03:00
committed by GitHub
parent 70254526bb
commit 9b06648edc
17 changed files with 218 additions and 609 deletions

View File

@@ -37,7 +37,7 @@ export async function execute(
throw new NodeOperationError(this.getNode(), 'At least one condition is required');
}
const updatedRows = await dataStoreProxy.updateRows({
const updatedRows = await dataStoreProxy.updateRow({
data: row,
filter,
});

View File

@@ -7,7 +7,7 @@ import {
} from 'n8n-workflow';
import { makeAddRow, getAddRow } from '../../common/addRow';
import { executeSelectMany, getSelectFields } from '../../common/selectMany';
import { getSelectFields, getSelectFilter } from '../../common/selectMany';
import { getDataTableProxyExecute } from '../../common/utils';
export const FIELD: string = 'upsert';
@@ -32,35 +32,16 @@ export async function execute(
const row = getAddRow(this, index);
const matches = await executeSelectMany(this, index, dataStoreProxy, true);
const filter = getSelectFilter(this, index);
// insert
if (matches.length === 0) {
const result = await dataStoreProxy.insertRows([row]);
return result.map((json) => ({ json }));
if (filter.filters.length === 0) {
throw new NodeOperationError(this.getNode(), 'At least one condition is required');
}
// update
const result = [];
for (const match of matches) {
const updatedRows = await dataStoreProxy.updateRows({
data: row,
filter: {
type: 'and',
filters: [{ columnName: 'id', condition: 'eq', value: match.json.id }],
},
});
if (updatedRows.length !== 1) {
throw new NodeOperationError(this.getNode(), 'invariant broken');
}
// The input object gets updated in the api call, somehow
// And providing this column to the backend causes an unexpected column error
// So let's just re-delete the field until we have a more aligned API
delete row['updatedAt'];
result.push(updatedRows[0]);
}
const result = await dataStoreProxy.upsertRow({
data: row,
filter,
});
return result.map((json) => ({ json }));
}