2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
2021-03-10 08:01:03 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2022-09-01 01:23:15 -07:00
|
|
|
ICredentialsDecrypted,
|
|
|
|
ICredentialTestFunctions,
|
2019-10-15 09:39:00 -07:00
|
|
|
IDataObject,
|
2022-09-01 01:23:15 -07:00
|
|
|
INodeCredentialTestResult,
|
2019-10-15 09:39:00 -07:00
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
2021-04-16 09:33:36 -07:00
|
|
|
INodeTypeDescription,
|
2022-03-12 11:01:44 -08:00
|
|
|
JsonObject,
|
2020-04-06 11:04:59 -07:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2021-03-10 08:01:03 -08:00
|
|
|
|
2022-09-14 02:54:05 -07:00
|
|
|
import { nodeDescription } from './MongoDbDescription';
|
2021-03-10 08:01:03 -08:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
import {
|
|
|
|
buildParameterizedConnString,
|
|
|
|
prepareFields,
|
|
|
|
prepareItems,
|
|
|
|
validateAndResolveMongoCredentials,
|
|
|
|
} from './GenericFunctions';
|
2022-09-01 01:23:15 -07:00
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
import type {
|
|
|
|
FindOneAndReplaceOptions,
|
|
|
|
FindOneAndUpdateOptions,
|
|
|
|
UpdateOptions,
|
|
|
|
Sort,
|
|
|
|
} from 'mongodb';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { MongoClient, ObjectId } from 'mongodb';
|
|
|
|
|
|
|
|
import type { IMongoParametricCredentials } from './mongoDb.types';
|
2019-10-15 09:39:00 -07:00
|
|
|
|
2019-10-27 14:45:48 -07:00
|
|
|
export class MongoDb implements INodeType {
|
2020-04-05 18:06:00 -07:00
|
|
|
description: INodeTypeDescription = nodeDescription;
|
2019-10-15 09:39:00 -07:00
|
|
|
|
2022-09-01 01:23:15 -07:00
|
|
|
methods = {
|
|
|
|
credentialTest: {
|
|
|
|
async mongoDbCredentialTest(
|
|
|
|
this: ICredentialTestFunctions,
|
|
|
|
credential: ICredentialsDecrypted,
|
|
|
|
): Promise<INodeCredentialTestResult> {
|
|
|
|
const credentials = credential.data as IDataObject;
|
2022-09-14 02:54:05 -07:00
|
|
|
|
2022-09-01 01:23:15 -07:00
|
|
|
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,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-14 02:54:05 -07:00
|
|
|
const client: MongoClient = await MongoClient.connect(connectionString);
|
2022-09-01 01:23:15 -07:00
|
|
|
|
|
|
|
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`);
|
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
await client.close();
|
2022-09-01 01:23:15 -07:00
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
status: 'Error',
|
|
|
|
message: error.message,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: 'OK',
|
|
|
|
message: 'Connection successful!',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-10-15 09:39:00 -07:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
2020-04-06 18:07:54 -07:00
|
|
|
const { database, connectionString } = validateAndResolveMongoCredentials(
|
2021-04-16 09:33:36 -07:00
|
|
|
this,
|
2021-08-20 09:57:30 -07:00
|
|
|
await this.getCredentials('mongoDb'),
|
2020-04-06 18:07:54 -07:00
|
|
|
);
|
|
|
|
|
2022-09-14 02:54:05 -07:00
|
|
|
const client: MongoClient = await MongoClient.connect(connectionString);
|
2020-04-05 18:06:00 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
const mdb = client.db(database);
|
2019-10-15 09:39:00 -07:00
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnItems: INodeExecutionData[] = [];
|
|
|
|
let responseData: IDataObject | IDataObject[] = [];
|
2019-10-15 09:39:00 -07:00
|
|
|
|
|
|
|
const items = this.getInputData();
|
2022-12-02 03:53:59 -08:00
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2019-10-15 09:39:00 -07:00
|
|
|
|
2022-03-12 11:01:44 -08:00
|
|
|
if (operation === 'aggregate') {
|
|
|
|
// ----------------------------------
|
|
|
|
// aggregate
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
try {
|
|
|
|
const queryParameter = JSON.parse(this.getNodeParameter('query', 0) as string);
|
|
|
|
|
|
|
|
if (queryParameter._id && typeof queryParameter._id === 'string') {
|
2023-02-27 19:39:43 -08:00
|
|
|
queryParameter._id = new ObjectId(queryParameter._id as string);
|
2022-03-12 11:01:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const query = mdb
|
|
|
|
.collection(this.getNodeParameter('collection', 0) as string)
|
2023-02-27 19:39:43 -08:00
|
|
|
.aggregate(queryParameter as Document[]);
|
2022-03-12 11:01:44 -08:00
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = await query.toArray();
|
2022-03-12 11:01:44 -08:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-09-01 01:23:15 -07:00
|
|
|
responseData = [{ error: (error as JsonObject).message }];
|
2022-03-12 11:01:44 -08:00
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (operation === 'delete') {
|
2020-06-13 10:22:49 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// delete
|
|
|
|
// ----------------------------------
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
const { deletedCount } = await mdb
|
|
|
|
.collection(this.getNodeParameter('collection', 0) as string)
|
2023-02-27 19:39:43 -08:00
|
|
|
.deleteMany(JSON.parse(this.getNodeParameter('query', 0) as string) as Document);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = [{ deletedCount }];
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = [{ error: (error as JsonObject).message }];
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2020-06-13 10:22:49 -07:00
|
|
|
} else if (operation === 'find') {
|
2019-10-15 09:39:00 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// find
|
|
|
|
// ----------------------------------
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2021-09-04 07:30:12 -07:00
|
|
|
const queryParameter = JSON.parse(this.getNodeParameter('query', 0) as string);
|
|
|
|
|
|
|
|
if (queryParameter._id && typeof queryParameter._id === 'string') {
|
2023-02-27 19:39:43 -08:00
|
|
|
queryParameter._id = new ObjectId(queryParameter._id as string);
|
2021-09-04 07:30:12 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let query = mdb
|
|
|
|
.collection(this.getNodeParameter('collection', 0) as string)
|
2023-02-27 19:39:43 -08:00
|
|
|
.find(queryParameter as Document);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
const options = this.getNodeParameter('options', 0);
|
2021-07-19 23:58:54 -07:00
|
|
|
const limit = options.limit as number;
|
|
|
|
const skip = options.skip as number;
|
2023-02-27 19:39:43 -08:00
|
|
|
const sort: Sort = options.sort && JSON.parse(options.sort as string);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (skip > 0) {
|
|
|
|
query = query.skip(skip);
|
|
|
|
}
|
|
|
|
if (limit > 0) {
|
|
|
|
query = query.limit(limit);
|
|
|
|
}
|
|
|
|
if (sort && Object.keys(sort).length !== 0 && sort.constructor === Object) {
|
|
|
|
query = query.sort(sort);
|
|
|
|
}
|
|
|
|
const queryResult = await query.toArray();
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = queryResult;
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = [{ error: (error as JsonObject).message }];
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
2021-04-14 04:41:56 -07:00
|
|
|
}
|
2022-09-01 01:23:15 -07:00
|
|
|
} 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') {
|
2022-09-14 02:54:05 -07:00
|
|
|
filter[updateKey] = new ObjectId(item[updateKey] as string);
|
2022-12-02 12:54:28 -08:00
|
|
|
delete item._id;
|
2022-09-01 01:23:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
await mdb
|
|
|
|
.collection(this.getNodeParameter('collection', 0) as string)
|
2022-09-14 02:54:05 -07:00
|
|
|
.findOneAndReplace(filter, item, updateOptions as FindOneAndReplaceOptions);
|
2022-09-01 01:23:15 -07:00
|
|
|
} 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') {
|
2022-09-14 02:54:05 -07:00
|
|
|
filter[updateKey] = new ObjectId(item[updateKey] as string);
|
2022-12-02 12:54:28 -08:00
|
|
|
delete item._id;
|
2022-09-01 01:23:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
await mdb
|
|
|
|
.collection(this.getNodeParameter('collection', 0) as string)
|
2022-09-14 02:54:05 -07:00
|
|
|
.findOneAndUpdate(filter, { $set: item }, updateOptions as FindOneAndUpdateOptions);
|
2022-09-01 01:23:15 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
item.json = { error: (error as JsonObject).message };
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = updateItems;
|
2020-04-06 11:04:59 -07:00
|
|
|
} else if (operation === 'insert') {
|
2019-10-15 09:39:00 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// insert
|
|
|
|
// ----------------------------------
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
// Prepare the data to insert and copy it to be returned
|
2022-09-01 01:23:15 -07:00
|
|
|
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,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2022-09-01 01:23:15 -07:00
|
|
|
const insertItems = prepareItems(items, fields, '', useDotNotation, dateFields);
|
2019-10-15 09:39:00 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const { insertedIds } = await mdb
|
|
|
|
.collection(this.getNodeParameter('collection', 0) as string)
|
|
|
|
.insertMany(insertItems);
|
|
|
|
|
|
|
|
// Add the id to the data
|
|
|
|
for (const i of Object.keys(insertedIds)) {
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData.push({
|
|
|
|
...insertItems[parseInt(i, 10)],
|
2022-09-14 02:54:05 -07:00
|
|
|
id: insertedIds[parseInt(i, 10)] as unknown as string,
|
2021-07-19 23:58:54 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = [{ error: (error as JsonObject).message }];
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
2019-10-15 12:14:26 -07:00
|
|
|
}
|
2020-04-06 11:04:59 -07:00
|
|
|
} else if (operation === 'update') {
|
2019-10-15 09:39:00 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// update
|
|
|
|
// ----------------------------------
|
|
|
|
|
2022-09-01 01:23:15 -07:00
|
|
|
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,
|
|
|
|
);
|
2021-03-10 08:01:03 -08:00
|
|
|
|
2022-09-01 01:23:15 -07:00
|
|
|
const updateKey = ((this.getNodeParameter('updateKey', 0) as string) || '').trim();
|
2019-10-27 14:45:48 -07:00
|
|
|
|
2021-04-14 04:41:56 -07:00
|
|
|
const updateOptions = (this.getNodeParameter('upsert', 0) as boolean)
|
2022-08-17 08:50:24 -07:00
|
|
|
? { upsert: true }
|
|
|
|
: undefined;
|
2021-04-14 04:41:56 -07:00
|
|
|
|
2022-09-01 01:23:15 -07:00
|
|
|
const updateItems = prepareItems(items, fields, updateKey, useDotNotation, dateFields);
|
2021-03-10 08:01:03 -08:00
|
|
|
|
2019-10-27 14:45:48 -07:00
|
|
|
for (const item of updateItems) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2022-09-01 01:23:15 -07:00
|
|
|
const filter = { [updateKey]: item[updateKey] };
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateKey === '_id') {
|
2022-09-14 02:54:05 -07:00
|
|
|
filter[updateKey] = new ObjectId(item[updateKey] as string);
|
2022-12-02 12:54:28 -08:00
|
|
|
delete item._id;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2022-09-01 01:23:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
await mdb
|
|
|
|
.collection(this.getNodeParameter('collection', 0) as string)
|
2022-09-14 02:54:05 -07:00
|
|
|
.updateOne(filter, { $set: item }, updateOptions as UpdateOptions);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-03-12 11:01:44 -08:00
|
|
|
item.json = { error: (error as JsonObject).message };
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2019-10-27 14:45:48 -07:00
|
|
|
}
|
2019-10-15 12:14:26 -07:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
responseData = updateItems;
|
2019-10-15 09:39:00 -07:00
|
|
|
} else {
|
2021-07-19 23:58:54 -07:00
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = [{ error: `The operation "${operation}" is not supported!` }];
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-09-01 01:23:15 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`The operation "${operation}" is not supported!`,
|
|
|
|
{ itemIndex: 0 },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2019-10-15 09:39:00 -07:00
|
|
|
}
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
await client.close();
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: 0 } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnItems.push(...executionData);
|
|
|
|
|
2019-10-15 09:39:00 -07:00
|
|
|
return this.prepareOutputData(returnItems);
|
|
|
|
}
|
|
|
|
}
|