fix(NocoDB Node): Fix for updating or deleting rows with not default primary keys

This commit is contained in:
Michael Kret 2023-04-12 16:27:19 +03:00 committed by GitHub
parent e79679c023
commit ee7f86394e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 42 deletions

View file

@ -1,4 +1,9 @@
import type { IAuthenticateGeneric, ICredentialType, INodeProperties } from 'n8n-workflow';
import type {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
export class NocoDbApiToken implements ICredentialType {
name = 'nocoDbApiToken';
@ -31,4 +36,11 @@ export class NocoDbApiToken implements ICredentialType {
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '={{ $credentials.host }}',
url: '/api/v1/auth/user/me',
},
};
}

View file

@ -328,17 +328,24 @@ export class NocoDB implements INodeType {
if (operation === 'delete') {
requestMethod = 'DELETE';
let primaryKey = 'id';
if (version === 1) {
endPoint = `/nc/${projectId}/api/v1/${table}/bulk`;
} else if (version === 2) {
endPoint = `/api/v1/db/data/bulk/noco/${projectId}/${table}`;
primaryKey = this.getNodeParameter('primaryKey', 0) as string;
if (primaryKey === 'custom') {
primaryKey = this.getNodeParameter('customPrimaryKey', 0) as string;
}
}
const body: IDataObject[] = [];
for (let i = 0; i < items.length; i++) {
const id = this.getNodeParameter('id', i) as string;
body.push({ id });
body.push({ [primaryKey]: id });
}
try {
@ -532,18 +539,25 @@ export class NocoDB implements INodeType {
if (operation === 'update') {
requestMethod = 'PATCH';
let primaryKey = 'id';
if (version === 1) {
endPoint = `/nc/${projectId}/api/v1/${table}/bulk`;
requestMethod = 'PUT';
} else if (version === 2) {
endPoint = `/api/v1/db/data/bulk/noco/${projectId}/${table}`;
primaryKey = this.getNodeParameter('primaryKey', 0) as string;
if (primaryKey === 'custom') {
primaryKey = this.getNodeParameter('customPrimaryKey', 0) as string;
}
}
const body: IDataObject[] = [];
for (let i = 0; i < items.length; i++) {
const id = this.getNodeParameter('id', i) as string;
const newItem: IDataObject = { id };
const newItem: IDataObject = { [primaryKey]: id };
const dataToSend = this.getNodeParameter('dataToSend', i) as
| 'defineBelow'
| 'autoMapInputData';

View file

@ -65,22 +65,65 @@ export const operationFields: INodeProperties[] = [
required: true,
description: 'The name of the table',
},
{
displayName: 'Primary Key Type',
name: 'primaryKey',
type: 'options',
default: 'id',
options: [
{
name: 'Default',
value: 'id',
description:
'Default, added when table was created from UI by those options: Create new table / Import from Excel / Import from CSV',
},
{
name: 'Imported From Airtable',
value: 'ncRecordId',
description: 'Select if table was imported from Airtable',
},
{
name: 'Custom',
value: 'custom',
description:
'When connecting to existing external database as existing primary key field is retained as is, enter the name of the primary key field below',
},
],
displayOptions: {
show: {
operation: ['delete', 'update'],
},
},
},
{
displayName: 'Field Name',
name: 'customPrimaryKey',
type: 'string',
default: '',
displayOptions: {
show: {
operation: ['delete', 'update'],
primaryKey: ['custom'],
},
},
},
{
displayName: 'Row ID Value',
name: 'id',
type: 'string',
default: '',
required: true,
description: 'The value of the ID field',
displayOptions: {
show: {
operation: ['delete', 'get', 'update'],
},
},
},
// ----------------------------------
// delete
// ----------------------------------
{
displayName: 'Row ID',
name: 'id',
type: 'string',
displayOptions: {
show: {
operation: ['delete'],
},
},
default: '',
required: true,
description: 'ID of the row to delete',
},
// ----------------------------------
// getAll
// ----------------------------------
@ -222,19 +265,6 @@ export const operationFields: INodeProperties[] = [
// ----------------------------------
// get
// ----------------------------------
{
displayName: 'Row ID',
name: 'id',
type: 'string',
displayOptions: {
show: {
operation: ['get'],
},
},
default: '',
required: true,
description: 'ID of the row to return',
},
{
displayName: 'Download Attachments',
name: 'downloadAttachments',
@ -265,19 +295,6 @@ export const operationFields: INodeProperties[] = [
// ----------------------------------
// update
// ----------------------------------
{
displayName: 'Row ID',
name: 'id',
type: 'string',
displayOptions: {
show: {
operation: ['update'],
},
},
default: '',
required: true,
description: 'ID of the row to update',
},
// ----------------------------------
// Shared
// ----------------------------------