From 8a8f766b4ac49526e82e6f17463bf6446ba18db2 Mon Sep 17 00:00:00 2001 From: Luca Faggianelli Date: Tue, 15 Oct 2019 18:39:00 +0200 Subject: [PATCH 1/2] mongodb credentials and node --- .../credentials/MongoDB.credentials.ts | 45 +++ .../nodes-base/nodes/MongoDB/MongoDB.node.ts | 304 ++++++++++++++++++ packages/nodes-base/nodes/MongoDB/mongodb.png | Bin 0 -> 1819 bytes packages/nodes-base/package.json | 4 + 4 files changed, 353 insertions(+) create mode 100644 packages/nodes-base/credentials/MongoDB.credentials.ts create mode 100644 packages/nodes-base/nodes/MongoDB/MongoDB.node.ts create mode 100644 packages/nodes-base/nodes/MongoDB/mongodb.png diff --git a/packages/nodes-base/credentials/MongoDB.credentials.ts b/packages/nodes-base/credentials/MongoDB.credentials.ts new file mode 100644 index 0000000000..141f9194d2 --- /dev/null +++ b/packages/nodes-base/credentials/MongoDB.credentials.ts @@ -0,0 +1,45 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + + +export class MongoDB implements ICredentialType { + name = 'mongodb'; + displayName = 'MongoDB'; + properties = [ + { + displayName: 'Host', + name: 'host', + type: 'string' as NodePropertyTypes, + default: 'localhost', + }, + { + displayName: 'Database', + name: 'database', + type: 'string' as NodePropertyTypes, + default: '', + }, + { + displayName: 'User', + name: 'user', + type: 'string' as NodePropertyTypes, + default: '', + }, + { + displayName: 'Password', + name: 'password', + type: 'string' as NodePropertyTypes, + typeOptions: { + password: true, + }, + default: '', + }, + { + displayName: 'Port', + name: 'port', + type: 'number' as NodePropertyTypes, + default: 0 + }, + ]; +} diff --git a/packages/nodes-base/nodes/MongoDB/MongoDB.node.ts b/packages/nodes-base/nodes/MongoDB/MongoDB.node.ts new file mode 100644 index 0000000000..6312132f36 --- /dev/null +++ b/packages/nodes-base/nodes/MongoDB/MongoDB.node.ts @@ -0,0 +1,304 @@ +import { IExecuteFunctions } from 'n8n-core'; +import { + IDataObject, + INodeExecutionData, + INodeType, + INodeTypeDescription, +} from 'n8n-workflow'; + +import { MongoClient } from 'mongodb'; + + +/** + * Returns of copy of the items which only contains the json data and + * of that only the define properties + * + * @param {INodeExecutionData[]} items The items to copy + * @param {string[]} properties The properties it should include + * @returns + */ +function getItemCopy(items: INodeExecutionData[], properties: string[]): IDataObject[] { + // Prepare the data to insert and copy it to be returned + let newItem: IDataObject; + return items.map((item) => { + newItem = {}; + for (const property of properties) { + if (item.json[property] === undefined) { + newItem[property] = null; + } else { + newItem[property] = JSON.parse(JSON.stringify(item.json[property])); + } + } + return newItem; + }); +} + + +export class MongoDB implements INodeType { + description: INodeTypeDescription = { + displayName: 'MongoDB', + name: 'mongodb', + icon: 'file:mongodb.png', + group: ['input'], + version: 1, + description: 'Find, insert and update documents in MongoDB.', + defaults: { + name: 'MongoDB', + color: '#13AA52', + }, + inputs: ['main'], + outputs: ['main'], + credentials: [ + { + name: 'mongodb', + required: true, + } + ], + properties: [ + { + displayName: 'Operation', + name: 'operation', + type: 'options', + options: [ + { + name: 'Find', + value: 'find', + description: 'Find documents.', + }, + { + name: 'Insert', + value: 'insert', + description: 'Insert documents.', + }, + { + name: 'Update', + value: 'update', + description: 'Updates documents.', + }, + ], + default: 'find', + description: 'The operation to perform.', + }, + + { + displayName: 'Collection', + name: 'collection', + type: 'string', + required: true, + default: '', + description: 'MongoDB Collection' + }, + + // ---------------------------------- + // find + // ---------------------------------- + { + displayName: 'Query (JSON format)', + name: 'query', + type: 'string', + typeOptions: { + rows: 5, + }, + displayOptions: { + show: { + operation: [ + 'find' + ], + }, + }, + default: '{}', + placeholder: `{ "birth": { "$gt": "1950-01-01" } }`, + required: true, + description: 'MongoDB Find query.', + }, + + + // ---------------------------------- + // insert + // ---------------------------------- + { + displayName: 'Table', + name: 'table', + type: 'string', + displayOptions: { + show: { + operation: [ + 'insert' + ], + }, + }, + default: '', + required: true, + description: 'Name of the table in which to insert data to.', + }, + { + displayName: 'Columns', + name: 'columns', + type: 'string', + displayOptions: { + show: { + operation: [ + 'insert' + ], + }, + }, + default: '', + placeholder: 'id,name,description', + description: 'Comma separated list of the properties which should used as columns for the new rows.', + }, + + + // ---------------------------------- + // update + // ---------------------------------- + { + displayName: 'Table', + name: 'table', + type: 'string', + displayOptions: { + show: { + operation: [ + 'update' + ], + }, + }, + default: '', + required: true, + description: 'Name of the table in which to update data in', + }, + { + displayName: 'Update Key', + name: 'updateKey', + type: 'string', + displayOptions: { + show: { + operation: [ + 'update' + ], + }, + }, + default: 'id', + required: true, + description: 'Name of the property which decides which rows in the database should be updated. Normally that would be "id".', + }, + { + displayName: 'Columns', + name: 'columns', + type: 'string', + displayOptions: { + show: { + operation: [ + 'update' + ], + }, + }, + default: '', + placeholder: 'name,description', + description: 'Comma separated list of the properties which should used as columns for rows to update.', + }, + + ] + }; + + + async execute(this: IExecuteFunctions): Promise { + + const credentials = this.getCredentials('mongodb'); + + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + let connectionUri = '' + + if (credentials.port) { + connectionUri = `mongodb://${credentials.user}:${credentials.password}@${credentials.host}:${credentials.port}` + } else { + connectionUri = `mongodb+srv://${credentials.user}:${credentials.password}@${credentials.host}` + } + + const client = await MongoClient.connect(connectionUri, { useNewUrlParser: true, useUnifiedTopology: true }); + const mdb = client.db(credentials.database as string); + + let returnItems = []; + + const items = this.getInputData(); + const operation = this.getNodeParameter('operation', 0) as string; + + if (operation === 'find') { + // ---------------------------------- + // find + // ---------------------------------- + + const queryResult = await mdb + .collection(this.getNodeParameter('collection', 0) as string) + .find(JSON.parse(this.getNodeParameter('query', 0) as string)) + .toArray(); + + returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]); + + // } else if (operation === 'insert') { + // ---------------------------------- + // insert + // ---------------------------------- + + // const table = this.getNodeParameter('table', 0) as string; + // const columnString = this.getNodeParameter('columns', 0) as string; + + // const columns = columnString.split(',').map(column => column.trim()); + + // const cs = new pgp.helpers.ColumnSet(columns, { table }); + + // // Prepare the data to insert and copy it to be returned + // const insertItems = getItemCopy(items, columns); + + // // Generate the multi-row insert query and return the id of new row + // const query = pgp.helpers.insert(insertItems, cs) + ' RETURNING id'; + + // // Executing the query to insert the data + // const insertData = await db.many(query); + + // // Add the id to the data + // for (let i = 0; i < insertData.length; i++) { + // returnItems.push({ + // json: { + // ...insertData[i], + // ...insertItems[i], + // } + // }); + // } + + // } else if (operation === 'update') { + // ---------------------------------- + // update + // ---------------------------------- + + // const table = this.getNodeParameter('table', 0) as string; + // const updateKey = this.getNodeParameter('updateKey', 0) as string; + // const columnString = this.getNodeParameter('columns', 0) as string; + + // const columns = columnString.split(',').map(column => column.trim()); + + // // Make sure that the updateKey does also get queried + // if (!columns.includes(updateKey)) { + // columns.unshift(updateKey); + // } + + // // Prepare the data to update and copy it to be returned + // const updateItems = getItemCopy(items, columns); + + // // Generate the multi-row update query + // const query = pgp.helpers.update(updateItems, columns, table) + ' WHERE v.' + updateKey + ' = t.' + updateKey; + + // // Executing the query to update the data + // await db.none(query); + + // returnItems = this.helpers.returnJsonArray(updateItems as IDataObject[]); + + } else { + throw new Error(`The operation "${operation}" is not supported!`); + } + + return this.prepareOutputData(returnItems); + } +} diff --git a/packages/nodes-base/nodes/MongoDB/mongodb.png b/packages/nodes-base/nodes/MongoDB/mongodb.png new file mode 100644 index 0000000000000000000000000000000000000000..3d74fee89400a0f6d356ed027f13203c1f2eefd6 GIT binary patch literal 1819 zcmV+$2juvPP)004R>004l5008;`004mK004C`008P>0026e000+ooVrmw00006 zVoOIv0RI600RN!9r;`8x010qNS#tmY3ljhU3ljkVnw%H_000McNliru;|dQE3Ia#c zi<$rc1`0_;K~!ko-J4x(6-5+)zcYLL!&NBK@}o#B5Pd)hmQcWmv=pNO2~}f4A;tuw zg#sEi1bHyV7l;~B1A$5m#s>}cL9j)Qnn22rK@lHNM2(stA{1*VMoRy7=lHO#7E0UQ zyL)d7CuzF(X3orfJ$uf~IWrY9EX&@#3n#}Exo!3g(D2S9Uu1NAlgYtW)Rs~(ai|fI z3a`yuXcEHGEiqdz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;rHCML)+sM1P~hv_6w#lgyr55~=T? zlF>rE#o}4_9RFuk$FgiL)dKjYV>n*ip_>;I-=oO8&S++FtH zMW5675xYJ0-mU`RZqkImE@w1`3?y(%=kFjX;xz!QdJ@0rmy!RQ>Ms8RyBF Date: Tue, 15 Oct 2019 21:14:26 +0200 Subject: [PATCH 2/2] mongodb update and insert --- .../nodes-base/nodes/MongoDB/MongoDB.node.ts | 121 +++++++----------- 1 file changed, 43 insertions(+), 78 deletions(-) diff --git a/packages/nodes-base/nodes/MongoDB/MongoDB.node.ts b/packages/nodes-base/nodes/MongoDB/MongoDB.node.ts index 6312132f36..7cf44b6326 100644 --- a/packages/nodes-base/nodes/MongoDB/MongoDB.node.ts +++ b/packages/nodes-base/nodes/MongoDB/MongoDB.node.ts @@ -117,8 +117,8 @@ export class MongoDB implements INodeType { // insert // ---------------------------------- { - displayName: 'Table', - name: 'table', + displayName: 'Fields', + name: 'fields', type: 'string', displayOptions: { show: { @@ -128,44 +128,14 @@ export class MongoDB implements INodeType { }, }, default: '', - required: true, - description: 'Name of the table in which to insert data to.', - }, - { - displayName: 'Columns', - name: 'columns', - type: 'string', - displayOptions: { - show: { - operation: [ - 'insert' - ], - }, - }, - default: '', - placeholder: 'id,name,description', - description: 'Comma separated list of the properties which should used as columns for the new rows.', + placeholder: 'name,description', + description: 'Comma separated list of the fields to be included into the new document.', }, // ---------------------------------- // update // ---------------------------------- - { - displayName: 'Table', - name: 'table', - type: 'string', - displayOptions: { - show: { - operation: [ - 'update' - ], - }, - }, - default: '', - required: true, - description: 'Name of the table in which to update data in', - }, { displayName: 'Update Key', name: 'updateKey', @@ -182,8 +152,8 @@ export class MongoDB implements INodeType { description: 'Name of the property which decides which rows in the database should be updated. Normally that would be "id".', }, { - displayName: 'Columns', - name: 'columns', + displayName: 'Fields', + name: 'fields', type: 'string', displayOptions: { show: { @@ -194,7 +164,7 @@ export class MongoDB implements INodeType { }, default: '', placeholder: 'name,description', - description: 'Comma separated list of the properties which should used as columns for rows to update.', + description: 'Comma separated list of the fields to be included into the new document.', }, ] @@ -237,63 +207,58 @@ export class MongoDB implements INodeType { returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]); - // } else if (operation === 'insert') { + } else if (operation === 'insert') { // ---------------------------------- // insert // ---------------------------------- - // const table = this.getNodeParameter('table', 0) as string; - // const columnString = this.getNodeParameter('columns', 0) as string; + // Prepare the data to insert and copy it to be returned + const fields = (this.getNodeParameter('fields', 0) as string) + .split(',') + .map(f => f.trim()) + .filter(f => !!f) - // const columns = columnString.split(',').map(column => column.trim()); + const insertItems = getItemCopy(items, fields); - // const cs = new pgp.helpers.ColumnSet(columns, { table }); + const { insertedIds } = await mdb + .collection(this.getNodeParameter('collection', 0) as string) + .insertMany(insertItems) - // // Prepare the data to insert and copy it to be returned - // const insertItems = getItemCopy(items, columns); - - // // Generate the multi-row insert query and return the id of new row - // const query = pgp.helpers.insert(insertItems, cs) + ' RETURNING id'; - - // // Executing the query to insert the data - // const insertData = await db.many(query); - - // // Add the id to the data - // for (let i = 0; i < insertData.length; i++) { - // returnItems.push({ - // json: { - // ...insertData[i], - // ...insertItems[i], - // } - // }); - // } - - // } else if (operation === 'update') { + // Add the id to the data + for (let i in insertedIds) { + returnItems.push({ + json: { + ...insertItems[i], + id: insertedIds[i] as string, + } + }); + } + } else if (operation === 'update') { // ---------------------------------- // update // ---------------------------------- - // const table = this.getNodeParameter('table', 0) as string; - // const updateKey = this.getNodeParameter('updateKey', 0) as string; - // const columnString = this.getNodeParameter('columns', 0) as string; + const fields = (this.getNodeParameter('fields', 0) as string) + .split(',') + .map(f => f.trim()) + .filter(f => !!f) - // const columns = columnString.split(',').map(column => column.trim()); + // Prepare the data to update and copy it to be returned + const updateItems = getItemCopy(items, fields); + const updateKey = this.getNodeParameter('updateKey', 0) as string; - // // Make sure that the updateKey does also get queried - // if (!columns.includes(updateKey)) { - // columns.unshift(updateKey); - // } + for (let item of updateItems) { + if (item[updateKey] === undefined) { continue } - // // Prepare the data to update and copy it to be returned - // const updateItems = getItemCopy(items, columns); + const filter: { [key: string] :string } = {}; + filter[updateKey] = item[updateKey] as string; - // // Generate the multi-row update query - // const query = pgp.helpers.update(updateItems, columns, table) + ' WHERE v.' + updateKey + ' = t.' + updateKey; + await mdb + .collection(this.getNodeParameter('collection', 0) as string) + .updateOne(filter, item) + } - // // Executing the query to update the data - // await db.none(query); - - // returnItems = this.helpers.returnJsonArray(updateItems as IDataObject[]); + returnItems = this.helpers.returnJsonArray(updateItems as IDataObject[]); } else { throw new Error(`The operation "${operation}" is not supported!`);