feat(MongoDb Node): Add Aggregate Operation

* MongoDB Aggregate Option

*  small improvements to UI

Co-authored-by: Michael Kret <michael.k@radency.com>
This commit is contained in:
Luiz Eduardo de Oliveira Fonseca 2022-03-12 16:01:44 -03:00 committed by GitHub
parent 195f104ef5
commit 2c9a06e863
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 5 deletions

View file

@ -7,6 +7,7 @@ import {
INodeExecutionData, INodeExecutionData,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
JsonObject,
NodeOperationError NodeOperationError
} from 'n8n-workflow'; } from 'n8n-workflow';
@ -46,7 +47,33 @@ export class MongoDb implements INodeType {
const items = this.getInputData(); const items = this.getInputData();
const operation = this.getNodeParameter('operation', 0) as string; const operation = this.getNodeParameter('operation', 0) as string;
if (operation === 'delete') { if (operation === 'aggregate') {
// ----------------------------------
// aggregate
// ----------------------------------
try {
const queryParameter = JSON.parse(this.getNodeParameter('query', 0) as string);
if (queryParameter._id && typeof queryParameter._id === 'string') {
queryParameter._id = new ObjectID(queryParameter._id);
}
const query = mdb
.collection(this.getNodeParameter('collection', 0) as string)
.aggregate(queryParameter);
const queryResult = await query.toArray();
returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]);
} catch (error) {
if (this.continueOnFail()) {
returnItems = this.helpers.returnJsonArray({ error: (error as JsonObject).message } );
} else {
throw error;
}
}
} else if (operation === 'delete') {
// ---------------------------------- // ----------------------------------
// delete // delete
// ---------------------------------- // ----------------------------------
@ -59,7 +86,7 @@ export class MongoDb implements INodeType {
returnItems = this.helpers.returnJsonArray([{ deletedCount }]); returnItems = this.helpers.returnJsonArray([{ deletedCount }]);
} catch (error) { } catch (error) {
if (this.continueOnFail()) { if (this.continueOnFail()) {
returnItems = this.helpers.returnJsonArray({ error: error.message }); returnItems = this.helpers.returnJsonArray({ error: (error as JsonObject).message });
} else { } else {
throw error; throw error;
} }
@ -99,7 +126,7 @@ export class MongoDb implements INodeType {
returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]); returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]);
} catch (error) { } catch (error) {
if (this.continueOnFail()) { if (this.continueOnFail()) {
returnItems = this.helpers.returnJsonArray({ error: error.message } ); returnItems = this.helpers.returnJsonArray({ error: (error as JsonObject).message } );
} else { } else {
throw error; throw error;
} }
@ -137,7 +164,7 @@ export class MongoDb implements INodeType {
} }
} catch (error) { } catch (error) {
if (this.continueOnFail()) { if (this.continueOnFail()) {
returnItems = this.helpers.returnJsonArray({ error: error.message }); returnItems = this.helpers.returnJsonArray({ error: (error as JsonObject).message });
} else { } else {
throw error; throw error;
} }
@ -188,7 +215,7 @@ export class MongoDb implements INodeType {
.updateOne(filter, { $set: item }, updateOptions); .updateOne(filter, { $set: item }, updateOptions);
} catch (error) { } catch (error) {
if (this.continueOnFail()) { if (this.continueOnFail()) {
item.json = { error: error.message }; item.json = { error: (error as JsonObject).message };
continue; continue;
} }
throw error; throw error;

View file

@ -29,6 +29,11 @@ export const nodeDescription: INodeTypeDescription = {
name: 'operation', name: 'operation',
type: 'options', type: 'options',
options: [ options: [
{
name: 'Aggregate',
value: 'aggregate',
description: 'Aggregate documents.',
},
{ {
name: 'Delete', name: 'Delete',
value: 'delete', value: 'delete',
@ -63,6 +68,30 @@ export const nodeDescription: INodeTypeDescription = {
description: 'MongoDB Collection', description: 'MongoDB Collection',
}, },
// ----------------------------------
// aggregate
// ----------------------------------
{
displayName: 'Query',
name: 'query',
type: 'json',
typeOptions: {
alwaysOpenEditWindow: true,
},
displayOptions: {
show: {
operation: [
'aggregate',
],
},
},
default: '',
placeholder: `[{ "$match": { "$gt": "1950-01-01" }, ... }]`,
hint: 'Learn more about aggregation pipeline <a href="https://docs.mongodb.com/manual/core/aggregation-pipeline/">here</a>',
required: true,
description: 'MongoDB aggregation pipeline query in JSON format',
},
// ---------------------------------- // ----------------------------------
// delete // delete
// ---------------------------------- // ----------------------------------
@ -149,6 +178,7 @@ export const nodeDescription: INodeTypeDescription = {
required: true, required: true,
description: 'MongoDB Find query.', description: 'MongoDB Find query.',
}, },
// ---------------------------------- // ----------------------------------
// insert // insert
// ---------------------------------- // ----------------------------------