mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
ndjson direct support
This commit is contained in:
parent
a20a34680d
commit
7c0f33383f
@ -6,6 +6,10 @@ function getJslFileName(jslid) {
|
||||
if (archiveMatch) {
|
||||
return path.join(resolveArchiveFolder(archiveMatch[1]), `${archiveMatch[2]}.jsonl`);
|
||||
}
|
||||
const fileMatch = jslid.match(/^file:\/\/(.*)$/);
|
||||
if (fileMatch) {
|
||||
return fileMatch[1];
|
||||
}
|
||||
return path.join(jsldir(), `${jslid}.jsonl`);
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,8 @@ import { FileFormatDefinition, QuickExportDefinition } from 'dbgate-types';
|
||||
const jsonlFormat = {
|
||||
storageType: 'jsonl',
|
||||
extension: 'jsonl',
|
||||
name: 'JSON lines',
|
||||
extensions: ['jsonl', 'ndjson'],
|
||||
name: 'JSON lines/NDJSON',
|
||||
readerFunc: 'jsonLinesReader',
|
||||
writerFunc: 'jsonLinesWriter',
|
||||
};
|
||||
@ -23,7 +24,7 @@ const sqlFormat = {
|
||||
};
|
||||
|
||||
const jsonlQuickExport = {
|
||||
label: 'JSON lines',
|
||||
label: 'JSON lines/NDJSON',
|
||||
extension: 'jsonl',
|
||||
createWriter: fileName => ({
|
||||
functionName: 'jsonLinesWriter',
|
||||
|
@ -1,5 +1,5 @@
|
||||
<script lang="ts" context="module">
|
||||
export const matchingProps = ['archiveFile', 'archiveFolder'];
|
||||
export const matchingProps = ['archiveFile', 'archiveFolder', 'jslid'];
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
|
@ -167,8 +167,8 @@ export const copyRowsFormatDefs = {
|
||||
formatter: clipboardJsonFormatter(),
|
||||
},
|
||||
jsonLines: {
|
||||
label: 'Copy as JSON lines',
|
||||
name: 'JSON lines',
|
||||
label: 'Copy as JSON lines/NDJSON',
|
||||
name: 'JSON lines/NDJSON',
|
||||
formatter: clipboardJsonLinesFormatter(),
|
||||
},
|
||||
yaml: {
|
||||
|
@ -7,6 +7,8 @@ import { currentDatabase, extensions } from '../stores';
|
||||
import { getUploadListener } from './uploadFiles';
|
||||
import { getDatabaseFileLabel } from './getConnectionLabel';
|
||||
import { apiCall } from './api';
|
||||
import openNewTab from './openNewTab';
|
||||
import _ from 'lodash';
|
||||
|
||||
export function canOpenByElectron(file, extensions) {
|
||||
if (!file) return false;
|
||||
@ -15,6 +17,7 @@ export function canOpenByElectron(file, extensions) {
|
||||
if (nameLower.endsWith('.db') || nameLower.endsWith('.sqlite') || nameLower.endsWith('.sqlite3')) return true;
|
||||
for (const format of extensions.fileFormats) {
|
||||
if (nameLower.endsWith(`.${format.extension}`)) return true;
|
||||
if (format.extensions?.find(ext => nameLower.endsWith(`.${ext}`))) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -50,6 +53,18 @@ function getFileEncoding(filePath, fs) {
|
||||
return e;
|
||||
}
|
||||
|
||||
function openElectronJsonLinesFile(filePath, parsed) {
|
||||
openNewTab({
|
||||
title: parsed.name,
|
||||
tooltip: filePath,
|
||||
icon: 'img sql-file',
|
||||
tabComponent: 'ArchiveFileTab',
|
||||
props: {
|
||||
jslid: `file://${filePath}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function openElectronFileCore(filePath, extensions) {
|
||||
const nameLower = filePath.toLowerCase();
|
||||
const path = window.require('path');
|
||||
@ -74,6 +89,10 @@ export function openElectronFileCore(filePath, extensions) {
|
||||
openSqliteFile(filePath);
|
||||
return;
|
||||
}
|
||||
if (nameLower.endsWith('.jsonl') || nameLower.endsWith('.ndjson')) {
|
||||
openElectronJsonLinesFile(filePath, parsed);
|
||||
return;
|
||||
}
|
||||
for (const format of extensions.fileFormats) {
|
||||
if (nameLower.endsWith(`.${format.extension}`)) {
|
||||
if (uploadListener) {
|
||||
@ -100,16 +119,19 @@ export function openElectronFileCore(filePath, extensions) {
|
||||
}
|
||||
|
||||
function getFileFormatFilters(extensions) {
|
||||
return extensions.fileFormats.filter(x => x.readerFunc).map(x => ({ name: x.name, extensions: [x.extension] }));
|
||||
return extensions.fileFormats
|
||||
.filter(x => x.readerFunc)
|
||||
.map(x => ({ name: x.name, extensions: x.extensions || [x.extension] }));
|
||||
}
|
||||
|
||||
function getFileFormatExtensions(extensions) {
|
||||
return extensions.fileFormats.filter(x => x.readerFunc).map(x => x.extension);
|
||||
return _.flatten(extensions.fileFormats.filter(x => x.readerFunc).map(x => x.extensions || [x.extension]));
|
||||
}
|
||||
|
||||
export async function openElectronFile() {
|
||||
const electron = getElectron();
|
||||
const ext = get(extensions);
|
||||
|
||||
const filePaths = await electron.showOpenDialog({
|
||||
filters: [
|
||||
{ name: `All supported files`, extensions: ['sql', 'sqlite', 'db', 'sqlite3', ...getFileFormatExtensions(ext)] },
|
||||
|
Loading…
Reference in New Issue
Block a user