2020-04-05 18:06:00 -07:00
|
|
|
import {
|
2020-10-01 05:01:39 -07:00
|
|
|
ICredentialDataDecryptedObject,
|
2020-04-05 18:06:00 -07:00
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
2020-04-06 11:04:59 -07:00
|
|
|
} from 'n8n-workflow';
|
2020-04-06 18:07:54 -07:00
|
|
|
import {
|
2020-10-01 05:01:39 -07:00
|
|
|
IMongoCredentials,
|
2020-04-06 18:07:54 -07:00
|
|
|
IMongoCredentialsType,
|
|
|
|
IMongoParametricCredentials,
|
|
|
|
} from './mongo.node.types';
|
2020-04-05 18:06:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Standard way of building the MongoDB connection string, unless overridden with a provided string
|
|
|
|
*
|
|
|
|
* @param {ICredentialDataDecryptedObject} credentials MongoDB credentials to use, unless conn string is overridden
|
|
|
|
*/
|
2020-04-06 18:07:54 -07:00
|
|
|
function buildParameterizedConnString(
|
2020-10-22 09:00:28 -07:00
|
|
|
credentials: IMongoParametricCredentials,
|
2020-04-05 18:06:00 -07:00
|
|
|
): string {
|
|
|
|
if (credentials.port) {
|
|
|
|
return `mongodb://${credentials.user}:${credentials.password}@${credentials.host}:${credentials.port}`;
|
|
|
|
} else {
|
|
|
|
return `mongodb+srv://${credentials.user}:${credentials.password}@${credentials.host}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:07:54 -07:00
|
|
|
/**
|
|
|
|
* Build mongoDb connection string and resolve database name.
|
|
|
|
* If a connection string override value is provided, that will be used in place of individual args
|
|
|
|
*
|
|
|
|
* @param {ICredentialDataDecryptedObject} credentials raw/input MongoDB credentials to use
|
|
|
|
*/
|
|
|
|
function buildMongoConnectionParams(
|
2020-10-22 09:00:28 -07:00
|
|
|
credentials: IMongoCredentialsType,
|
2020-04-06 18:07:54 -07:00
|
|
|
): IMongoCredentials {
|
|
|
|
const sanitizedDbName =
|
|
|
|
credentials.database && credentials.database.trim().length > 0
|
|
|
|
? credentials.database.trim()
|
|
|
|
: '';
|
2020-05-01 11:24:20 -07:00
|
|
|
if (credentials.configurationType === 'connectionString') {
|
2020-04-06 18:07:54 -07:00
|
|
|
if (
|
2020-05-01 11:24:20 -07:00
|
|
|
credentials.connectionString &&
|
|
|
|
credentials.connectionString.trim().length > 0
|
2020-04-06 18:07:54 -07:00
|
|
|
) {
|
|
|
|
return {
|
2020-05-01 11:24:20 -07:00
|
|
|
connectionString: credentials.connectionString.trim(),
|
2020-10-22 06:46:03 -07:00
|
|
|
database: sanitizedDbName,
|
2020-04-06 18:07:54 -07:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
2020-10-22 09:00:28 -07:00
|
|
|
'Cannot override credentials: valid MongoDB connection string not provided ',
|
2020-04-06 18:07:54 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
connectionString: buildParameterizedConnString(credentials),
|
2020-10-22 06:46:03 -07:00
|
|
|
database: sanitizedDbName,
|
2020-04-06 18:07:54 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify credentials. If ok, build mongoDb connection string and resolve database name.
|
|
|
|
*
|
|
|
|
* @param {ICredentialDataDecryptedObject} credentials raw/input MongoDB credentials to use
|
|
|
|
*/
|
|
|
|
export function validateAndResolveMongoCredentials(
|
2020-10-22 09:00:28 -07:00
|
|
|
credentials?: ICredentialDataDecryptedObject,
|
2020-04-06 18:07:54 -07:00
|
|
|
): IMongoCredentials {
|
2020-05-01 11:24:20 -07:00
|
|
|
if (credentials === undefined) {
|
2020-04-06 18:07:54 -07:00
|
|
|
throw new Error('No credentials got returned!');
|
|
|
|
} else {
|
|
|
|
return buildMongoConnectionParams(
|
2020-10-22 09:00:28 -07:00
|
|
|
credentials as unknown as IMongoCredentialsType,
|
2020-04-06 18:07:54 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 18:06:00 -07:00
|
|
|
/**
|
|
|
|
* Returns of copy of the items which only contains the json data and
|
|
|
|
* of that only the define properties
|
|
|
|
*
|
|
|
|
* @param {INodeExecutionData[]} items The items to copy
|
|
|
|
* @param {string[]} properties The properties it should include
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export function getItemCopy(
|
|
|
|
items: INodeExecutionData[],
|
2020-10-22 09:00:28 -07:00
|
|
|
properties: string[],
|
2020-04-05 18:06:00 -07:00
|
|
|
): IDataObject[] {
|
|
|
|
// Prepare the data to insert and copy it to be returned
|
|
|
|
let newItem: IDataObject;
|
2020-04-06 11:04:59 -07:00
|
|
|
return items.map(item => {
|
2020-04-05 18:06:00 -07:00
|
|
|
newItem = {};
|
|
|
|
for (const property of properties) {
|
|
|
|
if (item.json[property] === undefined) {
|
|
|
|
newItem[property] = null;
|
|
|
|
} else {
|
|
|
|
newItem[property] = JSON.parse(JSON.stringify(item.json[property]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newItem;
|
|
|
|
});
|
|
|
|
}
|
2021-03-10 08:01:03 -08:00
|
|
|
|
|
|
|
export function handleDateFields(insertItems: IDataObject[], fields: string) {
|
|
|
|
const dateFields = (fields as string).split(',');
|
|
|
|
for (let i = 0; i < insertItems.length; i++) {
|
|
|
|
for (const key of Object.keys(insertItems[i])) {
|
|
|
|
if (dateFields.includes(key)) {
|
|
|
|
insertItems[i][key] = new Date(insertItems[i][key] as string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|