mongo: create collection backup

This commit is contained in:
Jan Prochazka 2022-12-22 20:49:22 +01:00
parent b0d78250e1
commit 89219722a9
2 changed files with 21 additions and 3 deletions

View File

@ -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();

View File

@ -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;