Browse Source

fix: connection interfaces for dataSource.connection

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/3306/head
mertmit 2 years ago
parent
commit
e23ad3bf8f
  1. 67
      packages/nc-gui-v2/pages/index/index/create-external.vue
  2. 48
      packages/nc-gui-v2/utils/projectCreateUtils.ts
  3. 2
      packages/nc-gui-v2/utils/validation.ts

67
packages/nc-gui-v2/pages/index/index/create-external.vue

@ -2,7 +2,8 @@
import { Form, Modal, message } from 'ant-design-vue' import { Form, Modal, message } from 'ant-design-vue'
import type { SelectHandler } from 'ant-design-vue/es/vc-select/Select' import type { SelectHandler } from 'ant-design-vue/es/vc-select/Select'
import { import {
certTypes, CertTypes,
SSLUsage,
clientTypes, clientTypes,
computed, computed,
extractSdkResponseErrorMsg, extractSdkResponseErrorMsg,
@ -16,7 +17,6 @@ import {
projectTitleValidator, projectTitleValidator,
readFile, readFile,
ref, ref,
sslUsage,
useApi, useApi,
useI18n, useI18n,
useNuxtApp, useNuxtApp,
@ -24,6 +24,7 @@ import {
watch, watch,
} from '#imports' } from '#imports'
import { ClientType } from '~/lib' import { ClientType } from '~/lib'
import { DefaultConnection, SQLiteConnection } from '~/utils'
import type { ProjectCreateForm } from '~/utils' import type { ProjectCreateForm } from '~/utils'
const useForm = Form.useForm const useForm = Form.useForm
@ -47,7 +48,7 @@ let formState = $ref<ProjectCreateForm>({
inflectionColumn: 'camelize', inflectionColumn: 'camelize',
inflectionTable: 'camelize', inflectionTable: 'camelize',
}, },
sslUse: sslUsage.No, sslUse: SSLUsage.No,
extraParameters: [], extraParameters: [],
}) })
@ -58,7 +59,7 @@ const customFormState = ref<ProjectCreateForm>({
inflectionColumn: 'camelize', inflectionColumn: 'camelize',
inflectionTable: 'camelize', inflectionTable: 'camelize',
}, },
sslUse: sslUsage.No, sslUse: SSLUsage.No,
extraParameters: [], extraParameters: [],
}) })
@ -103,33 +104,39 @@ const onClientChange = () => {
populateName(formState.title) populateName(formState.title)
} }
const onSSLModeChange = ((mode: sslUsage) => { const onSSLModeChange = ((mode: SSLUsage) => {
if (formState.dataSource.client !== ClientType.SQLITE) {
const connection = formState.dataSource.connection as DefaultConnection
switch (mode) { switch (mode) {
case sslUsage.No: case SSLUsage.No:
delete formState.dataSource.connection.ssl delete connection.ssl
break break
case sslUsage.Allowed: case SSLUsage.Allowed:
formState.dataSource.connection.ssl = 'true' connection.ssl = 'true'
break break
default: default:
formState.dataSource.connection.ssl = { connection.ssl = {
ca: '', ca: '',
cert: '', cert: '',
key: '', key: '',
} }
break break
} }
}
}) as SelectHandler }) as SelectHandler
const updateSSLUse = () => { const updateSSLUse = () => {
if (formState.dataSource.connection.ssl) { if (formState.dataSource.client !== ClientType.SQLITE) {
if (typeof formState.dataSource.connection.ssl === 'string') { const connection = formState.dataSource.connection as DefaultConnection
formState.sslUse = sslUsage.Allowed if (connection.ssl) {
if (typeof connection.ssl === 'string') {
formState.sslUse = SSLUsage.Allowed
} else { } else {
formState.sslUse = sslUsage.Preferred formState.sslUse = SSLUsage.Preferred
} }
} else { } else {
formState.sslUse = sslUsage.No formState.sslUse = SSLUsage.No
}
} }
} }
@ -150,7 +157,7 @@ const caFileInput = ref<HTMLInputElement>()
const keyFileInput = ref<HTMLInputElement>() const keyFileInput = ref<HTMLInputElement>()
const certFileInput = ref<HTMLInputElement>() const certFileInput = ref<HTMLInputElement>()
const onFileSelect = (key: certTypes, el?: HTMLInputElement) => { const onFileSelect = (key: CertTypes, el?: HTMLInputElement) => {
if (!el) return if (!el) return
readFile(el, (content) => { readFile(el, (content) => {
@ -160,7 +167,7 @@ const onFileSelect = (key: certTypes, el?: HTMLInputElement) => {
} }
const sslFilesRequired = computed( const sslFilesRequired = computed(
() => !!formState.sslUse && formState.sslUse !== sslUsage.No && formState.sslUse !== sslUsage.Allowed, () => !!formState.sslUse && formState.sslUse !== SSLUsage.No && formState.sslUse !== SSLUsage.Allowed,
) )
function getConnectionConfig() { function getConnectionConfig() {
@ -173,7 +180,7 @@ function getConnectionConfig() {
if ('ssl' in connection && connection.ssl) { if ('ssl' in connection && connection.ssl) {
if ( if (
formState.sslUse === sslUsage.No || formState.sslUse === SSLUsage.No ||
(typeof connection.ssl === 'object' && Object.values(connection.ssl).every((v) => !v)) (typeof connection.ssl === 'object' && Object.values(connection.ssl).every((v) => !v))
) { ) {
delete connection.ssl delete connection.ssl
@ -365,28 +372,34 @@ onMounted(() => {
:label="$t('labels.sqliteFile')" :label="$t('labels.sqliteFile')"
v-bind="validateInfos['dataSource.connection.connection.filename']" v-bind="validateInfos['dataSource.connection.connection.filename']"
> >
<a-input v-model:value="formState.dataSource.connection.connection.filename" /> <a-input v-model:value="(formState.dataSource.connection as SQLiteConnection).connection.filename" />
</a-form-item> </a-form-item>
<template v-else> <template v-else>
<!-- Host Address --> <!-- Host Address -->
<a-form-item :label="$t('labels.hostAddress')" v-bind="validateInfos['dataSource.connection.host']"> <a-form-item :label="$t('labels.hostAddress')" v-bind="validateInfos['dataSource.connection.host']">
<a-input v-model:value="formState.dataSource.connection.host" class="nc-extdb-host-address" /> <a-input v-model:value="(formState.dataSource.connection as DefaultConnection).host" class="nc-extdb-host-address" />
</a-form-item> </a-form-item>
<!-- Port Number --> <!-- Port Number -->
<a-form-item :label="$t('labels.port')" v-bind="validateInfos['dataSource.connection.port']"> <a-form-item :label="$t('labels.port')" v-bind="validateInfos['dataSource.connection.port']">
<a-input-number v-model:value="formState.dataSource.connection.port" class="!w-full nc-extdb-host-port" /> <a-input-number
v-model:value="(formState.dataSource.connection as DefaultConnection).port"
class="!w-full nc-extdb-host-port"
/>
</a-form-item> </a-form-item>
<!-- Username --> <!-- Username -->
<a-form-item :label="$t('labels.username')" v-bind="validateInfos['dataSource.connection.user']"> <a-form-item :label="$t('labels.username')" v-bind="validateInfos['dataSource.connection.user']">
<a-input v-model:value="formState.dataSource.connection.user" class="nc-extdb-host-user" /> <a-input v-model:value="(formState.dataSource.connection as DefaultConnection).user" class="nc-extdb-host-user" />
</a-form-item> </a-form-item>
<!-- Password --> <!-- Password -->
<a-form-item :label="$t('labels.password')"> <a-form-item :label="$t('labels.password')">
<a-input-password v-model:value="formState.dataSource.connection.password" class="nc-extdb-host-password" /> <a-input-password
v-model:value="(formState.dataSource.connection as DefaultConnection).password"
class="nc-extdb-host-password"
/>
</a-form-item> </a-form-item>
<!-- Database --> <!-- Database -->
@ -421,7 +434,7 @@ onMounted(() => {
<!-- todo: add in i18n --> <!-- todo: add in i18n -->
<a-form-item label="SSL mode"> <a-form-item label="SSL mode">
<a-select v-model:value="formState.sslUse" @select="onSSLModeChange"> <a-select v-model:value="formState.sslUse" @select="onSSLModeChange">
<a-select-option v-for="opt in Object.values(sslUsage)" :key="opt" :value="opt">{{ opt }}</a-select-option> <a-select-option v-for="opt in Object.values(SSLUsage)" :key="opt" :value="opt">{{ opt }}</a-select-option>
</a-select> </a-select>
</a-form-item> </a-form-item>
@ -461,11 +474,11 @@ onMounted(() => {
</div> </div>
</a-form-item> </a-form-item>
<input ref="caFileInput" type="file" class="!hidden" @change="onFileSelect(certTypes.ca, caFileInput)" /> <input ref="caFileInput" type="file" class="!hidden" @change="onFileSelect(CertTypes.ca, caFileInput)" />
<input ref="certFileInput" type="file" class="!hidden" @change="onFileSelect(certTypes.cert, certFileInput)" /> <input ref="certFileInput" type="file" class="!hidden" @change="onFileSelect(CertTypes.cert, certFileInput)" />
<input ref="keyFileInput" type="file" class="!hidden" @change="onFileSelect(certTypes.key, keyFileInput)" /> <input ref="keyFileInput" type="file" class="!hidden" @change="onFileSelect(CertTypes.key, keyFileInput)" />
<a-divider /> <a-divider />

48
packages/nc-gui-v2/utils/projectCreateUtils.ts

@ -4,17 +4,35 @@ export interface ProjectCreateForm {
title: string title: string
dataSource: { dataSource: {
client: ClientType client: ClientType
connection: Record<string, any> connection: DefaultConnection | SQLiteConnection
searchPath?: string[] searchPath?: string[]
} }
inflection: { inflection: {
inflectionColumn?: string inflectionColumn?: string
inflectionTable?: string inflectionTable?: string
} }
sslUse?: sslUsage sslUse?: SSLUsage
extraParameters: Record<string, string>[] extraParameters: Record<string, string>[]
} }
export interface DefaultConnection {
host: string
database: string
user: string
password: string
port: number | string
ssl?: Record<CertTypes, string> | 'true'
}
export interface SQLiteConnection {
client: ClientType.SQLITE
database: string
connection: {
filename?: string
}
useNullAsDefault?: boolean
}
const defaultHost = 'localhost' const defaultHost = 'localhost'
const testDataBaseNames = { const testDataBaseNames = {
@ -58,28 +76,28 @@ const sampleConnectionData: Record<ClientType | string, ProjectCreateForm['dataS
user: 'postgres', user: 'postgres',
password: 'password', password: 'password',
database: '_test', database: '_test',
}, } as DefaultConnection,
[ClientType.MYSQL]: { [ClientType.MYSQL]: {
host: defaultHost, host: defaultHost,
port: '3306', port: '3306',
user: 'root', user: 'root',
password: 'password', password: 'password',
database: '_test', database: '_test',
}, } as DefaultConnection,
[ClientType.VITESS]: { [ClientType.VITESS]: {
host: defaultHost, host: defaultHost,
port: '15306', port: '15306',
user: 'root', user: 'root',
password: 'password', password: 'password',
database: '_test', database: '_test',
}, } as DefaultConnection,
[ClientType.MSSQL]: { [ClientType.MSSQL]: {
host: defaultHost, host: defaultHost,
port: 1433, port: 1433,
user: 'sa', user: 'sa',
password: 'Password123.', password: 'Password123.',
database: '_test', database: '_test',
}, } as DefaultConnection,
[ClientType.SQLITE]: { [ClientType.SQLITE]: {
client: ClientType.SQLITE, client: ClientType.SQLITE,
database: homeDir, database: homeDir,
@ -94,42 +112,42 @@ const sampleConnectionData: Record<ClientType | string, ProjectCreateForm['dataS
user: 'root', user: 'root',
password: '', password: '',
database: '_test', database: '_test',
}, } as DefaultConnection,
yugabyte: { yugabyte: {
host: defaultHost, host: defaultHost,
port: '5432', port: '5432',
user: 'postgres', user: 'postgres',
password: '', password: '',
database: '_test', database: '_test',
}, } as DefaultConnection,
citusdb: { citusdb: {
host: defaultHost, host: defaultHost,
port: '5432', port: '5432',
user: 'postgres', user: 'postgres',
password: '', password: '',
database: '_test', database: '_test',
}, } as DefaultConnection,
cockroachdb: { cockroachdb: {
host: defaultHost, host: defaultHost,
port: '5432', port: '5432',
user: 'postgres', user: 'postgres',
password: '', password: '',
database: '_test', database: '_test',
}, } as DefaultConnection,
greenplum: { greenplum: {
host: defaultHost, host: defaultHost,
port: '5432', port: '5432',
user: 'postgres', user: 'postgres',
password: '', password: '',
database: '_test', database: '_test',
}, } as DefaultConnection,
oracledb: { oracledb: {
host: defaultHost, host: defaultHost,
port: '1521', port: '1521',
user: 'system', user: 'system',
password: 'Oracle18', password: 'Oracle18',
database: '_test', database: '_test',
}, } as DefaultConnection,
} }
export const getDefaultConnectionConfig = (client: ClientType): ProjectCreateForm['dataSource'] => { export const getDefaultConnectionConfig = (client: ClientType): ProjectCreateForm['dataSource'] => {
@ -144,7 +162,7 @@ export const getDefaultConnectionConfig = (client: ClientType): ProjectCreateFor
} }
} }
enum sslUsage { enum SSLUsage {
No = 'No', No = 'No',
Allowed = 'Allowed', Allowed = 'Allowed',
Preferred = 'Preferred', Preferred = 'Preferred',
@ -153,10 +171,10 @@ enum sslUsage {
RequiredWithIdentity = 'Required-Identity', RequiredWithIdentity = 'Required-Identity',
} }
enum certTypes { enum CertTypes {
ca = 'ca', ca = 'ca',
cert = 'cert', cert = 'cert',
key = 'key', key = 'key',
} }
export { sslUsage, certTypes } export { SSLUsage, CertTypes }

2
packages/nc-gui-v2/utils/validation.ts

@ -129,7 +129,7 @@ export const importExcelUrlValidator = {
} }
export const extraParameterValidator = { export const extraParameterValidator = {
validator: (rule: any, value: any) => { validator: (_: unknown, value: any) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
for (const param of value) { for (const param of value) {
if (param.key === '') { if (param.key === '') {

Loading…
Cancel
Save