Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Create data source

天舟05b059609a94
diff --git a/frontend/src/components/DataSourceNewForm.vue b/frontend/src/components/DataSourceNewForm.vue
new file mode 100644
index 0000000000..5698621fed
--- /dev/null
+++ b/frontend/src/components/DataSourceNewForm.vue
@@ -0,0 +1,191 @@
+<template>
+ <form
+ class="px-4 space-y-4"
+ @submit.prevent="$emit('create', state.dataSource)"
+ >
+ <div class="pt-6 grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">
+ <div class="sm:col-span-2">
+ <label for="name" class="text-lg leading-6 font-medium text-control">
+ Name <span class="text-red-600">*</span>
+ </label>
+ <input
+ required
+ id="name"
+ name="name"
+ type="text"
+ class="textfield mt-2 w-full"
+ :value="state.dataSource.name"
+ @input="updateDataSource('name', $event.target.value)"
+ />
+ </div>
+ </div>
+
+ <div class="pt-6 grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">
+ <div class="sm:col-span-2">
+ <label
+ for="database"
+ class="text-lg leading-6 font-medium text-control"
+ >
+ Database <span class="text-red-600">*</span>
+ </label>
+ <DatabaseSelect
+ class="mt-2"
+ :instanceId="instanceId"
+ @select-database-id="
+ (databaseId) => {
+ updateDataSource('databaseId', databaseId);
+ }
+ "
+ />
+ </div>
+ </div>
+
+ <div class="pt-6 grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">
+ <div class="sm:col-span-2">
+ <label
+ for="username"
+ class="text-lg leading-6 font-medium text-control"
+ >
+ Username
+ </label>
+ <input
+ required
+ id="username"
+ name="username"
+ type="text"
+ class="textfield mt-2 w-full"
+ :value="state.dataSource.username"
+ @input="updateDataSource('username', $event.target.value)"
+ />
+ </div>
+ </div>
+
+ <div class="pt-6 grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">
+ <div class="sm:col-span-2">
+ <div class="flex flex-row space-x-1">
+ <label
+ for="username"
+ class="text-lg leading-6 font-medium text-control"
+ >
+ Password
+ </label>
+ <button
+ class="btn-icon"
+ @click.prevent="state.showPassword = !state.showPassword"
+ >
+ <svg
+ v-if="state.showPassword"
+ class="w-6 h-6"
+ fill="none"
+ stroke="currentColor"
+ viewBox="0 0 24 24"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="2"
+ d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.543 7a10.025 10.025 0 01-4.132 5.411m0 0L21 21"
+ ></path>
+ </svg>
+ <svg
+ v-else
+ class="w-6 h-6"
+ fill="none"
+ stroke="currentColor"
+ viewBox="0 0 24 24"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="2"
+ d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
+ ></path>
+ <path
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="2"
+ d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"
+ ></path>
+ </svg>
+ </button>
+ </div>
+ <input
+ required
+ id="password"
+ name="password"
+ autocomplete="off"
+ :type="state.showPassword ? 'text' : 'password'"
+ class="textfield mt-2 w-full"
+ :value="state.dataSource.password"
+ @input="updateDataSource('password', $event.target.value)"
+ />
+ </div>
+ </div>
+
+ <!-- Create button group -->
+ <div class="flex justify-end pt-5">
+ <button
+ type="button"
+ class="btn-normal py-2 px-4"
+ @click.prevent="$emit('cancel')"
+ >
+ Cancel
+ </button>
+ <button
+ type="submit"
+ class="btn-primary ml-3 inline-flex justify-center py-2 px-4"
+ :disabled="!allowCreate"
+ >
+ Create
+ </button>
+ </div>
+ </form>
+</template>
+
+<script lang="ts">
+import { computed, reactive } from "vue";
+import DatabaseSelect from "../components/DatabaseSelect.vue";
+import { DataSourceNew, ALL_DATABASE_ID } from "../types";
+
+interface LocalState {
+ dataSource: DataSourceNew;
+ showPassword: Boolean;
+}
+
+export default {
+ name: "DataSourceNewForm",
+ emits: ["create", "cancel"],
+ props: {
+ instanceId: {
+ required: true,
+ type: String,
+ },
+ },
+ components: { DatabaseSelect },
+ setup(props, ctx) {
+ const state = reactive<LocalState>({
+ dataSource: {
+ name: "New data source",
+ type: "RO",
+ },
+ showPassword: false,
+ });
+
+ const allowCreate = computed(() => {
+ return state.dataSource.name;
+ });
+
+ const updateDataSource = (field: string, value: string) => {
+ (state.dataSource as any)[field] = value;
+ };
+
+ return {
+ state,
+ allowCreate,
+ updateDataSource,
+ };
+ },
+};
+</script>
diff --git a/frontend/src/components/DataSourceTable.vue b/frontend/src/components/DataSourceTable.vue
index 86e0288ddb..71bf42693d 100644
--- a/frontend/src/components/DataSourceTable.vue
+++ b/frontend/src/components/DataSourceTable.vue
@@ -47,12 +47,33 @@
</svg>
</button>
</div>
- <BBTableSearch
- class="w-56"
- ref="searchField"
- :placeholder="'Search name, database'"
- @change-text="(text) => changeSearchText(text)"
- />
+ <div class="flex flex-row items-center space-x-4">
+ <button
+ class="w-8 h-8 items-center flex justify-center btn-icon-primary"
+ @click.prevent="state.showCreateModal = true"
+ >
+ <svg
+ class="w-6 h-6"
+ fill="none"
+ stroke="currentColor"
+ viewBox="0 0 24 24"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="2"
+ d="M12 6v6m0 0v6m0-6h6m-6 0H6"
+ ></path>
+ </svg>
+ </button>
+ <BBTableSearch
+ class="w-56"
+ ref="searchField"
+ :placeholder="'Search name, database'"
+ @change-text="(text) => changeSearchText(text)"
+ />
+ </div>
</div>
<BBTable
:columnList="columnList"
@@ -94,16 +115,28 @@
</template>
</BBTable>
</div>
+ <BBModal
+ v-if="state.showCreateModal"
+ :title="'Create data source'"
+ @close="state.showCreateModal = false"
+ >
+ <DataSourceNewForm
+ :instanceId="instance.id"
+ @create="doCreate"
+ @cancel="state.showCreateModal = false"
+ />
+ </BBModal>
</template>
<script lang="ts">
import { computed, reactive, PropType } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
-import { BBTableColumn, BBTableSectionDataSource } from "../bbkit/types";
+import DataSourceNewForm from "../components/DataSourceNewForm.vue";
+import { BBTableColumn } from "../bbkit/types";
import { dataSourceSlug, instanceSlug } from "../utils";
-import { Instance, Database, DataSource } from "../types";
+import { Instance, DataSource, DataSourceNew } from "../types";
const columnList: BBTableColumn[] = [
{
@@ -129,11 +162,12 @@ const columnList: BBTableColumn[] = [
interface LocalState {
searchText: string;
showPassword: boolean;
+ showCreateModal: boolean;
}
export default {
name: "DataSourceTable",
- components: {},
+ components: { DataSourceNewForm },
props: {
instance: {
required: true,
@@ -147,6 +181,7 @@ export default {
const state = reactive<LocalState>({
searchText: "",
showPassword: false,
+ showCreateModal: false,
});
const dataSourceSectionList = computed(() => {
@@ -215,14 +250,33 @@ export default {
return sectionList;
});
+ const doCreate = (newDataSource: DataSourceNew) => {
+ store
+ .dispatch("dataSource/createDataSource", {
+ instanceId: props.instance.id,
+ newDataSource: newDataSource,
+ })
+ .then((dataSource) => {
+ store.dispatch("notification/pushNotification", {
+ module: "bytebase",
+ style: "SUCCESS",
+ title: `Successfully created data source '${dataSource.name}'.`,
+ });
+ router.push(
+ `/instance/${instanceSlug(props.instance)}/ds/${dataSourceSlug(
+ dataSource
+ )}`
+ );
+ })
+ .catch((error) => {
+ console.log(error);
+ });
+ };
+
const clickDataSource = function (section: number, row: number) {
const ds = dataSourceSectionList.value[section].list![row];
- const environmentName = props.instance.environment.name;
router.push(
- `/instance/${instanceSlug(props.instance)}/ds/${dataSourceSlug(
- ds.name,
- ds.id
- )}`
+ `/instance/${instanceSlug(props.instance)}/ds/${dataSourceSlug(ds)}`
);
};
@@ -234,6 +288,7 @@ export default {
state,
columnList,
dataSourceSectionList,
+ doCreate,
clickDataSource,
changeSearchText,
};
diff --git a/frontend/src/utils/slug.ts b/frontend/src/utils/slug.ts
index fd9a9eb6cd..b59f8f8f6e 100644
--- a/frontend/src/utils/slug.ts
+++ b/frontend/src/utils/slug.ts
@@ -1,4 +1,4 @@
-import { DataSourceId, Instance, TaskId } from "../types";
+import { DataSource, Instance, TaskId } from "../types";
import slug from "slug";
export function taskSlug(taskName: string, taskId: TaskId) {
@@ -14,11 +14,8 @@ export function instanceSlug(instance: Instance): string {
].join("-");
}
-export function dataSourceSlug(
- dataSourceName: string,
- dataSourceId: DataSourceId
-): string {
- return [slug(dataSourceName), dataSourceId].join("-");
+export function dataSourceSlug(dataSource: DataSource): string {
+ return [slug(dataSource.name), dataSource.id].join("-");
}
export function idFromSlug(slug: string) {
diff --git a/frontend/src/views/DataSourceForm.vue b/frontend/src/views/DataSourceForm.vue
deleted file mode 100644
index 3caa84cc2d..0000000000
--- a/frontend/src/views/DataSourceForm.vue
+++ /dev/null
@@ -1,75 +0,0 @@
-<template>
- <form
- class="px-4 space-y-6 divide-y divide-gray-200"
- @submit.prevent="$emit('submit', state.environment)"
- >
- <div class="pt-6 grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">
- <div class="sm:col-span-2">
- <label for="name" class="text-lg leading-6 font-medium text-control">
- Instance Name <span class="text-red-600">*</span>
- </label>
- <input
- required
- id="name"
- name="name"
- type="text"
- class="textfield mt-4 w-full"
- :value="state.dataSource.name"
- @input="updateDataSource('name', $event.target.value)"
- />
- </div>
- </div>
- <!-- Create button group -->
- <div class="flex justify-end pt-5">
- <button
- type="button"
- class="btn-normal py-2 px-4"
- @click.prevent="$emit('cancel')"
- >
- Cancel
- </button>
- <button
- type="submit"
- class="btn-primary ml-3 inline-flex justify-center py-2 px-4"
- >
- Create
- </button>
- </div>
- </form>
-</template>
-
-<script lang="ts">
-import { computed, reactive, PropType } from "vue";
-import cloneDeep from "lodash-es/cloneDeep";
-import isEqual from "lodash-es/isEqual";
-import { DataSourceNew } from "../types";
-
-interface LocalState {
- dataSource: DataSourceNew;
-}
-
-export default {
- name: "DataSourceForm",
- emits: ["submit", "cancel", "delete"],
- props: {},
- setup(props, ctx) {
- const state = reactive<LocalState>({
- dataSource: {
- name: "New Data Source",
- type: "RW",
- },
- });
-
- const updateDataSource = (field: string, value: string) => {
- if (state.dataSource) {
- (state.dataSource as any)[field] = value;
- }
- };
-
- return {
- state,
- updateDataSource,
- };
- },
-};
-</script>