|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { ConnectionThroughParameters, DatasourceDriver } from '../../../../../libs/datasources' |
| 3 | +
|
| 4 | +import { useClipboard, useRefHistory } from '@vueuse/core' |
| 5 | +import { computed, onMounted, ref, watch } from 'vue' |
| 6 | +import { RouterLink, useRoute } from 'vue-router' |
| 7 | +
|
| 8 | +import Button from '../../../../../components/basic/Button.vue' |
| 9 | +import Editable from '../../../../../components/basic/Editable.vue' |
| 10 | +import { Input } from '../../../../../components/ui/input' |
| 11 | +import { useLocalPGLite } from '../../../../../composables/ipc/databases/local' |
| 12 | +import { dialog } from '../../../../../composables/ipc/electron' |
| 13 | +import { DatasourceDriverEnum, defaultParamsFromDriver, fromDSN, toDSN } from '../../../../../libs/datasources' |
| 14 | +import { useDatasourcesStore } from '../../../../../stores/datasources' |
| 15 | +
|
| 16 | +const route = useRoute('/datasources/sqlite/edit/[id]/') |
| 17 | +
|
| 18 | +const id = computed(() => route.params.id) |
| 19 | +const driver = computed(() => DatasourceDriverEnum.SQLite) |
| 20 | +
|
| 21 | +const testConnectionConnecting = ref(false) |
| 22 | +const testConnectionSucceeded = ref(false) |
| 23 | +const testConnectionErrored = ref(false) |
| 24 | +const testConnectionErrorMessage = ref('') |
| 25 | +
|
| 26 | +const datasourcesStore = useDatasourcesStore() |
| 27 | +
|
| 28 | +function datasourceFromId() { |
| 29 | + const datasource = datasourcesStore.datasources.find(ds => ds.id === id.value) |
| 30 | + if (typeof datasource === 'undefined') { |
| 31 | + const newDatasource = datasourcesStore.createDatasource(driver.value as DatasourceDriver) |
| 32 | + datasourcesStore.datasources.push(newDatasource) |
| 33 | + return newDatasource |
| 34 | + } |
| 35 | +
|
| 36 | + return datasource |
| 37 | +} |
| 38 | +
|
| 39 | +const datasource = computed({ |
| 40 | + get: () => datasourceFromId(), |
| 41 | + set: (value) => { |
| 42 | + const datasourceIndex = datasourcesStore.datasources.findIndex(ds => ds.id === id.value) |
| 43 | + if (datasourceIndex !== -1) { |
| 44 | + datasourcesStore.datasources[datasourceIndex] = value |
| 45 | + } |
| 46 | + else { |
| 47 | + console.error(`Datasource with id ${id.value} not found in store.`) |
| 48 | + } |
| 49 | + }, |
| 50 | +}) |
| 51 | +
|
| 52 | +const DSN = computed({ |
| 53 | + get: () => { |
| 54 | + return toDSN( |
| 55 | + driver.value, |
| 56 | + datasource.value as ConnectionThroughParameters, |
| 57 | + defaultParamsFromDriver(driver.value), |
| 58 | + ) |
| 59 | + }, |
| 60 | + set: (value) => { |
| 61 | + if (!datasource.value) |
| 62 | + return |
| 63 | +
|
| 64 | + const params = datasource.value as ConnectionThroughParameters |
| 65 | + const paramsFromDSN = fromDSN( |
| 66 | + value, |
| 67 | + defaultParamsFromDriver(driver.value), |
| 68 | + ) |
| 69 | +
|
| 70 | + params.host = paramsFromDSN.host |
| 71 | + params.port = paramsFromDSN.port |
| 72 | + params.user = paramsFromDSN.user |
| 73 | + params.password = paramsFromDSN.password |
| 74 | + params.database = paramsFromDSN.database |
| 75 | + params.extraOptions = paramsFromDSN.extraOptions || { sslmode: false } |
| 76 | + }, |
| 77 | +}) |
| 78 | +
|
| 79 | +const dbFilePath = computed({ |
| 80 | + get: () => { |
| 81 | + const paramsFromDSN = fromDSN( |
| 82 | + DSN.value, |
| 83 | + defaultParamsFromDriver(driver.value), |
| 84 | + ) |
| 85 | +
|
| 86 | + return paramsFromDSN.extraOptions?.dbFilePath as string |
| 87 | + }, |
| 88 | + set: (val) => { |
| 89 | + const dsn = toDSN( |
| 90 | + driver.value, |
| 91 | + { |
| 92 | + ...datasource.value, |
| 93 | + extraOptions: { dbFilePath: val }, |
| 94 | + } as ConnectionThroughParameters, |
| 95 | + defaultParamsFromDriver(driver.value), |
| 96 | + ) |
| 97 | +
|
| 98 | + DSN.value = dsn |
| 99 | + }, |
| 100 | +}) |
| 101 | +
|
| 102 | +const datasourceName = computed({ |
| 103 | + get: () => datasource.value.name || 'New Datasource', |
| 104 | + set: (value) => { |
| 105 | + datasource.value.name = value |
| 106 | + }, |
| 107 | +}) |
| 108 | +
|
| 109 | +const { undo, clear } = useRefHistory(datasourceName) |
| 110 | +
|
| 111 | +// TODO: ? |
| 112 | +onMounted(() => { |
| 113 | + DSN.value = toDSN( |
| 114 | + driver.value, |
| 115 | + datasource.value as ConnectionThroughParameters, |
| 116 | + defaultParamsFromDriver(driver.value), |
| 117 | + ) |
| 118 | +}) |
| 119 | +
|
| 120 | +watch([id, driver], () => { |
| 121 | + clear() |
| 122 | + datasource.value = datasourceFromId() |
| 123 | +}) |
| 124 | +
|
| 125 | +function handleBlur() { |
| 126 | + if (!datasourceName.value) { |
| 127 | + undo() |
| 128 | + } |
| 129 | +} |
| 130 | +
|
| 131 | +async function handleTestConnection() { |
| 132 | + const { connect, execute } = useLocalPGLite() |
| 133 | + let dsn = '' |
| 134 | +
|
| 135 | + if ('connectionString' in datasource.value && !!datasource.value.connectionString) { |
| 136 | + dsn = datasource.value.connectionString |
| 137 | + } |
| 138 | + else { |
| 139 | + const params = datasource.value as ConnectionThroughParameters |
| 140 | + dsn = toDSN(driver.value, params, defaultParamsFromDriver(driver.value)) |
| 141 | + } |
| 142 | +
|
| 143 | + try { |
| 144 | + testConnectionSucceeded.value = false |
| 145 | + testConnectionConnecting.value = true |
| 146 | +
|
| 147 | + await connect(dsn) |
| 148 | + // eslint-disable-next-line no-console |
| 149 | + console.debug(await execute('SELECT 1')) |
| 150 | +
|
| 151 | + testConnectionSucceeded.value = true |
| 152 | + } |
| 153 | + catch (err) { |
| 154 | + testConnectionErrored.value = true |
| 155 | +
|
| 156 | + const e = err as Error |
| 157 | + testConnectionErrorMessage.value = e.message || 'Unknown error occurred while testing connection.' |
| 158 | + if (e.cause != null) { |
| 159 | + testConnectionErrorMessage.value += ` Cause: ${String(e.cause)}` |
| 160 | + } |
| 161 | +
|
| 162 | + console.error('Error testing connection:', testConnectionErrorMessage.value) |
| 163 | + } |
| 164 | + finally { |
| 165 | + testConnectionConnecting.value = false |
| 166 | + } |
| 167 | +} |
| 168 | +
|
| 169 | +async function handlePick() { |
| 170 | + const res = await dialog('showOpenDialog').call({ |
| 171 | + properties: ['openFile', 'createDirectory'], |
| 172 | + }) |
| 173 | + if (!res.filePaths) { |
| 174 | + return |
| 175 | + } |
| 176 | +
|
| 177 | + dbFilePath.value = res.filePaths[0] || '' |
| 178 | +} |
| 179 | +
|
| 180 | +async function handlePasteDSN() { |
| 181 | + const { text } = useClipboard() |
| 182 | + DSN.value = text.value || '' |
| 183 | +} |
| 184 | +</script> |
| 185 | + |
1 | 186 | <template> |
2 | | - <slot /> |
| 187 | + <div h-full flex flex-col> |
| 188 | + <div flex> |
| 189 | + <h2 text="neutral-300/80" mb-1 flex flex-1> |
| 190 | + Edit SQLite Datasource |
| 191 | + </h2> |
| 192 | + <RouterLink to="/datasources"> |
| 193 | + <div i-ph:x-bold text="neutral-300/80" /> |
| 194 | + </RouterLink> |
| 195 | + </div> |
| 196 | + <div h-full flex flex-col> |
| 197 | + <div mt-3 flex flex-1 flex-col gap-2> |
| 198 | + <Editable v-model="datasourceName" mb-3 font-bold @blur="handleBlur"> |
| 199 | + {{ driver }} |
| 200 | + </Editable> |
| 201 | + <div> |
| 202 | + <label flex="~ col gap-2"> |
| 203 | + <div> |
| 204 | + <div class="flex items-center gap-1 text-sm font-medium"> |
| 205 | + Database File |
| 206 | + </div> |
| 207 | + <div class="text-xs text-neutral-500 dark:text-neutral-400"> |
| 208 | + Database file path of the SQLite database. |
| 209 | + </div> |
| 210 | + </div> |
| 211 | + <div flex items-center gap-2> |
| 212 | + <Input v-model="dbFilePath" flex-1 /> |
| 213 | + <Button @click="handlePick"> |
| 214 | + Pick |
| 215 | + </Button> |
| 216 | + <Button @click="handlePasteDSN"> |
| 217 | + Paste |
| 218 | + </Button> |
| 219 | + </div> |
| 220 | + </label> |
| 221 | + </div> |
| 222 | + </div> |
| 223 | + <div flex flex-col gap-3> |
| 224 | + <div v-if="testConnectionErrored" class="mt-2 text-sm text-red-500" border="2 solid red-800/50" bg="red-900/50" flex items-center gap-1 rounded-lg px-3 py-2 text-lg> |
| 225 | + <div i-ph:warning-circle-bold mr-1 inline-block /> |
| 226 | + {{ testConnectionErrorMessage }} |
| 227 | + </div> |
| 228 | + <button bg="green-800/50" flex items-center justify-center gap-2 rounded-lg px-3 py-2 @click="handleTestConnection"> |
| 229 | + Test |
| 230 | + <div v-if="testConnectionConnecting" i-svg-spinners:270-ring /> |
| 231 | + <div v-else-if="testConnectionSucceeded" i-ph:check-bold /> |
| 232 | + </button> |
| 233 | + </div> |
| 234 | + </div> |
| 235 | + </div> |
3 | 236 | </template> |
0 commit comments