mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
382 lines
13 KiB
382 lines
13 KiB
2 years ago
|
<script lang="ts" setup>
|
||
2 years ago
|
import { onMounted } from '@vue/runtime-core'
|
||
2 years ago
|
import { useI18n } from 'vue-i18n'
|
||
|
import { useToast } from 'vue-toastification'
|
||
|
import { Form, Modal } from 'ant-design-vue'
|
||
|
import { ref } from '#imports'
|
||
|
import { navigateTo, useNuxtApp } from '#app'
|
||
|
import type { ProjectCreateForm } from '~/lib/types'
|
||
|
import { extractSdkResponseErrorMsg } from '~/utils/errorUtils'
|
||
2 years ago
|
import {
|
||
|
clientTypes,
|
||
|
fieldRequiredValidator,
|
||
2 years ago
|
generateUniqueName,
|
||
2 years ago
|
getDefaultConnectionConfig,
|
||
|
getTestDatabaseName,
|
||
|
projectTitleValidator,
|
||
2 years ago
|
sslUsage,
|
||
|
} from '~/utils/projectCreateUtils'
|
||
|
import { readFile } from '~/utils/fileUtils'
|
||
2 years ago
|
|
||
2 years ago
|
const useForm = Form.useForm
|
||
|
const loading = ref(false)
|
||
|
const testSuccess = ref(false)
|
||
2 years ago
|
|
||
2 years ago
|
const { $api, $e, $state } = useNuxtApp()
|
||
2 years ago
|
const toast = useToast()
|
||
|
const { t } = useI18n()
|
||
2 years ago
|
|
||
2 years ago
|
const formState = $ref<ProjectCreateForm>({
|
||
2 years ago
|
title: '',
|
||
|
dataSource: { ...getDefaultConnectionConfig('mysql2') },
|
||
2 years ago
|
inflection: {
|
||
2 years ago
|
inflection_column: 'camelize',
|
||
|
inflection_table: 'camelize',
|
||
|
},
|
||
2 years ago
|
sslUse: 'No',
|
||
2 years ago
|
})
|
||
2 years ago
|
|
||
2 years ago
|
const validators = computed(() => {
|
||
|
return {
|
||
2 years ago
|
'title': [
|
||
2 years ago
|
{
|
||
|
required: true,
|
||
2 years ago
|
message: 'Please input project title',
|
||
2 years ago
|
},
|
||
2 years ago
|
projectTitleValidator,
|
||
2 years ago
|
],
|
||
2 years ago
|
'dataSource.client': [fieldRequiredValidator],
|
||
|
...(formState.dataSource.client === 'sqlite3'
|
||
2 years ago
|
? {
|
||
2 years ago
|
'dataSource.connection.connection.filename': [fieldRequiredValidator],
|
||
|
}
|
||
2 years ago
|
: {
|
||
2 years ago
|
'dataSource.connection.host': [fieldRequiredValidator],
|
||
|
'dataSource.connection.port': [fieldRequiredValidator],
|
||
|
'dataSource.connection.user': [fieldRequiredValidator],
|
||
|
'dataSource.connection.password': [fieldRequiredValidator],
|
||
|
'dataSource.connection.database': [fieldRequiredValidator],
|
||
|
...(['pg', 'mssql'].includes(formState.dataSource.client)
|
||
|
? {
|
||
|
'dataSource.connection.searchPath.0': [fieldRequiredValidator],
|
||
|
}
|
||
|
: {}),
|
||
|
}),
|
||
|
}
|
||
|
})
|
||
|
|
||
|
const { resetFields, validate, validateInfos } = useForm(formState, validators)
|
||
2 years ago
|
|
||
|
const onClientChange = () => {
|
||
2 years ago
|
formState.dataSource = { ...getDefaultConnectionConfig(formState.dataSource.client) }
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
const inflectionTypes = ['camelize', 'none']
|
||
|
const configEditDlg = ref(false)
|
||
2 years ago
|
|
||
2 years ago
|
// populate database name based on title
|
||
2 years ago
|
watch(
|
||
|
() => formState.title,
|
||
2 years ago
|
(v) => (formState.dataSource.connection.database = `${v}_noco`),
|
||
|
)
|
||
2 years ago
|
|
||
2 years ago
|
// generate a random project title
|
||
|
formState.title = generateUniqueName()
|
||
|
|
||
2 years ago
|
const caFileInput = ref<HTMLInputElement>()
|
||
|
const keyFileInput = ref<HTMLInputElement>()
|
||
|
const certFileInput = ref<HTMLInputElement>()
|
||
2 years ago
|
|
||
2 years ago
|
const onFileSelect = (key: 'ca' | 'cert' | 'key', el: HTMLInputElement) => {
|
||
2 years ago
|
readFile(el, (content) => {
|
||
2 years ago
|
if ('ssl' in formState.dataSource.connection && formState.dataSource.connection.ssl)
|
||
|
formState.dataSource.connection.ssl[key] = content ?? ''
|
||
|
})
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
const sslFilesRequired = computed<boolean>(() => {
|
||
2 years ago
|
return formState?.sslUse && formState.sslUse !== 'No'
|
||
|
})
|
||
2 years ago
|
|
||
2 years ago
|
function getConnectionConfig() {
|
||
|
const connection = {
|
||
2 years ago
|
...formState.dataSource.connection,
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
if ('ssl' in connection && connection.ssl && (!sslFilesRequired || Object.values(connection.ssl).every((v) => !v))) {
|
||
|
delete connection.ssl
|
||
2 years ago
|
}
|
||
2 years ago
|
return connection
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
const form = ref<any>()
|
||
|
const focusInvalidInput = () => {
|
||
|
form?.value?.$el.querySelector('.ant-form-item-explain-error')?.parentNode?.querySelector('input')?.focus()
|
||
|
}
|
||
|
|
||
2 years ago
|
const createProject = async () => {
|
||
2 years ago
|
try {
|
||
|
await validate()
|
||
|
} catch (e) {
|
||
|
focusInvalidInput()
|
||
2 years ago
|
return
|
||
2 years ago
|
}
|
||
2 years ago
|
loading.value = true
|
||
2 years ago
|
try {
|
||
2 years ago
|
const connection = getConnectionConfig()
|
||
|
const config = { ...formState.dataSource, connection }
|
||
2 years ago
|
const result = await $api.project.create({
|
||
|
title: formState.title,
|
||
|
bases: [
|
||
|
{
|
||
|
type: formState.dataSource.client,
|
||
|
config,
|
||
|
inflection_column: formState.inflection.inflection_column,
|
||
2 years ago
|
inflection_table: formState.inflection.inflection_table,
|
||
|
},
|
||
2 years ago
|
],
|
||
2 years ago
|
external: true,
|
||
|
})
|
||
|
$e('a:project:create:extdb')
|
||
|
await navigateTo(`/nc/${result.id}`)
|
||
2 years ago
|
} catch (e: any) {
|
||
|
// todo: toast
|
||
2 years ago
|
toast.error(await extractSdkResponseErrorMsg(e))
|
||
2 years ago
|
}
|
||
2 years ago
|
loading.value = false
|
||
|
}
|
||
2 years ago
|
|
||
|
const testConnection = async () => {
|
||
2 years ago
|
try {
|
||
|
await validate()
|
||
|
} catch (e) {
|
||
|
focusInvalidInput()
|
||
2 years ago
|
return
|
||
2 years ago
|
}
|
||
2 years ago
|
$e('a:project:create:extdb:test-connection', [])
|
||
2 years ago
|
try {
|
||
2 years ago
|
if (formState.dataSource.client === 'sqlite3') {
|
||
|
testSuccess.value = true
|
||
2 years ago
|
} else {
|
||
2 years ago
|
const connection: any = getConnectionConfig()
|
||
|
connection.database = getTestDatabaseName(formState.dataSource)
|
||
2 years ago
|
const testConnectionConfig = {
|
||
|
...formState.dataSource,
|
||
2 years ago
|
connection,
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
const result = await $api.utils.testConnection(testConnectionConfig)
|
||
2 years ago
|
|
||
|
if (result.code === 0) {
|
||
2 years ago
|
testSuccess.value = true
|
||
2 years ago
|
|
||
|
Modal.confirm({
|
||
2 years ago
|
title: t('msg.info.dbConnected'),
|
||
2 years ago
|
icon: null,
|
||
2 years ago
|
type: 'success',
|
||
2 years ago
|
|
||
2 years ago
|
okText: t('activity.OkSaveProject'),
|
||
|
okType: 'primary',
|
||
|
cancelText: 'Cancel',
|
||
|
onOk: createProject,
|
||
|
})
|
||
2 years ago
|
} else {
|
||
2 years ago
|
testSuccess.value = false
|
||
|
toast.error(`${t('msg.error.dbConnectionFailed')} ${result.message}`)
|
||
2 years ago
|
}
|
||
|
}
|
||
|
} catch (e: any) {
|
||
2 years ago
|
testSuccess.value = false
|
||
|
toast.error(await extractSdkResponseErrorMsg(e))
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
// hide sidebar
|
||
2 years ago
|
$state.sidebarOpen.value = false
|
||
2 years ago
|
|
||
|
// reset test status on config change
|
||
|
watch(
|
||
|
() => formState.dataSource,
|
||
|
() => (testSuccess.value = false),
|
||
|
{ deep: true },
|
||
|
)
|
||
2 years ago
|
|
||
|
// select and focus title field on load
|
||
|
onMounted(() => {
|
||
2 years ago
|
const input = form.value?.$el?.querySelector('input')
|
||
|
if (input) {
|
||
|
input.setSelectionRange(0, formState.title.length)
|
||
|
input.focus()
|
||
|
}
|
||
2 years ago
|
})
|
||
2 years ago
|
</script>
|
||
|
|
||
2 years ago
|
<template>
|
||
2 years ago
|
<a-card
|
||
2 years ago
|
class="max-w-[600px] mx-auto !mt-5 !mb-5"
|
||
2 years ago
|
:title="$t('activity.createProject')"
|
||
|
:head-style="{ textAlign: 'center', fontWeight: '700' }"
|
||
|
>
|
||
2 years ago
|
<a-form
|
||
|
ref="form"
|
||
|
:model="formState"
|
||
|
name="validate_other"
|
||
|
layout="horizontal"
|
||
|
:label-col="{ span: 8 }"
|
||
|
:wrapper-col="{ span: 18 }"
|
||
|
>
|
||
2 years ago
|
<a-form-item :label="$t('placeholder.projName')" v-bind="validateInfos.title">
|
||
|
<a-input v-model:value="formState.title" size="small" />
|
||
|
</a-form-item>
|
||
2 years ago
|
|
||
2 years ago
|
<a-form-item :label="$t('labels.dbType')" v-bind="validateInfos['dataSource.client']">
|
||
|
<a-select v-model:value="formState.dataSource.client" size="small" @change="onClientChange">
|
||
|
<a-select-option v-for="client in clientTypes" :key="client.value" :value="client.value"
|
||
2 years ago
|
>{{ client.text }}
|
||
2 years ago
|
</a-select-option>
|
||
|
</a-select>
|
||
|
</a-form-item>
|
||
2 years ago
|
|
||
|
<!-- SQLite File -->
|
||
|
<a-form-item
|
||
|
v-if="formState.dataSource.client === 'sqlite3'"
|
||
|
:label="$t('labels.sqliteFile')"
|
||
2 years ago
|
v-bind="validateInfos['dataSource.connection.connection.filename']"
|
||
2 years ago
|
>
|
||
|
<a-input v-model:value="formState.dataSource.connection.connection.filename" size="small" />
|
||
2 years ago
|
</a-form-item>
|
||
2 years ago
|
|
||
|
<template v-else>
|
||
|
<!-- Host Address -->
|
||
|
<a-form-item :label="$t('labels.hostAddress')" v-bind="validateInfos['dataSource.connection.host']">
|
||
|
<a-input v-model:value="formState.dataSource.connection.host" size="small" />
|
||
|
</a-form-item>
|
||
|
|
||
|
<!-- Port Number -->
|
||
|
<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" size="small" />
|
||
|
</a-form-item>
|
||
|
|
||
|
<!-- Username -->
|
||
|
<a-form-item :label="$t('labels.username')" v-bind="validateInfos['dataSource.connection.user']">
|
||
|
<a-input v-model:value="formState.dataSource.connection.user" size="small" />
|
||
|
</a-form-item>
|
||
|
|
||
|
<!-- Password -->
|
||
|
<a-form-item :label="$t('labels.password')">
|
||
|
<a-input-password v-model:value="formState.dataSource.connection.password" size="small" />
|
||
|
</a-form-item>
|
||
|
|
||
|
<!-- Database -->
|
||
|
<a-form-item :label="$t('labels.database')" v-bind="validateInfos['dataSource.connection.database']">
|
||
|
<!-- Database : create if not exists -->
|
||
|
<a-input
|
||
|
v-model:value="formState.dataSource.connection.database"
|
||
|
:placeholder="$t('labels.dbCreateIfNotExists')"
|
||
|
size="small"
|
||
|
/>
|
||
|
</a-form-item>
|
||
|
<!-- Schema name -->
|
||
2 years ago
|
<a-form-item
|
||
|
v-if="['mssql', 'pg'].includes(formState.dataSource.client)"
|
||
|
:label="$t('labels.schemaName')"
|
||
|
v-bind="validateInfos['dataSource.connection.searchPath.0']"
|
||
|
>
|
||
2 years ago
|
<a-input v-model:value="formState.dataSource.connection.searchPath[0]" size="small" />
|
||
|
</a-form-item>
|
||
|
|
||
|
<a-collapse ghost expand-icon-position="right">
|
||
|
<a-collapse-panel key="1" :header="$t('title.advancedParameters')">
|
||
|
<!-- todo: add in i18n -->
|
||
2 years ago
|
<a-form-item label="SSL mode">
|
||
|
<a-select v-model:value="formState.sslUse" size="small" @change="onClientChange">
|
||
2 years ago
|
<a-select-option v-for="opt in sslUsage" :key="opt" :value="opt">{{ opt }}</a-select-option>
|
||
|
</a-select>
|
||
|
</a-form-item>
|
||
|
|
||
2 years ago
|
<a-form-item label="SSL keys">
|
||
2 years ago
|
<div class="flex gap-2">
|
||
|
<a-tooltip placement="top">
|
||
|
<!-- Select .cert file -->
|
||
|
<template #title>
|
||
2 years ago
|
<span>{{ $t('tooltip.clientCert') }}</span>
|
||
2 years ago
|
</template>
|
||
2 years ago
|
<a-button :disabled="!sslFilesRequired" size="small" @click="certFileInput.click()">
|
||
2 years ago
|
{{ $t('labels.clientCert') }}
|
||
2 years ago
|
</a-button>
|
||
|
</a-tooltip>
|
||
|
<a-tooltip placement="top">
|
||
|
<!-- Select .key file -->
|
||
|
<template #title>
|
||
2 years ago
|
<span>{{ $t('tooltip.clientKey') }}</span>
|
||
2 years ago
|
</template>
|
||
2 years ago
|
<a-button :disabled="!sslFilesRequired" size="small" @click="keyFileInput.click()">
|
||
2 years ago
|
{{ $t('labels.clientKey') }}
|
||
2 years ago
|
</a-button>
|
||
|
</a-tooltip>
|
||
|
<a-tooltip placement="top">
|
||
|
<!-- Select CA file -->
|
||
|
<template #title>
|
||
2 years ago
|
<span>{{ $t('tooltip.clientCA') }}</span>
|
||
2 years ago
|
</template>
|
||
2 years ago
|
<a-button :disabled="!sslFilesRequired" size="small" @click="caFileInput.click()">
|
||
2 years ago
|
{{ $t('labels.serverCA') }}
|
||
2 years ago
|
</a-button>
|
||
|
</a-tooltip>
|
||
|
</div>
|
||
|
</a-form-item>
|
||
|
|
||
|
<input ref="caFileInput" type="file" class="!hidden" @change="onFileSelect('ca', caFileInput)" />
|
||
|
<input ref="certFileInput" type="file" class="!hidden" @change="onFileSelect('cert', certFileInput)" />
|
||
|
<input ref="keyFileInput" type="file" class="!hidden" @change="onFileSelect('key', keyFileInput)" />
|
||
|
|
||
2 years ago
|
<a-form-item :label="$t('labels.inflection.tableName')" v-bind="validateInfos['dataSource.client']">
|
||
2 years ago
|
<a-select v-model:value="formState.inflection.inflection_table" size="small" @change="onClientChange">
|
||
2 years ago
|
<a-select-option v-for="type in inflectionTypes" :key="type" :value="type">{{ type }}</a-select-option>
|
||
|
</a-select>
|
||
|
</a-form-item>
|
||
2 years ago
|
<a-form-item :label="$t('labels.inflection.columnName')" v-bind="validateInfos['dataSource.type']">
|
||
2 years ago
|
<a-select v-model:value="formState.inflection.inflection_column" size="small" @change="onClientChange">
|
||
2 years ago
|
<a-select-option v-for="type in inflectionTypes" :key="type" :value="type">{{ type }}</a-select-option>
|
||
|
</a-select>
|
||
|
</a-form-item>
|
||
|
<div class="flex justify-end">
|
||
|
<a-button size="small" @click="configEditDlg = true">
|
||
|
<!-- Edit connection JSON -->
|
||
2 years ago
|
{{ $t('activity.editConnJson') }}
|
||
2 years ago
|
</a-button>
|
||
2 years ago
|
</div>
|
||
2 years ago
|
</a-collapse-panel>
|
||
|
</a-collapse>
|
||
|
</template>
|
||
2 years ago
|
|
||
2 years ago
|
<a-form-item class="flex justify-center mt-5">
|
||
2 years ago
|
<div class="flex justify-center gap-2">
|
||
2 years ago
|
<a-button type="primary" :disabled="!testSuccess" @click="createProject">Submit</a-button>
|
||
2 years ago
|
<a-button type="primary" @click="testConnection">Test Connection</a-button>
|
||
2 years ago
|
</div>
|
||
|
</a-form-item>
|
||
|
</a-form>
|
||
2 years ago
|
|
||
|
<v-dialog v-model="configEditDlg">
|
||
2 years ago
|
<a-card>
|
||
|
<Monaco v-if="configEditDlg" v-model="formState" class="h-[400px] w-[600px]"></Monaco>
|
||
|
</a-card>
|
||
2 years ago
|
</v-dialog>
|
||
2 years ago
|
</a-card>
|
||
2 years ago
|
</template>
|
||
|
|
||
2 years ago
|
<style scoped>
|
||
|
:deep(.ant-collapse-header) {
|
||
2 years ago
|
@apply !pr-10 !-mt-4 text-right;
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
:deep(.ant-collapse-content-box) {
|
||
|
@apply !pr-0;
|
||
|
}
|
||
2 years ago
|
|
||
|
:deep(.ant-form-item-explain-error) {
|
||
|
@apply !text-xs;
|
||
|
}
|
||
2 years ago
|
</style>
|