From ef9178d9540a78f300da2ec542c29f69df72deb2 Mon Sep 17 00:00:00 2001 From: ricardo Date: Wed, 10 Mar 2021 11:01:03 -0500 Subject: [PATCH] :zap: Add "Date Fields" parameter to the insert & update operations MongoDB --- .../nodes-base/nodes/MongoDb/MongoDb.node.ts | 37 +++++++++++--- .../nodes/MongoDb/mongo.node.options.ts | 48 +++++++++++++++--- .../nodes/MongoDb/mongo.node.utils.ts | 11 ++++ packages/nodes-base/nodes/MongoDb/mongoDb.png | Bin 1018 -> 0 bytes packages/nodes-base/nodes/MongoDb/mongodb.svg | 1 + 5 files changed, 84 insertions(+), 13 deletions(-) delete mode 100644 packages/nodes-base/nodes/MongoDb/mongoDb.png create mode 100644 packages/nodes-base/nodes/MongoDb/mongodb.svg diff --git a/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts b/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts index e9abdc2bd2..c03a62816f 100644 --- a/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts +++ b/packages/nodes-base/nodes/MongoDb/MongoDb.node.ts @@ -1,14 +1,26 @@ -import { IExecuteFunctions } from 'n8n-core'; +import { + IExecuteFunctions, +} from 'n8n-core'; + import { IDataObject, INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow'; -import { nodeDescription } from './mongo.node.options'; -import { MongoClient } from 'mongodb'; + +import { + nodeDescription, +} from './mongo.node.options'; + +import { + MongoClient, + ObjectID, +} from 'mongodb'; + import { getItemCopy, + handleDateFields, validateAndResolveMongoCredentials } from './mongo.node.utils'; @@ -65,8 +77,13 @@ export class MongoDb implements INodeType { .map(f => f.trim()) .filter(f => !!f); + const options = this.getNodeParameter('options', 0) as IDataObject; const insertItems = getItemCopy(items, fields); + if (options.dateFields) { + handleDateFields(insertItems, options.dateFields as string); + } + const { insertedIds } = await mdb .collection(this.getNodeParameter('collection', 0) as string) .insertMany(insertItems); @@ -90,6 +107,8 @@ export class MongoDb implements INodeType { .map(f => f.trim()) .filter(f => !!f); + const options = this.getNodeParameter('options', 0) as IDataObject; + let updateKey = this.getNodeParameter('updateKey', 0) as string; updateKey = updateKey.trim(); @@ -100,19 +119,25 @@ export class MongoDb implements INodeType { // Prepare the data to update and copy it to be returned const updateItems = getItemCopy(items, fields); + if (options.dateFields) { + handleDateFields(updateItems, options.dateFields as string); + } + for (const item of updateItems) { if (item[updateKey] === undefined) { continue; } - const filter: { [key: string]: string } = {}; + const filter: { [key: string]: string | ObjectID } = {}; filter[updateKey] = item[updateKey] as string; - + if (updateKey === '_id') { + filter[updateKey] = new ObjectID(filter[updateKey]); + delete item['_id']; + } await mdb .collection(this.getNodeParameter('collection', 0) as string) .updateOne(filter, { $set: item }); } - returnItems = this.helpers.returnJsonArray(updateItems as IDataObject[]); } else { throw new Error(`The operation "${operation}" is not supported!`); diff --git a/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts b/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts index d4bd0a37b5..ec09998450 100644 --- a/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts +++ b/packages/nodes-base/nodes/MongoDb/mongo.node.options.ts @@ -1,4 +1,6 @@ -import { INodeTypeDescription } from 'n8n-workflow'; +import { + INodeTypeDescription, +} from 'n8n-workflow'; /** * Options to be displayed @@ -6,7 +8,7 @@ import { INodeTypeDescription } from 'n8n-workflow'; export const nodeDescription: INodeTypeDescription = { displayName: 'MongoDB', name: 'mongoDb', - icon: 'file:mongoDb.png', + icon: 'file:mongoDb.svg', group: ['input'], version: 1, description: 'Find, insert and update documents in MongoDB.', @@ -46,7 +48,7 @@ export const nodeDescription: INodeTypeDescription = { { name: 'Update', value: 'update', - description: 'Updates documents.', + description: 'Update documents.', }, ], default: 'find', @@ -97,7 +99,9 @@ export const nodeDescription: INodeTypeDescription = { }, displayOptions: { show: { - operation: ['find'], + operation: [ + 'find', + ], }, }, default: '{}', @@ -115,7 +119,9 @@ export const nodeDescription: INodeTypeDescription = { type: 'string', displayOptions: { show: { - operation: ['insert'], + operation: [ + 'insert', + ], }, }, default: '', @@ -133,7 +139,9 @@ export const nodeDescription: INodeTypeDescription = { type: 'string', displayOptions: { show: { - operation: ['update'], + operation: [ + 'update', + ], }, }, default: 'id', @@ -147,7 +155,9 @@ export const nodeDescription: INodeTypeDescription = { type: 'string', displayOptions: { show: { - operation: ['update'], + operation: [ + 'update', + ], }, }, default: '', @@ -155,5 +165,29 @@ export const nodeDescription: INodeTypeDescription = { description: 'Comma separated list of the fields to be included into the new document.', }, + { + displayName: 'Options', + name: 'options', + type: 'collection', + displayOptions: { + show: { + operation: [ + 'update', + 'insert', + ], + }, + }, + placeholder: 'Add Option', + default: {}, + options: [ + { + displayName: 'Date Fields', + name: 'dateFields', + type: 'string', + default: '', + description: 'Comma separeted list of fields that will be parse as Mongo Date type.', + }, + ], + }, ], }; diff --git a/packages/nodes-base/nodes/MongoDb/mongo.node.utils.ts b/packages/nodes-base/nodes/MongoDb/mongo.node.utils.ts index bc2d308a76..87fefd69b3 100644 --- a/packages/nodes-base/nodes/MongoDb/mongo.node.utils.ts +++ b/packages/nodes-base/nodes/MongoDb/mongo.node.utils.ts @@ -102,3 +102,14 @@ export function getItemCopy( return newItem; }); } + +export function handleDateFields(insertItems: IDataObject[], fields: string) { + const dateFields = (fields as string).split(','); + for (let i = 0; i < insertItems.length; i++) { + for (const key of Object.keys(insertItems[i])) { + if (dateFields.includes(key)) { + insertItems[i][key] = new Date(insertItems[i][key] as string); + } + } + } +} diff --git a/packages/nodes-base/nodes/MongoDb/mongoDb.png b/packages/nodes-base/nodes/MongoDb/mongoDb.png deleted file mode 100644 index 0980afcb2b411bba963fbf23820d2f8faab7661b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1018 zcmeAS@N?(olHy`uVBq!ia0vp^HXzKw3=&b&bYNg$j12GzaRqWSH#sCOwFZ*$3$1`; zpi zz9mo#5Jk_hfS3bRA2HQ5Vw!2vGHW0MD44v$He`-Y>^w^#8z>7@96JYOFGM4p0p!Bj z>Fe!5X3jE)sE3G0fVITVwMja!r8PMW%=`Ov<7hxZ+T zh#cO3AYz({5a;SjVBqGL1o;Is{AK?6`PH8c&R=hy+}k%L^!wK@@i9UE+LDrj{QrJ@ z|Ni0Z$?Pz17i$AGMQ*k~-@ktS^7+m6t9!RBT{L|{M`cPEGW!fByUd1mA%Q zzJB@g>Ep-8cW$3MxNq0`bqo8u8)|Ec)BJoK^|eG~RwiEr#_vN<7sn8f<4dolhaWN! zVS8Y*b?c@%4PDHFqO9NVRlnc+ecsNm|LX;^``L0;&G+1&U+iBrdG>_*$tx5tEmTha z^dqqQ|W1xr*8gv*JtgwBTsXtEH$i{uTrq^m_%?IV@+>{b@2(i%Ule5IhW|Z zPY%mcl&xpobTB?_qX7#K&%9UL3g@-%_7XI^bl%9|i&^($qp#v^45C}7s=qP+X2Q8W z;%{@JfofVW_p!v3my&PU#hD+3*`AuSVZG}Swcj!u^-RPMNws%Buzs#NQBC{Y4eg?U zHc4^5*OQrgt%?r{2<+&wwb6S}pwo0ypL?;G?bU~EyJu~Z&pFEbdf}RirjIL`^lrL4 z?XzCz{%B`RfiIg|UIb^}+4HiJ65DSlp5=Y-pFY2?)aD-3fgWb72VpnotL%8Y?%-Xm zNf+j9loLPTR)37s+3x`7(T_ zh$bI6mbEq4wnIN~$MIux&Gs#u5g&2o5clf$4gdKs24>%!T`l&_S2(06QMEU`%xCOUaxy|We*s8a>T99$*uc%S9&?i{3pI8H)Hxug;p++D?7VbTxsU^zopr02S=r6951J diff --git a/packages/nodes-base/nodes/MongoDb/mongodb.svg b/packages/nodes-base/nodes/MongoDb/mongodb.svg new file mode 100644 index 0000000000..80d3a99ac2 --- /dev/null +++ b/packages/nodes-base/nodes/MongoDb/mongodb.svg @@ -0,0 +1 @@ + \ No newline at end of file