From 1f0ae98c88a9ccb2be4e7b55e76108d403eadaef Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sun, 27 Mar 2022 12:57:44 +0200 Subject: [PATCH] redis export keys --- .../api/src/controllers/databaseConnections.js | 5 +++++ .../api/src/proc/databaseConnectionProcess.js | 5 +++++ packages/types/engines.d.ts | 1 + packages/web/src/widgets/DbKeysTreeNode.svelte | 18 ++++++++++++++++++ .../dbgate-plugin-redis/src/backend/driver.js | 14 ++++++++++++++ 5 files changed, 43 insertions(+) diff --git a/packages/api/src/controllers/databaseConnections.js b/packages/api/src/controllers/databaseConnections.js index 32511167..164652fa 100644 --- a/packages/api/src/controllers/databaseConnections.js +++ b/packages/api/src/controllers/databaseConnections.js @@ -180,6 +180,11 @@ module.exports = { return this.loadDataCore('loadKeys', { conid, database, root }); }, + exportKeys_meta: true, + async exportKeys({ conid, database, options }) { + return this.loadDataCore('exportKeys', { conid, database, options }); + }, + loadKeyInfo_meta: true, async loadKeyInfo({ conid, database, key }) { return this.loadDataCore('loadKeyInfo', { conid, database, key }); diff --git a/packages/api/src/proc/databaseConnectionProcess.js b/packages/api/src/proc/databaseConnectionProcess.js index 9f036ded..aea80591 100644 --- a/packages/api/src/proc/databaseConnectionProcess.js +++ b/packages/api/src/proc/databaseConnectionProcess.js @@ -201,6 +201,10 @@ async function handleLoadKeys({ msgid, root }) { return handleDriverDataCore(msgid, driver => driver.loadKeys(systemConnection, root)); } +async function handleExportKeys({ msgid, options }) { + return handleDriverDataCore(msgid, driver => driver.exportKeys(systemConnection, options)); +} + async function handleLoadKeyInfo({ msgid, key }) { return handleDriverDataCore(msgid, driver => driver.loadKeyInfo(systemConnection, key)); } @@ -307,6 +311,7 @@ const messageHandlers = { generateDeploySql: handleGenerateDeploySql, loadFieldValues: handleLoadFieldValues, sqlSelect: handleSqlSelect, + exportKeys: handleExportKeys, // runCommand: handleRunCommand, }; diff --git a/packages/types/engines.d.ts b/packages/types/engines.d.ts index 94f233fa..75ab67b9 100644 --- a/packages/types/engines.d.ts +++ b/packages/types/engines.d.ts @@ -89,6 +89,7 @@ export interface EngineDriver { }[] >; loadKeys(pool, root: string): Promise; + exportKeys(pool, options: {}): Promise; loadKeyInfo(pool, key): Promise; loadKeyTableRange(pool, key, cursor, count): Promise; loadFieldValues(pool: any, name: NamedObjectInfo, field: string, search: string): Promise; diff --git a/packages/web/src/widgets/DbKeysTreeNode.svelte b/packages/web/src/widgets/DbKeysTreeNode.svelte index 2bfe9900..900b4ea6 100644 --- a/packages/web/src/widgets/DbKeysTreeNode.svelte +++ b/packages/web/src/widgets/DbKeysTreeNode.svelte @@ -6,6 +6,7 @@ import FontIcon from '../icons/FontIcon.svelte'; import ConfirmModal from '../modals/ConfirmModal.svelte'; import { showModal } from '../modals/modalTools'; + import newQuery from '../query/newQuery'; import { activeDbKeysStore } from '../stores'; import { apiCall } from '../utility/api'; import openNewTab from '../utility/openNewTab'; @@ -77,6 +78,23 @@ }, }, ], + { + label: 'Generate script', + onClick: async () => { + const data = await apiCall('database-connections/export-keys', { + conid, + database, + options: { + keyPrefix: item.root || item.key, + }, + }); + + newQuery({ + title: 'Export #', + initialData: data, + }); + }, + }, ]; } diff --git a/plugins/dbgate-plugin-redis/src/backend/driver.js b/plugins/dbgate-plugin-redis/src/backend/driver.js index 987821e3..63eb1d42 100644 --- a/plugins/dbgate-plugin-redis/src/backend/driver.js +++ b/plugins/dbgate-plugin-redis/src/backend/driver.js @@ -163,6 +163,20 @@ const driver = { return res; }, + async exportKeys(pool, options) { + const dump = new RedisDump({ client: pool }); + return new Promise((resolve, reject) => { + dump.export({ + type: 'redis', + keyPrefix: options.keyPrefix, + callback: (err, data) => { + if (err) reject(err); + else resolve(data); + }, + }); + }); + }, + async getKeys(pool, keyQuery = '*') { const res = []; let cursor = 0;