readonly sessions on mongo (+checks on BE)

This commit is contained in:
Jan Prochazka 2022-03-17 13:17:30 +01:00
parent 7d196c7c62
commit 8f1d76fd2a
8 changed files with 51 additions and 13 deletions

View File

@ -159,7 +159,7 @@ module.exports = {
async collectionData({ conid, database, options }) {
const opened = await this.ensureOpened(conid, database);
const res = await this.sendRequest(opened, { msgtype: 'collectionData', options });
return res.result;
return res.result || null;
},
async loadDataCore(msgtype, { conid, database, ...args }) {
@ -167,6 +167,10 @@ module.exports = {
const res = await this.sendRequest(opened, { msgtype, ...args });
if (res.errorMessage) {
console.error(res.errorMessage);
return {
errorMessage: res.errorMessage,
};
}
return res.result || null;
},
@ -193,19 +197,26 @@ module.exports = {
callMethod_meta: true,
async callMethod({ conid, database, method, args }) {
const opened = await this.ensureOpened(conid, database);
const res = await this.sendRequest(opened, { msgtype: 'callMethod', method, args });
if (res.errorMessage) {
console.error(res.errorMessage);
}
return res.result || null;
return this.loadDataCore('callMethod', { conid, database, method, args });
// const opened = await this.ensureOpened(conid, database);
// const res = await this.sendRequest(opened, { msgtype: 'callMethod', method, args });
// if (res.errorMessage) {
// console.error(res.errorMessage);
// }
// return res.result || null;
},
updateCollection_meta: true,
async updateCollection({ conid, database, changeSet }) {
const opened = await this.ensureOpened(conid, database);
const res = await this.sendRequest(opened, { msgtype: 'updateCollection', changeSet });
return res.result;
if (res.errorMessage) {
return {
errorMessage: res.errorMessage,
};
}
return res.result || null;
},
status_meta: true,

View File

@ -155,6 +155,7 @@ async function handleRunScript({ msgid, sql }) {
await waitConnected();
const driver = requireEngineDriver(storedConnection);
try {
ensureExecuteCustomScript(driver);
await driver.script(systemConnection, sql);
process.send({ msgtype: 'response', msgid });
} catch (err) {
@ -166,6 +167,7 @@ async function handleQueryData({ msgid, sql }) {
await waitConnected();
const driver = requireEngineDriver(storedConnection);
try {
ensureExecuteCustomScript(driver);
const res = await driver.query(systemConnection, sql);
process.send({ msgtype: 'response', msgid, ...res });
} catch (err) {
@ -204,7 +206,10 @@ async function handleLoadKeyInfo({ msgid, key }) {
}
async function handleCallMethod({ msgid, method, args }) {
return handleDriverDataCore(msgid, driver => driver.callMethod(systemConnection, method, args));
return handleDriverDataCore(msgid, driver => {
ensureExecuteCustomScript(driver);
return driver.callMethod(systemConnection, method, args);
});
}
async function handleLoadKeyTableRange({ msgid, key, cursor, count }) {
@ -217,10 +222,20 @@ async function handleLoadFieldValues({ msgid, schemaName, pureName, field, searc
);
}
function ensureExecuteCustomScript(driver) {
if (driver.readOnlySessions) {
return;
}
if (storedConnection.isReadOnly) {
throw new Error('Connection is read only');
}
}
async function handleUpdateCollection({ msgid, changeSet }) {
await waitConnected();
const driver = requireEngineDriver(storedConnection);
try {
ensureExecuteCustomScript(driver);
const result = await driver.updateCollection(systemConnection, changeSet);
process.send({ msgtype: 'response', msgid, result });
} catch (err) {

View File

@ -135,10 +135,20 @@ function handleStream(driver, resultIndexHolder, sql) {
});
}
function ensureExecuteCustomScript(driver) {
if (driver.readOnlySessions) {
return;
}
if (storedConnection.isReadOnly) {
throw new Error('Connection is read only');
}
}
async function handleConnect(connection) {
storedConnection = connection;
const driver = requireEngineDriver(storedConnection);
ensureExecuteCustomScript(driver);
systemConnection = await connectUtility(driver, storedConnection);
for (const [resolve] of afterConnectCallbacks) {
resolve();

View File

@ -47,6 +47,7 @@ export interface EngineDriver {
title: string;
defaultPort?: number;
databaseEngineTypes: string[];
readOnlySessions: boolean,
supportedKeyTypes: { name: string; label: string }[];
supportsDatabaseUrl?: boolean;
isElectronOnly?: boolean;

View File

@ -173,6 +173,6 @@
<ToolStripCommandButton command="dataGrid.deleteSelectedRows" hideDisabled />
<ToolStripCommandButton command="dataGrid.switchToJson" hideDisabled />
<ToolStripCommandButton command="dataGrid.switchToTable" hideDisabled />
<ToolStripExportButton {quickExportHandlerRef} command='collectionDataGrid.export' />
<ToolStripExportButton {quickExportHandlerRef} command="collectionDataGrid.export" />
</svelte:fragment>
</ToolStripContainer>

View File

@ -140,7 +140,7 @@
}
export function hasConnection() {
return !!conid;
return !!conid && (!$connection.isReadOnly || driver.readOnlySessions);
}
async function executeCore(sql) {

View File

@ -38,9 +38,9 @@ const driver = {
showConnectionField: (field, values) => {
if (field == 'useDatabaseUrl') return true;
if (values.useDatabaseUrl) {
return ['databaseUrl', 'defaultDatabase', 'singleDatabase'].includes(field);
return ['databaseUrl', 'defaultDatabase', 'singleDatabase', 'isReadOnly'].includes(field);
}
return ['server', 'port', 'user', 'password', 'defaultDatabase', 'singleDatabase'].includes(field);
return ['server', 'port', 'user', 'password', 'defaultDatabase', 'singleDatabase', 'isReadOnly'].includes(field);
},
importExportArgs: [

View File

@ -46,6 +46,7 @@ const mysqlDriverBase = {
dialect,
defaultPort: 3306,
getQuerySplitterOptions: () => mysqlSplitterOptions,
readOnlySessions: true,
getNewObjectTemplates() {
return [