ndjson direct support

This commit is contained in:
Jan Prochazka 2022-03-27 20:35:30 +02:00
parent a20a34680d
commit 7c0f33383f
5 changed files with 34 additions and 7 deletions

View File

@ -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`);
}

View File

@ -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',

View File

@ -1,5 +1,5 @@
<script lang="ts" context="module">
export const matchingProps = ['archiveFile', 'archiveFolder'];
export const matchingProps = ['archiveFile', 'archiveFolder', 'jslid'];
</script>
<script lang="ts">

View File

@ -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: {

View File

@ -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)] },