From 89219722a9275f111e8d95d1f14c7adbd70d0295 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Thu, 22 Dec 2022 20:49:22 +0100 Subject: [PATCH] mongo: create collection backup --- .../src/appobj/DatabaseObjectAppObject.svelte | 19 ++++++++++++++++++- .../dbgate-plugin-mongo/src/backend/driver.js | 5 +++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/web/src/appobj/DatabaseObjectAppObject.svelte b/packages/web/src/appobj/DatabaseObjectAppObject.svelte index 375d8e86..c39bd1d1 100644 --- a/packages/web/src/appobj/DatabaseObjectAppObject.svelte +++ b/packages/web/src/appobj/DatabaseObjectAppObject.svelte @@ -359,10 +359,17 @@ { label: 'Drop collection', isDropCollection: true, + requiresWriteAccess: true, }, { label: 'Rename collection', isRenameCollection: true, + requiresWriteAccess: true, + }, + { + label: 'Create collection backup', + isDuplicateCollection: true, + requiresWriteAccess: true, }, { divider: true, @@ -516,8 +523,8 @@ showModal(ConfirmModal, { message: `Really drop collection ${data.pureName}?`, onConfirm: async () => { - saveScriptToDatabase(_.pick(data, ['conid', 'database']), `db.dropCollection('${data.pureName}')`); const dbid = _.pick(data, ['conid', 'database']); + saveScriptToDatabase(dbid, `db.dropCollection('${data.pureName}')`); }, }); } else if (menu.isRenameCollection) { @@ -534,6 +541,16 @@ apiCall('database-connections/sync-model', dbid); }, }); + } else if (menu.isDuplicateCollection) { + const newName = `_${data.pureName}_${dateFormat(new Date(), 'yyyy-MM-dd-hh-mm-ss')}`; + + showModal(ConfirmModal, { + message: `Really create collection copy named ${newName}?`, + onConfirm: async () => { + const dbid = _.pick(data, ['conid', 'database']); + saveScriptToDatabase(dbid, `db.collection('${data.pureName}').aggregate([{$out: '${newName}'}]).toArray()`); + }, + }); } else if (menu.isDuplicateTable) { const driver = await getDriver(); const dmp = driver.createDumper(); diff --git a/plugins/dbgate-plugin-mongo/src/backend/driver.js b/plugins/dbgate-plugin-mongo/src/backend/driver.js index b28ccde5..1fd8444a 100644 --- a/plugins/dbgate-plugin-mongo/src/backend/driver.js +++ b/plugins/dbgate-plugin-mongo/src/backend/driver.js @@ -90,9 +90,10 @@ const driver = { }, async script(pool, sql) { let func; - func = eval(`(db,ObjectId) => { ${sql} }`); + func = eval(`(db,ObjectId) => ${sql}`); const db = await getScriptableDb(pool); - func(db, ObjectId); + const res = func(db, ObjectId); + if (isPromise(res)) await res; }, async stream(pool, sql, options) { let func;