mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
collections script templates
This commit is contained in:
parent
e71d278b20
commit
73ec42a9c8
@ -161,6 +161,12 @@ export const driverBase = {
|
|||||||
getCollectionExportQueryJson(collection: string, condition: any, sort: any) {
|
getCollectionExportQueryJson(collection: string, condition: any, sort: any) {
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
|
getScriptTemplates(objectTypeField) {
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
getScriptTemplateContent(scriptTemplate, props) {
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
|
||||||
dataEditorTypesBehaviour: {
|
dataEditorTypesBehaviour: {
|
||||||
parseSqlNull: true,
|
parseSqlNull: true,
|
||||||
|
13
packages/types/engines.d.ts
vendored
13
packages/types/engines.d.ts
vendored
@ -2,7 +2,16 @@ import stream from 'stream';
|
|||||||
import { QueryResult } from './query';
|
import { QueryResult } from './query';
|
||||||
import { SqlDialect } from './dialect';
|
import { SqlDialect } from './dialect';
|
||||||
import { SqlDumper } from './dumper';
|
import { SqlDumper } from './dumper';
|
||||||
import { DatabaseInfo, NamedObjectInfo, TableInfo, ViewInfo, ProcedureInfo, FunctionInfo, TriggerInfo, CollectionInfo } from './dbinfo';
|
import {
|
||||||
|
DatabaseInfo,
|
||||||
|
NamedObjectInfo,
|
||||||
|
TableInfo,
|
||||||
|
ViewInfo,
|
||||||
|
ProcedureInfo,
|
||||||
|
FunctionInfo,
|
||||||
|
TriggerInfo,
|
||||||
|
CollectionInfo,
|
||||||
|
} from './dbinfo';
|
||||||
import { FilterBehaviour } from './filter-type';
|
import { FilterBehaviour } from './filter-type';
|
||||||
|
|
||||||
export interface StreamOptions {
|
export interface StreamOptions {
|
||||||
@ -211,6 +220,8 @@ export interface EngineDriver extends FilterBehaviourProvider {
|
|||||||
getAccessTokenFromAuth(connection, req): Promise<string | null>;
|
getAccessTokenFromAuth(connection, req): Promise<string | null>;
|
||||||
getCollectionExportQueryScript(collection: string, condition: any, sort?: CollectionSortDefinition): string;
|
getCollectionExportQueryScript(collection: string, condition: any, sort?: CollectionSortDefinition): string;
|
||||||
getCollectionExportQueryJson(collection: string, condition: any, sort?: CollectionSortDefinition): {};
|
getCollectionExportQueryJson(collection: string, condition: any, sort?: CollectionSortDefinition): {};
|
||||||
|
getScriptTemplates(objectTypeField: keyof DatabaseInfo): { label: string; scriptTemplate: string }[];
|
||||||
|
getScriptTemplateContent(scriptTemplate: string, props: any): Promise<string>;
|
||||||
|
|
||||||
analyserClass?: any;
|
analyserClass?: any;
|
||||||
dumperClass?: any;
|
dumperClass?: any;
|
||||||
|
@ -31,7 +31,10 @@
|
|||||||
queries: 'QueryDataTab',
|
queries: 'QueryDataTab',
|
||||||
};
|
};
|
||||||
|
|
||||||
function createMenusCore(objectTypeField): {
|
function createMenusCore(
|
||||||
|
objectTypeField,
|
||||||
|
driver
|
||||||
|
): {
|
||||||
label?: string;
|
label?: string;
|
||||||
tab?: string;
|
tab?: string;
|
||||||
forceNewTab?: boolean;
|
forceNewTab?: boolean;
|
||||||
@ -400,31 +403,24 @@
|
|||||||
functionName: 'tableReader',
|
functionName: 'tableReader',
|
||||||
},
|
},
|
||||||
hasPermission('dbops/model/edit') && {
|
hasPermission('dbops/model/edit') && {
|
||||||
label: 'Drop collection',
|
label: `Drop ${driver?.collectionSingularLabel ?? 'collection/container'}`,
|
||||||
isDropCollection: true,
|
isDropCollection: true,
|
||||||
requiresWriteAccess: true,
|
requiresWriteAccess: true,
|
||||||
},
|
},
|
||||||
hasPermission('dbops/table/rename') && {
|
hasPermission('dbops/table/rename') && {
|
||||||
label: 'Rename collection',
|
label: `Rename ${driver?.collectionSingularLabel ?? 'collection/container'}`,
|
||||||
isRenameCollection: true,
|
isRenameCollection: true,
|
||||||
requiresWriteAccess: true,
|
requiresWriteAccess: true,
|
||||||
},
|
},
|
||||||
hasPermission('dbops/table/backup') && {
|
hasPermission('dbops/table/backup') && {
|
||||||
label: 'Create collection backup',
|
label: `Create ${driver?.collectionSingularLabel ?? 'collection/container'} backup`,
|
||||||
isDuplicateCollection: true,
|
isDuplicateCollection: true,
|
||||||
requiresWriteAccess: true,
|
requiresWriteAccess: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
divider: true,
|
divider: true,
|
||||||
},
|
},
|
||||||
{
|
...(driver?.getScriptTemplates?.('collections') || []),
|
||||||
label: 'JS: dropCollection()',
|
|
||||||
scriptTemplate: 'dropCollection',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'JS: find()',
|
|
||||||
scriptTemplate: 'findCollection',
|
|
||||||
},
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -557,9 +553,10 @@
|
|||||||
} else if (menu.isCopyTableName) {
|
} else if (menu.isCopyTableName) {
|
||||||
copyTextToClipboard(data.pureName);
|
copyTextToClipboard(data.pureName);
|
||||||
} else if (menu.isRenameCollection) {
|
} else if (menu.isRenameCollection) {
|
||||||
|
const driver = await getDriver();
|
||||||
showModal(InputTextModal, {
|
showModal(InputTextModal, {
|
||||||
label: `New collection/container name`,
|
label: `New ${driver?.collectionSingularLabel ?? 'collection/container'} name`,
|
||||||
header: `Rename collection/container`,
|
header: `Rename ${driver?.collectionSingularLabel ?? 'collection/container'}`,
|
||||||
value: data.pureName,
|
value: data.pureName,
|
||||||
onConfirm: async newName => {
|
onConfirm: async newName => {
|
||||||
const dbid = _.pick(data, ['conid', 'database']);
|
const dbid = _.pick(data, ['conid', 'database']);
|
||||||
@ -572,9 +569,10 @@
|
|||||||
});
|
});
|
||||||
} else if (menu.isDuplicateCollection) {
|
} else if (menu.isDuplicateCollection) {
|
||||||
const newName = `_${data.pureName}_${dateFormat(new Date(), 'yyyy-MM-dd-hh-mm-ss')}`;
|
const newName = `_${data.pureName}_${dateFormat(new Date(), 'yyyy-MM-dd-hh-mm-ss')}`;
|
||||||
|
const driver = await getDriver();
|
||||||
|
|
||||||
showModal(ConfirmModal, {
|
showModal(ConfirmModal, {
|
||||||
message: `Really create collection copy named ${newName}?`,
|
message: `Really create ${driver?.collectionSingularLabel ?? 'collection/container'} copy named ${newName}?`,
|
||||||
onConfirm: async () => {
|
onConfirm: async () => {
|
||||||
const dbid = _.pick(data, ['conid', 'database']);
|
const dbid = _.pick(data, ['conid', 'database']);
|
||||||
runOperationOnDatabase(dbid, {
|
runOperationOnDatabase(dbid, {
|
||||||
@ -642,8 +640,8 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createMenus(objectTypeField): ReturnType<typeof createMenusCore> {
|
function createMenus(objectTypeField, driver): ReturnType<typeof createMenusCore> {
|
||||||
return createMenusCore(objectTypeField).filter(x => {
|
return createMenusCore(objectTypeField, driver).filter(x => {
|
||||||
if (x.scriptTemplate) {
|
if (x.scriptTemplate) {
|
||||||
return hasPermission(`dbops/sql-template/${x.scriptTemplate}`);
|
return hasPermission(`dbops/sql-template/${x.scriptTemplate}`);
|
||||||
}
|
}
|
||||||
@ -703,9 +701,10 @@
|
|||||||
|
|
||||||
export function handleDatabaseObjectClick(data, forceNewTab = false) {
|
export function handleDatabaseObjectClick(data, forceNewTab = false) {
|
||||||
const { schemaName, pureName, conid, database, objectTypeField } = data;
|
const { schemaName, pureName, conid, database, objectTypeField } = data;
|
||||||
|
const driver = findEngineDriver(data, getExtensions());
|
||||||
|
|
||||||
const configuredAction = getCurrentSettings()[`defaultAction.dbObjectClick.${objectTypeField}`];
|
const configuredAction = getCurrentSettings()[`defaultAction.dbObjectClick.${objectTypeField}`];
|
||||||
const overrideMenu = createMenus(objectTypeField).find(x => x.label && x.label == configuredAction);
|
const overrideMenu = createMenus(objectTypeField, driver).find(x => x.label && x.label == configuredAction);
|
||||||
if (overrideMenu) {
|
if (overrideMenu) {
|
||||||
databaseObjectMenuClickHandler(data, overrideMenu);
|
databaseObjectMenuClickHandler(data, overrideMenu);
|
||||||
return;
|
return;
|
||||||
@ -739,8 +738,10 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function createDatabaseObjectMenu(data, connection = null) {
|
export function createDatabaseObjectMenu(data, connection = null) {
|
||||||
|
const driver = findEngineDriver(data, getExtensions());
|
||||||
|
|
||||||
const { objectTypeField } = data;
|
const { objectTypeField } = data;
|
||||||
return createMenus(objectTypeField)
|
return createMenus(objectTypeField, driver)
|
||||||
.filter(x => x)
|
.filter(x => x)
|
||||||
.map(menu => {
|
.map(menu => {
|
||||||
if (menu.divider) return menu;
|
if (menu.divider) return menu;
|
||||||
|
@ -56,12 +56,10 @@ export default async function applyScriptTemplate(scriptTemplate, extensions, pr
|
|||||||
if (procedureInfo) dmp.put('^execute %f', procedureInfo);
|
if (procedureInfo) dmp.put('^execute %f', procedureInfo);
|
||||||
return dmp.s;
|
return dmp.s;
|
||||||
}
|
}
|
||||||
if (scriptTemplate == 'dropCollection') {
|
|
||||||
return `db.collection('${props.pureName}').drop()`;
|
|
||||||
}
|
|
||||||
if (scriptTemplate == 'findCollection') {
|
|
||||||
return `db.collection('${props.pureName}').find()`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
const connection = await getConnectionInfo(props);
|
||||||
|
const driver = findEngineDriver(connection, extensions) || driverBase;
|
||||||
|
const res = await driver.getScriptTemplateContent(scriptTemplate, props);
|
||||||
|
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
@ -150,6 +150,33 @@ const driver = {
|
|||||||
|
|
||||||
supportFieldRemoval: true,
|
supportFieldRemoval: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getScriptTemplates(objectTypeField) {
|
||||||
|
switch (objectTypeField) {
|
||||||
|
case 'collections':
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
label: 'JS: dropCollection()',
|
||||||
|
scriptTemplate: 'dropCollection',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'JS: find()',
|
||||||
|
scriptTemplate: 'findCollection',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
|
||||||
|
async getScriptTemplateContent(scriptTemplate, props) {
|
||||||
|
switch (scriptTemplate) {
|
||||||
|
case 'dropCollection':
|
||||||
|
return `db.${props.pureName}.drop();`;
|
||||||
|
case 'findCollection':
|
||||||
|
return `db.${props.pureName}.find();`;
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = driver;
|
module.exports = driver;
|
||||||
|
Loading…
Reference in New Issue
Block a user