mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-24 04:04:06 -08:00
⚡ Add "Date Fields" parameter to the insert & update operations
MongoDB
This commit is contained in:
parent
9b23611a8d
commit
ef9178d954
|
@ -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!`);
|
||||
|
|
|
@ -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.',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 1,018 B |
1
packages/nodes-base/nodes/MongoDb/mongodb.svg
Normal file
1
packages/nodes-base/nodes/MongoDb/mongodb.svg
Normal file
|
@ -0,0 +1 @@
|
|||
<svg height="64" viewBox="0 0 32 32" width="64" xmlns="http://www.w3.org/2000/svg"><path d="m15.9.087.854 1.604c.192.296.4.558.645.802.715.715 1.394 1.464 2.004 2.266 1.447 1.9 2.423 4.01 3.12 6.292.418 1.394.645 2.824.662 4.27.07 4.323-1.412 8.035-4.4 11.12-.488.488-1.01.94-1.57 1.342-.296 0-.436-.227-.558-.436-.227-.383-.366-.82-.436-1.255-.105-.523-.174-1.046-.14-1.586v-.244c-.024-.052-.285-24.052-.181-24.175z" fill="#599636"/><path d="m15.9.034c-.035-.07-.07-.017-.105.017.017.35-.105.662-.296.96-.21.296-.488.523-.767.767-1.55 1.342-2.77 2.963-3.747 4.776-1.3 2.44-1.97 5.055-2.16 7.808-.087.993.314 4.497.627 5.508.854 2.684 2.388 4.933 4.375 6.885.488.47 1.01.906 1.55 1.325.157 0 .174-.14.21-.244a4.78 4.78 0 0 0 .157-.68l.35-2.614z" fill="#6cac48"/><path d="m16.754 28.845c.035-.4.227-.732.436-1.063-.21-.087-.366-.26-.488-.453-.105-.174-.192-.383-.26-.575-.244-.732-.296-1.5-.366-2.248v-.453c-.087.07-.105.662-.105.75a17.37 17.37 0 0 1 -.314 2.353c-.052.314-.087.627-.28.906 0 .035 0 .07.017.122.314.924.4 1.865.453 2.824v.35c0 .418-.017.33.33.47.14.052.296.07.436.174.105 0 .122-.087.122-.157l-.052-.575v-1.604c-.017-.28.035-.558.07-.82z" fill="#c2bfbf"/></svg>
|
After Width: | Height: | Size: 1.1 KiB |
Loading…
Reference in a new issue