enhance .editorconfig and apply formatting cleanup to last commit

This commit is contained in:
Chris Lonardo
2020-04-06 14:04:59 -04:00
parent c6aa73cc2f
commit 00fc6e5980
5 changed files with 140 additions and 133 deletions

View File

@@ -1,41 +1,41 @@
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 { getItemCopy, buildParameterizedConnString } from "./mongo.node.utils";
INodeTypeDescription
} from 'n8n-workflow';
import { nodeDescription } from './mongo.node.options';
import { MongoClient } from 'mongodb';
import { getItemCopy, buildParameterizedConnString } from './mongo.node.utils';
export class MongoDb implements INodeType {
description: INodeTypeDescription = nodeDescription;
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const credentials = this.getCredentials("mongoDb");
const credentials = this.getCredentials('mongoDb');
// user can optionally override connection string
const useOverriddenConnString = this.getNodeParameter(
"shouldOverrideConnString",
'shouldOverrideConnString',
0
) as boolean;
if (credentials === undefined) {
throw new Error("No credentials got returned!");
throw new Error('No credentials got returned!');
}
let connectionUri = "";
let connectionUri = '';
if (useOverriddenConnString === true) {
const connStringInput = this.getNodeParameter(
"connStringOverrideVal",
'connStringOverrideVal',
0
) as string;
if (connStringInput && connStringInput.length > 0) {
connectionUri = connStringInput;
} else {
throw new Error(
"Cannot override credentials: valid MongoDB connection string not provided "
'Cannot override credentials: valid MongoDB connection string not provided '
);
}
} else {
@@ -44,7 +44,7 @@ export class MongoDb implements INodeType {
const client: MongoClient = await MongoClient.connect(connectionUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useUnifiedTopology: true
});
const mdb = client.db(credentials.database as string);
@@ -52,34 +52,34 @@ export class MongoDb implements INodeType {
let returnItems = [];
const items = this.getInputData();
const operation = this.getNodeParameter("operation", 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
if (operation === "find") {
if (operation === 'find') {
// ----------------------------------
// find
// ----------------------------------
const queryResult = await mdb
.collection(this.getNodeParameter("collection", 0) as string)
.find(JSON.parse(this.getNodeParameter("query", 0) as string))
.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") {
} else if (operation === 'insert') {
// ----------------------------------
// insert
// ----------------------------------
// 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 fields = (this.getNodeParameter('fields', 0) as string)
.split(',')
.map(f => f.trim())
.filter(f => !!f);
const insertItems = getItemCopy(items, fields);
const { insertedIds } = await mdb
.collection(this.getNodeParameter("collection", 0) as string)
.collection(this.getNodeParameter('collection', 0) as string)
.insertMany(insertItems);
// Add the id to the data
@@ -87,21 +87,21 @@ export class MongoDb implements INodeType {
returnItems.push({
json: {
...insertItems[parseInt(i, 10)],
id: insertedIds[parseInt(i, 10)] as string,
},
id: insertedIds[parseInt(i, 10)] as string
}
});
}
} else if (operation === "update") {
} else if (operation === 'update') {
// ----------------------------------
// update
// ----------------------------------
const fields = (this.getNodeParameter("fields", 0) as string)
.split(",")
.map((f) => f.trim())
.filter((f) => !!f);
const fields = (this.getNodeParameter('fields', 0) as string)
.split(',')
.map(f => f.trim())
.filter(f => !!f);
let updateKey = this.getNodeParameter("updateKey", 0) as string;
let updateKey = this.getNodeParameter('updateKey', 0) as string;
updateKey = updateKey.trim();
if (!fields.includes(updateKey)) {
@@ -120,7 +120,7 @@ export class MongoDb implements INodeType {
filter[updateKey] = item[updateKey] as string;
await mdb
.collection(this.getNodeParameter("collection", 0) as string)
.collection(this.getNodeParameter('collection', 0) as string)
.updateOne(filter, { $set: item });
}