Small fixes and changes to MongoDB-Node

This commit is contained in:
Jan Oberhauser 2019-10-27 22:45:48 +01:00
parent 75a25431d1
commit a23f3fdbbc
4 changed files with 33 additions and 25 deletions

View file

@ -4,8 +4,8 @@ import {
} from 'n8n-workflow';
export class MongoDB implements ICredentialType {
name = 'mongodb';
export class MongoDb implements ICredentialType {
name = 'mongoDb';
displayName = 'MongoDB';
properties = [
{
@ -39,7 +39,7 @@ export class MongoDB implements ICredentialType {
displayName: 'Port',
name: 'port',
type: 'number' as NodePropertyTypes,
default: 0
default: 27017,
},
];
}

View file

@ -34,11 +34,11 @@ function getItemCopy(items: INodeExecutionData[], properties: string[]): IDataOb
}
export class MongoDB implements INodeType {
export class MongoDb implements INodeType {
description: INodeTypeDescription = {
displayName: 'MongoDB',
name: 'mongodb',
icon: 'file:mongodb.png',
name: 'mongoDb',
icon: 'file:mongoDb.png',
group: ['input'],
version: 1,
description: 'Find, insert and update documents in MongoDB.',
@ -50,7 +50,7 @@ export class MongoDB implements INodeType {
outputs: ['main'],
credentials: [
{
name: 'mongodb',
name: 'mongoDb',
required: true,
}
],
@ -173,19 +173,19 @@ export class MongoDB implements INodeType {
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const credentials = this.getCredentials('mongodb');
const credentials = this.getCredentials('mongoDb');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
let connectionUri = ''
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}`
}
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);
@ -216,20 +216,20 @@ export class MongoDB implements INodeType {
const fields = (this.getNodeParameter('fields', 0) as string)
.split(',')
.map(f => f.trim())
.filter(f => !!f)
.filter(f => !!f);
const insertItems = getItemCopy(items, fields);
const { insertedIds } = await mdb
.collection(this.getNodeParameter('collection', 0) as string)
.insertMany(insertItems)
.insertMany(insertItems);
// Add the id to the data
for (let i in insertedIds) {
for (const i of Object.keys(insertedIds)) {
returnItems.push({
json: {
...insertItems[i],
id: insertedIds[i] as string,
...insertItems[parseInt(i, 10)],
id: insertedIds[parseInt(i, 10)] as string,
}
});
}
@ -241,21 +241,29 @@ export class MongoDB implements INodeType {
const fields = (this.getNodeParameter('fields', 0) as string)
.split(',')
.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
const updateItems = getItemCopy(items, fields);
const updateKey = this.getNodeParameter('updateKey', 0) as string;
for (let item of updateItems) {
if (item[updateKey] === undefined) { continue }
for (const item of updateItems) {
if (item[updateKey] === undefined) {
continue;
}
const filter: { [key: string] :string } = {};
filter[updateKey] = item[updateKey] as string;
await mdb
.collection(this.getNodeParameter('collection', 0) as string)
.updateOne(filter, item)
.updateOne(filter, { $set: item });
}
returnItems = this.helpers.returnJsonArray(updateItems as IDataObject[]);

View file

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View file

@ -42,7 +42,7 @@
"dist/credentials/LinkFishApi.credentials.js",
"dist/credentials/MailgunApi.credentials.js",
"dist/credentials/MattermostApi.credentials.js",
"dist/credentials/MongoDB.credentials.js",
"dist/credentials/MongoDb.credentials.js",
"dist/credentials/NextCloudApi.credentials.js",
"dist/credentials/OpenWeatherMapApi.credentials.js",
"dist/credentials/PipedriveApi.credentials.js",
@ -87,7 +87,7 @@
"dist/nodes/Mailgun/Mailgun.node.js",
"dist/nodes/Mattermost/Mattermost.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/NoOp.node.js",
"dist/nodes/OpenWeatherMap.node.js",