mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-29 02:56:01 +00:00
file formats refactor
This commit is contained in:
@@ -3,6 +3,7 @@ import _ from 'lodash';
|
|||||||
import { DropDownMenuItem } from '../modals/DropDownMenu';
|
import { DropDownMenuItem } from '../modals/DropDownMenu';
|
||||||
import { openNewTab } from '../utility/common';
|
import { openNewTab } from '../utility/common';
|
||||||
import ImportExportModal from '../modals/ImportExportModal';
|
import ImportExportModal from '../modals/ImportExportModal';
|
||||||
|
import { defaultFileFormat } from '../fileformats';
|
||||||
|
|
||||||
function Menu({ data, setOpenedTabs, showModal }) {
|
function Menu({ data, setOpenedTabs, showModal }) {
|
||||||
const { connection, name } = data;
|
const { connection, name } = data;
|
||||||
@@ -26,7 +27,7 @@ function Menu({ data, setOpenedTabs, showModal }) {
|
|||||||
<ImportExportModal
|
<ImportExportModal
|
||||||
modalState={modalState}
|
modalState={modalState}
|
||||||
initialValues={{
|
initialValues={{
|
||||||
sourceStorageType: 'csv',
|
sourceStorageType: defaultFileFormat.storageType,
|
||||||
targetStorageType: 'database',
|
targetStorageType: 'database',
|
||||||
targetConnectionId: data.connection._id,
|
targetConnectionId: data.connection._id,
|
||||||
targetDatabaseName: data.name,
|
targetDatabaseName: data.name,
|
||||||
@@ -40,7 +41,7 @@ function Menu({ data, setOpenedTabs, showModal }) {
|
|||||||
<ImportExportModal
|
<ImportExportModal
|
||||||
modalState={modalState}
|
modalState={modalState}
|
||||||
initialValues={{
|
initialValues={{
|
||||||
targetStorageType: 'csv',
|
targetStorageType: defaultFileFormat.storageType,
|
||||||
sourceStorageType: 'database',
|
sourceStorageType: 'database',
|
||||||
sourceConnectionId: data.connection._id,
|
sourceConnectionId: data.connection._id,
|
||||||
sourceDatabaseName: data.name,
|
sourceDatabaseName: data.name,
|
||||||
|
|||||||
7
packages/web/src/fileformats/csv.ts
Normal file
7
packages/web/src/fileformats/csv.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export default {
|
||||||
|
storageType: 'csv',
|
||||||
|
extension: 'csv',
|
||||||
|
readerFunc: 'csvReader',
|
||||||
|
writerFunc: 'csvWriter',
|
||||||
|
filesTitle: 'CSV files',
|
||||||
|
};
|
||||||
6
packages/web/src/fileformats/excel.ts
Normal file
6
packages/web/src/fileformats/excel.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
export default {
|
||||||
|
storageType: 'excel',
|
||||||
|
extension: 'xlsx',
|
||||||
|
readerFunc: 'excelSheetReader',
|
||||||
|
filesTitle: 'MS Excel files',
|
||||||
|
};
|
||||||
20
packages/web/src/fileformats/index.ts
Normal file
20
packages/web/src/fileformats/index.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import csv from './csv';
|
||||||
|
import jsonl from './jsonl';
|
||||||
|
import excel from './excel';
|
||||||
|
import { FileFormatDefinition } from './types';
|
||||||
|
|
||||||
|
export const fileformats = [csv, jsonl, excel];
|
||||||
|
|
||||||
|
export function findFileFormat(storageType): FileFormatDefinition {
|
||||||
|
return fileformats.find((x) => x.storageType == storageType);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getFileFormatDirections(format: FileFormatDefinition) {
|
||||||
|
if (!format) return [];
|
||||||
|
const res = [];
|
||||||
|
if (format.readerFunc) res.push('source');
|
||||||
|
if (format.writerFunc) res.push('target');
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const defaultFileFormat = csv;
|
||||||
7
packages/web/src/fileformats/jsonl.ts
Normal file
7
packages/web/src/fileformats/jsonl.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export default {
|
||||||
|
storageType: 'jsonl',
|
||||||
|
extension: 'jsonl',
|
||||||
|
readerFunc: 'jsonLinesReader',
|
||||||
|
writerFunc: 'jsonLinesWriter',
|
||||||
|
filesTitle: 'JSON lines',
|
||||||
|
};
|
||||||
7
packages/web/src/fileformats/types.ts
Normal file
7
packages/web/src/fileformats/types.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export interface FileFormatDefinition {
|
||||||
|
storageType: string;
|
||||||
|
extension: string;
|
||||||
|
readerFunc?: string;
|
||||||
|
writerFunc?: string;
|
||||||
|
filesTitle: string;
|
||||||
|
}
|
||||||
@@ -25,6 +25,7 @@ import SqlEditor from '../sqleditor/SqlEditor';
|
|||||||
import { useUploadsProvider } from '../utility/UploadsProvider';
|
import { useUploadsProvider } from '../utility/UploadsProvider';
|
||||||
import { FontIcon } from '../icons';
|
import { FontIcon } from '../icons';
|
||||||
import useTheme from '../theme/useTheme';
|
import useTheme from '../theme/useTheme';
|
||||||
|
import { fileformats, findFileFormat, getFileFormatDirections } from '../fileformats';
|
||||||
|
|
||||||
const Container = styled.div`
|
const Container = styled.div`
|
||||||
// max-height: 50vh;
|
// max-height: 50vh;
|
||||||
@@ -89,9 +90,8 @@ const Title = styled.div`
|
|||||||
|
|
||||||
function getFileFilters(storageType) {
|
function getFileFilters(storageType) {
|
||||||
const res = [];
|
const res = [];
|
||||||
if (storageType == 'csv') res.push({ name: 'CSV files', extensions: ['csv'] });
|
const format = findFileFormat(storageType);
|
||||||
if (storageType == 'jsonl') res.push({ name: 'JSON lines', extensions: ['jsonl'] });
|
if (format) res.push({ name: format.filesTitle, extensions: [format.extension] });
|
||||||
if (storageType == 'excel') res.push({ name: 'MS Excel files', extensions: ['xlsx'] });
|
|
||||||
res.push({ name: 'All Files', extensions: ['*'] });
|
res.push({ name: 'All Files', extensions: ['*'] });
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -193,9 +193,11 @@ function SourceTargetConfig({
|
|||||||
? [{ value: 'jsldata', label: 'Query result data', directions: ['source'] }]
|
? [{ value: 'jsldata', label: 'Query result data', directions: ['source'] }]
|
||||||
: [
|
: [
|
||||||
{ value: 'database', label: 'Database', directions: ['source', 'target'] },
|
{ value: 'database', label: 'Database', directions: ['source', 'target'] },
|
||||||
{ value: 'csv', label: 'CSV file(s)', directions: ['source', 'target'] },
|
...fileformats.map((format) => ({
|
||||||
{ value: 'jsonl', label: 'JSON lines file(s)', directions: ['source', 'target'] },
|
value: format.storageType,
|
||||||
{ value: 'excel', label: 'MS Excel file(s)', directions: ['source'] },
|
label: format.filesTitle,
|
||||||
|
directions: getFileFormatDirections(format),
|
||||||
|
})),
|
||||||
{ value: 'query', label: 'SQL Query', directions: ['source'] },
|
{ value: 'query', label: 'SQL Query', directions: ['source'] },
|
||||||
{ value: 'archive', label: 'Archive', directions: ['source', 'target'] },
|
{ value: 'archive', label: 'Archive', directions: ['source', 'target'] },
|
||||||
];
|
];
|
||||||
@@ -374,7 +376,7 @@ export default function ImportExportConfigurator({ uploadedFile = undefined, onC
|
|||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const supportsPreview = ['csv', 'jsonl', 'excel'].includes(values.sourceStorageType);
|
const supportsPreview = !!findFileFormat(values.sourceStorageType);
|
||||||
|
|
||||||
const handleChangePreviewSource = async () => {
|
const handleChangePreviewSource = async () => {
|
||||||
if (previewSource && supportsPreview) {
|
if (previewSource && supportsPreview) {
|
||||||
|
|||||||
@@ -4,17 +4,18 @@ import getAsArray from '../utility/getAsArray';
|
|||||||
import { getConnectionInfo } from '../utility/metadataLoaders';
|
import { getConnectionInfo } from '../utility/metadataLoaders';
|
||||||
import engines from 'dbgate-engines';
|
import engines from 'dbgate-engines';
|
||||||
import { findObjectLike } from 'dbgate-tools';
|
import { findObjectLike } from 'dbgate-tools';
|
||||||
|
import { findFileFormat } from '../fileformats';
|
||||||
|
|
||||||
export function getTargetName(source, values) {
|
export function getTargetName(source, values) {
|
||||||
const key = `targetName_${source}`;
|
const key = `targetName_${source}`;
|
||||||
if (values[key]) return values[key];
|
if (values[key]) return values[key];
|
||||||
if (values.targetStorageType == 'csv') return `${source}.csv`;
|
const format = findFileFormat(values.targetStorageType);
|
||||||
if (values.targetStorageType == 'jsonl') return `${source}.jsonl`;
|
if (format) return `${source}.${format.extension}`;
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isFileStorage(storageType) {
|
export function isFileStorage(storageType) {
|
||||||
return storageType == 'csv' || storageType == 'jsonl' || storageType == 'excel';
|
return !!findFileFormat(storageType);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getConnection(storageType, conid, database) {
|
async function getConnection(storageType, conid, database) {
|
||||||
@@ -55,14 +56,9 @@ function getSourceExpr(sourceName, values, sourceConnection, sourceDriver) {
|
|||||||
}
|
}
|
||||||
if (isFileStorage(sourceStorageType)) {
|
if (isFileStorage(sourceStorageType)) {
|
||||||
const sourceFile = values[`sourceFile_${sourceName}`];
|
const sourceFile = values[`sourceFile_${sourceName}`];
|
||||||
if (sourceStorageType == 'excel') {
|
const format = findFileFormat(sourceStorageType);
|
||||||
return ['excelSheetReader', sourceFile];
|
if (format && format.readerFunc) {
|
||||||
}
|
return [format.readerFunc, sourceFile];
|
||||||
if (sourceStorageType == 'jsonl') {
|
|
||||||
return ['jsonLinesReader', sourceFile];
|
|
||||||
}
|
|
||||||
if (sourceStorageType == 'csv') {
|
|
||||||
return ['csvReader', sourceFile];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (sourceStorageType == 'jsldata') {
|
if (sourceStorageType == 'jsldata') {
|
||||||
@@ -101,21 +97,15 @@ function getFlagsFroAction(action) {
|
|||||||
|
|
||||||
function getTargetExpr(sourceName, values, targetConnection, targetDriver) {
|
function getTargetExpr(sourceName, values, targetConnection, targetDriver) {
|
||||||
const { targetStorageType } = values;
|
const { targetStorageType } = values;
|
||||||
if (targetStorageType == 'csv') {
|
const format = findFileFormat(targetStorageType);
|
||||||
|
if (format && format.writerFunc) {
|
||||||
return [
|
return [
|
||||||
'csvWriter',
|
format.writerFunc,
|
||||||
{
|
|
||||||
fileName: getTargetName(sourceName, values),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
}
|
|
||||||
if (targetStorageType == 'jsonl') {
|
|
||||||
return [
|
|
||||||
'jsonLinesWriter',
|
|
||||||
{
|
{
|
||||||
fileName: getTargetName(sourceName, values),
|
fileName: getTargetName(sourceName, values),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
}
|
}
|
||||||
if (targetStorageType == 'database') {
|
if (targetStorageType == 'database') {
|
||||||
return [
|
return [
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import useSocket from '../utility/SocketProvider';
|
|||||||
import LoadingInfo from '../widgets/LoadingInfo';
|
import LoadingInfo from '../widgets/LoadingInfo';
|
||||||
import { FontIcon } from '../icons';
|
import { FontIcon } from '../icons';
|
||||||
import LargeButton from '../widgets/LargeButton';
|
import LargeButton from '../widgets/LargeButton';
|
||||||
|
import { defaultFileFormat } from '../fileformats';
|
||||||
|
|
||||||
const headerHeight = '60px';
|
const headerHeight = '60px';
|
||||||
const footerHeight = '100px';
|
const footerHeight = '100px';
|
||||||
@@ -193,7 +194,7 @@ export default function ImportExportModal({
|
|||||||
onSubmit={handleExecute}
|
onSubmit={handleExecute}
|
||||||
initialValues={{
|
initialValues={{
|
||||||
sourceStorageType: 'database',
|
sourceStorageType: 'database',
|
||||||
targetStorageType: importToArchive ? 'archive' : 'csv',
|
targetStorageType: importToArchive ? 'archive' : defaultFileFormat.storageType,
|
||||||
sourceArchiveFolder: archive,
|
sourceArchiveFolder: archive,
|
||||||
targetArchiveFolder,
|
targetArchiveFolder,
|
||||||
...initialValues,
|
...initialValues,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { useDropzone } from 'react-dropzone';
|
import { useDropzone } from 'react-dropzone';
|
||||||
|
import { findFileFormat } from '../fileformats';
|
||||||
import ImportExportModal from '../modals/ImportExportModal';
|
import ImportExportModal from '../modals/ImportExportModal';
|
||||||
import useShowModal from '../modals/showModal';
|
import useShowModal from '../modals/showModal';
|
||||||
import resolveApi from './resolveApi';
|
import resolveApi from './resolveApi';
|
||||||
@@ -42,7 +43,7 @@ export function useUploadsZone() {
|
|||||||
if (uploadListener) {
|
if (uploadListener) {
|
||||||
uploadListener(fileData);
|
uploadListener(fileData);
|
||||||
} else {
|
} else {
|
||||||
if (['csv', 'excel', 'jsonl'].includes(fileData.storageType)) {
|
if (findFileFormat(fileData.storageType)) {
|
||||||
showModal((modalState) => (
|
showModal((modalState) => (
|
||||||
<ImportExportModal
|
<ImportExportModal
|
||||||
uploadedFile={fileData}
|
uploadedFile={fileData}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { openNewTab } from '../utility/common';
|
|||||||
import useNewFreeTable from '../freetable/useNewFreeTable';
|
import useNewFreeTable from '../freetable/useNewFreeTable';
|
||||||
import ImportExportModal from '../modals/ImportExportModal';
|
import ImportExportModal from '../modals/ImportExportModal';
|
||||||
import useShowModal from '../modals/showModal';
|
import useShowModal from '../modals/showModal';
|
||||||
|
import { defaultFileFormat } from '../fileformats';
|
||||||
|
|
||||||
const ToolbarContainer = styled.div`
|
const ToolbarContainer = styled.div`
|
||||||
display: flex;
|
display: flex;
|
||||||
@@ -40,7 +41,7 @@ export default function ToolBar({ toolbarPortalRef }) {
|
|||||||
modalState={modalState}
|
modalState={modalState}
|
||||||
importToArchive
|
importToArchive
|
||||||
initialValues={{
|
initialValues={{
|
||||||
sourceStorageType: 'csv',
|
sourceStorageType: defaultFileFormat.storageType,
|
||||||
// sourceConnectionId: data.conid,
|
// sourceConnectionId: data.conid,
|
||||||
// sourceDatabaseName: data.database,
|
// sourceDatabaseName: data.database,
|
||||||
// sourceSchemaName: data.schemaName,
|
// sourceSchemaName: data.schemaName,
|
||||||
|
|||||||
Reference in New Issue
Block a user