Refactor to implement specifyIndexBy

This commit is contained in:
Iván Ovejero 2021-06-08 17:38:26 +02:00
parent 41643a72fd
commit 02ea0d3080
5 changed files with 211 additions and 183 deletions

View file

@ -4,6 +4,7 @@ import {
import { import {
IDataObject, IDataObject,
ILoadOptionsFunctions,
INodeExecutionData, INodeExecutionData,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
@ -70,6 +71,21 @@ export class Elasticsearch implements INodeType {
], ],
}; };
methods = {
loadOptions: {
async getDocuments(this: ILoadOptionsFunctions) {
const indexId = this.getNodeParameter('indexId', 0);
const responseData = await elasticsearchApiRequest.call(this, 'GET', `/${indexId}/_search`);
return responseData.hits.hits.map(({ _id }: { _id: string }) => ({ name: _id, value: _id }));
},
async getIndices(this: ILoadOptionsFunctions) {
const responseData = await elasticsearchApiRequest.call(this, 'GET', '/_aliases');
return Object.keys(responseData).map((index: string) => ({ name: index, value: index }));
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData(); const items = this.getInputData();
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];

View file

@ -8,6 +8,7 @@ import {
import { import {
IDataObject, IDataObject,
ILoadOptionsFunctions,
NodeApiError, NodeApiError,
} from 'n8n-workflow'; } from 'n8n-workflow';
@ -16,7 +17,7 @@ import {
} from './types'; } from './types';
export async function elasticsearchApiRequest( export async function elasticsearchApiRequest(
this: IExecuteFunctions, this: IExecuteFunctions | ILoadOptionsFunctions,
method: 'GET' | 'PUT' | 'POST' | 'DELETE', method: 'GET' | 'PUT' | 'POST' | 'DELETE',
endpoint: string, endpoint: string,
body: IDataObject = {}, body: IDataObject = {},
@ -51,8 +52,7 @@ export async function elasticsearchApiRequest(
} }
try { try {
// console.log(options); return await this.helpers.request!(options);
return await this.helpers.request(options);
} catch (error) { } catch (error) {
throw new NodeApiError(this.getNode(), error); throw new NodeApiError(this.getNode(), error);
} }

View file

@ -3,6 +3,13 @@ import {
} from 'n8n-workflow'; } from 'n8n-workflow';
import * as placeholders from './placeholders'; import * as placeholders from './placeholders';
import {
makeDocumentInputField,
makeDocumentListField,
makeIndexInputField,
makeIndexListField,
makeSpecifyIndexByField,
} from './SharedFields';
export const documentOperations = [ export const documentOperations = [
{ {
@ -44,85 +51,26 @@ export const documentOperations = [
] as INodeProperties[]; ] as INodeProperties[];
export const documentFields = [ export const documentFields = [
// ----------------------------------------
// shared fields
// ----------------------------------------
makeSpecifyIndexByField('document'),
// ---------------------------------------- // ----------------------------------------
// document: delete // document: delete
// ---------------------------------------- // ----------------------------------------
{ makeIndexListField('document', 'delete'),
displayName: 'Index ID', makeDocumentListField('document', 'delete'),
name: 'indexId', makeIndexInputField('document', 'delete'),
description: 'ID of the index containing the document to delete.', makeDocumentInputField('document', 'delete'),
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'delete',
],
},
},
},
{
displayName: 'Document ID',
name: 'documentId',
description: 'ID of the document to delete.',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'delete',
],
},
},
},
// ---------------------------------------- // ----------------------------------------
// document: get // document: get
// ---------------------------------------- // ----------------------------------------
{ makeIndexListField('document', 'get'),
displayName: 'Index ID', makeDocumentListField('document', 'get'),
name: 'indexId', makeIndexInputField('document', 'get'),
description: 'ID of the index containing the document to retrieve.', makeDocumentInputField('document', 'get'),
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'get',
],
},
},
},
{
displayName: 'Document ID',
name: 'documentId',
description: 'ID of the document to retrieve.',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'get',
],
},
},
},
{ {
displayName: 'Additional Fields', displayName: 'Additional Fields',
name: 'additionalFields', name: 'additionalFields',
@ -174,24 +122,8 @@ export const documentFields = [
// ---------------------------------------- // ----------------------------------------
// document: getAll // document: getAll
// ---------------------------------------- // ----------------------------------------
{ makeIndexListField('document', 'getAll'),
displayName: 'Index ID', makeIndexInputField('document', 'getAll'),
name: 'indexId',
description: 'ID of the index containing the documents to retrieve.',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'getAll',
],
},
},
},
{ {
displayName: 'Return All', displayName: 'Return All',
name: 'returnAll', name: 'returnAll',
@ -486,24 +418,8 @@ export const documentFields = [
// ---------------------------------------- // ----------------------------------------
// document: index // document: index
// ---------------------------------------- // ----------------------------------------
{ makeIndexListField('document', 'index'),
displayName: 'Index ID', makeIndexInputField('document', 'index'),
name: 'indexId',
description: 'ID of the index to add the document to.',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'index',
],
},
},
},
{ {
displayName: 'Document ID', displayName: 'Document ID',
name: 'documentId', name: 'documentId',
@ -581,42 +497,10 @@ export const documentFields = [
// ---------------------------------------- // ----------------------------------------
// document: update // document: update
// ---------------------------------------- // ----------------------------------------
{ makeIndexListField('document', 'update'),
displayName: 'Index ID', makeDocumentListField('document', 'update'),
name: 'indexId', makeIndexInputField('document', 'update'),
description: 'ID of the document to update.', makeDocumentInputField('document', 'update'),
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'update',
],
},
},
},
{
displayName: 'Document ID',
name: 'documentId',
description: 'ID of the document to update.',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'update',
],
},
},
},
{ {
displayName: 'Script', displayName: 'Script',
name: 'script', name: 'script',

View file

@ -3,6 +3,11 @@ import {
} from 'n8n-workflow'; } from 'n8n-workflow';
import * as placeholders from './placeholders'; import * as placeholders from './placeholders';
import {
makeIndexInputField,
makeIndexListField,
makeSpecifyIndexByField,
} from './SharedFields';
export const indexOperations = [ export const indexOperations = [
{ {
@ -40,6 +45,11 @@ export const indexOperations = [
] as INodeProperties[]; ] as INodeProperties[];
export const indexFields = [ export const indexFields = [
// ----------------------------------------
// shared fields
// ----------------------------------------
makeSpecifyIndexByField('index'),
// ---------------------------------------- // ----------------------------------------
// index: create // index: create
// ---------------------------------------- // ----------------------------------------
@ -145,46 +155,14 @@ export const indexFields = [
// ---------------------------------------- // ----------------------------------------
// index: delete // index: delete
// ---------------------------------------- // ----------------------------------------
{ makeIndexListField('index', 'delete'),
displayName: 'Index ID', makeIndexInputField('index', 'delete'),
name: 'indexId',
description: 'ID of the index to delete.',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'index',
],
operation: [
'delete',
],
},
},
},
// ---------------------------------------- // ----------------------------------------
// index: get // index: get
// ---------------------------------------- // ----------------------------------------
{ makeIndexListField('index', 'get'),
displayName: 'Index ID', makeIndexInputField('index', 'get'),
name: 'indexId',
description: 'ID of the index to retrieve.',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'index',
],
operation: [
'get',
],
},
},
},
{ {
displayName: 'Additional Fields', displayName: 'Additional Fields',
name: 'additionalFields', name: 'additionalFields',

View file

@ -0,0 +1,150 @@
export const makeSpecifyIndexByField = (resource: 'document' | 'index') => ({
displayName: 'Specify Index By',
name: 'specifyIndexBy',
description: 'Method for specifying the index.',
type: 'options',
required: true,
options: [
{
name: 'ID',
value: 'id',
},
{
name: 'List',
value: 'list',
},
],
default: 'list',
displayOptions: {
show: {
resource: [
resource,
],
operation: [
'delete',
'get',
'update',
'getAll',
'index',
],
},
},
});
export const makeIndexListField = (resource: 'document' | 'index', operation: string) => {
const operationVerb = operation === 'get' ? 'retrieve' : operation;
const description = resource === 'document'
? `Index containing the document to ${operationVerb}.`
: `Index to ${operationVerb}.`
return {
displayName: 'Index',
name: 'indexId',
description,
type: 'options',
required: true,
default: [],
typeOptions: {
loadOptionsMethod: 'getIndices',
},
displayOptions: {
show: {
resource: [
resource,
],
operation: [
operation,
],
specifyIndexBy: [
'list',
],
},
},
}
}
export const makeDocumentListField = (resource: 'document' | 'index', operation: string) => ({
displayName: 'Document',
name: 'documentId',
description: `Document to ${operation}.`,
type: 'options',
required: true,
default: [],
typeOptions: {
loadOptionsDependsOn: [
'indexId',
],
loadOptionsMethod: 'getDocuments',
},
displayOptions: {
show: {
resource: [
resource,
],
operation: [
operation,
],
specifyIndexBy: [
'list',
],
},
},
});
export const makeIndexInputField = (resource: 'document' | 'index', operation: string) => {
const operationVerb = operation === 'get' ? 'retrieve' : operation;
const description = resource === 'document'
? `Index containing the document to ${operationVerb}.`
: `Index to ${operationVerb}.`
return {
displayName: 'Index ID',
name: 'indexId',
description,
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
resource,
],
operation: [
operation,
],
specifyIndexBy: [
'id',
],
},
},
};
}
export const makeDocumentInputField = (resource: 'document' | 'index', operation: string) => {
const operationInDescription = operation === 'get' ? 'retrieve' : operation;
return {
displayName: 'Document ID',
name: 'documentId',
description: `ID of the document to ${operationInDescription}.`,
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
resource,
],
operation: [
operation,
],
specifyIndexBy: [
'id',
],
},
},
};
}