mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 20:54:07 -08:00
⚡ Small fixes and changes to MongoDB-Node
This commit is contained in:
parent
75a25431d1
commit
a23f3fdbbc
|
@ -4,8 +4,8 @@ import {
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
|
|
||||||
export class MongoDB implements ICredentialType {
|
export class MongoDb implements ICredentialType {
|
||||||
name = 'mongodb';
|
name = 'mongoDb';
|
||||||
displayName = 'MongoDB';
|
displayName = 'MongoDB';
|
||||||
properties = [
|
properties = [
|
||||||
{
|
{
|
||||||
|
@ -39,7 +39,7 @@ export class MongoDB implements ICredentialType {
|
||||||
displayName: 'Port',
|
displayName: 'Port',
|
||||||
name: 'port',
|
name: 'port',
|
||||||
type: 'number' as NodePropertyTypes,
|
type: 'number' as NodePropertyTypes,
|
||||||
default: 0
|
default: 27017,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
|
@ -34,11 +34,11 @@ function getItemCopy(items: INodeExecutionData[], properties: string[]): IDataOb
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export class MongoDB implements INodeType {
|
export class MongoDb implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
displayName: 'MongoDB',
|
displayName: 'MongoDB',
|
||||||
name: 'mongodb',
|
name: 'mongoDb',
|
||||||
icon: 'file:mongodb.png',
|
icon: 'file:mongoDb.png',
|
||||||
group: ['input'],
|
group: ['input'],
|
||||||
version: 1,
|
version: 1,
|
||||||
description: 'Find, insert and update documents in MongoDB.',
|
description: 'Find, insert and update documents in MongoDB.',
|
||||||
|
@ -50,7 +50,7 @@ export class MongoDB implements INodeType {
|
||||||
outputs: ['main'],
|
outputs: ['main'],
|
||||||
credentials: [
|
credentials: [
|
||||||
{
|
{
|
||||||
name: 'mongodb',
|
name: 'mongoDb',
|
||||||
required: true,
|
required: true,
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -173,19 +173,19 @@ export class MongoDB implements INodeType {
|
||||||
|
|
||||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||||
|
|
||||||
const credentials = this.getCredentials('mongodb');
|
const credentials = this.getCredentials('mongoDb');
|
||||||
|
|
||||||
if (credentials === undefined) {
|
if (credentials === undefined) {
|
||||||
throw new Error('No credentials got returned!');
|
throw new Error('No credentials got returned!');
|
||||||
}
|
}
|
||||||
|
|
||||||
let connectionUri = ''
|
let connectionUri = '';
|
||||||
|
|
||||||
if (credentials.port) {
|
if (credentials.port) {
|
||||||
connectionUri = `mongodb://${credentials.user}:${credentials.password}@${credentials.host}:${credentials.port}`
|
connectionUri = `mongodb://${credentials.user}:${credentials.password}@${credentials.host}:${credentials.port}`;
|
||||||
} else {
|
} else {
|
||||||
connectionUri = `mongodb+srv://${credentials.user}:${credentials.password}@${credentials.host}`
|
connectionUri = `mongodb+srv://${credentials.user}:${credentials.password}@${credentials.host}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = await MongoClient.connect(connectionUri, { useNewUrlParser: true, useUnifiedTopology: true });
|
const client = await MongoClient.connect(connectionUri, { useNewUrlParser: true, useUnifiedTopology: true });
|
||||||
const mdb = client.db(credentials.database as string);
|
const mdb = client.db(credentials.database as string);
|
||||||
|
@ -216,20 +216,20 @@ export class MongoDB implements INodeType {
|
||||||
const fields = (this.getNodeParameter('fields', 0) as string)
|
const fields = (this.getNodeParameter('fields', 0) as string)
|
||||||
.split(',')
|
.split(',')
|
||||||
.map(f => f.trim())
|
.map(f => f.trim())
|
||||||
.filter(f => !!f)
|
.filter(f => !!f);
|
||||||
|
|
||||||
const insertItems = getItemCopy(items, fields);
|
const insertItems = getItemCopy(items, fields);
|
||||||
|
|
||||||
const { insertedIds } = await mdb
|
const { insertedIds } = await mdb
|
||||||
.collection(this.getNodeParameter('collection', 0) as string)
|
.collection(this.getNodeParameter('collection', 0) as string)
|
||||||
.insertMany(insertItems)
|
.insertMany(insertItems);
|
||||||
|
|
||||||
// Add the id to the data
|
// Add the id to the data
|
||||||
for (let i in insertedIds) {
|
for (const i of Object.keys(insertedIds)) {
|
||||||
returnItems.push({
|
returnItems.push({
|
||||||
json: {
|
json: {
|
||||||
...insertItems[i],
|
...insertItems[parseInt(i, 10)],
|
||||||
id: insertedIds[i] as string,
|
id: insertedIds[parseInt(i, 10)] as string,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -241,21 +241,29 @@ export class MongoDB implements INodeType {
|
||||||
const fields = (this.getNodeParameter('fields', 0) as string)
|
const fields = (this.getNodeParameter('fields', 0) as string)
|
||||||
.split(',')
|
.split(',')
|
||||||
.map(f => f.trim())
|
.map(f => f.trim())
|
||||||
.filter(f => !!f)
|
.filter(f => !!f);
|
||||||
|
|
||||||
|
let updateKey = this.getNodeParameter('updateKey', 0) as string;
|
||||||
|
updateKey = updateKey.trim();
|
||||||
|
|
||||||
|
if (!fields.includes(updateKey)) {
|
||||||
|
fields.push(updateKey);
|
||||||
|
}
|
||||||
|
|
||||||
// Prepare the data to update and copy it to be returned
|
// Prepare the data to update and copy it to be returned
|
||||||
const updateItems = getItemCopy(items, fields);
|
const updateItems = getItemCopy(items, fields);
|
||||||
const updateKey = this.getNodeParameter('updateKey', 0) as string;
|
|
||||||
|
|
||||||
for (let item of updateItems) {
|
for (const item of updateItems) {
|
||||||
if (item[updateKey] === undefined) { continue }
|
if (item[updateKey] === undefined) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const filter: { [key: string] :string } = {};
|
const filter: { [key: string] :string } = {};
|
||||||
filter[updateKey] = item[updateKey] as string;
|
filter[updateKey] = item[updateKey] as string;
|
||||||
|
|
||||||
await mdb
|
await mdb
|
||||||
.collection(this.getNodeParameter('collection', 0) as string)
|
.collection(this.getNodeParameter('collection', 0) as string)
|
||||||
.updateOne(filter, item)
|
.updateOne(filter, { $set: item });
|
||||||
}
|
}
|
||||||
|
|
||||||
returnItems = this.helpers.returnJsonArray(updateItems as IDataObject[]);
|
returnItems = this.helpers.returnJsonArray(updateItems as IDataObject[]);
|
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB |
|
@ -42,7 +42,7 @@
|
||||||
"dist/credentials/LinkFishApi.credentials.js",
|
"dist/credentials/LinkFishApi.credentials.js",
|
||||||
"dist/credentials/MailgunApi.credentials.js",
|
"dist/credentials/MailgunApi.credentials.js",
|
||||||
"dist/credentials/MattermostApi.credentials.js",
|
"dist/credentials/MattermostApi.credentials.js",
|
||||||
"dist/credentials/MongoDB.credentials.js",
|
"dist/credentials/MongoDb.credentials.js",
|
||||||
"dist/credentials/NextCloudApi.credentials.js",
|
"dist/credentials/NextCloudApi.credentials.js",
|
||||||
"dist/credentials/OpenWeatherMapApi.credentials.js",
|
"dist/credentials/OpenWeatherMapApi.credentials.js",
|
||||||
"dist/credentials/PipedriveApi.credentials.js",
|
"dist/credentials/PipedriveApi.credentials.js",
|
||||||
|
@ -87,7 +87,7 @@
|
||||||
"dist/nodes/Mailgun/Mailgun.node.js",
|
"dist/nodes/Mailgun/Mailgun.node.js",
|
||||||
"dist/nodes/Mattermost/Mattermost.node.js",
|
"dist/nodes/Mattermost/Mattermost.node.js",
|
||||||
"dist/nodes/Merge.node.js",
|
"dist/nodes/Merge.node.js",
|
||||||
"dist/nodes/MongoDB/MongoDB.node.js",
|
"dist/nodes/MongoDb/MongoDb.node.js",
|
||||||
"dist/nodes/NextCloud/NextCloud.node.js",
|
"dist/nodes/NextCloud/NextCloud.node.js",
|
||||||
"dist/nodes/NoOp.node.js",
|
"dist/nodes/NoOp.node.js",
|
||||||
"dist/nodes/OpenWeatherMap.node.js",
|
"dist/nodes/OpenWeatherMap.node.js",
|
||||||
|
|
Loading…
Reference in a new issue