mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-09 22:24:05 -08:00
⚡ Refactor to implement specifyIndexBy
This commit is contained in:
parent
41643a72fd
commit
02ea0d3080
|
@ -4,6 +4,7 @@ import {
|
|||
|
||||
import {
|
||||
IDataObject,
|
||||
ILoadOptionsFunctions,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
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[][]> {
|
||||
const items = this.getInputData();
|
||||
const returnData: IDataObject[] = [];
|
||||
|
|
|
@ -8,6 +8,7 @@ import {
|
|||
|
||||
import {
|
||||
IDataObject,
|
||||
ILoadOptionsFunctions,
|
||||
NodeApiError,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
|
@ -16,7 +17,7 @@ import {
|
|||
} from './types';
|
||||
|
||||
export async function elasticsearchApiRequest(
|
||||
this: IExecuteFunctions,
|
||||
this: IExecuteFunctions | ILoadOptionsFunctions,
|
||||
method: 'GET' | 'PUT' | 'POST' | 'DELETE',
|
||||
endpoint: string,
|
||||
body: IDataObject = {},
|
||||
|
@ -51,8 +52,7 @@ export async function elasticsearchApiRequest(
|
|||
}
|
||||
|
||||
try {
|
||||
// console.log(options);
|
||||
return await this.helpers.request(options);
|
||||
return await this.helpers.request!(options);
|
||||
} catch (error) {
|
||||
throw new NodeApiError(this.getNode(), error);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,13 @@ import {
|
|||
} from 'n8n-workflow';
|
||||
|
||||
import * as placeholders from './placeholders';
|
||||
import {
|
||||
makeDocumentInputField,
|
||||
makeDocumentListField,
|
||||
makeIndexInputField,
|
||||
makeIndexListField,
|
||||
makeSpecifyIndexByField,
|
||||
} from './SharedFields';
|
||||
|
||||
export const documentOperations = [
|
||||
{
|
||||
|
@ -44,85 +51,26 @@ export const documentOperations = [
|
|||
] as INodeProperties[];
|
||||
|
||||
export const documentFields = [
|
||||
// ----------------------------------------
|
||||
// shared fields
|
||||
// ----------------------------------------
|
||||
makeSpecifyIndexByField('document'),
|
||||
|
||||
// ----------------------------------------
|
||||
// document: delete
|
||||
// ----------------------------------------
|
||||
{
|
||||
displayName: 'Index ID',
|
||||
name: 'indexId',
|
||||
description: 'ID of the index containing the document to 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',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
makeIndexListField('document', 'delete'),
|
||||
makeDocumentListField('document', 'delete'),
|
||||
makeIndexInputField('document', 'delete'),
|
||||
makeDocumentInputField('document', 'delete'),
|
||||
|
||||
// ----------------------------------------
|
||||
// document: get
|
||||
// ----------------------------------------
|
||||
{
|
||||
displayName: 'Index ID',
|
||||
name: 'indexId',
|
||||
description: 'ID of the index containing the document to retrieve.',
|
||||
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',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
makeIndexListField('document', 'get'),
|
||||
makeDocumentListField('document', 'get'),
|
||||
makeIndexInputField('document', 'get'),
|
||||
makeDocumentInputField('document', 'get'),
|
||||
{
|
||||
displayName: 'Additional Fields',
|
||||
name: 'additionalFields',
|
||||
|
@ -174,24 +122,8 @@ export const documentFields = [
|
|||
// ----------------------------------------
|
||||
// document: getAll
|
||||
// ----------------------------------------
|
||||
{
|
||||
displayName: 'Index ID',
|
||||
name: 'indexId',
|
||||
description: 'ID of the index containing the documents to retrieve.',
|
||||
type: 'string',
|
||||
required: true,
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'document',
|
||||
],
|
||||
operation: [
|
||||
'getAll',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
makeIndexListField('document', 'getAll'),
|
||||
makeIndexInputField('document', 'getAll'),
|
||||
{
|
||||
displayName: 'Return All',
|
||||
name: 'returnAll',
|
||||
|
@ -486,24 +418,8 @@ export const documentFields = [
|
|||
// ----------------------------------------
|
||||
// document: index
|
||||
// ----------------------------------------
|
||||
{
|
||||
displayName: 'Index ID',
|
||||
name: 'indexId',
|
||||
description: 'ID of the index to add the document to.',
|
||||
type: 'string',
|
||||
required: true,
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'document',
|
||||
],
|
||||
operation: [
|
||||
'index',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
makeIndexListField('document', 'index'),
|
||||
makeIndexInputField('document', 'index'),
|
||||
{
|
||||
displayName: 'Document ID',
|
||||
name: 'documentId',
|
||||
|
@ -581,42 +497,10 @@ export const documentFields = [
|
|||
// ----------------------------------------
|
||||
// document: update
|
||||
// ----------------------------------------
|
||||
{
|
||||
displayName: 'Index ID',
|
||||
name: 'indexId',
|
||||
description: 'ID of the document to 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',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
makeIndexListField('document', 'update'),
|
||||
makeDocumentListField('document', 'update'),
|
||||
makeIndexInputField('document', 'update'),
|
||||
makeDocumentInputField('document', 'update'),
|
||||
{
|
||||
displayName: 'Script',
|
||||
name: 'script',
|
||||
|
|
|
@ -3,6 +3,11 @@ import {
|
|||
} from 'n8n-workflow';
|
||||
|
||||
import * as placeholders from './placeholders';
|
||||
import {
|
||||
makeIndexInputField,
|
||||
makeIndexListField,
|
||||
makeSpecifyIndexByField,
|
||||
} from './SharedFields';
|
||||
|
||||
export const indexOperations = [
|
||||
{
|
||||
|
@ -40,6 +45,11 @@ export const indexOperations = [
|
|||
] as INodeProperties[];
|
||||
|
||||
export const indexFields = [
|
||||
// ----------------------------------------
|
||||
// shared fields
|
||||
// ----------------------------------------
|
||||
makeSpecifyIndexByField('index'),
|
||||
|
||||
// ----------------------------------------
|
||||
// index: create
|
||||
// ----------------------------------------
|
||||
|
@ -145,46 +155,14 @@ export const indexFields = [
|
|||
// ----------------------------------------
|
||||
// index: delete
|
||||
// ----------------------------------------
|
||||
{
|
||||
displayName: 'Index ID',
|
||||
name: 'indexId',
|
||||
description: 'ID of the index to delete.',
|
||||
type: 'string',
|
||||
required: true,
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'index',
|
||||
],
|
||||
operation: [
|
||||
'delete',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
makeIndexListField('index', 'delete'),
|
||||
makeIndexInputField('index', 'delete'),
|
||||
|
||||
// ----------------------------------------
|
||||
// index: get
|
||||
// ----------------------------------------
|
||||
{
|
||||
displayName: 'Index ID',
|
||||
name: 'indexId',
|
||||
description: 'ID of the index to retrieve.',
|
||||
type: 'string',
|
||||
required: true,
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'index',
|
||||
],
|
||||
operation: [
|
||||
'get',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
makeIndexListField('index', 'get'),
|
||||
makeIndexInputField('index', 'get'),
|
||||
{
|
||||
displayName: 'Additional Fields',
|
||||
name: 'additionalFields',
|
||||
|
|
|
@ -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',
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in a new issue