2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
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';
|
2024-05-22 05:28:09 -07:00
|
|
|
import { ApplicationError } from 'n8n-workflow';
|
2021-03-10 08:01:03 -08:00
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
import type {
|
|
|
|
FindOneAndReplaceOptions,
|
|
|
|
FindOneAndUpdateOptions,
|
|
|
|
UpdateOptions,
|
|
|
|
Sort,
|
|
|
|
} from 'mongodb';
|
2024-01-10 05:02:05 -08:00
|
|
|
import { ObjectId } from 'mongodb';
|
2023-10-10 08:36:20 -07:00
|
|
|
import { generatePairedItemData } from '../../utils/utilities';
|
2023-08-29 08:44:37 -07:00
|
|
|
import { nodeProperties } from './MongoDbProperties';
|
|
|
|
|
|
|
|
import {
|
|
|
|
buildParameterizedConnString,
|
2024-01-10 05:02:05 -08:00
|
|
|
connectMongoClient,
|
2023-08-29 08:44:37 -07:00
|
|
|
prepareFields,
|
|
|
|
prepareItems,
|
|
|
|
stringifyObjectIDs,
|
|
|
|
validateAndResolveMongoCredentials,
|
|
|
|
} from './GenericFunctions';
|
2023-01-27 03:22:44 -08:00
|
|
|
|
|
|
|
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 {
|
2023-08-29 08:44:37 -07:00
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'MongoDB',
|
|
|
|
name: 'mongoDb',
|
|
|
|
icon: 'file:mongodb.svg',
|
|
|
|
group: ['input'],
|
2024-05-22 05:28:09 -07:00
|
|
|
version: [1, 1.1],
|
2023-08-29 08:44:37 -07:00
|
|
|
description: 'Find, insert and update documents in MongoDB',
|
|
|
|
defaults: {
|
|
|
|
name: 'MongoDB',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'mongoDb',
|
|
|
|
required: true,
|
|
|
|
testedBy: 'mongoDbCredentialTest',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: nodeProperties,
|
|
|
|
};
|
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,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-10 05:02:05 -08:00
|
|
|
const client = await connectMongoClient(connectionString, credentials);
|
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
|
2023-12-05 02:17:08 -08:00
|
|
|
throw new ApplicationError(`Database "${database}" does not exist`, {
|
|
|
|
level: 'warning',
|
|
|
|
});
|
2022-09-01 01:23:15 -07:00
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
await client.close();
|
2022-09-01 01:23:15 -07:00
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
status: 'Error',
|
2023-08-29 08:44:37 -07:00
|
|
|
message: (error as Error).message,
|
2022-09-01 01:23:15 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: 'OK',
|
|
|
|
message: 'Connection successful!',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-10-15 09:39:00 -07:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
2024-01-10 05:02:05 -08:00
|
|
|
const credentials = await this.getCredentials('mongoDb');
|
|
|
|
const { database, connectionString } = validateAndResolveMongoCredentials(this, credentials);
|
2020-04-06 18:07:54 -07:00
|
|
|
|
2024-01-10 05:02:05 -08:00
|
|
|
const client = await connectMongoClient(connectionString, credentials);
|
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
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
let returnData: INodeExecutionData[] = [];
|
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);
|
2024-05-22 05:28:09 -07:00
|
|
|
const nodeVersion = this.getNode().typeVersion;
|
2019-10-15 09:39:00 -07:00
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
let itemsLength = items.length ? 1 : 0;
|
|
|
|
let fallbackPairedItems;
|
2022-03-12 11:01:44 -08:00
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
if (nodeVersion >= 1.1) {
|
|
|
|
itemsLength = items.length;
|
|
|
|
} else {
|
|
|
|
fallbackPairedItems = generatePairedItemData(items.length);
|
|
|
|
}
|
2022-03-12 11:01:44 -08:00
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
if (operation === 'aggregate') {
|
|
|
|
for (let i = 0; i < itemsLength; i++) {
|
|
|
|
try {
|
|
|
|
const queryParameter = JSON.parse(
|
|
|
|
this.getNodeParameter('query', i) as string,
|
|
|
|
) as IDataObject;
|
2022-03-12 11:01:44 -08:00
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
if (queryParameter._id && typeof queryParameter._id === 'string') {
|
|
|
|
queryParameter._id = new ObjectId(queryParameter._id);
|
|
|
|
}
|
2022-03-12 11:01:44 -08:00
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
const query = mdb
|
|
|
|
.collection(this.getNodeParameter('collection', i) as string)
|
|
|
|
.aggregate(queryParameter as unknown as Document[]);
|
|
|
|
|
|
|
|
for (const entry of await query.toArray()) {
|
|
|
|
returnData.push({ json: entry, pairedItem: fallbackPairedItems ?? [{ item: i }] });
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({
|
|
|
|
json: { error: (error as JsonObject).message },
|
|
|
|
pairedItem: fallbackPairedItems ?? [{ item: i }],
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
2022-03-12 11:01:44 -08:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2024-05-22 05:28:09 -07:00
|
|
|
}
|
2020-06-13 10:22:49 -07:00
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
if (operation === 'delete') {
|
|
|
|
for (let i = 0; i < itemsLength; i++) {
|
|
|
|
try {
|
|
|
|
const { deletedCount } = await mdb
|
|
|
|
.collection(this.getNodeParameter('collection', i) as string)
|
|
|
|
.deleteMany(JSON.parse(this.getNodeParameter('query', i) as string) as Document);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
returnData.push({
|
|
|
|
json: { deletedCount },
|
|
|
|
pairedItem: fallbackPairedItems ?? [{ item: i }],
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({
|
|
|
|
json: { error: (error as JsonObject).message },
|
|
|
|
pairedItem: fallbackPairedItems ?? [{ item: i }],
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2024-05-22 05:28:09 -07:00
|
|
|
}
|
2019-10-15 09:39:00 -07:00
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
if (operation === 'find') {
|
|
|
|
for (let i = 0; i < itemsLength; i++) {
|
|
|
|
try {
|
|
|
|
const queryParameter = JSON.parse(
|
|
|
|
this.getNodeParameter('query', i) as string,
|
|
|
|
) as IDataObject;
|
2021-09-04 07:30:12 -07:00
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
if (queryParameter._id && typeof queryParameter._id === 'string') {
|
|
|
|
queryParameter._id = new ObjectId(queryParameter._id);
|
|
|
|
}
|
2021-09-04 07:30:12 -07:00
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
let query = mdb
|
|
|
|
.collection(this.getNodeParameter('collection', i) as string)
|
|
|
|
.find(queryParameter as unknown as Document);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
const options = this.getNodeParameter('options', i);
|
|
|
|
const limit = options.limit as number;
|
|
|
|
const skip = options.skip as number;
|
|
|
|
const sort = options.sort && (JSON.parse(options.sort as string) as Sort);
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
for (const entry of queryResult) {
|
|
|
|
returnData.push({ json: entry, pairedItem: fallbackPairedItems ?? [{ item: i }] });
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({
|
|
|
|
json: { error: (error as JsonObject).message },
|
|
|
|
pairedItem: fallbackPairedItems ?? [{ item: i }],
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
throw error;
|
|
|
|
}
|
2021-04-14 04:41:56 -07:00
|
|
|
}
|
2024-05-22 05:28:09 -07:00
|
|
|
}
|
2022-09-01 01:23:15 -07:00
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
if (operation === 'findOneAndReplace') {
|
|
|
|
fallbackPairedItems = fallbackPairedItems ?? generatePairedItemData(items.length);
|
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,
|
|
|
|
);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
returnData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(updateItems),
|
|
|
|
{ itemData: fallbackPairedItems },
|
|
|
|
);
|
|
|
|
}
|
2022-09-01 01:23:15 -07:00
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
if (operation === 'findOneAndUpdate') {
|
|
|
|
fallbackPairedItems = fallbackPairedItems ?? generatePairedItemData(items.length);
|
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,
|
|
|
|
);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
returnData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(updateItems),
|
|
|
|
{ itemData: fallbackPairedItems },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'insert') {
|
|
|
|
fallbackPairedItems = fallbackPairedItems ?? generatePairedItemData(items.length);
|
|
|
|
let responseData: IDataObject[] = [];
|
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
|
|
|
}
|
2019-10-15 09:39:00 -07:00
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
returnData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: fallbackPairedItems },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'update') {
|
|
|
|
fallbackPairedItems = fallbackPairedItems ?? generatePairedItemData(items.length);
|
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
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
returnData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(updateItems),
|
|
|
|
{ itemData: fallbackPairedItems },
|
|
|
|
);
|
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
|
|
|
|
2024-05-22 05:28:09 -07:00
|
|
|
return [stringifyObjectIDs(returnData)];
|
2019-10-15 09:39:00 -07:00
|
|
|
}
|
|
|
|
}
|