Small improvements to Mongo connection string support

This commit is contained in:
Jan Oberhauser
2020-05-01 20:24:20 +02:00
parent eb1213248e
commit 384efade9f
4 changed files with 98 additions and 44 deletions

View File

@@ -21,7 +21,8 @@
<font-awesome-icon icon="question-circle" /> <font-awesome-icon icon="question-circle" />
</el-tooltip> </el-tooltip>
</div> </div>
<el-row v-for="parameter in credentialTypeData.properties" :key="parameter.name" class="parameter-wrapper"> <span v-for="parameter in credentialTypeData.properties" :key="parameter.name">
<el-row v-if="displayCredentialParameter(parameter)" class="parameter-wrapper">
<el-col :span="6" class="parameter-name"> <el-col :span="6" class="parameter-name">
{{parameter.displayName}}: {{parameter.displayName}}:
<el-tooltip placement="top" class="parameter-info" v-if="parameter.description" effect="light"> <el-tooltip placement="top" class="parameter-info" v-if="parameter.description" effect="light">
@@ -33,6 +34,7 @@
<parameter-input :parameter="parameter" :value="propertyValue[parameter.name]" :path="parameter.name" :isCredential="true" @valueChanged="valueChanged" /> <parameter-input :parameter="parameter" :value="propertyValue[parameter.name]" :path="parameter.name" :isCredential="true" @valueChanged="valueChanged" />
</el-col> </el-col>
</el-row> </el-row>
</span>
<el-row class="nodes-access-wrapper"> <el-row class="nodes-access-wrapper">
<el-col :span="6" class="headline"> <el-col :span="6" class="headline">
@@ -85,6 +87,7 @@ import {
ICredentialType, ICredentialType,
ICredentialNodeAccess, ICredentialNodeAccess,
INodeCredentialDescription, INodeCredentialDescription,
INodeProperties,
INodeTypeDescription, INodeTypeDescription,
} from 'n8n-workflow'; } from 'n8n-workflow';
@@ -162,6 +165,14 @@ export default mixins(
tempValue[name] = parameterData.value; tempValue[name] = parameterData.value;
Vue.set(this, 'propertyValue', tempValue); Vue.set(this, 'propertyValue', tempValue);
}, },
displayCredentialParameter (parameter: INodeProperties): boolean {
if (parameter.displayOptions === undefined) {
// If it is not defined no need to do a proper check
return true;
}
return this.displayParameter(this.propertyValue, parameter, '');
},
async createCredentials (): Promise<void> { async createCredentials (): Promise<void> {
const nodesAccess = this.nodesAccess.map((nodeType) => { const nodesAccess = this.nodesAccess.map((nodeType) => {
return { return {

View File

@@ -4,10 +4,53 @@ export class MongoDb implements ICredentialType {
name = 'mongoDb'; name = 'mongoDb';
displayName = 'MongoDB'; displayName = 'MongoDB';
properties = [ properties = [
{
displayName: 'Configuration Type',
name: 'configurationType',
type: 'options' as NodePropertyTypes,
options: [
{
name: 'Connection String',
value: 'connectionString',
description: 'Provide connection data via string',
},
{
name: 'Values',
value: 'values',
description: 'Provide connection data via values',
},
],
default: 'values',
description: 'The operation to perform.',
},
{
displayName: 'Connection String',
name: 'connectionString',
type: 'string' as NodePropertyTypes,
displayOptions: {
show: {
configurationType: [
'connectionString',
],
},
},
default: '',
placeholder: 'mongodb://<USERNAME>:<PASSWORD>@localhost:27017/?authSource=admin&readPreference=primary&appname=n8n&ssl=false',
required: false,
description: `If provided, the value here will be used as a MongoDB connection string,<br />
and the MongoDB credentials will be ignored`
},
{ {
displayName: 'Host', displayName: 'Host',
name: 'host', name: 'host',
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
displayOptions: {
show: {
configurationType: [
'values',
],
},
},
default: 'localhost' default: 'localhost'
}, },
{ {
@@ -15,13 +58,19 @@ export class MongoDb implements ICredentialType {
name: 'database', name: 'database',
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
default: '', default: '',
description: description: 'Note: the database should still be provided even if using an override connection string'
'Note: the database should still be provided even if using an override connection string'
}, },
{ {
displayName: 'User', displayName: 'User',
name: 'user', name: 'user',
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
displayOptions: {
show: {
configurationType: [
'values',
],
},
},
default: '' default: ''
}, },
{ {
@@ -31,34 +80,27 @@ export class MongoDb implements ICredentialType {
typeOptions: { typeOptions: {
password: true password: true
}, },
displayOptions: {
show: {
configurationType: [
'values',
],
},
},
default: '' default: ''
}, },
{ {
displayName: 'Port', displayName: 'Port',
name: 'port', name: 'port',
type: 'number' as NodePropertyTypes, type: 'number' as NodePropertyTypes,
displayOptions: {
show: {
configurationType: [
'values',
],
},
},
default: 27017 default: 27017
}, },
{
displayName: 'Override conn string',
name: 'shouldOverrideConnString',
type: 'boolean' as NodePropertyTypes,
default: false,
required: false,
description:
'Whether to override the generated connection string. Credentials will also be ignored in this case.'
},
{
displayName: 'Conn string override',
name: 'connStringOverrideVal',
type: 'string' as NodePropertyTypes,
typeOptions: {
rows: 1
},
default: '',
placeholder: `mongodb://USERNAMEHERE:PASSWORDHERE@localhost:27017/?authSource=admin&readPreference=primary&appname=n8n&ssl=false`,
required: false,
description: `If provided, the value here will be used as a MongoDB connection string, and the MongoDB credentials will be ignored`
}
]; ];
} }

View File

@@ -7,7 +7,8 @@ export interface IMongoParametricCredentials {
/** /**
* Whether to allow overriding the parametric credentials with a connection string * Whether to allow overriding the parametric credentials with a connection string
*/ */
shouldOverrideConnString: false | undefined | null; configurationType: 'values';
host: string; host: string;
database: string; database: string;
user: string; user: string;
@@ -22,11 +23,11 @@ export interface IMongoOverrideCredentials {
/** /**
* Whether to allow overriding the parametric credentials with a connection string * Whether to allow overriding the parametric credentials with a connection string
*/ */
shouldOverrideConnString: true; configurationType: 'connectionString';
/** /**
* If using an override connection string, this is where it will be. * If using an override connection string, this is where it will be.
*/ */
connStringOverrideVal: string; connectionString: string;
database: string; database: string;
} }

View File

@@ -37,13 +37,13 @@ function buildMongoConnectionParams(
credentials.database && credentials.database.trim().length > 0 credentials.database && credentials.database.trim().length > 0
? credentials.database.trim() ? credentials.database.trim()
: ''; : '';
if (credentials.shouldOverrideConnString) { if (credentials.configurationType === 'connectionString') {
if ( if (
credentials.connStringOverrideVal && credentials.connectionString &&
credentials.connStringOverrideVal.trim().length > 0 credentials.connectionString.trim().length > 0
) { ) {
return { return {
connectionString: credentials.connStringOverrideVal.trim(), connectionString: credentials.connectionString.trim(),
database: sanitizedDbName database: sanitizedDbName
}; };
} else { } else {
@@ -67,11 +67,11 @@ function buildMongoConnectionParams(
export function validateAndResolveMongoCredentials( export function validateAndResolveMongoCredentials(
credentials?: ICredentialDataDecryptedObject credentials?: ICredentialDataDecryptedObject
): IMongoCredentials { ): IMongoCredentials {
if (credentials == undefined) { if (credentials === undefined) {
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
} else { } else {
return buildMongoConnectionParams( return buildMongoConnectionParams(
(credentials as any) as IMongoCredentialsType credentials as unknown as IMongoCredentialsType,
); );
} }
} }