feature: add MongoDB credential testing and two operations: findOneAndReplace and findOneAndUpdate (#3901)

* feature: add MongoDB credential testing and two operations: findOneAndReplace and findOneAndUpdate
Co-authored-by: Anas Naim <anas.naim@hotmail.com>
This commit is contained in:
Michael Kret 2022-09-01 11:23:15 +03:00 committed by GitHub
parent ee519b0c08
commit b5511e5ac7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 219 additions and 95 deletions

View file

@ -1,7 +1,10 @@
import { IExecuteFunctions } from 'n8n-core'; import { IExecuteFunctions } from 'n8n-core';
import { import {
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject, IDataObject,
INodeCredentialTestResult,
INodeExecutionData, INodeExecutionData,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
@ -11,18 +14,62 @@ import {
import { nodeDescription } from './mongo.node.options'; import { nodeDescription } from './mongo.node.options';
import { buildParameterizedConnString, prepareFields, prepareItems } from './mongo.node.utils';
import { MongoClient, ObjectID } from 'mongodb'; import { MongoClient, ObjectID } from 'mongodb';
import { import { validateAndResolveMongoCredentials } from './mongo.node.utils';
getItemCopy,
handleDateFields, import { IMongoParametricCredentials } from './mongo.node.types';
handleDateFieldsWithDotNotation,
validateAndResolveMongoCredentials,
} from './mongo.node.utils';
export class MongoDb implements INodeType { export class MongoDb implements INodeType {
description: INodeTypeDescription = nodeDescription; description: INodeTypeDescription = nodeDescription;
methods = {
credentialTest: {
async mongoDbCredentialTest(
this: ICredentialTestFunctions,
credential: ICredentialsDecrypted,
): Promise<INodeCredentialTestResult> {
const credentials = credential.data as IDataObject;
try {
const database = ((credentials.database as string) || '').trim();
let connectionString = '';
if (credentials.configurationType === 'connectionString') {
connectionString = ((credentials.connectionString as string) || '').trim();
} else {
connectionString = buildParameterizedConnString(
credentials as unknown as IMongoParametricCredentials,
);
}
const client: MongoClient = await MongoClient.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const { databases } = await client.db().admin().listDatabases();
if (!(databases as IDataObject[]).map((db) => db.name).includes(database)) {
// eslint-disable-next-line n8n-nodes-base/node-execute-block-wrong-error-thrown
throw new Error(`Database "${database}" does not exist`);
}
client.close();
} catch (error) {
return {
status: 'Error',
message: error.message,
};
}
return {
status: 'OK',
message: 'Connection successful!',
};
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const { database, connectionString } = validateAndResolveMongoCredentials( const { database, connectionString } = validateAndResolveMongoCredentials(
this, this,
@ -59,10 +106,9 @@ export class MongoDb implements INodeType {
.aggregate(queryParameter); .aggregate(queryParameter);
responseData = await query.toArray(); responseData = await query.toArray();
} catch (error) { } catch (error) {
if (this.continueOnFail()) { if (this.continueOnFail()) {
responseData = [ { error: (error as JsonObject).message } ]; responseData = [{ error: (error as JsonObject).message }];
} else { } else {
throw error; throw error;
} }
@ -124,25 +170,99 @@ export class MongoDb implements INodeType {
throw error; throw error;
} }
} }
} else if (operation === 'findOneAndReplace') {
// ----------------------------------
// findOneAndReplace
// ----------------------------------
const fields = prepareFields(this.getNodeParameter('fields', 0) as string);
const useDotNotation = this.getNodeParameter('options.useDotNotation', 0, false) as boolean;
const dateFields = prepareFields(
this.getNodeParameter('options.dateFields', 0, '') as string,
);
const updateKey = ((this.getNodeParameter('updateKey', 0) as string) || '').trim();
const updateOptions = (this.getNodeParameter('upsert', 0) as boolean)
? { upsert: true }
: undefined;
const updateItems = prepareItems(items, fields, updateKey, useDotNotation, dateFields);
for (const item of updateItems) {
try {
const filter = { [updateKey]: item[updateKey] };
if (updateKey === '_id') {
filter[updateKey] = new ObjectID(item[updateKey] as string);
delete item['_id'];
}
await mdb
.collection(this.getNodeParameter('collection', 0) as string)
.findOneAndReplace(filter, item, updateOptions);
} catch (error) {
if (this.continueOnFail()) {
item.json = { error: (error as JsonObject).message };
continue;
}
throw error;
}
}
responseData = updateItems;
} else if (operation === 'findOneAndUpdate') {
// ----------------------------------
// findOneAndUpdate
// ----------------------------------
const fields = prepareFields(this.getNodeParameter('fields', 0) as string);
const useDotNotation = this.getNodeParameter('options.useDotNotation', 0, false) as boolean;
const dateFields = prepareFields(
this.getNodeParameter('options.dateFields', 0, '') as string,
);
const updateKey = ((this.getNodeParameter('updateKey', 0) as string) || '').trim();
const updateOptions = (this.getNodeParameter('upsert', 0) as boolean)
? { upsert: true }
: undefined;
const updateItems = prepareItems(items, fields, updateKey, useDotNotation, dateFields);
for (const item of updateItems) {
try {
const filter = { [updateKey]: item[updateKey] };
if (updateKey === '_id') {
filter[updateKey] = new ObjectID(item[updateKey] as string);
delete item['_id'];
}
await mdb
.collection(this.getNodeParameter('collection', 0) as string)
.findOneAndUpdate(filter, { $set: item }, updateOptions);
} catch (error) {
if (this.continueOnFail()) {
item.json = { error: (error as JsonObject).message };
continue;
}
throw error;
}
}
responseData = updateItems;
} else if (operation === 'insert') { } else if (operation === 'insert') {
// ---------------------------------- // ----------------------------------
// insert // insert
// ---------------------------------- // ----------------------------------
try { try {
// Prepare the data to insert and copy it to be returned // Prepare the data to insert and copy it to be returned
const fields = (this.getNodeParameter('fields', 0) as string) const fields = prepareFields(this.getNodeParameter('fields', 0) as string);
.split(',') const useDotNotation = this.getNodeParameter('options.useDotNotation', 0, false) as boolean;
.map((f) => f.trim()) const dateFields = prepareFields(
.filter((f) => !!f); this.getNodeParameter('options.dateFields', 0, '') as string,
);
const options = this.getNodeParameter('options', 0) as IDataObject; const insertItems = prepareItems(items, fields, '', useDotNotation, dateFields);
const insertItems = getItemCopy(items, fields);
if (options.dateFields && !options.useDotNotation) {
handleDateFields(insertItems, options.dateFields as string);
} else if (options.dateFields && options.useDotNotation) {
handleDateFieldsWithDotNotation(insertItems, options.dateFields as string);
}
const { insertedIds } = await mdb const { insertedIds } = await mdb
.collection(this.getNodeParameter('collection', 0) as string) .collection(this.getNodeParameter('collection', 0) as string)
@ -167,45 +287,28 @@ export class MongoDb implements INodeType {
// update // update
// ---------------------------------- // ----------------------------------
const fields = (this.getNodeParameter('fields', 0) as string) const fields = prepareFields(this.getNodeParameter('fields', 0) as string);
.split(',') const useDotNotation = this.getNodeParameter('options.useDotNotation', 0, false) as boolean;
.map((f) => f.trim()) const dateFields = prepareFields(
.filter((f) => !!f); this.getNodeParameter('options.dateFields', 0, '') as string,
);
const options = this.getNodeParameter('options', 0) as IDataObject; const updateKey = ((this.getNodeParameter('updateKey', 0) as string) || '').trim();
let updateKey = this.getNodeParameter('updateKey', 0) as string;
updateKey = updateKey.trim();
const updateOptions = (this.getNodeParameter('upsert', 0) as boolean) const updateOptions = (this.getNodeParameter('upsert', 0) as boolean)
? { upsert: true } ? { upsert: true }
: undefined; : undefined;
if (!fields.includes(updateKey)) { const updateItems = prepareItems(items, fields, updateKey, useDotNotation, dateFields);
fields.push(updateKey);
}
// Prepare the data to update and copy it to be returned
const updateItems = getItemCopy(items, fields);
if (options.dateFields && !options.useDotNotation) {
handleDateFields(updateItems, options.dateFields as string);
} else if (options.dateFields && options.useDotNotation) {
handleDateFieldsWithDotNotation(updateItems, options.dateFields as string);
}
for (const item of updateItems) { for (const item of updateItems) {
try { try {
if (item[updateKey] === undefined) { const filter = { [updateKey]: item[updateKey] };
continue;
}
const filter: { [key: string]: string | ObjectID } = {};
filter[updateKey] = item[updateKey] as string;
if (updateKey === '_id') { if (updateKey === '_id') {
filter[updateKey] = new ObjectID(filter[updateKey]); filter[updateKey] = new ObjectID(item[updateKey] as string);
delete item['_id']; delete item['_id'];
} }
await mdb await mdb
.collection(this.getNodeParameter('collection', 0) as string) .collection(this.getNodeParameter('collection', 0) as string)
.updateOne(filter, { $set: item }, updateOptions); .updateOne(filter, { $set: item }, updateOptions);
@ -223,7 +326,11 @@ export class MongoDb implements INodeType {
if (this.continueOnFail()) { if (this.continueOnFail()) {
responseData = [{ error: `The operation "${operation}" is not supported!` }]; responseData = [{ error: `The operation "${operation}" is not supported!` }];
} else { } else {
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not supported!`, {itemIndex: 0}); throw new NodeOperationError(
this.getNode(),
`The operation "${operation}" is not supported!`,
{ itemIndex: 0 },
);
} }
} }

View file

@ -20,6 +20,7 @@ export const nodeDescription: INodeTypeDescription = {
{ {
name: 'mongoDb', name: 'mongoDb',
required: true, required: true,
testedBy: 'mongoDbCredentialTest',
}, },
], ],
properties: [ properties: [
@ -47,6 +48,18 @@ export const nodeDescription: INodeTypeDescription = {
description: 'Find documents', description: 'Find documents',
action: 'Find documents', action: 'Find documents',
}, },
{
name: 'Find And Replace',
value: 'findOneAndReplace',
description: 'Find and replace documents',
action: 'Find and replace documents',
},
{
name: 'Find And Update',
value: 'findOneAndUpdate',
description: 'Find and update documents',
action: 'Find and update documents',
},
{ {
name: 'Insert', name: 'Insert',
value: 'insert', value: 'insert',
@ -207,7 +220,7 @@ export const nodeDescription: INodeTypeDescription = {
type: 'string', type: 'string',
displayOptions: { displayOptions: {
show: { show: {
operation: ['update'], operation: ['update', 'findOneAndReplace', 'findOneAndUpdate'],
}, },
}, },
default: 'id', default: 'id',
@ -222,7 +235,7 @@ export const nodeDescription: INodeTypeDescription = {
type: 'string', type: 'string',
displayOptions: { displayOptions: {
show: { show: {
operation: ['update'], operation: ['update', 'findOneAndReplace', 'findOneAndUpdate'],
}, },
}, },
default: '', default: '',
@ -235,7 +248,7 @@ export const nodeDescription: INodeTypeDescription = {
type: 'boolean', type: 'boolean',
displayOptions: { displayOptions: {
show: { show: {
operation: ['update'], operation: ['update', 'findOneAndReplace', 'findOneAndUpdate'],
}, },
}, },
default: false, default: false,
@ -247,7 +260,7 @@ export const nodeDescription: INodeTypeDescription = {
type: 'collection', type: 'collection',
displayOptions: { displayOptions: {
show: { show: {
operation: ['update', 'insert'], operation: ['update', 'insert', 'findOneAndReplace', 'findOneAndUpdate'],
}, },
}, },
placeholder: 'Add Option', placeholder: 'Add Option',

View file

@ -1,10 +1,12 @@
import { IExecuteFunctions } from 'n8n-core'; import { IExecuteFunctions } from 'n8n-core';
import { import {
ICredentialDataDecryptedObject, ICredentialDataDecryptedObject,
IDataObject, IDataObject,
INodeExecutionData, INodeExecutionData,
NodeOperationError, NodeOperationError,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
IMongoCredentials, IMongoCredentials,
IMongoCredentialsType, IMongoCredentialsType,
@ -18,7 +20,7 @@ import { get, set } from 'lodash';
* *
* @param {ICredentialDataDecryptedObject} credentials MongoDB credentials to use, unless conn string is overridden * @param {ICredentialDataDecryptedObject} credentials MongoDB credentials to use, unless conn string is overridden
*/ */
function buildParameterizedConnString(credentials: IMongoParametricCredentials): string { export function buildParameterizedConnString(credentials: IMongoParametricCredentials): string {
if (credentials.port) { if (credentials.port) {
return `mongodb://${credentials.user}:${credentials.password}@${credentials.host}:${credentials.port}`; return `mongodb://${credentials.user}:${credentials.password}@${credentials.host}:${credentials.port}`;
} else { } else {
@ -78,52 +80,54 @@ export function validateAndResolveMongoCredentials(
} }
} }
/** export function prepareItems(
* Returns of copy of the items which only contains the json data and items: INodeExecutionData[],
* of that only the define properties fields: string[],
* updateKey = '',
* @param {INodeExecutionData[]} items The items to copy useDotNotation = false,
* @param {string[]} properties The properties it should include dateFields: string[] = [],
* @returns ) {
*/ let data = items;
export function getItemCopy(items: INodeExecutionData[], properties: string[]): IDataObject[] {
// Prepare the data to insert and copy it to be returned if (updateKey) {
let newItem: IDataObject; if (!fields.includes(updateKey)) {
return items.map((item) => { fields.push(updateKey);
newItem = {}; }
for (const property of properties) { data = items.filter((item) => item.json[updateKey] !== undefined);
if (item.json[property] === undefined) { }
newItem[property] = null;
const preperedItems = data.map(({ json }) => {
const updateItem: IDataObject = {};
for (const field of fields) {
let fieldData;
if (useDotNotation) {
fieldData = get(json, field, null);
} else { } else {
newItem[property] = JSON.parse(JSON.stringify(item.json[property])); fieldData = json[field] !== undefined ? json[field] : null;
}
if (fieldData && dateFields.includes(field)) {
fieldData = new Date(fieldData as string);
}
if (useDotNotation) {
set(updateItem, field, fieldData);
} else {
updateItem[field] = fieldData;
} }
} }
return newItem;
return updateItem;
}); });
return preperedItems;
} }
export function handleDateFields(insertItems: IDataObject[], fields: string) { export function prepareFields(fields: string) {
const dateFields = (fields as string).split(','); return fields
for (let i = 0; i < insertItems.length; i++) { .split(',')
for (const key of Object.keys(insertItems[i])) { .map((field) => field.trim())
if (dateFields.includes(key)) { .filter((field) => !!field);
insertItems[i][key] = new Date(insertItems[i][key] as string);
}
}
}
}
export function handleDateFieldsWithDotNotation(insertItems: IDataObject[], fields: string) {
const dateFields = fields.split(',').map((field) => field.trim());
for (let i = 0; i < insertItems.length; i++) {
for (const field of dateFields) {
const fieldValue = get(insertItems[i], field) as string;
const date = new Date(fieldValue);
if (fieldValue && !isNaN(date.valueOf())) {
set(insertItems[i], field, date);
}
}
}
} }