From be62e9405171aeaa75d4b160bc373b3a4b020070 Mon Sep 17 00:00:00 2001 From: Krzysztof Janda Date: Sat, 11 Apr 2020 17:06:57 +0200 Subject: [PATCH 1/8] Fix default resource value --- packages/nodes-base/nodes/Cockpit/Cockpit.node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nodes-base/nodes/Cockpit/Cockpit.node.ts b/packages/nodes-base/nodes/Cockpit/Cockpit.node.ts index bac4b2213..b0961fd1e 100644 --- a/packages/nodes-base/nodes/Cockpit/Cockpit.node.ts +++ b/packages/nodes-base/nodes/Cockpit/Cockpit.node.ts @@ -56,7 +56,7 @@ export class Cockpit implements INodeType { displayName: 'Resource', name: 'resource', type: 'options', - default: 'collections', + default: 'collection', description: 'Resource to consume.', options: [ { From c99910750fae7792a420b21af68672c71a57a8e3 Mon Sep 17 00:00:00 2001 From: Krzysztof Janda Date: Sat, 11 Apr 2020 17:07:14 +0200 Subject: [PATCH 2/8] Simplify if condition --- packages/nodes-base/nodes/Cockpit/Cockpit.node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nodes-base/nodes/Cockpit/Cockpit.node.ts b/packages/nodes-base/nodes/Cockpit/Cockpit.node.ts index b0961fd1e..076c0e8eb 100644 --- a/packages/nodes-base/nodes/Cockpit/Cockpit.node.ts +++ b/packages/nodes-base/nodes/Cockpit/Cockpit.node.ts @@ -131,7 +131,7 @@ export class Cockpit implements INodeType { const options = this.getNodeParameter('options', i) as IDataObject; const returnAll = this.getNodeParameter('returnAll', i) as boolean; - if (returnAll !== true) { + if (!returnAll) { options.limit = this.getNodeParameter('limit', i) as number; } From fe3cca5344a18b73ffc326bbd33ac67229f1184f Mon Sep 17 00:00:00 2001 From: Krzysztof Janda Date: Sat, 11 Apr 2020 20:24:51 +0200 Subject: [PATCH 3/8] Add data fields to collections --- .../nodes/Cockpit/CollectionDescription.ts | 111 +++++++++++++----- .../nodes/Cockpit/CollectionFunctions.ts | 2 +- 2 files changed, 80 insertions(+), 33 deletions(-) diff --git a/packages/nodes-base/nodes/Cockpit/CollectionDescription.ts b/packages/nodes-base/nodes/Cockpit/CollectionDescription.ts index e448ce670..24873a3c8 100644 --- a/packages/nodes-base/nodes/Cockpit/CollectionDescription.ts +++ b/packages/nodes-base/nodes/Cockpit/CollectionDescription.ts @@ -54,29 +54,6 @@ export const collectionFields = [ description: 'Name of the collection to operate on.' }, - // Collection:entry:create - { - displayName: 'Data', - name: 'data', - type: 'json', - required: true, - default: '', - typeOptions: { - alwaysOpenEditWindow: true, - }, - displayOptions: { - show: { - resource: [ - 'collection', - ], - operation: [ - 'create', - ] - }, - }, - description: 'The data to create.', - }, - // Collection:entry:getAll { displayName: 'Return All', @@ -214,25 +191,95 @@ export const collectionFields = [ }, description: 'The entry ID.', }, + + // Collection:entry:create + // Collection:entry:update { - displayName: 'Data', - name: 'data', - type: 'json', - required: true, - default: '', - typeOptions: { - alwaysOpenEditWindow: true, - }, + displayName: 'JSON Data fields', + name: 'jsonDataFields', + type: 'boolean', + default: false, displayOptions: { show: { resource: [ 'collection', ], operation: [ + 'create', 'update', ] }, }, - description: 'The data to update.', + description: 'If new entry fields should be set via the value-key pair UI or JSON.', + }, + { + displayName: 'Data fields', + name: 'dataFieldsJson', + type: 'json', + default: '', + typeOptions: { + alwaysOpenEditWindow: true, + }, + displayOptions: { + show: { + jsonDataFields: [ + true, + ], + resource: [ + 'collection', + ], + operation: [ + 'create', + 'update', + ] + }, + }, + description: 'Data to send as JSON.', + }, + { + displayName: 'Data fields', + name: 'dataFieldsUi', + type: 'fixedCollection', + typeOptions: { + multipleValues: true, + }, + default: {}, + displayOptions: { + show: { + jsonDataFields: [ + false, + ], + resource: [ + 'collection', + ], + operation: [ + 'create', + 'update', + ] + }, + }, + options: [ + { + displayName: 'Field', + name: 'field', + values: [ + { + displayName: 'Name', + name: 'name', + type: 'string', + default: '', + description: 'Name of the field.', + }, + { + displayName: 'Value', + name: 'value', + type: 'string', + default: '', + description: 'Value of the field.', + }, + ], + }, + ], + description: 'Data field to send.', }, ] as INodeProperties[]; diff --git a/packages/nodes-base/nodes/Cockpit/CollectionFunctions.ts b/packages/nodes-base/nodes/Cockpit/CollectionFunctions.ts index 4a2c640f8..344b48340 100644 --- a/packages/nodes-base/nodes/Cockpit/CollectionFunctions.ts +++ b/packages/nodes-base/nodes/Cockpit/CollectionFunctions.ts @@ -9,7 +9,7 @@ import { cockpitApiRequest } from './GenericFunctions'; export async function createCollectionEntry(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resourceName: string, data: IDataObject, id?: string): Promise { // tslint:disable-line:no-any const body: ICollection = { - data: JSON.parse(data.toString()) + data, }; if (id) { From 348afdaf5ce7cdb42e90e81e76be4120aef4d82b Mon Sep 17 00:00:00 2001 From: Krzysztof Janda Date: Sat, 11 Apr 2020 20:24:58 +0200 Subject: [PATCH 4/8] Add data fields to forms --- .../nodes/Cockpit/FormDescription.ts | 75 ++++++++++++++++++- .../nodes-base/nodes/Cockpit/FormFunctions.ts | 2 +- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/packages/nodes-base/nodes/Cockpit/FormDescription.ts b/packages/nodes-base/nodes/Cockpit/FormDescription.ts index 8488cbe09..78f77ba54 100644 --- a/packages/nodes-base/nodes/Cockpit/FormDescription.ts +++ b/packages/nodes-base/nodes/Cockpit/FormDescription.ts @@ -44,21 +44,88 @@ export const formFields = [ // Form:submit { - displayName: 'Form data', - name: 'form', + displayName: 'JSON Data fields', + name: 'jsonDataFields', + type: 'boolean', + default: false, + displayOptions: { + show: { + resource: [ + 'form', + ], + operation: [ + 'submit', + ] + }, + }, + description: 'If form fields should be set via the value-key pair UI or JSON.', + }, + { + displayName: 'Form fields', + name: 'dataFieldsJson', type: 'json', - required: true, default: '', typeOptions: { alwaysOpenEditWindow: true, }, displayOptions: { show: { + jsonDataFields: [ + true, + ], resource: [ 'form', ], + operation: [ + 'submit', + ] }, }, - description: 'The data to save.', + description: 'Form to send as JSON.', + }, + { + displayName: 'Data fields', + name: 'dataFieldsUi', + type: 'fixedCollection', + typeOptions: { + multipleValues: true, + }, + default: {}, + displayOptions: { + show: { + jsonDataFields: [ + false, + ], + resource: [ + 'form', + ], + operation: [ + 'submit', + ] + }, + }, + options: [ + { + displayName: 'Field', + name: 'field', + values: [ + { + displayName: 'Name', + name: 'name', + type: 'string', + default: '', + description: 'Name of the field.', + }, + { + displayName: 'Value', + name: 'value', + type: 'string', + default: '', + description: 'Value of the field.', + }, + ], + }, + ], + description: 'Form field to send.', }, ] as INodeProperties[]; diff --git a/packages/nodes-base/nodes/Cockpit/FormFunctions.ts b/packages/nodes-base/nodes/Cockpit/FormFunctions.ts index 437ed210a..36a1bdc38 100644 --- a/packages/nodes-base/nodes/Cockpit/FormFunctions.ts +++ b/packages/nodes-base/nodes/Cockpit/FormFunctions.ts @@ -9,7 +9,7 @@ import { cockpitApiRequest } from './GenericFunctions'; export async function submitForm(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resourceName: string, form: IDataObject) { const body: IForm = { - form: JSON.parse(form.toString()) + form }; return cockpitApiRequest.call(this, 'post', `/forms/submit/${resourceName}`, body); From 5d2f2ff4936894fb11df417f118cb73a7841e261 Mon Sep 17 00:00:00 2001 From: Krzysztof Janda Date: Sat, 11 Apr 2020 20:25:20 +0200 Subject: [PATCH 5/8] Add method to create data from parameters --- .../nodes/Cockpit/GenericFunctions.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/nodes-base/nodes/Cockpit/GenericFunctions.ts b/packages/nodes-base/nodes/Cockpit/GenericFunctions.ts index ed923d3bd..8db5c5b7a 100644 --- a/packages/nodes-base/nodes/Cockpit/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Cockpit/GenericFunctions.ts @@ -44,3 +44,29 @@ export async function cockpitApiRequest(this: IExecuteFunctions | IExecuteSingle throw new Error(`Cockpit error [${error.statusCode}]: ` + errorMessage); } } + +export function createDataFromParameters(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, itemIndex: number): IDataObject { + const dataFieldsAreJson = this.getNodeParameter('jsonDataFields', itemIndex) as boolean; + + if (dataFieldsAreJson) { + // Parameters are defined as JSON + return JSON.parse(this.getNodeParameter('dataFieldsJson', itemIndex, {}) as string); + } + + // Parameters are defined in UI + return unpackUiDataFields(this.getNodeParameter('dataFieldsUi', itemIndex, {}) as IDataObject); +} + +function unpackUiDataFields(uiDataFields: IDataObject): IDataObject { + const unpacked: IDataObject = {}; + + if (uiDataFields.field === undefined) { + return unpacked; + } + + for (const field of uiDataFields!.field as IDataObject[]) { + unpacked[field!.name as string] = field!.value; + } + + return unpacked; +} From 294a9ea599e17dc560d9c65b6e94f8cefb7341bb Mon Sep 17 00:00:00 2001 From: Krzysztof Janda Date: Sat, 11 Apr 2020 20:26:15 +0200 Subject: [PATCH 6/8] Add new fields to the node --- packages/nodes-base/nodes/Cockpit/Cockpit.node.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/nodes-base/nodes/Cockpit/Cockpit.node.ts b/packages/nodes-base/nodes/Cockpit/Cockpit.node.ts index 076c0e8eb..efd233f74 100644 --- a/packages/nodes-base/nodes/Cockpit/Cockpit.node.ts +++ b/packages/nodes-base/nodes/Cockpit/Cockpit.node.ts @@ -21,6 +21,7 @@ import { formOperations } from './FormDescription'; import { submitForm } from './FormFunctions'; +import { createDataFromParameters } from './GenericFunctions'; import { singletonFields, singletonOperations, @@ -74,7 +75,6 @@ export class Cockpit implements INodeType { ], }, - ...collectionOperations, ...collectionFields, ...formOperations, @@ -84,7 +84,6 @@ export class Cockpit implements INodeType { ], }; - methods = { loadOptions: { async getCollections(this: ILoadOptionsFunctions): Promise { @@ -123,8 +122,9 @@ export class Cockpit implements INodeType { for (let i = 0; i < length; i++) { if (resource === 'collection') { const collectionName = this.getNodeParameter('collection', i) as string; + if (operation === 'create') { - const data = this.getNodeParameter('data', i) as IDataObject; + const data = createDataFromParameters.call(this, i); responseData = await createCollectionEntry.call(this, collectionName, data); } else if (operation === 'getAll') { @@ -138,19 +138,21 @@ export class Cockpit implements INodeType { responseData = await getAllCollectionEntries.call(this, collectionName, options); } else if (operation === 'update') { const id = this.getNodeParameter('id', i) as string; - const data = this.getNodeParameter('data', i) as IDataObject; + const data = createDataFromParameters.call(this, i); responseData = await createCollectionEntry.call(this, collectionName, data, id); } } else if (resource === 'form') { const formName = this.getNodeParameter('form', i) as string; + if (operation === 'submit') { - const form = this.getNodeParameter('form', i) as IDataObject; + const form = createDataFromParameters.call(this, i); responseData = await submitForm.call(this, formName, form); } } else if (resource === 'singleton') { const singletonName = this.getNodeParameter('singleton', i) as string; + if (operation === 'get') { responseData = await getSingleton.call(this, singletonName); } From 532b9a929447348b1b2390823e701ebd77e09ae6 Mon Sep 17 00:00:00 2001 From: Krzysztof Janda Date: Sat, 11 Apr 2020 20:30:28 +0200 Subject: [PATCH 7/8] Simplify convert method --- packages/nodes-base/nodes/Cockpit/GenericFunctions.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/nodes-base/nodes/Cockpit/GenericFunctions.ts b/packages/nodes-base/nodes/Cockpit/GenericFunctions.ts index 8db5c5b7a..3f3bf39ac 100644 --- a/packages/nodes-base/nodes/Cockpit/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Cockpit/GenericFunctions.ts @@ -54,10 +54,7 @@ export function createDataFromParameters(this: IExecuteFunctions | IExecuteSingl } // Parameters are defined in UI - return unpackUiDataFields(this.getNodeParameter('dataFieldsUi', itemIndex, {}) as IDataObject); -} - -function unpackUiDataFields(uiDataFields: IDataObject): IDataObject { + const uiDataFields = this.getNodeParameter('dataFieldsUi', itemIndex, {}) as IDataObject; const unpacked: IDataObject = {}; if (uiDataFields.field === undefined) { From ef26a4bfe5690d0f9531e09a0104dba01ec5a748 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Sun, 12 Apr 2020 12:28:36 +0200 Subject: [PATCH 8/8] :zap: Small improvement to Cockpit-Node --- .../nodes/Cockpit/CollectionDescription.ts | 29 ++++++++++--------- .../nodes/Cockpit/CollectionFunctions.ts | 11 ++++++- .../nodes/Cockpit/FormDescription.ts | 10 +++---- .../nodes/Cockpit/SingletonDescription.ts | 2 +- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/packages/nodes-base/nodes/Cockpit/CollectionDescription.ts b/packages/nodes-base/nodes/Cockpit/CollectionDescription.ts index 24873a3c8..92420c257 100644 --- a/packages/nodes-base/nodes/Cockpit/CollectionDescription.ts +++ b/packages/nodes-base/nodes/Cockpit/CollectionDescription.ts @@ -14,17 +14,17 @@ export const collectionOperations = [ }, options: [ { - name: 'Create an entry', + name: 'Create an Entry', value: 'create', description: 'Create a collection entry', }, { - name: 'Get all entries', + name: 'Get all Entries', value: 'getAll', description: 'Get all collection entries', }, { - name: 'Update an entry', + name: 'Update an Entry', value: 'update', description: 'Update a collection entries', }, @@ -116,22 +116,24 @@ export const collectionFields = [ { displayName: 'Fields', name: 'fields', - type: 'json', + type: 'string', default: '', typeOptions: { alwaysOpenEditWindow: true, }, - description: 'Fields to get.', + placeholder: '_id,name', + description: 'Comma separated list of fields to get.', }, { - displayName: 'Filter', + displayName: 'Filter Query', name: 'filter', type: 'json', default: '', typeOptions: { alwaysOpenEditWindow: true, }, - description: 'Filter result by fields.', + placeholder: '{"name": "Jim"}', + description: 'Filter query in Mongolite format.', }, { displayName: 'Language', @@ -163,11 +165,12 @@ export const collectionFields = [ description: 'Skip number of entries.', }, { - displayName: 'Sort', + displayName: 'Sort Query', name: 'sort', type: 'json', default: '', - description: 'Sort result by fields.', + placeholder: '{"price": -1}', + description: 'Sort query in Mongolite format.', }, ], }, @@ -213,7 +216,7 @@ export const collectionFields = [ description: 'If new entry fields should be set via the value-key pair UI or JSON.', }, { - displayName: 'Data fields', + displayName: 'Entry Data', name: 'dataFieldsJson', type: 'json', default: '', @@ -234,10 +237,10 @@ export const collectionFields = [ ] }, }, - description: 'Data to send as JSON.', + description: 'Entry data to send as JSON.', }, { - displayName: 'Data fields', + displayName: 'Entry Data', name: 'dataFieldsUi', type: 'fixedCollection', typeOptions: { @@ -280,6 +283,6 @@ export const collectionFields = [ ], }, ], - description: 'Data field to send.', + description: 'Entry data to send.', }, ] as INodeProperties[]; diff --git a/packages/nodes-base/nodes/Cockpit/CollectionFunctions.ts b/packages/nodes-base/nodes/Cockpit/CollectionFunctions.ts index 344b48340..d50862984 100644 --- a/packages/nodes-base/nodes/Cockpit/CollectionFunctions.ts +++ b/packages/nodes-base/nodes/Cockpit/CollectionFunctions.ts @@ -27,7 +27,16 @@ export async function getAllCollectionEntries(this: IExecuteFunctions | IExecute const body: ICollection = {}; if (options.fields) { - body.fields = JSON.parse(options.fields.toString()); + const fields = (options.fields as string).split(',').map(field => field.trim() ); + + const bodyFields = { + _id: false, + } as IDataObject; + for (const field of fields) { + bodyFields[field] = true; + } + + body.fields = bodyFields; } if (options.filter) { diff --git a/packages/nodes-base/nodes/Cockpit/FormDescription.ts b/packages/nodes-base/nodes/Cockpit/FormDescription.ts index 78f77ba54..66a5c052e 100644 --- a/packages/nodes-base/nodes/Cockpit/FormDescription.ts +++ b/packages/nodes-base/nodes/Cockpit/FormDescription.ts @@ -14,7 +14,7 @@ export const formOperations = [ }, options: [ { - name: 'Submit a form', + name: 'Submit a Form', value: 'submit', description: 'Store submission of a form', }, @@ -61,7 +61,7 @@ export const formFields = [ description: 'If form fields should be set via the value-key pair UI or JSON.', }, { - displayName: 'Form fields', + displayName: 'Form Data', name: 'dataFieldsJson', type: 'json', default: '', @@ -81,10 +81,10 @@ export const formFields = [ ] }, }, - description: 'Form to send as JSON.', + description: 'Form data to send as JSON.', }, { - displayName: 'Data fields', + displayName: 'Form Data', name: 'dataFieldsUi', type: 'fixedCollection', typeOptions: { @@ -126,6 +126,6 @@ export const formFields = [ ], }, ], - description: 'Form field to send.', + description: 'Form data to send.', }, ] as INodeProperties[]; diff --git a/packages/nodes-base/nodes/Cockpit/SingletonDescription.ts b/packages/nodes-base/nodes/Cockpit/SingletonDescription.ts index e9774b4f5..98df8a03d 100644 --- a/packages/nodes-base/nodes/Cockpit/SingletonDescription.ts +++ b/packages/nodes-base/nodes/Cockpit/SingletonDescription.ts @@ -16,7 +16,7 @@ export const singletonOperations = [ { name: 'Get', value: 'get', - description: 'Gets a singleton', + description: 'Gets a Singleton', }, ], default: 'get',