Skip to content

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

chore: implement kv storage store to intial statements for creating issue (#14642)

boojack0841b622fa49
diff --git a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
index 2a5d7c9d39..38b561e3b1 100644
--- a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
@@ -154,6 +154,7 @@ import {
useNotificationStore,
useDBSchemaV1Store,
useAppFeature,
+ useStorageStore,
} from "@/store";
import type { ComposedDatabase } from "@/types";
import { dialectOfEngineV1, isValidProjectName, unknownProject } from "@/types";
@@ -454,7 +455,7 @@ const handlePreviewIssue = async () => {
);
const sqlStorageKey = `bb.issues.sql.${uuidv4()}`;
- localStorage.setItem(sqlStorageKey, state.editStatement);
+ useStorageStore().put(sqlStorageKey, state.editStatement);
query.sqlStorageKey = sqlStorageKey;
} else {
query.name = generateIssueTitle(
@@ -493,7 +494,7 @@ const handlePreviewIssue = async () => {
sqlMap[db.name] = sql;
});
const sqlMapStorageKey = `bb.issues.sql-map.${uuidv4()}`;
- localStorage.setItem(sqlMapStorageKey, JSON.stringify(sqlMap));
+ useStorageStore().put(sqlMapStorageKey, JSON.stringify(sqlMap));
query.sqlMapStorageKey = sqlMapStorageKey;
const databaseNameList = databaseList.value.map((db) => db.databaseName);
query.name = generateIssueTitle(
diff --git a/frontend/src/components/Branch/BranchDetailView.vue b/frontend/src/components/Branch/BranchDetailView.vue
index 3294f4bed7..dd6c0aee5c 100644
--- a/frontend/src/components/Branch/BranchDetailView.vue
+++ b/frontend/src/components/Branch/BranchDetailView.vue
@@ -140,6 +140,7 @@ import {
pushNotification,
useDatabaseV1Store,
useCurrentUserV1,
+ useStorageStore,
} from "@/store";
import { useBranchStore } from "@/store/modules/branch";
import { getProjectAndBranchId } from "@/store/modules/v1/common";
@@ -451,7 +452,7 @@ const handleApplyToDatabase = async (databaseNameList: string[]) => {
databaseStore.getDatabaseByName(name)
);
const sqlStorageKey = `bb.issues.sql.${uuidv4()}`;
- localStorage.setItem(sqlStorageKey, result.statement);
+ useStorageStore().put(sqlStorageKey, result.statement);
const query: Record<string, any> = {
template: "bb.issue.database.schema.update",
mode: "normal",
diff --git a/frontend/src/components/IssueV1/components/Sidebar/GhostSection/common.ts b/frontend/src/components/IssueV1/components/Sidebar/GhostSection/common.ts
index f35dc47716..511d7bcab3 100644
--- a/frontend/src/components/IssueV1/components/Sidebar/GhostSection/common.ts
+++ b/frontend/src/components/IssueV1/components/Sidebar/GhostSection/common.ts
@@ -11,7 +11,11 @@ import {
specForTask,
useIssueContext,
} from "@/components/IssueV1/logic";
-import { useCurrentUserV1, useSubscriptionV1Store } from "@/store";
+import {
+ useCurrentUserV1,
+ useStorageStore,
+ useSubscriptionV1Store,
+} from "@/store";
import type { ComposedDatabase, ComposedIssue } from "@/types";
import { Engine } from "@/types/proto/v1/common";
import { IssueStatus } from "@/types/proto/v1/issue_service";
@@ -133,7 +137,7 @@ export const provideIssueGhostContext = () => {
sqlMap[target] = getSheetStatement(sheet);
});
const sqlMapStorageKey = `bb.issues.sql-map.${uuidv4()}`;
- localStorage.setItem(sqlMapStorageKey, JSON.stringify(sqlMap));
+ useStorageStore().put(sqlMapStorageKey, JSON.stringify(sqlMap));
overrides["sqlMapStorageKey"] = sqlMapStorageKey;
await reInitialize(overrides);
diff --git a/frontend/src/components/IssueV1/components/Sidebar/PreBackupSection/TaskRollbackButton.vue b/frontend/src/components/IssueV1/components/Sidebar/PreBackupSection/TaskRollbackButton.vue
index e8110b1c1a..6bd13bcebe 100644
--- a/frontend/src/components/IssueV1/components/Sidebar/PreBackupSection/TaskRollbackButton.vue
+++ b/frontend/src/components/IssueV1/components/Sidebar/PreBackupSection/TaskRollbackButton.vue
@@ -19,7 +19,7 @@ import {
} from "@/components/IssueV1/logic";
import { rolloutServiceClient } from "@/grpcweb";
import { PROJECT_V1_ROUTE_ISSUE_DETAIL } from "@/router/dashboard/projectV1";
-import { pushNotification, useSheetV1Store } from "@/store";
+import { pushNotification, useSheetV1Store, useStorageStore } from "@/store";
import {
extractIssueUID,
extractProjectResourceName,
@@ -63,7 +63,7 @@ const createRestoreIssue = async () => {
isLoading.value = false;
const sqlStorageKey = `bb.issues.sql.${uuidv4()}`;
- localStorage.setItem(sqlStorageKey, statement);
+ useStorageStore().put(sqlStorageKey, statement);
const query: Record<string, any> = {
template: "bb.issue.database.data.update",
name: `Rollback ${selectedTask.value.title} in issue#${extractIssueUID(issue.value.name)}`,
diff --git a/frontend/src/components/IssueV1/logic/initialize/create.ts b/frontend/src/components/IssueV1/logic/initialize/create.ts
index 50dccdae8e..f084455b22 100644
--- a/frontend/src/components/IssueV1/logic/initialize/create.ts
+++ b/frontend/src/components/IssueV1/logic/initialize/create.ts
@@ -13,6 +13,7 @@ import {
useEnvironmentV1Store,
useProjectV1Store,
useSheetV1Store,
+ useStorageStore,
} from "@/store";
import {
databaseNamePrefix,
@@ -78,7 +79,7 @@ export const createIssueSkeleton = async (
const params: CreateIssueParams = {
project,
query,
- initialSQL: extractInitialSQLFromQuery(query),
+ initialSQL: await extractInitialSQLFromQuery(query),
branch: query.branch || undefined,
};
@@ -471,9 +472,10 @@ const maybeSetInitialDatabaseConfigForSpec = async (
}
};
-const extractInitialSQLFromQuery = (
+const extractInitialSQLFromQuery = async (
query: Record<string, string>
-): InitialSQL => {
+): Promise<InitialSQL> => {
+ const storageStore = useStorageStore();
const sql = query.sql;
if (sql && typeof sql === "string") {
return {
@@ -482,14 +484,14 @@ const extractInitialSQLFromQuery = (
}
const sqlStorageKey = query.sqlStorageKey;
if (sqlStorageKey && typeof sqlStorageKey === "string") {
- const sql = localStorage.getItem(sqlStorageKey) ?? "";
+ const sql = (await storageStore.get(sqlStorageKey)) || "";
return {
sql,
};
}
const sqlMapStorageKey = query.sqlMapStorageKey;
if (sqlMapStorageKey && typeof sqlMapStorageKey === "string") {
- const sqlMapJSON = localStorage.getItem(sqlMapStorageKey) ?? "{}";
+ const sqlMapJSON = (await storageStore.get(sqlStorageKey)) || "{}";
try {
const sqlMap = JSON.parse(sqlMapJSON) as Record<string, string>;
const keys = Object.keys(sqlMap);
diff --git a/frontend/src/components/Plan/logic/initialize/create.ts b/frontend/src/components/Plan/logic/initialize/create.ts
index 3b13399147..fea0ea8284 100644
--- a/frontend/src/components/Plan/logic/initialize/create.ts
+++ b/frontend/src/components/Plan/logic/initialize/create.ts
@@ -9,6 +9,7 @@ import {
useEnvironmentV1Store,
useProjectV1Store,
useSheetV1Store,
+ useStorageStore,
} from "@/store";
import { projectNamePrefix } from "@/store/modules/v1/common";
import { useDatabaseV1List } from "@/store/modules/v1/databaseList";
@@ -69,7 +70,7 @@ export const createPlanSkeleton = async (
databaseNameList,
project,
query,
- initialSQL: extractInitialSQLFromQuery(query),
+ initialSQL: await extractInitialSQLFromQuery(query),
};
// Prepare params context for building plan.
@@ -369,9 +370,10 @@ const maybeSetInitialDatabaseConfigForSpec = async (
}
};
-const extractInitialSQLFromQuery = (
+const extractInitialSQLFromQuery = async (
query: Record<string, string>
-): InitialSQL => {
+): Promise<InitialSQL> => {
+ const storageStore = useStorageStore();
const sql = query.sql;
if (sql && typeof sql === "string") {
return {
@@ -380,14 +382,14 @@ const extractInitialSQLFromQuery = (
}
const sqlStorageKey = query.sqlStorageKey;
if (sqlStorageKey && typeof sqlStorageKey === "string") {
- const sql = localStorage.getItem(sqlStorageKey) ?? "";
+ const sql = (await storageStore.get(sqlStorageKey)) || "";
return {
sql,
};
}
const sqlMapStorageKey = query.sqlMapStorageKey;
if (sqlMapStorageKey && typeof sqlMapStorageKey === "string") {
- const sqlMapJSON = localStorage.getItem(sqlMapStorageKey) ?? "{}";
+ const sqlMapJSON = (await storageStore.get(sqlStorageKey)) || "{}";
try {
const sqlMap = JSON.parse(sqlMapJSON) as Record<string, string>;
const keys = Object.keys(sqlMap);
diff --git a/frontend/src/components/SlowQuery/Panel/IndexAdvisor.vue b/frontend/src/components/SlowQuery/Panel/IndexAdvisor.vue
index 68619a6bd5..05f8ef64fe 100644
--- a/frontend/src/components/SlowQuery/Panel/IndexAdvisor.vue
+++ b/frontend/src/components/SlowQuery/Panel/IndexAdvisor.vue
@@ -64,7 +64,7 @@ import { FeatureBadge, FeatureModal } from "@/components/FeatureGuard";
import { databaseServiceClient } from "@/grpcweb";
import { PROJECT_V1_ROUTE_ISSUE_DETAIL } from "@/router/dashboard/projectV1";
import { SETTING_ROUTE_WORKSPACE_GENERAL } from "@/router/dashboard/workspaceSetting";
-import { featureToRef, hasFeature } from "@/store";
+import { featureToRef, hasFeature, useStorageStore } from "@/store";
import { useSettingV1Store } from "@/store/modules/v1/setting";
import type { ComposedSlowQueryLog } from "@/types";
import { extractProjectResourceName, generateIssueTitle } from "@/utils";
@@ -115,7 +115,7 @@ const showIndexAdvisor = computed(() => {
const handleCreateIndex = () => {
const sqlStorageKey = `bb.issues.sql.${uuidv4()}`;
- localStorage.setItem(sqlStorageKey, state.createIndexStatement);
+ useStorageStore().put(sqlStorageKey, state.createIndexStatement);
const query: Record<string, any> = {
template: "bb.issue.database.schema.update",
name: generateIssueTitle("bb.issue.database.schema.update", [
diff --git a/frontend/src/components/SyncDatabaseSchema/index.vue b/frontend/src/components/SyncDatabaseSchema/index.vue
index b5f15e0152..83cc4097b4 100644
--- a/frontend/src/components/SyncDatabaseSchema/index.vue
+++ b/frontend/src/components/SyncDatabaseSchema/index.vue
@@ -73,7 +73,7 @@ import { useRouter } from "vue-router";
import { StepTab } from "@/components/v2";
import { PROJECT_V1_ROUTE_ISSUE_DETAIL } from "@/router/dashboard/projectV1";
import { WORKSPACE_ROOT_MODULE } from "@/router/dashboard/workspaceRoutes";
-import { useProjectV1Store } from "@/store";
+import { useProjectV1Store, useStorageStore } from "@/store";
import type { ComposedProject } from "@/types";
import {
isValidDatabaseName,
@@ -257,7 +257,7 @@ const tryFinishSetup = async () => {
});
query.databaseList = Object.keys(sqlMap).join(",");
const sqlMapStorageKey = `bb.issues.sql-map.${uuidv4()}`;
- localStorage.setItem(sqlMapStorageKey, JSON.stringify(sqlMap));
+ useStorageStore().put(sqlMapStorageKey, JSON.stringify(sqlMap));
query.sqlMapStorageKey = sqlMapStorageKey;
query.name = generateIssueTitle(
"bb.issue.database.schema.update",
diff --git a/frontend/src/store/modules/v1/index.ts b/frontend/src/store/modules/v1/index.ts
index 018533707f..2c769c167f 100644
--- a/frontend/src/store/modules/v1/index.ts
+++ b/frontend/src/store/modules/v1/index.ts
@@ -28,3 +28,4 @@ export * from "./group";
export * from "./permission";
export * from "./workspace";
export * from "./instanceResource";
+export * from "./storage";
diff --git a/frontend/src/store/modules/v1/storage.ts b/frontend/src/store/modules/v1/storage.ts
new file mode 100644
index 0000000000..8df60ec1fa
--- /dev/null
+++ b/frontend/src/store/modules/v1/storage.ts
@@ -0,0 +1,48 @@
+import { defineStore } from "pinia";
+import PouchDB from "pouchdb";
+import PouchDBFind from "pouchdb-find";
+
+PouchDB.plugin(PouchDBFind);
+
+// The simple storage store for saving key-value pairs.
+export const useStorageStore = defineStore("storageStore", () => {
+ const db = new PouchDB<{
+ _id: string;
+ value: any;
+ }>("bb.storage", {
+ // Remove unused and old data automatically.
+ auto_compaction: true,
+ // Do not save and track old revisions, this is for saving storage usage.
+ revs_limit: 1,
+ });
+
+ const put = async (key: string, value: any) => {
+ await db.put(
+ {
+ _id: key,
+ value,
+ },
+ { force: true }
+ );
+ };
+ const get = async (key: string) => {
+ try {
+ const doc = await db.get(key);
+ return doc.value;
+ } catch {
+ return undefined;
+ }
+ };
+ const remove = async (key: string) => {
+ const doc = await db.get(key);
+ if (doc) {
+ await db.remove(doc);
+ }
+ };
+
+ return {
+ get,
+ put,
+ remove,
+ };
+});
diff --git a/frontend/src/views/sql-editor/EditorCommon/ExecuteHint.vue b/frontend/src/views/sql-editor/EditorCommon/ExecuteHint.vue
index 9ceacbb31b..863ab8c839 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ExecuteHint.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ExecuteHint.vue
@@ -76,6 +76,7 @@ import {
useAppFeature,
useDatabaseV1Store,
useSQLEditorTabStore,
+ useStorageStore,
} from "@/store";
import type { ComposedDatabase } from "@/types";
import { DatabaseChangeMode } from "@/types/proto/v1/setting_service";
@@ -176,7 +177,7 @@ const gotoCreateIssue = () => {
const db = useDatabaseV1Store().getDatabaseByName(database);
const sqlStorageKey = `bb.issues.sql.${uuidv4()}`;
- localStorage.setItem(sqlStorageKey, statement.value);
+ useStorageStore().put(sqlStorageKey, statement.value);
const route = router.resolve({
name: PROJECT_V1_ROUTE_ISSUE_DETAIL,
params: {
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
index d67a3243af..c89daa730d 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
@@ -217,6 +217,7 @@ import {
useAppFeature,
pushNotification,
usePolicyByParentAndType,
+ useStorageStore,
} from "@/store";
import { useExportData } from "@/store/modules/export";
import type {
@@ -515,7 +516,7 @@ const handleRequestExport = async () => {
const project = database.projectEntity;
const issueType = "bb.issue.database.data.export";
const sqlStorageKey = `bb.issues.sql.${uuidv4()}`;
- localStorage.setItem(sqlStorageKey, props.result.statement);
+ useStorageStore().put(sqlStorageKey, props.result.statement);
const query: Record<string, any> = {
template: issueType,
name: generateIssueTitle(issueType, [database.databaseName]),