mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-22 09:56:01 +00:00
SYNC: Merge pull request #9 from dbgate/feature/apps
This commit is contained in:
@@ -1,33 +1,131 @@
|
||||
import type { ApplicationDefinition, StoredConnection } from 'dbgate-types';
|
||||
import type { ApplicationDefinition, DatabaseInfo, StoredConnection } from 'dbgate-types';
|
||||
import { apiCall } from '../utility/api';
|
||||
import _ from 'lodash';
|
||||
import { match } from 'fuzzy';
|
||||
import { getConnectionInfo } from './metadataLoaders';
|
||||
import openNewTab from './openNewTab';
|
||||
|
||||
export async function saveDbToApp(conid: string, database: string, app: string) {
|
||||
if (app == '#new') {
|
||||
const folder = await apiCall('apps/create-folder', { folder: database });
|
||||
// export async function saveDbToApp(conid: string, database: string, app: string) {
|
||||
// const connection = await getConnectionInfo({ conid });
|
||||
|
||||
await apiCall('connections/update-database', {
|
||||
conid,
|
||||
database,
|
||||
values: {
|
||||
[`useApp:${folder}`]: true,
|
||||
},
|
||||
});
|
||||
// if (app == '#new') {
|
||||
// const appJson = {
|
||||
// applicationName: _.startCase(database),
|
||||
// usageRules: [
|
||||
// {
|
||||
// serverHostsList: connection?.server ? [connection.server] : undefined,
|
||||
// databaseNamesList: [database],
|
||||
// conditionGroup: '1',
|
||||
// },
|
||||
// ],
|
||||
// };
|
||||
|
||||
return folder;
|
||||
// const file =
|
||||
|
||||
// const folder = await apiCall('apps/create-folder', { folder: database });
|
||||
|
||||
// await apiCall('connections/update-database', {
|
||||
// conid,
|
||||
// database,
|
||||
// values: {
|
||||
// [`useApp:${folder}`]: true,
|
||||
// },
|
||||
// });
|
||||
|
||||
// return folder;
|
||||
// }
|
||||
|
||||
// await apiCall('connections/update-database', {
|
||||
// conid,
|
||||
// database,
|
||||
// values: {
|
||||
// [`useApp:${app}`]: true,
|
||||
// },
|
||||
// });
|
||||
|
||||
// return app;
|
||||
// }
|
||||
|
||||
export function filterAppsForDatabase(
|
||||
connection,
|
||||
database: string,
|
||||
apps: ApplicationDefinition[],
|
||||
dbinfo: DatabaseInfo = null
|
||||
): ApplicationDefinition[] {
|
||||
if (!apps) {
|
||||
return [];
|
||||
}
|
||||
|
||||
await apiCall('connections/update-database', {
|
||||
conid,
|
||||
database,
|
||||
values: {
|
||||
[`useApp:${app}`]: true,
|
||||
},
|
||||
// console.log('ALL APPS:', apps);
|
||||
// console.log('DB INFO:', dbinfo);
|
||||
// console.log('CONNECTION:', connection);
|
||||
// console.log('DATABASE:', database);
|
||||
return apps.filter(app => {
|
||||
const groupedConditions = _.groupBy(app.usageRules, rule => rule.conditionGroup || '1');
|
||||
for (const group of Object.values(groupedConditions)) {
|
||||
let groupMatch = true;
|
||||
for (const rule of group) {
|
||||
let ruleMatch = true;
|
||||
if (rule.serverHostsRegex) {
|
||||
const re = new RegExp(rule.serverHostsRegex);
|
||||
ruleMatch = ruleMatch && !!connection?.server && re.test(connection.server);
|
||||
}
|
||||
if (rule.serverHostsList) {
|
||||
ruleMatch = ruleMatch && !!connection?.server && rule.serverHostsList.includes(connection.server);
|
||||
}
|
||||
if (rule.databaseNamesRegex) {
|
||||
const re = new RegExp(rule.databaseNamesRegex);
|
||||
ruleMatch = ruleMatch && !!database && re.test(database);
|
||||
}
|
||||
if (rule.databaseNamesList) {
|
||||
ruleMatch = ruleMatch && !!database && rule.databaseNamesList.includes(database);
|
||||
}
|
||||
let matchedTables = dbinfo?.tables;
|
||||
if (rule.tableNamesRegex) {
|
||||
const re = new RegExp(rule.tableNamesRegex);
|
||||
matchedTables = dbinfo?.tables?.filter(table => !!table && re.test(table.pureName)) || [];
|
||||
ruleMatch = ruleMatch && matchedTables.length > 0;
|
||||
}
|
||||
if (rule.tableNamesList) {
|
||||
matchedTables =
|
||||
dbinfo?.tables?.filter(table => !!table && rule.tableNamesList.includes(table.pureName)) || [];
|
||||
ruleMatch = ruleMatch && matchedTables.length > 0;
|
||||
}
|
||||
if (rule.columnNamesRegex) {
|
||||
const re = new RegExp(rule.columnNamesRegex);
|
||||
ruleMatch =
|
||||
ruleMatch &&
|
||||
matchedTables.some(table => !!table?.columns?.some(column => !!column && re.test(column.columnName)));
|
||||
}
|
||||
if (rule.columnNamesList) {
|
||||
ruleMatch =
|
||||
ruleMatch &&
|
||||
matchedTables.some(
|
||||
table => !!table?.columns?.some(column => !!column && rule.columnNamesList.includes(column.columnName))
|
||||
);
|
||||
}
|
||||
groupMatch = groupMatch && ruleMatch;
|
||||
}
|
||||
if (groupMatch) return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
return app;
|
||||
// const db = (connection?.databases || []).find(x => x.name == database);
|
||||
// return apps?.filter(app => db && db[`useApp:${app.name}`]);
|
||||
}
|
||||
|
||||
export function filterAppsForDatabase(connection, database: string, $apps): ApplicationDefinition[] {
|
||||
const db = (connection?.databases || []).find(x => x.name == database);
|
||||
return $apps?.filter(app => db && db[`useApp:${app.name}`]);
|
||||
export async function openApplicationEditor(appid) {
|
||||
const dataContent = await apiCall('files/load', { folder: 'apps', file: appid, format: 'json' });
|
||||
openNewTab(
|
||||
{
|
||||
title: appid,
|
||||
icon: 'img app',
|
||||
tabComponent: 'AppEditorTab',
|
||||
props: {
|
||||
savedFile: appid,
|
||||
savedFolder: 'apps',
|
||||
savedFormat: 'json',
|
||||
},
|
||||
},
|
||||
{ editor: dataContent }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ const cachedByKey = {};
|
||||
const cachedPromisesByKey = {};
|
||||
const cachedKeysByReloadTrigger = {};
|
||||
const subscriptionsByReloadTrigger = {};
|
||||
const subscriptionsByByCacheKeyPeek = {};
|
||||
const cacheGenerationByKey = {};
|
||||
|
||||
let cacheGeneration = 0;
|
||||
@@ -29,6 +30,7 @@ function cacheSet(cacheKey, value, reloadTrigger, generation) {
|
||||
addCacheKeyToReloadTrigger(cacheKey, reloadTrigger);
|
||||
delete cachedPromisesByKey[cacheKey];
|
||||
cacheGenerationByKey[cacheKey] = generation;
|
||||
dispatchCacheChangePeek(cacheKey);
|
||||
}
|
||||
|
||||
function cacheClean(reloadTrigger) {
|
||||
@@ -64,6 +66,10 @@ function getCacheGenerationForKey(cacheKey) {
|
||||
return cacheGenerationByKey[cacheKey] || 0;
|
||||
}
|
||||
|
||||
export function getCachedValue(cacheKey) {
|
||||
return cacheGet(cacheKey);
|
||||
}
|
||||
|
||||
export async function loadCachedValue(reloadTrigger, cacheKey, func) {
|
||||
const fromCache = cacheGet(cacheKey);
|
||||
if (fromCache) {
|
||||
@@ -107,12 +113,36 @@ export async function unsubscribeCacheChange(reloadTrigger, cacheKey, reloadHand
|
||||
x => x != reloadHandler
|
||||
);
|
||||
}
|
||||
if (subscriptionsByReloadTrigger[itemString].length == 0) {
|
||||
if (subscriptionsByReloadTrigger[itemString]?.length == 0) {
|
||||
delete subscriptionsByReloadTrigger[itemString];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function subscribeCachePeek(cacheKey, peekHandler) {
|
||||
if (!subscriptionsByByCacheKeyPeek[cacheKey]) {
|
||||
subscriptionsByByCacheKeyPeek[cacheKey] = [];
|
||||
}
|
||||
subscriptionsByByCacheKeyPeek[cacheKey].push(peekHandler);
|
||||
}
|
||||
|
||||
export function unsubscribeCachePeek(cacheKey, peekHandler) {
|
||||
if (subscriptionsByByCacheKeyPeek[cacheKey]) {
|
||||
subscriptionsByByCacheKeyPeek[cacheKey] = subscriptionsByByCacheKeyPeek[cacheKey].filter(x => x != peekHandler);
|
||||
}
|
||||
if (subscriptionsByByCacheKeyPeek[cacheKey]?.length == 0) {
|
||||
delete subscriptionsByByCacheKeyPeek[cacheKey];
|
||||
}
|
||||
}
|
||||
|
||||
function dispatchCacheChangePeek(cacheKey) {
|
||||
if (subscriptionsByByCacheKeyPeek[cacheKey]) {
|
||||
for (const handler of subscriptionsByByCacheKeyPeek[cacheKey]) {
|
||||
handler();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function dispatchCacheChange(reloadTrigger) {
|
||||
cacheClean(reloadTrigger);
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import type { DictionaryDescription } from 'dbgate-datalib';
|
||||
import type { ApplicationDefinition, TableInfo } from 'dbgate-types';
|
||||
import type { ApplicationDefinition, DatabaseInfo, TableInfo } from 'dbgate-types';
|
||||
import _ from 'lodash';
|
||||
import { apiCall } from './api';
|
||||
import { filterAppsForDatabase, saveDbToApp } from './appTools';
|
||||
import { filterAppsForDatabase } from './appTools';
|
||||
// import { filterAppsForDatabase, saveDbToApp } from './appTools';
|
||||
|
||||
function checkDescriptionColumns(columns: string[], table: TableInfo) {
|
||||
if (!columns?.length) return false;
|
||||
@@ -17,7 +18,8 @@ export function getDictionaryDescription(
|
||||
database: string,
|
||||
apps: ApplicationDefinition[],
|
||||
connections,
|
||||
skipCheckSaved: boolean = false
|
||||
skipCheckSaved: boolean = false,
|
||||
dbInfo: DatabaseInfo = null
|
||||
): DictionaryDescription {
|
||||
const conn = connections?.find(x => x._id == conid);
|
||||
|
||||
@@ -25,7 +27,7 @@ export function getDictionaryDescription(
|
||||
return null;
|
||||
}
|
||||
|
||||
const dbApps = filterAppsForDatabase(conn, database, apps);
|
||||
const dbApps = filterAppsForDatabase(conn, database, apps, dbInfo);
|
||||
|
||||
if (!dbApps) {
|
||||
return null;
|
||||
@@ -70,22 +72,20 @@ export function changeDelimitedColumnList(columns, columnName, isChecked) {
|
||||
return parsed.join(',');
|
||||
}
|
||||
|
||||
export async function saveDictionaryDescription(
|
||||
table: TableInfo,
|
||||
conid: string,
|
||||
database: string,
|
||||
expression: string,
|
||||
delimiter: string,
|
||||
targetApplication: string
|
||||
) {
|
||||
const appFolder = await saveDbToApp(conid, database, targetApplication);
|
||||
|
||||
await apiCall('apps/save-dictionary-description', {
|
||||
appFolder,
|
||||
schemaName: table.schemaName,
|
||||
pureName: table.pureName,
|
||||
columns: parseDelimitedColumnList(expression),
|
||||
expression,
|
||||
delimiter,
|
||||
});
|
||||
}
|
||||
// export async function saveDictionaryDescription(
|
||||
// table: TableInfo,
|
||||
// conid: string,
|
||||
// database: string,
|
||||
// expression: string,
|
||||
// delimiter: string,
|
||||
// targetApplication: string
|
||||
// ) {
|
||||
// await apiCall('apps/save-dictionary-description', {
|
||||
// appFolder,
|
||||
// schemaName: table.schemaName,
|
||||
// pureName: table.pureName,
|
||||
// columns: parseDelimitedColumnList(expression),
|
||||
// expression,
|
||||
// delimiter,
|
||||
// });
|
||||
// }
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
import _ from 'lodash';
|
||||
import { loadCachedValue, subscribeCacheChange, unsubscribeCacheChange } from './cache';
|
||||
import {
|
||||
getCachedValue,
|
||||
loadCachedValue,
|
||||
subscribeCacheChange,
|
||||
subscribeCachePeek,
|
||||
unsubscribeCacheChange,
|
||||
unsubscribeCachePeek,
|
||||
} from './cache';
|
||||
import stableStringify from 'json-stable-stringify';
|
||||
import { derived } from 'svelte/store';
|
||||
import { extendDatabaseInfo } from 'dbgate-tools';
|
||||
@@ -107,17 +114,17 @@ const archiveFilesLoader = ({ folder }) => ({
|
||||
reloadTrigger: { key: `archive-files-changed`, folder },
|
||||
});
|
||||
|
||||
const appFoldersLoader = () => ({
|
||||
url: 'apps/folders',
|
||||
params: {},
|
||||
reloadTrigger: { key: `app-folders-changed` },
|
||||
});
|
||||
// const appFoldersLoader = () => ({
|
||||
// url: 'apps/folders',
|
||||
// params: {},
|
||||
// reloadTrigger: { key: `app-folders-changed` },
|
||||
// });
|
||||
|
||||
const appFilesLoader = ({ folder }) => ({
|
||||
url: 'apps/files',
|
||||
params: { folder },
|
||||
reloadTrigger: { key: `app-files-changed`, app: folder },
|
||||
});
|
||||
// const appFilesLoader = ({ folder }) => ({
|
||||
// url: 'apps/files',
|
||||
// params: { folder },
|
||||
// reloadTrigger: { key: `app-files-changed`, app: folder },
|
||||
// });
|
||||
|
||||
// const dbAppsLoader = ({ conid, database }) => ({
|
||||
// url: 'apps/get-apps-for-db',
|
||||
@@ -125,10 +132,10 @@ const appFilesLoader = ({ folder }) => ({
|
||||
// reloadTrigger: `db-apps-changed-${conid}-${database}`,
|
||||
// });
|
||||
|
||||
const usedAppsLoader = ({ conid, database }) => ({
|
||||
url: 'apps/get-used-apps',
|
||||
const allAppsLoader = () => ({
|
||||
url: 'apps/get-all-apps',
|
||||
params: {},
|
||||
reloadTrigger: { key: `used-apps-changed` },
|
||||
reloadTrigger: { key: `files-changed`, folder: 'apps' },
|
||||
});
|
||||
|
||||
const serverStatusLoader = () => ({
|
||||
@@ -227,6 +234,37 @@ function useCore(loader, args) {
|
||||
};
|
||||
}
|
||||
|
||||
function useCorePeek(loader, args) {
|
||||
const { url, params, reloadTrigger, transform, onLoaded } = loader(args);
|
||||
const cacheKey = stableStringify({ url, ...params });
|
||||
let openedCount = 0;
|
||||
|
||||
return {
|
||||
subscribe: onChange => {
|
||||
async function handlePeek() {
|
||||
const res = getCachedValue(cacheKey);
|
||||
if (openedCount > 0) {
|
||||
onChange(res);
|
||||
}
|
||||
}
|
||||
openedCount += 1;
|
||||
handlePeek();
|
||||
|
||||
if (reloadTrigger) {
|
||||
subscribeCachePeek(cacheKey, handlePeek);
|
||||
return () => {
|
||||
openedCount -= 1;
|
||||
unsubscribeCachePeek(cacheKey, handlePeek);
|
||||
};
|
||||
} else {
|
||||
return () => {
|
||||
openedCount -= 1;
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** @returns {Promise<import('dbgate-types').DatabaseInfo>} */
|
||||
export function getDatabaseInfo(args) {
|
||||
return getCore(databaseInfoLoader, args);
|
||||
@@ -237,6 +275,10 @@ export function useDatabaseInfo(args) {
|
||||
return useCore(databaseInfoLoader, args);
|
||||
}
|
||||
|
||||
export function useDatabaseInfoPeek(args) {
|
||||
return useCorePeek(databaseInfoLoader, args);
|
||||
}
|
||||
|
||||
export async function getDbCore(args, objectTypeField = undefined) {
|
||||
const db = await getDatabaseInfo(args);
|
||||
if (!db) return null;
|
||||
@@ -392,25 +434,25 @@ export function useArchiveFolders(args = {}) {
|
||||
return useCore(archiveFoldersLoader, args);
|
||||
}
|
||||
|
||||
export function getAppFiles(args) {
|
||||
return getCore(appFilesLoader, args);
|
||||
}
|
||||
export function useAppFiles(args) {
|
||||
return useCore(appFilesLoader, args);
|
||||
}
|
||||
// export function getAppFiles(args) {
|
||||
// return getCore(appFilesLoader, args);
|
||||
// }
|
||||
// export function useAppFiles(args) {
|
||||
// return useCore(appFilesLoader, args);
|
||||
// }
|
||||
|
||||
export function getAppFolders(args = {}) {
|
||||
return getCore(appFoldersLoader, args);
|
||||
}
|
||||
export function useAppFolders(args = {}) {
|
||||
return useCore(appFoldersLoader, args);
|
||||
}
|
||||
// export function getAppFolders(args = {}) {
|
||||
// return getCore(appFoldersLoader, args);
|
||||
// }
|
||||
// export function useAppFolders(args = {}) {
|
||||
// return useCore(appFoldersLoader, args);
|
||||
// }
|
||||
|
||||
export function getUsedApps(args = {}) {
|
||||
return getCore(usedAppsLoader, args);
|
||||
export function getAllApps(args = {}) {
|
||||
return getCore(allAppsLoader, args);
|
||||
}
|
||||
export function useUsedApps(args = {}) {
|
||||
return useCore(usedAppsLoader, args);
|
||||
export function useAllApps(args = {}) {
|
||||
return useCore(allAppsLoader, args);
|
||||
}
|
||||
|
||||
// export function getDbApps(args = {}) {
|
||||
|
||||
Reference in New Issue
Block a user